2
0

Thread.hx 4.9 KB

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