2
0

Thread.hx 4.7 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. import cs.system.threading.Thread as NativeThread;
  24. import cs.system.WeakReference;
  25. import cs.Lib;
  26. abstract Thread(HaxeThread) {
  27. public var events(get,never):EventLoop;
  28. inline function new(thread:HaxeThread) {
  29. this = thread;
  30. }
  31. public static function create(job:Void->Void):Thread {
  32. var hx:Null<HaxeThread> = null;
  33. var native = new NativeThread(job);
  34. native.IsBackground = true;
  35. hx = HaxeThread.allocate(native, false);
  36. native.Start();
  37. return new Thread(hx);
  38. }
  39. public static inline function runWithEventLoop(job:()->Void):Void {
  40. HaxeThread.runWithEventLoop(job);
  41. }
  42. public static inline function createWithEventLoop(job:()->Void):Thread {
  43. var hx:Null<HaxeThread> = null;
  44. var native = new NativeThread(() -> {
  45. job();
  46. if(hx == null) {
  47. HaxeThread.get(NativeThread.CurrentThread).events.loop();
  48. } else {
  49. hx.events.loop();
  50. }
  51. });
  52. native.IsBackground = true;
  53. hx = HaxeThread.allocate(native, true);
  54. native.Start();
  55. return new Thread(hx);
  56. }
  57. public static inline function current():Thread {
  58. return new Thread(HaxeThread.get(NativeThread.CurrentThread));
  59. }
  60. public static function readMessage(block:Bool):Dynamic {
  61. return current().readMessageImpl(block);
  62. }
  63. public inline function sendMessage(msg:Dynamic):Void {
  64. this.sendMessage(msg);
  65. }
  66. inline function readMessageImpl(block:Bool):Dynamic {
  67. return this.readMessage(block);
  68. }
  69. function get_events():EventLoop {
  70. if(this.events == null)
  71. throw new NoEventLoopException();
  72. return this.events;
  73. }
  74. @:keep
  75. static function initEventLoop():Void {
  76. @:privateAccess HaxeThread.get(NativeThread.CurrentThread).events = new EventLoop();
  77. }
  78. @:keep
  79. static function processEvents():Void {
  80. HaxeThread.get(NativeThread.CurrentThread).events.loop();
  81. }
  82. }
  83. private class HaxeThread {
  84. static final mainNativeThread = NativeThread.CurrentThread;
  85. static final mainHaxeThread = new HaxeThread(NativeThread.CurrentThread);
  86. static final threads = new Map<Int, WeakReference>();
  87. static final threadsMutex = new cs.system.threading.Mutex();
  88. static var allocateCount = 0;
  89. public final native:NativeThread;
  90. public var events(default,null):Null<EventLoop>;
  91. final messages = new Deque<Dynamic>();
  92. public static function get(native:NativeThread):HaxeThread {
  93. if(native == mainNativeThread) {
  94. return mainHaxeThread;
  95. }
  96. var native = NativeThread.CurrentThread;
  97. var key = native.ManagedThreadId;
  98. threadsMutex.WaitOne();
  99. var ref = threads.get(key);
  100. threadsMutex.ReleaseMutex();
  101. if (ref == null || !ref.IsAlive) {
  102. return allocate(native, false);
  103. }
  104. return ref.Target;
  105. }
  106. public static function allocate(native:NativeThread, withEventLoop:Bool):HaxeThread {
  107. threadsMutex.WaitOne();
  108. allocateCount++;
  109. inline function cleanup() {
  110. if (allocateCount % 100 == 0) {
  111. for (key => ref in threads) {
  112. if (!ref.IsAlive) {
  113. threads.remove(key);
  114. }
  115. }
  116. }
  117. }
  118. var hx = new HaxeThread(native);
  119. if(withEventLoop)
  120. hx.events = new EventLoop();
  121. var ref = new WeakReference(hx);
  122. cleanup();
  123. threads.set(native.ManagedThreadId, ref);
  124. threadsMutex.ReleaseMutex();
  125. return hx;
  126. }
  127. public static function runWithEventLoop(job:()->Void):Void {
  128. var thread = get(NativeThread.CurrentThread);
  129. if(thread.events == null) {
  130. thread.events = new EventLoop();
  131. try {
  132. job();
  133. thread.events.loop();
  134. thread.events = null;
  135. } catch(e) {
  136. thread.events = null;
  137. throw e;
  138. }
  139. } else {
  140. job();
  141. }
  142. }
  143. function new(native:NativeThread) {
  144. this.native = native;
  145. }
  146. public inline function readMessage(block:Bool):Dynamic {
  147. return messages.pop(block);
  148. }
  149. public function sendMessage(msg:Dynamic):Void {
  150. messages.add(msg);
  151. }
  152. }