Thread.hx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. class Thread {
  24. var nativeThread: NativeThread;
  25. var messages: Deque<Dynamic>;
  26. static var threads = new haxe.ds.ObjectMap<NativeThread, Thread>();
  27. static var threadsMutex: Mutex = new Mutex();
  28. static var mainThread: Thread;
  29. private function new(t:NativeThread) {
  30. nativeThread = t;
  31. messages = new Deque<Dynamic>();
  32. }
  33. public function sendMessage(msg:Dynamic):Void {
  34. messages.add(msg);
  35. }
  36. public static function current():Thread {
  37. threadsMutex.acquire();
  38. var ct = PyThreadingAPI.current_thread();
  39. if (ct == PyThreadingAPI.main_thread()) {
  40. if (mainThread == null) mainThread = new Thread(ct);
  41. threadsMutex.release();
  42. return mainThread;
  43. }
  44. // If the current thread was not created via the haxe API, it can still be wrapped
  45. if (!threads.exists(ct)) {
  46. threads.set(ct, new Thread(ct));
  47. }
  48. var t = threads.get(ct);
  49. threadsMutex.release();
  50. return t;
  51. }
  52. public static function create(callb:Void->Void):Thread {
  53. var nt:NativeThread = null;
  54. // Wrap the callback so it will clear the thread reference once the thread is finished
  55. var wrappedCallB = () -> {
  56. callb();
  57. threadsMutex.acquire();
  58. threads.remove(nt);
  59. threadsMutex.release();
  60. }
  61. nt = new NativeThread(null, wrappedCallB);
  62. var t = new Thread(nt);
  63. threadsMutex.acquire();
  64. threads.set(nt, t);
  65. threadsMutex.release();
  66. nt.start();
  67. return t;
  68. }
  69. public static function readMessage(block:Bool):Dynamic {
  70. return current().messages.pop(block);
  71. }
  72. }
  73. @:pythonImport("threading", "Thread")
  74. @:native("Thread")
  75. private extern class NativeThread {
  76. function new(group:Dynamic, target:Void->Void);
  77. function start():Void;
  78. }
  79. @:pythonImport("threading")
  80. @:native("threading")
  81. private extern class PyThreadingAPI {
  82. static function current_thread():NativeThread;
  83. static function main_thread():NativeThread;
  84. }