Thread.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. abstract Thread(HxThread) from HxThread {
  24. public var events(get,never):EventLoop;
  25. public static inline function current():Thread {
  26. return HxThread.current();
  27. }
  28. public static inline function create(callb:Void->Void):Thread {
  29. return HxThread.create(callb, false);
  30. }
  31. public static inline function runWithEventLoop(job:()->Void):Void {
  32. HxThread.runWithEventLoop(job);
  33. }
  34. public static inline function createWithEventLoop(job:()->Void):Thread {
  35. return HxThread.create(job, true);
  36. }
  37. public static inline function readMessage(block:Bool):Dynamic {
  38. return HxThread.readMessage(block);
  39. }
  40. public inline function sendMessage(msg:Dynamic):Void {
  41. this.sendMessage(msg);
  42. }
  43. function get_events():EventLoop {
  44. if(this.events == null)
  45. throw new NoEventLoopException();
  46. return this.events;
  47. }
  48. @:keep
  49. static function initEventLoop() {
  50. @:privateAccess HxThread.current().events = new EventLoop();
  51. }
  52. @:keep
  53. static public function processEvents() {
  54. HxThread.current().events.loop();
  55. }
  56. }
  57. private class HxThread {
  58. public var events(default,null):Null<EventLoop>;
  59. final nativeThread:NativeThread;
  60. final messages = new Deque<Dynamic>();
  61. static var threads = new haxe.ds.ObjectMap<NativeThread, HxThread>();
  62. static var threadsMutex: Mutex = new Mutex();
  63. static var mainThread: HxThread;
  64. private function new(t:NativeThread) {
  65. nativeThread = t;
  66. }
  67. public function sendMessage(msg:Dynamic):Void {
  68. messages.add(msg);
  69. }
  70. public static function current():HxThread {
  71. threadsMutex.acquire();
  72. var ct = PyThreadingAPI.current_thread();
  73. if (ct == PyThreadingAPI.main_thread()) {
  74. if (mainThread == null) mainThread = new HxThread(ct);
  75. threadsMutex.release();
  76. return mainThread;
  77. }
  78. // If the current thread was not created via the haxe API, it can still be wrapped
  79. if (!threads.exists(ct)) {
  80. threads.set(ct, new HxThread(ct));
  81. }
  82. var t = threads.get(ct);
  83. threadsMutex.release();
  84. return t;
  85. }
  86. public static function create(callb:Void->Void, withEventLoop:Bool):HxThread {
  87. var nt:NativeThread = null;
  88. var t:HxThread = null;
  89. // Wrap the callback so it will clear the thread reference once the thread is finished
  90. var wrappedCallB = () -> {
  91. try {
  92. callb();
  93. if(withEventLoop)
  94. t.events.loop();
  95. } catch(e) {
  96. dropThread(nt);
  97. throw e;
  98. }
  99. dropThread(nt);
  100. }
  101. nt = new NativeThread(null, wrappedCallB);
  102. t = new HxThread(nt);
  103. if(withEventLoop)
  104. t.events = new EventLoop();
  105. threadsMutex.acquire();
  106. threads.set(nt, t);
  107. threadsMutex.release();
  108. nt.start();
  109. return t;
  110. }
  111. public static function runWithEventLoop(job:()->Void):Void {
  112. var thread = current();
  113. if(thread.events == null) {
  114. thread.events = new EventLoop();
  115. try {
  116. job();
  117. thread.events.loop();
  118. thread.events = null;
  119. } catch(e) {
  120. thread.events = null;
  121. throw e;
  122. }
  123. } else {
  124. job();
  125. }
  126. }
  127. static inline function dropThread(nt:NativeThread) {
  128. threadsMutex.acquire();
  129. threads.remove(nt);
  130. threadsMutex.release();
  131. }
  132. public static function readMessage(block:Bool):Dynamic {
  133. return current().messages.pop(block);
  134. }
  135. }
  136. @:pythonImport("threading", "Thread")
  137. @:native("Thread")
  138. private extern class NativeThread {
  139. function new(group:Dynamic, target:Void->Void);
  140. function start():Void;
  141. }
  142. @:pythonImport("threading")
  143. @:native("threading")
  144. private extern class PyThreadingAPI {
  145. static function current_thread():NativeThread;
  146. static function main_thread():NativeThread;
  147. }