blob: b2ab9313901bf1f8d8d285d371588c8e177df805 (
plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/**
* The osthread module provides types used in threads modules.
*
* Copyright: Copyright Sean Kelly 2005 - 2012.
* License: Distributed under the
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
* (See accompanying file LICENSE)
* Authors: Sean Kelly, Walter Bright, Alex Rønne Petersen, Martin Nowak
* Source: $(DRUNTIMESRC core/thread/threadgroup.d)
*/
module core.thread.threadgroup;
import core.thread.osthread;
/**
* This class is intended to simplify certain common programming techniques.
*/
class ThreadGroup
{
/**
* Creates and starts a new Thread object that executes fn and adds it to
* the list of tracked threads.
*
* Params:
* fn = The thread function.
*
* Returns:
* A reference to the newly created thread.
*/
final Thread create(void function() fn)
{
Thread t = new Thread(fn).start();
synchronized(this)
{
m_all[t] = t;
}
return t;
}
/**
* Creates and starts a new Thread object that executes dg and adds it to
* the list of tracked threads.
*
* Params:
* dg = The thread function.
*
* Returns:
* A reference to the newly created thread.
*/
final Thread create(void delegate() dg)
{
Thread t = new Thread(dg).start();
synchronized(this)
{
m_all[t] = t;
}
return t;
}
/**
* Add t to the list of tracked threads if it is not already being tracked.
*
* Params:
* t = The thread to add.
*
* In:
* t must not be null.
*/
final void add(Thread t)
in
{
assert(t);
}
do
{
synchronized(this)
{
m_all[t] = t;
}
}
/**
* Removes t from the list of tracked threads. No operation will be
* performed if t is not currently being tracked by this object.
*
* Params:
* t = The thread to remove.
*
* In:
* t must not be null.
*/
final void remove(Thread t)
in
{
assert(t);
}
do
{
synchronized(this)
{
m_all.remove(t);
}
}
/**
* Operates on all threads currently tracked by this object.
*/
final int opApply(scope int delegate(ref Thread) dg)
{
synchronized(this)
{
int ret = 0;
// NOTE: This loop relies on the knowledge that m_all uses the
// Thread object for both the key and the mapped value.
foreach (Thread t; m_all.keys)
{
ret = dg(t);
if (ret)
break;
}
return ret;
}
}
/**
* Iteratively joins all tracked threads. This function will block add,
* remove, and opApply until it completes.
*
* Params:
* rethrow = Rethrow any unhandled exception which may have caused the
* current thread to terminate.
*
* Throws:
* Any exception not handled by the joined threads.
*/
final void joinAll(bool rethrow = true)
{
synchronized(this)
{
// NOTE: This loop relies on the knowledge that m_all uses the
// Thread object for both the key and the mapped value.
foreach (Thread t; m_all.keys)
{
t.join(rethrow);
}
}
}
private:
Thread[Thread] m_all;
}
|