2
0

Thread.hx 4.3 KB

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