Thread.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.lang.Runnable;
  24. import java.lang.System;
  25. import java.lang.Thread as JavaThread;
  26. import java.util.Collections;
  27. import java.util.WeakHashMap;
  28. import java.util.concurrent.LinkedBlockingDeque;
  29. import java.util.concurrent.atomic.AtomicInteger;
  30. import jvm.Int64 as Long;
  31. private typedef ThreadImpl = HaxeThread;
  32. abstract Thread(ThreadImpl) from ThreadImpl {
  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 // TODO: keep only if events are actually used
  64. static function processEvents():Void {
  65. current().getHandle().events.loop();
  66. }
  67. }
  68. private class HaxeThread {
  69. static var nativeThreads:java.util.Map<JavaThread, HaxeThread>;
  70. static var mainJavaThread:JavaThread;
  71. static var mainHaxeThread:HaxeThread;
  72. static function __init__() {
  73. nativeThreads = Collections.synchronizedMap(new WeakHashMap<JavaThread, HaxeThread>());
  74. mainJavaThread = JavaThread.currentThread();
  75. mainHaxeThread = new HaxeThread();
  76. mainHaxeThread.events = new EventLoop();
  77. }
  78. public final messages = new LinkedBlockingDeque<Dynamic>();
  79. public var events(default, null):Null<EventLoop>;
  80. public static function create(job:() -> Void, withEventLoop:Bool):HaxeThread {
  81. var hx = new HaxeThread();
  82. if (withEventLoop)
  83. hx.events = new EventLoop();
  84. var thread = new NativeHaxeThread(hx, job, withEventLoop);
  85. thread.setDaemon(true);
  86. thread.start();
  87. return hx;
  88. }
  89. public static function get(javaThread:JavaThread):HaxeThread {
  90. if (javaThread == mainJavaThread) {
  91. return mainHaxeThread;
  92. } else if (javaThread is NativeHaxeThread) {
  93. return (cast javaThread : NativeHaxeThread).haxeThread;
  94. } else {
  95. switch nativeThreads.get(javaThread) {
  96. case null:
  97. var hx = new HaxeThread();
  98. nativeThreads.put(javaThread, hx);
  99. return hx;
  100. case hx:
  101. return hx;
  102. }
  103. }
  104. }
  105. public static function runWithEventLoop(job:() -> Void):Void {
  106. var thread = get(JavaThread.currentThread());
  107. if (thread.events == null) {
  108. thread.events = new EventLoop();
  109. try {
  110. job();
  111. thread.events.loop();
  112. thread.events = null;
  113. } catch (e) {
  114. thread.events = null;
  115. throw e;
  116. }
  117. } else {
  118. job();
  119. }
  120. }
  121. function new() {}
  122. public function sendMessage(msg:Dynamic):Void {
  123. messages.add(msg);
  124. }
  125. public function readMessage(block:Bool):Dynamic {
  126. return block ? messages.take() : messages.poll();
  127. }
  128. }
  129. private class NativeHaxeThread extends java.lang.Thread {
  130. public final haxeThread:HaxeThread;
  131. final withEventLoop:Bool;
  132. public function new(haxeThread:HaxeThread, job:() -> Void, withEventLoop:Bool) {
  133. super(new Job(job));
  134. this.haxeThread = haxeThread;
  135. this.withEventLoop = withEventLoop;
  136. }
  137. override overload public function run() {
  138. super.run();
  139. if (withEventLoop)
  140. haxeThread.events.loop();
  141. }
  142. }
  143. private abstract Job(Runnable) from Runnable to Runnable {
  144. public inline function new(job:() -> Void) {
  145. this = cast job;
  146. }
  147. }