Last active
December 29, 2021 20:44
-
-
Save ruby0x1/e66e72ec702bdcedf5af45f8f4712109 to your computer and use it in GitHub Desktop.
A simple Haxe class for easily running threads and calling functions on the primary thread.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
#if cpp | |
import cpp.vm.Thread; | |
import cpp.vm.Deque; | |
#elseif neko | |
import neko.vm.Thread; | |
import neko.vm.Deque; | |
#end | |
/** | |
A simple Haxe class for easily running threads and calling functions on the primary thread. | |
from https://github.com/underscorediscovery/ | |
Usage: | |
- call Runner.init() from your primary thread | |
- call Runner.run() periodically to service callbacks (i.e inside your main loop) | |
- use Runner.thread(function() { ... }) to make a thread | |
- use Runner.call_primary(function() { ... }) to run code on the main thread | |
- use call_primary_ret to run code on the main thread and wait for the return value | |
*/ | |
class Runner { | |
public static var primary : Thread; | |
static var queue : Deque<Void->Void>; | |
/** Call this on your thread to make primary, | |
the calling thread will be used for callbacks. */ | |
public static function init() { | |
queue = new Deque<Void->Void>(); | |
primary = Thread.current(); | |
} | |
/** Call this on the primary manually, | |
Returns the number of callbacks called. */ | |
public static function run() : Int { | |
var more = true; | |
var count = 0; | |
while(more) { | |
var item = queue.pop(false); | |
if(item != null) { | |
count++; item(); item = null; | |
} else { | |
more = false; break; | |
} | |
} | |
return count; | |
} //process | |
/** Call a function on the primary thread without waiting or blocking. | |
If you want return values see call_primary_ret */ | |
public static function call_primary( _fn:Void->Void ) { | |
queue.push(_fn); | |
} //call_primary | |
/** Call a function on the primary thread and wait for the return value. | |
This will block the calling thread for a maximum of _timeout, default to 0.1s. | |
To call without a return or blocking, use call_primary */ | |
public static function call_primary_ret<T>( _fn:Void->T, _timeout:Float=0.1 ) : Null<T> { | |
var res:T = null; | |
var start = haxe.Timer.stamp(); | |
var lock = new cpp.vm.Lock(); | |
//add to main to call this | |
queue.push(function() { | |
res = _fn(); | |
lock.release(); | |
}); | |
//wait for the lock release or timeout | |
lock.wait(_timeout); | |
//clean up | |
lock = null; | |
//return result | |
return res; | |
} //call_primary_ret | |
/** Create a thread using the given function */ | |
public static function thread( fn:Void->Void ) : Thread { | |
return Thread.create( fn ); | |
} | |
} //Runner |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment