<commit_before>package lime.system;


import lime.app.Application;
import lime.app.Event;

#if cpp
import cpp.vm.Deque;
import cpp.vm.Thread;
#elseif neko
import neko.vm.Deque;
import neko.vm.Thread;
#end

#if !lime_debug
@:fileXml('tags="haxe,release"')
@:noDebug
#end


class ThreadPool {
	
	
	public var currentThreads (default, null):Int;
	public var doWork = new Event<Dynamic->Void> ();
	public var maxThreads:Int;
	public var minThreads:Int;
	public var onComplete = new Event<Dynamic->Void> ();
	public var onError = new Event<Dynamic->Void> ();
	public var onProgress = new Event<Dynamic->Void> ();
	
	#if (cpp || neko)
	private var __workCompleted:Int;
	private var __workIncoming = new Deque<ThreadPoolMessage> ();
	private var __workQueued:Int;
	private var __workResult = new Deque<ThreadPoolMessage> ();
	#end
	
	
	public function new (minThreads:Int = 0, maxThreads:Int = 1) {
		
		this.minThreads = minThreads;
		this.maxThreads = maxThreads;
		
		currentThreads = 0;
		
		#if (cpp || neko)
		__workQueued = 0;
		__workCompleted = 0;
		#end
		
	}
	
	
	//public function cancel (id:String):Void {
		//
		//
		//
	//}
	
	
	//public function isCanceled (id:String):Bool {
		//
		//
		//
	//}
	
	
	public function queue (state:Dynamic = null):Void {
		
		#if (cpp || neko)
		
		__workIncoming.add (new ThreadPoolMessage (WORK, state));
		__workQueued++;
		
		if (currentThreads < maxThreads && currentThreads < (__workQueued - __workCompleted)) {
			
			currentThreads++;
			Thread.create (__doWork);
			
		}
		
		if (!Application.current.onUpdate.has (__update)) {
			
			Application.current.onUpdate.add (__update);
			
		}
		
		#else
		
		doWork.dispatch (state);
		
		#end
		
	}
	
	
	public function sendComplete (state:Dynamic = null):Void {
		
		#if (cpp || neko)
		__workResult.add (new ThreadPoolMessage (COMPLETE, state));
		#else
		onComplete.dispatch (state);
		#end
		
	}
	
	
	public function sendError (state:Dynamic = null):Void {
		
		#if (cpp || neko)
		__workResult.add (new ThreadPoolMessage (ERROR, state));
		#else
		onError.dispatch (state);
		#end
		
	}
	
	
	public function sendProgress (state:Dynamic = null):Void {
		
		#if (cpp || neko)
		__workResult.add (new ThreadPoolMessage (PROGRESS, state));
		#else
		onProgress.dispatch (state);
		#end
		
	}
	
	
	#if (cpp || neko)<commit_msg>Improve ThreadPool in case Application.current is not set<commit_after>package lime.system;


import lime.app.Application;
import lime.app.Event;

#if cpp
import cpp.vm.Deque;
import cpp.vm.Thread;
#elseif neko
import neko.vm.Deque;
import neko.vm.Thread;
#end

#if !lime_debug
@:fileXml('tags="haxe,release"')
@:noDebug
#end


class ThreadPool {
	
	
	public var currentThreads (default, null):Int;
	public var doWork = new Event<Dynamic->Void> ();
	public var maxThreads:Int;
	public var minThreads:Int;
	public var onComplete = new Event<Dynamic->Void> ();
	public var onError = new Event<Dynamic->Void> ();
	public var onProgress = new Event<Dynamic->Void> ();
	
	#if (cpp || neko)
	private var __synchronous:Bool;
	private var __workCompleted:Int;
	private var __workIncoming = new Deque<ThreadPoolMessage> ();
	private var __workQueued:Int;
	private var __workResult = new Deque<ThreadPoolMessage> ();
	#end
	
	
	public function new (minThreads:Int = 0, maxThreads:Int = 1) {
		
		this.minThreads = minThreads;
		this.maxThreads = maxThreads;
		
		currentThreads = 0;
		
		#if (cpp || neko)
		__workQueued = 0;
		__workCompleted = 0;
		#end
		
	}
	
	
	//public function cancel (id:String):Void {
		//
		//
		//
	//}
	
	
	//public function isCanceled (id:String):Bool {
		//
		//
		//
	//}
	
	
	public function queue (state:Dynamic = null):Void {
		
		#if (cpp || neko)
		
		if (Application.current != null && !__synchronous) {
			
			__workIncoming.add (new ThreadPoolMessage (WORK, state));
			__workQueued++;
			
			if (currentThreads < maxThreads && currentThreads < (__workQueued - __workCompleted)) {
				
				currentThreads++;
				Thread.create (__doWork);
				
			}
			
			if (!Application.current.onUpdate.has (__update)) {
				
				Application.current.onUpdate.add (__update);
				
			}
			
		} else {
			
			__synchronous = true;
			doWork.dispatch (state);
			
		}
		
		#else
		
		doWork.dispatch (state);
		
		#end
		
	}
	
	
	public function sendComplete (state:Dynamic = null):Void {
		
		#if (cpp || neko)
		if (!__synchronous) {
			
			__workResult.add (new ThreadPoolMessage (COMPLETE, state));
			return;
			
		}
		#end
		
		onComplete.dispatch (state);
		
	}
	
	
	public function sendError (state:Dynamic = null):Void {
		
		#if (cpp || neko)
		if (!__synchronous) {
			
			__workResult.add (new ThreadPoolMessage (ERROR, state));
			return;
			
		}
		#end
		
		onError.dispatch (state);
		
	}
	
	
	public function sendProgress (state:Dynamic = null):Void {
		
		#if (cpp || neko)
		if (!__synchronous) {
			
			__workResult.add (new ThreadPoolMessage (PROGRESS, state));
			return;
			
		}
		#end
		
		onProgress.dispatch (state);
		
	}
	
	
	#if (cpp || neko)
{'fraction_non_alphanumeric': 0.12944466640031962, 'fraction_numerical': 0.001997602876548142, 'mean_word_length': 3.1690258118234804, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, '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': '7525494', 'n_tokens_mistral': 2063, 'n_tokens_neox': 1717, 'n_words': 408}