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