Skip to content

Commit

Permalink
changed the way mainloop events are executed within thread event loop (
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaxyh committed Apr 17, 2023
1 parent 07f49ff commit 32964ee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
32 changes: 1 addition & 31 deletions std/haxe/MainLoop.hx
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ class MainEvent {

@:access(haxe.MainEvent)
class MainLoop {
#if (target.threaded && !cppia)
static var eventLoopHandler:Null<EventHandler>;
static var mutex = new sys.thread.Mutex();
static var mainThread(get,never) : Thread;
static var _mainThread : Thread = Thread.current();
static function get_mainThread() {
if( _mainThread == null ) _mainThread = Thread.current();
return _mainThread;
}
#end

static var pending:MainEvent;

Expand Down Expand Up @@ -99,7 +89,7 @@ class MainLoop {
/**
Add a pending event to be run into the main loop.
**/
public static function add(f:Void->Void, priority = 0):MainEvent@:privateAccess {
public static function add(f:Void->Void, priority = 0) : MainEvent {
if (f == null)
throw "Event function is null";
var e = new MainEvent(f, priority);
Expand All @@ -108,29 +98,9 @@ class MainLoop {
head.prev = e;
e.next = head;
pending = e;
injectIntoEventLoop(0);
return e;
}

static function injectIntoEventLoop(waitMs:Int) {
#if (target.threaded && !cppia)
mutex.acquire();
if(eventLoopHandler != null)
mainThread.events.cancel(eventLoopHandler);
eventLoopHandler = mainThread.events.repeat(
() -> {
mainThread.events.cancel(eventLoopHandler);
var wait = tick();
if(hasEvents()) {
injectIntoEventLoop(Std.int(wait * 1000));
}
},
waitMs
);
mutex.release();
#end
}

static function sortEvents() {
// pending = haxe.ds.ListSort.sort(pending, function(e1, e2) return e1.nextRun > e2.nextRun ? -1 : 1);
// we can't use directly ListSort because it requires prev/next to be public, which we don't want here
Expand Down
19 changes: 17 additions & 2 deletions std/sys/thread/EventLoop.hx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ class EventLoop {
final waitLock = new Lock();
var promisedEventsCount = 0;
var regularEvents:Null<RegularEvent>;
var isMainThread:Bool;
static var CREATED : Bool;

public function new():Void {}
public function new():Void {
isMainThread = !CREATED;
CREATED = true;
}

/**
Schedule event for execution every `intervalMs` milliseconds in current loop.
Expand Down Expand Up @@ -222,7 +227,7 @@ class EventLoop {

// Run regular events
for(i in 0...eventsToRunIdx) {
if(!regularsToRun[i].cancelled)
if(!regularsToRun[i].cancelled)
regularsToRun[i].run();
regularsToRun[i] = null;
}
Expand Down Expand Up @@ -250,6 +255,16 @@ class EventLoop {
oneTimersToRun[i] = null;
}

// run main events
if( isMainThread ) {
var next = @:privateAccess haxe.MainLoop.tick();
if( haxe.MainLoop.hasEvents() ) {
eventsToRunIdx++;
if( nextEventAt > next )
nextEventAt = next;
}
}

// Some events were executed. They could add new events to run.
if(eventsToRunIdx > 0) {
nextEventAt = -2;
Expand Down

0 comments on commit 32964ee

Please sign in to comment.