Thread.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 ThreadHandle {}
  60. private class HaxeThread {
  61. static final threads = new Array<{thread:HaxeThread, handle:ThreadHandle}>();
  62. static final threadsMutex = new Mutex();
  63. static var mainThreadHandle:ThreadHandle = currentHandle();
  64. static var mainThread:HaxeThread = new HaxeThread(currentHandle());
  65. public var events(default,null):Null<EventLoop>;
  66. public var handle:ThreadHandle;
  67. final messages = new Deque<Dynamic>();
  68. static public function current():HaxeThread {
  69. var handle = currentHandle();
  70. if(handle == mainThreadHandle) {
  71. return mainThread;
  72. }
  73. threadsMutex.acquire();
  74. var thread = null;
  75. for(item in threads) {
  76. if(item.handle == handle) {
  77. thread = item.thread;
  78. break;
  79. }
  80. }
  81. if(thread == null) {
  82. thread = new HaxeThread(handle);
  83. threads.push({thread:thread, handle:handle});
  84. }
  85. threadsMutex.release();
  86. return thread;
  87. }
  88. public static function create(job:()->Void, withEventLoop:Bool):Thread {
  89. var item = {handle:null, thread:new HaxeThread(null)};
  90. threadsMutex.acquire();
  91. var index = threads.push(item);
  92. threadsMutex.release();
  93. if(withEventLoop)
  94. item.thread.events = new EventLoop();
  95. item.handle = createHandle(() -> {
  96. if(item.thread.handle == null) {
  97. item.handle = currentHandle();
  98. item.thread.handle = item.handle;
  99. }
  100. try {
  101. job();
  102. if(withEventLoop)
  103. item.thread.events.loop();
  104. } catch(e) {
  105. dropThread(item, index);
  106. throw e;
  107. }
  108. dropThread(item, index);
  109. });
  110. item.thread.handle = item.handle;
  111. return item.thread;
  112. }
  113. public static function runWithEventLoop(job:()->Void):Void {
  114. var thread = current();
  115. if(thread.events == null) {
  116. thread.events = new EventLoop();
  117. try {
  118. job();
  119. thread.events.loop();
  120. thread.events = null;
  121. } catch(e) {
  122. thread.events = null;
  123. throw e;
  124. }
  125. } else {
  126. job();
  127. }
  128. }
  129. static function dropThread(item, probableIndex:Int) {
  130. threadsMutex.acquire();
  131. if(threads[probableIndex] == item) {
  132. threads.splice(probableIndex, 1);
  133. } else {
  134. for(i => item2 in threads) {
  135. if(item2 == item) {
  136. threads.splice(i, 1);
  137. break;
  138. }
  139. }
  140. }
  141. threadsMutex.release();
  142. }
  143. function new(h:ThreadHandle):Void {
  144. handle = h;
  145. }
  146. public inline function sendMessage(msg:Dynamic):Void {
  147. messages.add(msg);
  148. }
  149. static inline function currentHandle():ThreadHandle {
  150. return untyped __global__.__hxcpp_thread_current();
  151. }
  152. static inline function createHandle(callb:Void->Void):ThreadHandle {
  153. return untyped __global__.__hxcpp_thread_create(callb);
  154. }
  155. public static inline function readMessage(block:Bool):Dynamic {
  156. return current().messages.pop(block);
  157. }
  158. }