Thread.hx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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) {
  26. this.sendMessage(msg);
  27. }
  28. public static inline function readMessage(block = true):Dynamic {
  29. return HaxeThread.current().readMessage(block);
  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 current():Thread {
  41. return HaxeThread.current();
  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. private typedef ThreadHandle = hl.Abstract<"hl_thread">;
  58. private class HaxeThread {
  59. static final mainThreadHandle:ThreadHandle = currentHandle();
  60. static final mainThread:HaxeThread = new HaxeThread();
  61. static final threads = new Array<{thread:HaxeThread, handle:ThreadHandle}>();
  62. static final threadsMutex = new Mutex();
  63. public var events(default,null):Null<EventLoop>;
  64. final messages = new Deque();
  65. static var ids = 0;
  66. var id = ids++;
  67. @:hlNative("std", "thread_create")
  68. static function createHandle(callb:Void->Void):ThreadHandle {
  69. return null;
  70. }
  71. @:hlNative("std", "thread_current")
  72. static function currentHandle():ThreadHandle {
  73. return null;
  74. }
  75. static public function current():HaxeThread {
  76. var handle = currentHandle();
  77. if(handle == mainThreadHandle) {
  78. return mainThread;
  79. }
  80. threadsMutex.acquire();
  81. var thread = null;
  82. for(item in threads) {
  83. if(item.handle == handle) {
  84. thread = item.thread;
  85. break;
  86. }
  87. }
  88. if(thread == null) {
  89. thread = new HaxeThread();
  90. threads.push({thread:thread, handle:handle});
  91. }
  92. threadsMutex.release();
  93. return thread;
  94. }
  95. public static function create(callb:()->Void, withEventLoop:Bool):Thread {
  96. var item = {handle:null, thread:new HaxeThread()};
  97. threadsMutex.acquire();
  98. threads.push(item);
  99. threadsMutex.release();
  100. if(withEventLoop)
  101. item.thread.events = new EventLoop();
  102. item.handle = createHandle(() -> {
  103. if(item.handle == null) {
  104. item.handle = currentHandle();
  105. }
  106. try {
  107. callb();
  108. if(withEventLoop)
  109. item.thread.events.loop();
  110. } catch(e) {
  111. dropThread(item);
  112. throw e;
  113. }
  114. dropThread(item);
  115. });
  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(deleteItem) {
  135. threadsMutex.acquire();
  136. for(i => item in threads) {
  137. if(item == deleteItem) {
  138. threads.splice(i, 1);
  139. break;
  140. }
  141. }
  142. threadsMutex.release();
  143. }
  144. public function readMessage(block:Bool):Dynamic {
  145. return messages.pop(block);
  146. }
  147. public function new() {}
  148. public function sendMessage(msg:Dynamic) {
  149. messages.add(msg);
  150. }
  151. }