Thread.hx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys.thread;
  23. private typedef ThreadImpl = HxThread;
  24. abstract Thread(ThreadImpl) from ThreadImpl {
  25. public var events(get,never):EventLoop;
  26. public static inline function current():Thread {
  27. return HxThread.current();
  28. }
  29. public static inline function create(callb:Void->Void):Thread {
  30. return HxThread.create(callb, false);
  31. }
  32. public static inline function runWithEventLoop(job:()->Void):Void {
  33. HxThread.runWithEventLoop(job);
  34. }
  35. public static inline function createWithEventLoop(job:()->Void):Thread {
  36. return HxThread.create(job, true);
  37. }
  38. public static inline function readMessage(block:Bool):Dynamic {
  39. return HxThread.readMessage(block);
  40. }
  41. public inline function sendMessage(msg:Dynamic):Void {
  42. this.sendMessage(msg);
  43. }
  44. function get_events():EventLoop {
  45. if(this.events == null)
  46. throw new NoEventLoopException();
  47. return this.events;
  48. }
  49. @:keep
  50. static function initEventLoop() {
  51. @:privateAccess HxThread.current().events = new EventLoop();
  52. }
  53. @:keep
  54. static public function processEvents() {
  55. HxThread.current().events.loop();
  56. }
  57. }
  58. private class HxThread {
  59. public var events(default,null):Null<EventLoop>;
  60. final nativeThread:NativeThread;
  61. final messages = new Deque<Dynamic>();
  62. static var threads = new haxe.ds.ObjectMap<NativeThread, HxThread>();
  63. static var threadsMutex: Mutex = new Mutex();
  64. static var mainThread: HxThread;
  65. private function new(t:NativeThread) {
  66. nativeThread = t;
  67. }
  68. public function sendMessage(msg:Dynamic):Void {
  69. messages.add(msg);
  70. }
  71. public static function current():HxThread {
  72. threadsMutex.acquire();
  73. var ct = PyThreadingAPI.current_thread();
  74. if (ct == PyThreadingAPI.main_thread()) {
  75. if (mainThread == null) mainThread = new HxThread(ct);
  76. threadsMutex.release();
  77. return mainThread;
  78. }
  79. // If the current thread was not created via the haxe API, it can still be wrapped
  80. if (!threads.exists(ct)) {
  81. threads.set(ct, new HxThread(ct));
  82. }
  83. var t = threads.get(ct);
  84. threadsMutex.release();
  85. return t;
  86. }
  87. public static function create(callb:Void->Void, withEventLoop:Bool):HxThread {
  88. var nt:NativeThread = null;
  89. var t:HxThread = null;
  90. // Wrap the callback so it will clear the thread reference once the thread is finished
  91. var wrappedCallB = () -> {
  92. try {
  93. callb();
  94. if(withEventLoop)
  95. t.events.loop();
  96. } catch(e) {
  97. dropThread(nt);
  98. throw e;
  99. }
  100. dropThread(nt);
  101. }
  102. nt = new NativeThread(null, wrappedCallB);
  103. t = new HxThread(nt);
  104. if(withEventLoop)
  105. t.events = new EventLoop();
  106. threadsMutex.acquire();
  107. threads.set(nt, t);
  108. threadsMutex.release();
  109. nt.start();
  110. return t;
  111. }
  112. public static function runWithEventLoop(job:()->Void):Void {
  113. var thread = current();
  114. if(thread.events == null) {
  115. thread.events = new EventLoop();
  116. try {
  117. job();
  118. thread.events.loop();
  119. thread.events = null;
  120. } catch(e) {
  121. thread.events = null;
  122. throw e;
  123. }
  124. } else {
  125. job();
  126. }
  127. }
  128. static inline function dropThread(nt:NativeThread) {
  129. threadsMutex.acquire();
  130. threads.remove(nt);
  131. threadsMutex.release();
  132. }
  133. public static function readMessage(block:Bool):Dynamic {
  134. return current().messages.pop(block);
  135. }
  136. }
  137. @:pythonImport("threading", "Thread")
  138. @:native("Thread")
  139. private extern class NativeThread {
  140. function new(group:Dynamic, target:Void->Void);
  141. function start():Void;
  142. }
  143. @:pythonImport("threading")
  144. @:native("threading")
  145. private extern class PyThreadingAPI {
  146. static function current_thread():NativeThread;
  147. static function main_thread():NativeThread;
  148. }