Thread.hx 5.0 KB

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