Thread.hx 3.9 KB

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