Thread.hx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. abstract Thread(NativeThread) {
  25. inline function new(t:NativeThread)
  26. {
  27. this = t;
  28. }
  29. public static function create(callb:Void->Void):Thread
  30. {
  31. var ret = new NativeThread();
  32. var t = new HaxeThread(ret, callb);
  33. t.start();
  34. return new Thread(ret);
  35. }
  36. public static function current():Thread
  37. {
  38. return new Thread(NativeThread.getThread( java.lang.Thread.currentThread() ));
  39. }
  40. public static function readMessage(block : Bool) : Dynamic
  41. {
  42. return current().getHandle().messages.pop(block);
  43. }
  44. public inline function sendMessage(msg:Dynamic):Void
  45. {
  46. this.sendMessage(msg);
  47. }
  48. private inline function getHandle():NativeThread {
  49. return this;
  50. }
  51. }
  52. @:native('haxe.java.vm.Thread') private class NativeThread
  53. {
  54. @:private static var javaThreadToHaxe = new haxe.ds.WeakMap<java.lang.Thread, NativeThread>();
  55. @:private static var mainJavaThread = java.lang.Thread.currentThread();
  56. @:private static var mainHaxeThread = {
  57. var ret = new NativeThread();
  58. javaThreadToHaxe.set(mainJavaThread, ret);
  59. ret;
  60. };
  61. public static function getThread(jt:java.lang.Thread):NativeThread
  62. {
  63. if (Std.is(jt, HaxeThread))
  64. {
  65. var t:HaxeThread = cast jt;
  66. return t.threadObject;
  67. }
  68. else if (jt == mainJavaThread)
  69. {
  70. return mainHaxeThread;
  71. }
  72. else
  73. {
  74. var ret = null;
  75. untyped __lock__(javaThreadToHaxe, {
  76. ret = javaThreadToHaxe.get(jt);
  77. if (ret == null)
  78. {
  79. ret = new NativeThread();
  80. javaThreadToHaxe.set(jt, ret);
  81. }
  82. });
  83. return ret;
  84. }
  85. }
  86. public var messages:Deque<Dynamic>;
  87. public function new()
  88. {
  89. this.messages = new Deque();
  90. }
  91. public function sendMessage(msg:Dynamic):Void
  92. {
  93. messages.add(msg);
  94. }
  95. }
  96. @:native('haxe.java.vm.HaxeThread')
  97. private class HaxeThread extends java.lang.Thread
  98. {
  99. public var threadObject(default, null):NativeThread;
  100. private var runFunction:Void->Void;
  101. @:overload override public function run():Void
  102. {
  103. runFunction();
  104. }
  105. public function new(hxThread:NativeThread, run:Void->Void)
  106. {
  107. super();
  108. threadObject = hxThread;
  109. runFunction = run;
  110. setDaemon(true);
  111. }
  112. }