Thread.hx 4.8 KB

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