Thread.hx 4.6 KB

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