Thread.hx 4.5 KB

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