Thread.hx 3.9 KB

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