Thread.hx 2.8 KB

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