<commit_before>/* This file is part of magic-sdk, an sdk for the open source programming language magic.
 *
 * Copyright (C) 2016 magic-lang
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

use base
use collections
use concurrent

_Task: abstract class {
	_state := _PromiseState Unfinished
	_mutex: Mutex
	_waitCondition := WaitCondition new()
	_freeOnCompletion := false
	init: func (=_mutex)
	free: override func {
		this _mutex lock()
		if (this _state == _PromiseState Unfinished) {
			this _freeOnCompletion = true
			this _mutex unlock()
		} else {
			this _free()
			this _waitCondition free()
			this _mutex unlock()
			super()
		}
	}
	_free: abstract func
	run: abstract func
	wait: func -> Bool {
		this _mutex lock()
		while (this _state == _PromiseState Unfinished)
			this _waitCondition wait(this _mutex)
		this _mutex unlock()
		this _state == _PromiseState Finished
	}
	wait: func ~timeout (time: TimeSpan) -> Bool {
		status := false
		if (time == TimeSpan maximumValue)
			this wait()
		else {
			timer := WallTimer new() . start()
			while (timer stop() < time && !status) {
				this _mutex with(|| status = (this _state != _PromiseState Unfinished))
				if (!status)
					Time sleepMilli(1)
			}
			timer free()
		}
		this _state == _PromiseState Finished
	}
	cancel: func -> Bool {
		status := false
		this _mutex lock()
		if (this _state == _PromiseState Unfinished) {
			this _state = _PromiseState Cancelled
			status = true
		}
		this _mutex unlock()
		status
	}
	_finishedTask: func {
		this _mutex lock()
		if (this _state != _PromiseState Cancelled)
			this _state = _PromiseState Finished
		if (this _freeOnCompletion) {
			this _mutex unlock()
			this free()
		} else {
			this _waitCondition broadcast()
			this _mutex unlock()
		}
	}
}

_ActionTask: class extends _Task {
	_action: Func
	init: func (=_action, mutex: Mutex) { super(mutex) }
	_free: override func { (this _action as Closure) free(Owner Receiver) }<commit_msg>fixed race condition on _state variable<commit_after>/* This file is part of magic-sdk, an sdk for the open source programming language magic.
 *
 * Copyright (C) 2016 magic-lang
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

use base
use collections
use concurrent

_Task: abstract class {
	_state := _PromiseState Unfinished
	_mutex: Mutex
	_waitCondition := WaitCondition new()
	_freeOnCompletion := false
	init: func (=_mutex)
	free: override func {
		this _mutex lock()
		if (this _state == _PromiseState Unfinished) {
			this _freeOnCompletion = true
			this _mutex unlock()
		} else {
			this _free()
			this _waitCondition free()
			this _mutex unlock()
			super()
		}
	}
	_free: abstract func
	run: abstract func
	wait: func -> Bool {
		this _mutex lock()
		while (this _state == _PromiseState Unfinished)
			this _waitCondition wait(this _mutex)
		this _mutex unlock()
		this _state == _PromiseState Finished
	}
	wait: func ~timeout (time: TimeSpan) -> Bool {
		status := false
		if (time == TimeSpan maximumValue)
			this wait()
		else {
			timer := WallTimer new() . start()
			while (timer stop() < time && !status) {
				this _mutex with(|| status = (this _state != _PromiseState Unfinished))
				if (!status)
					Time sleepMilli(1)
			}
			timer free()
		}
		this _mutex lock()
		status = this _state == _PromiseState Finished
		this _mutex unlock()
		status
	}
	cancel: func -> Bool {
		status := false
		this _mutex lock()
		if (this _state == _PromiseState Unfinished) {
			this _state = _PromiseState Cancelled
			status = true
		}
		this _mutex unlock()
		status
	}
	_finishedTask: func {
		this _mutex lock()
		if (this _state != _PromiseState Cancelled)
			this _state = _PromiseState Finished
		if (this _freeOnCompletion) {
			this _mutex unlock()
			this free()
		} else {
			this _waitCondition broadcast()
			this _mutex unlock()
		}
	}
}

_ActionTask: class extends _Task {
	_action: Func
	init: func (=_action, mutex: Mutex) { super(mutex) }
	_free: override func { (this _action as Closure) free(Owner Receiver) }
{'fraction_non_alphanumeric': 0.09524952015355087, 'fraction_numerical': 0.0023992322456813818, 'mean_word_length': 3.507027027027027, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7199193', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1303, 'n_words': 477}