-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathThreads.hx
132 lines (117 loc) · 3.75 KB
/
Threads.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com) and contributors
* SPDX-FileContributor: Sebastian Thomschke, Vegard IT GmbH
* SPDX-License-Identifier: Apache-2.0
*/
package hx.concurrent.thread;
import hx.concurrent.internal.Dates;
class Threads {
/**
* <pre><code>
* >>> Threads.current == Threads.current
* </code></pre>
*
* @return a target-specific object or ID representing the current thread.
*/
public static var current(get, never):Dynamic;
static function get_current():Dynamic {
#if eval
return cast(sys.thread.Thread.current(), eval.vm.NativeThread).id();
#elseif cs
return cs.system.threading.Thread.CurrentThread;
#elseif java
return java.lang.Thread.currentThread();
#elseif threads
return sys.thread.Thread.current();
#elseif flash
var worker = flash.system.Worker.current;
return worker == null ? "MainThread" : worker;
#else // javascript, lua, php
return "MainThread";
#end
}
/**
* @return true if spawning threads is supported by current target
*/
public static var isSupported(get, never):Bool;
#if !python inline #end
static function get_isSupported():Bool {
#if threads
#if python
try {
python.Syntax.code("from threading import Thread");
return true;
} catch (ex) {
return false;
}
#end
return true;
#else
return false;
#end
}
#if (flash || sys)
/**
* Blocks the current thread until `condition` returns `true`.
*
* If <code>timeoutMS</code> is set 0, the function immediatly returns with the value returned by `condition`.
* If <code>timeoutMS</code> is set to value > 0, the function waits up to the given timespan for a new message.
* If <code>timeoutMS</code> is set to `-1`, the function waits indefinitely until a new message is available.
* If <code>timeoutMS</code> is set to value lower than -1, results in an exception.
*/
public static function await(condition:() -> Bool, timeoutMS:Int, waitLoopSleepMS = 10):Bool {
if (timeoutMS < -1)
throw "[timeoutMS] must be >= -1";
if (timeoutMS == 0)
return condition();
#if flash
final cond = new flash.concurrent.Condition(new flash.concurrent.Mutex());
#else
final waitLoopSleepSecs = waitLoopSleepMS / 1000.0;
#end
final startAt = Dates.now();
while (!condition()) {
if (timeoutMS > 0) {
final elapsedMS = Dates.now() - startAt;
if (elapsedMS >= timeoutMS)
return false;
}
#if flash
cond.wait(waitLoopSleepMS);
#else
Sys.sleep(waitLoopSleepSecs);
#end
}
return true;
}
/**
* Puts the current thread to sleep for the given milliseconds.
*/
#if !flash inline #end
public static function sleep(timeMS:Int):Void {
#if flash
final cond = new flash.concurrent.Condition(new flash.concurrent.Mutex());
cond.wait(timeMS);
#else
Sys.sleep(timeMS / 1000);
#end
}
#end
#if threads
/**
* Spawns a new deamon thread (i.e. terminates with the main thread) to execute the given function.
*/
#if !python inline #end
public static function spawn(func:() -> Void):Void {
#if (cpp || cs || (threads && eval) || java || neko || hl)
sys.thread.Thread.create(func);
#elseif python
final t = new python.lib.threading.Thread({target: func});
t.daemon = true;
t.start();
#else // flash, javascript, lua
throw "Unsupported operation.";
#end
}
#end
}