Explorar el Código

changed the way mainloop events are executed within thread event loop (close #10682)

Nicolas Cannasse hace 3 años
padre
commit
f06078a2e1
Se han modificado 2 ficheros con 18 adiciones y 33 borrados
  1. 1 31
      std/haxe/MainLoop.hx
  2. 17 2
      std/sys/thread/EventLoop.hx

+ 1 - 31
std/haxe/MainLoop.hx

@@ -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;
 
@@ -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);
@@ -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

+ 17 - 2
std/sys/thread/EventLoop.hx

@@ -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.
@@ -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;
 		}
@@ -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;