Thread.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 cpp.vm;
  26. typedef ThreadHandle = Dynamic;
  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 readed 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(_) { callb(); },[]));
  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. function __compare(t) {
  59. return untyped 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 immediatly. 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 cpp.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 = cpp.Lib.load("os","os_loop",0);
  107. os_loop_stop = cpp.Lib.load("os","os_loop_stop",1);
  108. os_sync = cpp.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 = cpp.Lib.load("std","thread_create",2);
  115. static var thread_current = cpp.Lib.load("std","thread_current",0);
  116. static var thread_send = cpp.Lib.load("std","thread_send",2);
  117. static var thread_read_message = cpp.Lib.load("std","thread_read_message",1);
  118. }