Thread.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. @:callable
  24. @:coreType
  25. private abstract ThreadHandle {}
  26. abstract Thread(ThreadHandle) {
  27. inline function new(h:ThreadHandle):Void {
  28. this = h;
  29. }
  30. public inline function sendMessage(msg:Dynamic):Void {
  31. thread_send(this, msg);
  32. }
  33. public static inline function current():Thread {
  34. return new Thread(thread_current());
  35. }
  36. public static inline function create(callb:Void->Void):Thread {
  37. return new Thread(thread_create(function(_) {
  38. return callb();
  39. }, null));
  40. }
  41. public static inline function readMessage(block:Bool):Dynamic {
  42. return thread_read_message(block);
  43. }
  44. @:op(A == B)
  45. inline function equals(other:Thread):Bool {
  46. return getHandle() == other.getHandle();
  47. }
  48. private inline function getHandle():ThreadHandle {
  49. return this;
  50. }
  51. /**
  52. Starts an OS message loop after [osInitialize] has been done.
  53. In that state, the UI handled by this thread will be updated and
  54. [sync] calls can be performed. The loop returns when [exitLoop] is
  55. called for this thread.
  56. **
  57. public static function osLoop() {
  58. if( os_loop == null ) throw "Please call osInitialize() first";
  59. os_loop();
  60. }
  61. /**
  62. The function [f] will be called by this thread if it's in [osLoop].
  63. [sync] returns immediately. See [osInitialize] remarks.
  64. **
  65. public function sync( f : Void -> Void ) {
  66. os_sync(handle,f);
  67. }
  68. /**
  69. The function [f] will be called by this thread and the calling thread
  70. will wait until the result is available then return its value.
  71. **
  72. public function syncResult<T>( f : Void -> T ) : T {
  73. if( this == current() )
  74. return f();
  75. var v = new neko.vm.Lock();
  76. var r = null;
  77. sync(function() {
  78. r = f();
  79. v.release();
  80. });
  81. v.wait();
  82. return r;
  83. }
  84. /**
  85. Exit from [osLoop].
  86. **
  87. public function exitLoop() {
  88. os_loop_stop(handle);
  89. }
  90. /**
  91. If you want to use the [osLoop], [sync] and [syncResult] methods, you
  92. need to call [osInitialize] before creating any thread or calling [current].
  93. This will load [os.ndll] library and initialize UI methods for each thread.
  94. **
  95. public static function osInitialize() {
  96. os_loop = neko.Lib.load("os","os_loop",0);
  97. os_loop_stop = neko.Lib.load("os","os_loop_stop",1);
  98. os_sync = neko.Lib.load("os","os_sync",2);
  99. }
  100. static var os_loop = null;
  101. static var os_loop_stop = null;
  102. static var os_sync = null;
  103. */
  104. static var thread_create = neko.Lib.load("std", "thread_create", 2);
  105. static var thread_current = neko.Lib.load("std", "thread_current", 0);
  106. static var thread_send = neko.Lib.load("std", "thread_send", 2);
  107. static var thread_read_message = neko.Lib.load("std", "thread_read_message", 1);
  108. }