Thread.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package neko.vm;
  26. enum ThreadHandle {
  27. }
  28. class Thread {
  29. var handle : ThreadHandle;
  30. function new(h) {
  31. handle = h;
  32. }
  33. /**
  34. Send a message to the thread queue. This message can be readed by using [readMessage].
  35. **/
  36. public function sendMessage( msg : Dynamic ) {
  37. thread_send(handle,msg);
  38. }
  39. /**
  40. Returns the current thread.
  41. **/
  42. public static function current() {
  43. return new Thread(thread_current());
  44. }
  45. /**
  46. Creates a new thread that will execute the [callb] function, then exit.
  47. **/
  48. public static function create( callb : Void -> Void ) {
  49. return new Thread(thread_create(function(_) { return callb(); },null));
  50. }
  51. /**
  52. Reads a message from the thread queue. If [block] is true, the function
  53. blocks until a message is available. If [block] is false, the function
  54. returns [null] if no message is available.
  55. **/
  56. public static function readMessage( block : Bool ) : Dynamic {
  57. return thread_read_message(block);
  58. }
  59. function __compare(t) {
  60. return untyped __dollar__compare(handle,t.handle);
  61. }
  62. /**
  63. Starts an OS message loop after [osInitialize] has been done.
  64. In that state, the UI handled by this thread will be updated and
  65. [sync] calls can be performed. The loop returns when [exitLoop] is
  66. called for this thread.
  67. **
  68. public static function osLoop() {
  69. if( os_loop == null ) throw "Please call osInitialize() first";
  70. os_loop();
  71. }
  72. /**
  73. The function [f] will be called by this thread if it's in [osLoop].
  74. [sync] returns immediatly. See [osInitialize] remarks.
  75. **
  76. public function sync( f : Void -> Void ) {
  77. os_sync(handle,f);
  78. }
  79. /**
  80. The function [f] will be called by this thread and the calling thread
  81. will wait until the result is available then return its value.
  82. **
  83. public function syncResult<T>( f : Void -> T ) : T {
  84. if( this == current() )
  85. return f();
  86. var v = new neko.vm.Lock();
  87. var r = null;
  88. sync(function() {
  89. r = f();
  90. v.release();
  91. });
  92. v.wait();
  93. return r;
  94. }
  95. /**
  96. Exit from [osLoop].
  97. **
  98. public function exitLoop() {
  99. os_loop_stop(handle);
  100. }
  101. /**
  102. If you want to use the [osLoop], [sync] and [syncResult] methods, you
  103. need to call [osInitialize] before creating any thread or calling [current].
  104. This will load [os.ndll] library and initialize UI methods for each thread.
  105. **
  106. public static function osInitialize() {
  107. os_loop = neko.Lib.load("os","os_loop",0);
  108. os_loop_stop = neko.Lib.load("os","os_loop_stop",1);
  109. os_sync = neko.Lib.load("os","os_sync",2);
  110. }
  111. static var os_loop = null;
  112. static var os_loop_stop = null;
  113. static var os_sync = null;
  114. */
  115. static var thread_create = neko.Lib.load("std","thread_create",2);
  116. static var thread_current = neko.Lib.load("std","thread_current",0);
  117. static var thread_send = neko.Lib.load("std","thread_send",2);
  118. static var thread_read_message = neko.Lib.load("std","thread_read_message",1);
  119. }