Thread.hx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. abstract Thread(HaxeThread) from HaxeThread {
  29. inline function new(t:HaxeThread) {
  30. this = t;
  31. }
  32. public static inline function create(callb:()->Void):Thread {
  33. return HaxeThread.create(callb);
  34. }
  35. public static inline function current():Thread {
  36. return HaxeThread.get(JavaThread.currentThread());
  37. }
  38. public static inline function readMessage(block:Bool):Dynamic {
  39. return current().getHandle().readMessage(block);
  40. }
  41. public inline function sendMessage(msg:Dynamic):Void {
  42. this.sendMessage(msg);
  43. }
  44. inline function getHandle():HaxeThread {
  45. return this;
  46. }
  47. }
  48. private class HaxeThread {
  49. static final nativeThreads = Collections.synchronizedMap(new WeakHashMap<JavaThread,HaxeThread>());
  50. static final mainJavaThread = JavaThread.currentThread();
  51. static final mainHaxeThread = new HaxeThread();
  52. public final messages = new Deque<Dynamic>();
  53. public static function create(callb:()->Void):HaxeThread {
  54. var hx = new HaxeThread();
  55. var thread = new NativeHaxeThread(hx, callb);
  56. thread.setDaemon(true);
  57. thread.start();
  58. return hx;
  59. }
  60. public static function get(javaThread:JavaThread):HaxeThread {
  61. if(javaThread == mainJavaThread) {
  62. return mainHaxeThread;
  63. } else if(Std.isOfType(javaThread, NativeHaxeThread)) {
  64. return (cast javaThread:NativeHaxeThread).haxeThread;
  65. } else {
  66. switch nativeThreads.get(javaThread) {
  67. case null:
  68. var hx = new HaxeThread();
  69. nativeThreads.put(javaThread, hx);
  70. return hx;
  71. case hx:
  72. return hx;
  73. }
  74. }
  75. }
  76. function new() {}
  77. public inline function sendMessage(msg:Dynamic):Void {
  78. messages.add(msg);
  79. }
  80. public inline function readMessage(block:Bool):Dynamic {
  81. return messages.pop(block);
  82. }
  83. }
  84. private class NativeHaxeThread extends java.lang.Thread {
  85. public final haxeThread:HaxeThread;
  86. public function new(haxeThread:HaxeThread, callb:()->Void) {
  87. super((cast callb:Runnable));
  88. this.haxeThread = haxeThread;
  89. }
  90. }