Thread.hx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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(HaxeThread) from HaxeThread to HaxeThread {
  24. public var events(get,never):EventLoop;
  25. public inline function sendMessage(msg:Dynamic):Void {
  26. this.sendMessage(msg);
  27. }
  28. public static inline function current():Thread {
  29. return HaxeThread.current();
  30. }
  31. public static inline function create(job:()->Void):Thread {
  32. return HaxeThread.create(job, false);
  33. }
  34. public static inline function runWithEventLoop(job:()->Void):Void {
  35. HaxeThread.runWithEventLoop(job);
  36. }
  37. public static inline function createWithEventLoop(job:()->Void):Thread {
  38. return HaxeThread.create(job, true);
  39. }
  40. public static function readMessage(block:Bool):Dynamic {
  41. return HaxeThread.readMessage(block);
  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 HaxeThread.current().events = new EventLoop();
  51. }
  52. @:keep
  53. static public function processEvents() {
  54. HaxeThread.current().events.loop();
  55. }
  56. }
  57. @:callable
  58. @:coreType
  59. private abstract NativeThreadHandle {}
  60. private typedef ThreadHandle = NativeThreadHandle;
  61. private class HaxeThread {
  62. static final threads = new Array<{thread:HaxeThread, handle:ThreadHandle}>();
  63. static final threadsMutex = new Mutex();
  64. static var mainThreadHandle:ThreadHandle = currentHandle();
  65. static var mainThread:HaxeThread = new HaxeThread(currentHandle());
  66. public var events(default,null):Null<EventLoop>;
  67. public var handle:ThreadHandle;
  68. final messages = new Deque<Dynamic>();
  69. static public function current():HaxeThread {
  70. var handle = currentHandle();
  71. if(handle == mainThreadHandle) {
  72. return mainThread;
  73. }
  74. threadsMutex.acquire();
  75. var thread = null;
  76. for(item in threads) {
  77. if(item.handle == handle) {
  78. thread = item.thread;
  79. break;
  80. }
  81. }
  82. if(thread == null) {
  83. thread = new HaxeThread(handle);
  84. threads.push({thread:thread, handle:handle});
  85. }
  86. threadsMutex.release();
  87. return thread;
  88. }
  89. public static function create(job:()->Void, withEventLoop:Bool):Thread {
  90. var item = {handle:null, thread:new HaxeThread(null)};
  91. threadsMutex.acquire();
  92. var index = threads.push(item);
  93. threadsMutex.release();
  94. if(withEventLoop)
  95. item.thread.events = new EventLoop();
  96. item.handle = createHandle(() -> {
  97. if(item.thread.handle == null) {
  98. item.handle = currentHandle();
  99. item.thread.handle = item.handle;
  100. }
  101. try {
  102. job();
  103. if(withEventLoop)
  104. item.thread.events.loop();
  105. } catch(e) {
  106. dropThread(item, index);
  107. throw e;
  108. }
  109. dropThread(item, index);
  110. });
  111. item.thread.handle = item.handle;
  112. return item.thread;
  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 function dropThread(item, probableIndex:Int) {
  131. threadsMutex.acquire();
  132. if(threads[probableIndex] == item) {
  133. threads.splice(probableIndex, 1);
  134. } else {
  135. for(i => item2 in threads) {
  136. if(item2 == item) {
  137. threads.splice(i, 1);
  138. break;
  139. }
  140. }
  141. }
  142. threadsMutex.release();
  143. }
  144. function new(h:ThreadHandle):Void {
  145. handle = h;
  146. }
  147. public inline function sendMessage(msg:Dynamic):Void {
  148. messages.add(msg);
  149. }
  150. static #if !scriptable inline #end function currentHandle():ThreadHandle {
  151. return untyped __global__.__hxcpp_thread_current();
  152. }
  153. static #if !scriptable inline #end function createHandle(callb:Void->Void):ThreadHandle {
  154. return untyped __global__.__hxcpp_thread_create(callb);
  155. }
  156. public static #if !scriptable inline #end function readMessage(block:Bool):Dynamic {
  157. return current().messages.pop(block);
  158. }
  159. }