2
0

Thread.hx 5.0 KB

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