Thread.hx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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) {
  27. this.sendMessage(msg);
  28. }
  29. public static inline function readMessage(block = true):Dynamic {
  30. return HaxeThread.current().readMessage(block);
  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 current():Thread {
  42. return HaxeThread.current();
  43. }
  44. public function setName( name : String ) {
  45. #if (hl_ver >= version("1.13.0"))
  46. set_name(@:privateAccess this.handle, @:privateAccess name.toUtf8());
  47. #end
  48. }
  49. public function getName() : Null<String> {
  50. #if (hl_ver >= version("1.13.0"))
  51. var name = get_name(@:privateAccess this.handle);
  52. return name == null ? null : @:privateAccess String.fromUTF8(name);
  53. #else
  54. return null;
  55. #end
  56. }
  57. #if (hl_ver >= version("1.13.0"))
  58. @:hlNative("?std", "thread_set_name") static function set_name( t : ThreadHandle, name : hl.Bytes ) {}
  59. @:hlNative("?std", "thread_get_name") static function get_name( t : ThreadHandle ) : hl.Bytes { return null; }
  60. #end
  61. function get_events():EventLoop {
  62. if(this.events == null)
  63. throw new NoEventLoopException();
  64. return this.events;
  65. }
  66. @:keep
  67. static public function processEvents() {
  68. HaxeThread.current().events.loop();
  69. }
  70. }
  71. private typedef ThreadHandle = hl.Abstract<"hl_thread">;
  72. private class HaxeThread {
  73. static var mainThread:HaxeThread;
  74. static var threads:Array<HaxeThread>;
  75. static var threadsMutex:Mutex;
  76. static var UID = 0;
  77. static function __init__() {
  78. threadsMutex = new Mutex();
  79. threads = [];
  80. mainThread = new HaxeThread(currentHandle());
  81. mainThread.events = new EventLoop();
  82. }
  83. var id = UID++;
  84. public var events(default,null):Null<EventLoop>;
  85. var handle : ThreadHandle;
  86. final messages = new Deque();
  87. @:hlNative("std", "thread_create")
  88. static function createHandle(callb:Void->Void):ThreadHandle {
  89. return null;
  90. }
  91. @:hlNative("std", "thread_current")
  92. static function currentHandle():ThreadHandle {
  93. return null;
  94. }
  95. static public function current():HaxeThread {
  96. var handle = currentHandle();
  97. if(handle == mainThread.handle) {
  98. return mainThread;
  99. }
  100. threadsMutex.acquire();
  101. var thread = null;
  102. for(item in threads) {
  103. if(item.handle == handle) {
  104. thread = item;
  105. break;
  106. }
  107. }
  108. if(thread == null) {
  109. thread = new HaxeThread(handle);
  110. threads.push(thread);
  111. }
  112. threadsMutex.release();
  113. return thread;
  114. }
  115. public static function create(callb:()->Void, withEventLoop:Bool):Thread {
  116. var item = new HaxeThread(null);
  117. threadsMutex.acquire();
  118. threads.push(item);
  119. threadsMutex.release();
  120. if(withEventLoop)
  121. item.events = new EventLoop();
  122. item.handle = createHandle(() -> {
  123. if(item.handle == null) {
  124. item.handle = currentHandle();
  125. }
  126. try {
  127. hl.Api.setErrorHandler(function(_){});
  128. callb();
  129. if(withEventLoop)
  130. item.events.loop();
  131. } catch(e) {
  132. hl.Api.setErrorHandler(null);
  133. dropThread(item);
  134. hl.Api.rethrow(e);
  135. }
  136. dropThread(item);
  137. });
  138. return item;
  139. }
  140. public static function runWithEventLoop(job:()->Void):Void {
  141. var thread = current();
  142. if(thread.events == null) {
  143. thread.events = new EventLoop();
  144. try {
  145. job();
  146. thread.events.loop();
  147. thread.events = null;
  148. } catch(e) {
  149. thread.events = null;
  150. throw e;
  151. }
  152. } else {
  153. job();
  154. }
  155. }
  156. static function dropThread(deleteItem) {
  157. threadsMutex.acquire();
  158. for(i => item in threads) {
  159. if(item == deleteItem) {
  160. threads.splice(i, 1);
  161. break;
  162. }
  163. }
  164. threadsMutex.release();
  165. }
  166. public function readMessage(block:Bool):Dynamic {
  167. return messages.pop(block);
  168. }
  169. public function new(h) {
  170. handle = h;
  171. }
  172. public function sendMessage(msg:Dynamic) {
  173. messages.add(msg);
  174. }
  175. }