Thread.hx 3.1 KB

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