Thread.hx 2.8 KB

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