-
Notifications
You must be signed in to change notification settings - Fork 3
/
threading.d
75 lines (65 loc) · 1.39 KB
/
threading.d
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
/**
* Module for threading.
*
* Authors:
* Jacob Jensen
* License:
* https://github.com/PoisonEngine/poison-ui/blob/master/LICENSE
*/
module poison.core.threading;
import std.concurrency : send, receive, Tid, thisTid, receiveTimeout;
import core.thread : dur;
import poison.core.action;
package(poison.core) {
/// The tid of the ui thread.
__gshared Tid _uiTid;
}
/// Gets a boolean determining whether the current thread is the ui thread.
@property bool isUIThread() {
return thisTid == _uiTid;
}
/**
* Executes an action on the UI thread.
* Params:
* f = The function pointer to execute.
*/
void executeUI(void function() f) {
executeUI(new Action(f));
}
/**
* Executes an action on the UI thread.
* Params:
* d = The delegate to execute.
*/
void executeUI(void delegate() d) {
executeUI(new Action(d));
}
/**
* Executes an action on the UI thread.
* Params:
* action = The action to execute.
*/
void executeUI(Action action) {
if (isUIThread) {
action();
return;
}
send(_uiTid, cast(shared)action);
}
/**
* Receives a concurrent message non-blocking.
* Params:
* ops = The message ops.
*/
private void receiveNonBlocking(T...)(T ops) {
receiveTimeout(
dur!("nsecs")(-1),
ops
);
}
/// Receives all messages for the current thread and executes them.
void receiveMessages() {
receiveNonBlocking(
(shared(Action) a) { (cast(Action)a)(); }
);
}