Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_concurrency.d

raw
1 @system unittest
2 {
3 import std.concurrency;
4
5 __gshared string received;
6 static void spawnedFunc(Tid ownerTid)
7 {
8 import std.conv : text;
9 // Receive a message from the owner thread.
10 receive((int i){
11 received = text("Received the number ", i);
12
13 // Send a message back to the owner thread
14 // indicating success.
15 send(ownerTid, true);
16 });
17 }
18
19 // Start spawnedFunc in a new thread.
20 auto childTid = spawn(&spawnedFunc, thisTid);
21
22 // Send the number 42 to this new thread.
23 send(childTid, 42);
24
25 // Receive the result code.
26 auto wasSuccessful = receiveOnly!(bool);
27 assert(wasSuccessful);
28 assert(received == "Received the number 42");
29 }
30
31 @system unittest
32 {
33 import std.concurrency;
34
35 static void f(string msg)
36 {
37 assert(msg == "Hello World");
38 }
39
40 auto tid = spawn(&f, "Hello World");
41 }
42
43 @system unittest
44 {
45 import std.concurrency;
46
47 string msg = "Hello, World!";
48
49 static void f1(string msg) {}
50 static assert(!__traits(compiles, spawn(&f1, msg.dup)));
51 static assert( __traits(compiles, spawn(&f1, msg.idup)));
52
53 static void f2(char[] msg) {}
54 static assert(!__traits(compiles, spawn(&f2, msg.dup)));
55 static assert(!__traits(compiles, spawn(&f2, msg.idup)));
56 }
57
58 @system unittest
59 {
60 import std.concurrency;
61
62 spawn({
63 ownerTid.send("This is so great!");
64 });
65 assert(receiveOnly!string == "This is so great!");
66 }
67
68 @system unittest
69 {
70 import std.concurrency;
71
72 import std.variant : Variant;
73
74 auto process = ()
75 {
76 receive(
77 (int i) { ownerTid.send(1); },
78 (double f) { ownerTid.send(2); },
79 (Variant v) { ownerTid.send(3); }
80 );
81 };
82
83 {
84 auto tid = spawn(process);
85 send(tid, 42);
86 assert(receiveOnly!int == 1);
87 }
88
89 {
90 auto tid = spawn(process);
91 send(tid, 3.14);
92 assert(receiveOnly!int == 2);
93 }
94
95 {
96 auto tid = spawn(process);
97 send(tid, "something else");
98 assert(receiveOnly!int == 3);
99 }
100 }
101
102 @system unittest
103 {
104 import std.concurrency;
105
106 auto tid = spawn(
107 {
108 assert(receiveOnly!int == 42);
109 });
110 send(tid, 42);
111 }
112
113 @system unittest
114 {
115 import std.concurrency;
116
117 auto tid = spawn(
118 {
119 assert(receiveOnly!string == "text");
120 });
121 send(tid, "text");
122 }
123
124 @system unittest
125 {
126 import std.concurrency;
127
128 struct Record { string name; int age; }
129
130 auto tid = spawn(
131 {
132 auto msg = receiveOnly!(double, Record);
133 assert(msg[0] == 0.5);
134 assert(msg[1].name == "Alice");
135 assert(msg[1].age == 31);
136 });
137
138 send(tid, 0.5, Record("Alice", 31));
139 }
140
141 @system unittest
142 {
143 import std.concurrency;
144
145 auto tid = spawn({
146 int i;
147 while (i < 9)
148 i = receiveOnly!int;
149
150 ownerTid.send(i * 2);
151 });
152
153 auto r = new Generator!int({
154 foreach (i; 1 .. 10)
155 yield(i);
156 });
157
158 foreach (e; r)
159 tid.send(e);
160
161 assert(receiveOnly!int == 18);
162 }
163
164 @system unittest
165 {
166 import std.concurrency;
167
168 import std.range;
169
170 InputRange!int myIota = iota(10).inputRangeObject;
171
172 myIota.popFront();
173 myIota.popFront();
174 assert(myIota.moveFront == 2);
175 assert(myIota.front == 2);
176 myIota.popFront();
177 assert(myIota.front == 3);
178
179 //can be assigned to std.range.interfaces.InputRange directly
180 myIota = new Generator!int(
181 {
182 foreach (i; 0 .. 10) yield(i);
183 });
184
185 myIota.popFront();
186 myIota.popFront();
187 assert(myIota.moveFront == 2);
188 assert(myIota.front == 2);
189 myIota.popFront();
190 assert(myIota.front == 3);
191
192 size_t[2] counter = [0, 0];
193 foreach (i, unused; myIota) counter[] += [1, i];
194
195 assert(myIota.empty);
196 assert(counter == [7, 21]);
197 }
198
199 @system unittest
200 {
201 import std.concurrency;
202
203 static class MySingleton
204 {
205 static MySingleton instance()
206 {
207 __gshared MySingleton inst;
208 return initOnce!inst(new MySingleton);
209 }
210 }
211
212 assert(MySingleton.instance !is null);
213 }
214
215 @system unittest
216 {
217 import std.concurrency;
218
219 import core.sync.mutex : Mutex;
220
221 static shared bool varA, varB;
222 static shared Mutex m;
223 m = new shared Mutex;
224
225 spawn({
226 // use a different mutex for varB to avoid a dead-lock
227 initOnce!varB(true, m);
228 ownerTid.send(true);
229 });
230 // init depends on the result of the spawned thread
231 initOnce!varA(receiveOnly!bool);
232 assert(varA == true);
233 assert(varB == true);
234 }
235