This repository was archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasync.cpp
More file actions
413 lines (379 loc) · 13.7 KB
/
async.cpp
File metadata and controls
413 lines (379 loc) · 13.7 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* Copyright (c) 2017 Unpause, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* See the file LICENSE included with this distribution for more
* information.
*/
#include <iostream>
#include <random>
#include <atomic>
#include <stdio.h>
#include <assert.h>
#include <inttypes.h>
static std::atomic<int> s_order(0);
static int order() { return s_order.fetch_add(1); }
#define log_v(x, ...) printf("[%d] %3d:\t" x "\n", order(), __LINE__, ##__VA_ARGS__); fflush(stdout);
#define log(x) printf("[%d] %3d:\t" x "\n", order(), __LINE__); fflush(stdout);
#include <unpause/async>
static const uint64_t iterations = 500000;
void task_test() {
log("------- Testing async::task -------");
using namespace unpause;
{
int val = 0;
log("lambda capture with return value...");
auto t = async::make_task([&]{ return val+1; });
int res = t();
log_v("res=%d", res);
assert(res == (val+1));
log("OK");
}
{
log("argument passing with return value...");
auto t = async::make_task([](int val){ return val+1; }, 1);
int res = t();
log_v("res=%d", res);
assert(res == 2);
log("OK");
}
{
log("argument passing with return value and after lambda with capture...");
int val = 0;
auto t = async::make_task([](int val){ return val+1; }, (int)val); // variables are default pass-by-reference
t.after = [&](int result){ val = result; };
int res = t();
log_v("res=%d val=%d", res, val);
assert(res == val);
log("OK");
}
{
log("move with argument passing and return value along with lambda capture");
int val = 2;
auto t = async::make_task([](int val){ return val+1; }, (int)val); // val should be 3
t.after = [&](int result){ val *= result; }; // val should be 6
auto t2 = std::move(t);
int res = t2();
log_v("res=%d val=%d", res, val);
assert(res==(val/2));
log("OK");
}
log("------- Testing async::detail::task_container -------");
{
log("before_internal and after_internal");
int res = 1;
auto t = async::make_task([](int& val) { return ++val; }, res); // res should = 6 at this point.
t.after = [&] (int ret) { res += ret; }; // res should = 12
t.before_internal = [&] { res = res * 5; }; // res should = 5 at this point
t.after_internal = [&] { res = res * 5; }; // res should = 60 at this point.
t();
log_v("res=%d", res);
assert(res == 60);
log("OK");
}
{
log("before_internal and after_internal with move");
int res = 1;
auto t = async::make_task([](int& val) { return ++val; }, res); // res should = 6 at this point.
t.after = [&] (int ret) { res += ret; }; // res should = 12
t.before_internal = [&] { res = res * 5; }; // res should = 5 at this point
t.after_internal = [&] { res = res * 5; }; // res should = 60 at this point.
auto t2 = std::move(t);
t2();
log_v("res=%d", res);
assert(res == 60);
log("OK");
}
}
void task_queue_test()
{
using namespace unpause;
log("------- Testing async::task_queue -------");
{
log("argument passing tasks");
async::task_queue queue;
uint64_t val = 0;
const uint64_t n = iterations;
for(uint64_t i = 1 ; i <= n ; i++) {
queue.add([&](uint64_t in) { val += in; }, (uint64_t)i);
}
while(queue.next());
log_v("val=%" PRId64 " n=%" PRId64 " t=%" PRId64, val, n, (n*(n+1)/2));
assert(val==(n*(n+1)/2));
log("OK");
}
{
log("task passing with after");
async::task_queue queue;
uint64_t val = 0;
const uint64_t n = iterations;
for(uint64_t i = 1 ; i <= n ; i++) {
auto t = async::make_task([&](uint64_t in) { val += in; return in; }, (uint64_t)i);
t.after = [&](uint64_t i){ val += i; };
queue.add(t);
}
while(queue.next());
log_v("val=%" PRId64 " n=%" PRId64 " t=%" PRId64, val, n, (n*(n+1)/2));
assert(val==(n*(n+1)/2)*2);
log("OK");
}
}
void thread_pool_test()
{
using namespace unpause;
log("------- Testing async::thread_pool -------");
{
log("async dispatch on any thread");
std::atomic<uint64_t> val(0);
const uint64_t n = iterations;
std::atomic<uint64_t> ct(n);
{
async::thread_pool pool;
for(uint64_t i = 1 ; i <= n ; i++) {
async::run(pool, [&](uint64_t in) { val += in; --ct; }, (uint64_t)i);
}
while(ct.load() > 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
log_v("val=%" PRId64 " n=%" PRId64 " t=%" PRId64, val.load(), n, (n*(n+1)/2));
assert(val==(n*(n+1)/2));
log("OK");
}
{
log("sync dispatch on any thread");
std::atomic<uint64_t> val(0);
const uint64_t n = iterations;
std::atomic<uint64_t> ct(n);
{
async::thread_pool pool;
auto div = n / 4;
for(uint64_t i = 1 ; i <= n ; i++) {
async::run_sync(pool, [&](uint64_t in) { val += in; --ct; }, (uint64_t)i);
if(!(i%div)) {
log_v("ct=%" PRId64, n-i);
}
}
}
log_v("val=%" PRId64 " n=%" PRId64 " t=%" PRId64, val.load(), n, (n*(n+1)/2));
assert(val==(n*(n+1)/2));
log("OK");
}
{
log("async dispatch in a serial queue");
std::vector<int> res;
const int n = iterations;
res.reserve(n);
std::atomic<int> ct(n);
std::mutex m;
async::thread_pool pool;
async::task_queue queue;
for(int i = 0 ; i < n; i++) {
async::run(pool, queue, [&](int val) {
m.lock();
res.push_back(val);
m.unlock();
--ct;
}, (int)i);
}
while(ct.load() > 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
log("Checking order");
for(int i = 0 ; i < n ; ++i ) {
assert(i == res.at(i));
}
log("OK");
}
}
void run_loop_test() {
log("------- Testing async::run_loop -------");
using namespace unpause;
{
log("schedule with thread pool");
async::thread_pool pool;
auto start = std::chrono::steady_clock::now();
auto end1 = start;
auto end2 = start;
auto end3 = start;
async::schedule(pool, std::chrono::seconds(3) + std::chrono::steady_clock::now(), [&end2] {
log("3s...");
end2 = std::chrono::steady_clock::now();
});
async::schedule(pool, std::chrono::milliseconds(2500) + std::chrono::steady_clock::now(), [&end1] {
log("2.5s...");
end1 = std::chrono::steady_clock::now();
});
async::schedule(pool, std::chrono::seconds(4) + std::chrono::steady_clock::now(), [&end3] {
log("4s...");
end3 = std::chrono::steady_clock::now();
});
log("start.");
std::this_thread::sleep_for(std::chrono::seconds(5));
auto diff1 = std::chrono::duration_cast<std::chrono::microseconds>(end1 - start).count();
log_v("Diff=%" PRId64, diff1);
assert(diff1 <= 3000000 && diff1 > 2500000);
auto diff2 = std::chrono::duration_cast<std::chrono::microseconds>(end2 - start).count();
log_v("Diff2=%" PRId64, diff2);
assert(diff2 <= 3500000 && diff2 > 3000000);
auto diff3 = std::chrono::duration_cast<std::chrono::microseconds>(end3 - start).count();
log_v("Diff3=%" PRId64, diff3);
assert(diff3 <= 4500000 && diff3 > 4000000);
}
{
log("schedule with thread pool and queue");
async::thread_pool pool;
async::task_queue queue;
auto start = std::chrono::steady_clock::now();
auto end1 = start;
auto end2 = start;
auto end3 = start;
async::schedule(pool, queue, std::chrono::seconds(3) + std::chrono::steady_clock::now(), [&end2] {
log("3s (sleeping for 1500ms)...");
end2 = std::chrono::steady_clock::now();
std::this_thread::sleep_for(std::chrono::milliseconds(1500));
});
async::schedule(pool, queue, std::chrono::milliseconds(2500) + std::chrono::steady_clock::now(), [&end1] {
log("2.5s...");
end1 = std::chrono::steady_clock::now();
});
async::schedule(pool, queue, std::chrono::seconds(4) + std::chrono::steady_clock::now(), [&end3] {
log("4s (should be closer to 4.5s)...");
end3 = std::chrono::steady_clock::now();
});
std::this_thread::sleep_for(std::chrono::seconds(5));
auto diff1 = std::chrono::duration_cast<std::chrono::microseconds>(end1 - start).count();
log_v("Diff=%" PRId64, diff1);
assert(diff1 <= 3000000 && diff1 > 2500000);
auto diff2 = std::chrono::duration_cast<std::chrono::microseconds>(end2 - start).count();
log_v("Diff2=%" PRId64, diff2);
assert(diff2 <= 4000000 && diff2 > 3000000);
auto diff3 = std::chrono::duration_cast<std::chrono::microseconds>(end3 - start).count();
log_v("Diff3=%" PRId64, diff3);
assert(diff3 <= 5000000 && diff3 > 4500000);
}
{
log("schedule with loop");
async::run_loop loop;
auto start = std::chrono::steady_clock::now();
auto end1 = start;
auto end2 = start;
auto end3 = start;
async::schedule(loop, std::chrono::seconds(3) + std::chrono::steady_clock::now(), [&end2] {
log("3s...");
end2 = std::chrono::steady_clock::now();
});
async::schedule(loop, std::chrono::milliseconds(2500) + std::chrono::steady_clock::now(), [&end1] {
log("2.5s...");
end1 = std::chrono::steady_clock::now();
});
async::schedule(loop, std::chrono::seconds(4) + std::chrono::steady_clock::now(), [&end3] {
log("4s...");
end3 = std::chrono::steady_clock::now();
});
std::this_thread::sleep_for(std::chrono::seconds(5));
auto diff1 = std::chrono::duration_cast<std::chrono::microseconds>(end1 - start).count();
log_v("Diff=%" PRId64, diff1);
assert(diff1 <= 3000000 && diff1 > 2500000);
auto diff2 = std::chrono::duration_cast<std::chrono::microseconds>(end2 - start).count();
log_v("Diff2=%" PRId64, diff2);
assert(diff2 <= 3500000 && diff2 > 3000000);
auto diff3 = std::chrono::duration_cast<std::chrono::microseconds>(end3 - start).count();
log_v("Diff3=%" PRId64, diff3);
assert(diff3 <= 4500000 && diff3 > 4000000);
}
}
void interleave_test() {
log("------- Testing interleaving queues with thread pool -------");
using namespace unpause;
async::thread_pool p(4);
async::task_queue q1;
async::task_queue q2;
for(int i = 0 ; i < 100000 ; i++) {
std::atomic<int> val1(0);
std::atomic<int> val2(0);
if(i % 10000 == 0) {
log_v("i=%d/100000", i);
}
async::run(p, q1, [&val1]{
assert(val1.load() == 0);
val1++;
});
async::run(p, q1, [&val1]{
assert(val1.load() == 1);
val1++;
});
async::run(p, q2, [&val2]{
assert(val2.load() == 0);
val2++;
});
async::run(p, q1, [&q2, &p, &val1]{
assert(val1.load() == 2);
val1++;
async::run_sync(p, q2, [&val1]{
assert(val1.load() == 3);
val1++;
});
});
async::run_sync(p, q1, [&val1]{
assert(val1.load() == 4);
});
}
log("OK");
}
void abrupt_exit_test( int ct ) {
log("------- Testing abrupt dealloc of queue -------");
using namespace unpause;
async::thread_pool p;
for(int i = 0 ; i < ct ; i++)
{
{
async::task_queue q;
for(int j = 0 ; j < 100 ; j++) {
async::run(p, q, [i, j] {
return i*j;
});
}
}
}
log("------- Testing abrupt dealloc of queue with schedule -------");
log("will take approx. 250 seconds...");
std::vector<async::task_queue*> qs;
for(int i = 0 ; i < ct; i++) {
qs.push_back(new async::task_queue());
}
for(int i = 0 ; i < ct ; i++)
{
{
async::task_queue* q = qs[i];
q->set_name(std::to_string(i));
for(int j = 0 ; j < 100 ; j++) {
auto start = i;
async::schedule(p, *q, std::chrono::steady_clock::now() + std::chrono::milliseconds(j), [start, &i, j] {
if(start!=i) {
log_v("%d != %d [%d]", start, i, j);
}
assert(start==i);
});
}
std::this_thread::sleep_for(std::chrono::milliseconds(25));
delete qs[i];
}
if((i%1000)==0) {
log_v("%d", i);
}
}
log("OK");
}
int main(void)
{
task_test();
task_queue_test();
thread_pool_test();
run_loop_test();
interleave_test();
abrupt_exit_test(10000);
return 0;
}