Thread.hx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 java.Lib;
  24. import java.lang.Runnable;
  25. import java.util.WeakHashMap;
  26. import java.util.Collections;
  27. import java.lang.Thread as JavaThread;
  28. import java.lang.System;
  29. import java.StdTypes.Int64 as Long;
  30. import java.util.concurrent.atomic.AtomicInteger;
  31. import java.util.concurrent.LinkedBlockingDeque;
  32. abstract Thread(HaxeThread) from HaxeThread {
  33. public var events(get,never):EventLoop;
  34. inline function new(t:HaxeThread) {
  35. this = t;
  36. }
  37. public static inline function create(job:()->Void):Thread {
  38. return HaxeThread.create(job, false);
  39. }
  40. public static inline function current():Thread {
  41. return HaxeThread.get(JavaThread.currentThread());
  42. }
  43. public static inline function runWithEventLoop(job:()->Void):Void {
  44. HaxeThread.runWithEventLoop(job);
  45. }
  46. public static inline function createWithEventLoop(job:()->Void):Thread {
  47. return HaxeThread.create(job, true);
  48. }
  49. public static inline function readMessage(block:Bool):Dynamic {
  50. return current().getHandle().readMessage(block);
  51. }
  52. public inline function sendMessage(msg:Dynamic):Void {
  53. this.sendMessage(msg);
  54. }
  55. inline function getHandle():HaxeThread {
  56. return this;
  57. }
  58. function get_events():EventLoop {
  59. if(this.events == null)
  60. throw new NoEventLoopException();
  61. return this.events;
  62. }
  63. @:keep
  64. static function initEventLoop() {
  65. @:privateAccess HaxeThread.get(JavaThread.currentThread()).events = new EventLoop();
  66. }
  67. @:keep //TODO: keep only if events are actually used
  68. static function processEvents():Void {
  69. current().getHandle().events.loop();
  70. }
  71. }
  72. private class HaxeThread {
  73. static final nativeThreads = Collections.synchronizedMap(new WeakHashMap<JavaThread,HaxeThread>());
  74. static final mainJavaThread = JavaThread.currentThread();
  75. static final mainHaxeThread = new HaxeThread();
  76. public final messages = new LinkedBlockingDeque<Dynamic>();
  77. public var events(default,null):Null<EventLoop>;
  78. public static function create(job:()->Void, withEventLoop:Bool):HaxeThread {
  79. var hx = new HaxeThread();
  80. if(withEventLoop)
  81. hx.events = new EventLoop();
  82. var thread = new NativeHaxeThread(hx, job, withEventLoop);
  83. thread.setDaemon(true);
  84. thread.start();
  85. return hx;
  86. }
  87. public static function get(javaThread:JavaThread):HaxeThread {
  88. if(javaThread == mainJavaThread) {
  89. return mainHaxeThread;
  90. } else if(javaThread is NativeHaxeThread) {
  91. return (cast javaThread:NativeHaxeThread).haxeThread;
  92. } else {
  93. switch nativeThreads.get(javaThread) {
  94. case null:
  95. var hx = new HaxeThread();
  96. nativeThreads.put(javaThread, hx);
  97. return hx;
  98. case hx:
  99. return hx;
  100. }
  101. }
  102. }
  103. public static function runWithEventLoop(job:()->Void):Void {
  104. var thread = get(JavaThread.currentThread());
  105. if(thread.events == null) {
  106. thread.events = new EventLoop();
  107. try {
  108. job();
  109. thread.events.loop();
  110. thread.events = null;
  111. } catch(e) {
  112. thread.events = null;
  113. throw e;
  114. }
  115. } else {
  116. job();
  117. }
  118. }
  119. function new() {}
  120. public function sendMessage(msg:Dynamic):Void {
  121. messages.add(msg);
  122. }
  123. public function readMessage(block:Bool):Dynamic {
  124. return block ? messages.take() : messages.poll();
  125. }
  126. }
  127. private class NativeHaxeThread extends java.lang.Thread {
  128. public final haxeThread:HaxeThread;
  129. final withEventLoop:Bool;
  130. public function new(haxeThread:HaxeThread, job:()->Void, withEventLoop:Bool) {
  131. super(new Job(job));
  132. this.haxeThread = haxeThread;
  133. this.withEventLoop = withEventLoop;
  134. }
  135. override overload public function run() {
  136. super.run();
  137. if(withEventLoop)
  138. haxeThread.events.loop();
  139. }
  140. }
  141. #if jvm
  142. private abstract Job(Runnable) from Runnable to Runnable {
  143. public inline function new(job:()->Void) {
  144. this = cast job;
  145. }
  146. }
  147. #else
  148. private class Job implements Runnable {
  149. final job:()->Void;
  150. public function new(job:()->Void) {
  151. this.job = job;
  152. }
  153. public function run() {
  154. job();
  155. }
  156. }
  157. #end