-
Notifications
You must be signed in to change notification settings - Fork 2
/
taskqueue.h
445 lines (348 loc) · 8.61 KB
/
taskqueue.h
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#ifndef TASKQUEUE_H
#define TASKQUEUE_H
#include <functional>
#include <cstdint>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <chrono>
#include <atomic>
namespace Task {
template<typename T>
class Lazy
{
std::atomic<T*> item;
public:
typedef T value_type;
typedef T* pointer;
typedef T const* const_pointer;
typedef T& reference;
typedef T const& const_reference;
Lazy();
~Lazy();
Lazy(const_reference) = delete;
reference operator=(const_reference) = delete;
operator bool() const;
bool empty() const;
void clear();
pointer detach();
const_pointer get() const;
pointer get(bool create = true);
const_reference operator*() const;
reference operator*();
const_pointer operator->() const;
pointer operator->();
// Execute lambda or other callable object only if the instance exists
template<typename F>
void ifExists(F const& f);
};
template<typename T>
Lazy<T>::Lazy()
: item(nullptr)
{
}
template<typename T>
Lazy<T>::~Lazy()
{
clear();
}
template<typename T>
Lazy<T>::operator bool() const
{
return item != nullptr;
}
template<typename T>
bool Lazy<T>::empty() const
{
return item != nullptr;
}
template<typename T>
void Lazy<T>::clear()
{
delete item;
item = nullptr;
}
template<typename T>
typename Lazy<T>::pointer Lazy<T>::detach()
{
pointer p = item;
item = nullptr;
return p;
}
template<typename T>
typename Lazy<T>::const_pointer Lazy<T>::get() const
{
return item;
}
template<typename T>
typename Lazy<T>::pointer Lazy<T>::get(bool create)
{
pointer p = item, newItem;
if (p)
return p;
if (create)
{
newItem = new value_type;
// Race to set the pointer to the new object
if (item.compare_exchange_strong(p, newItem))
return newItem;
// Another thread won the race, delete the one we made
// Return the one the winner created
delete newItem;
return p;
}
return nullptr;
}
template<typename T>
typename Lazy<T>::const_reference Lazy<T>::operator*() const
{
return *get();
}
template<typename T>
typename Lazy<T>::reference Lazy<T>::operator*()
{
return *get();
}
template<typename T>
typename Lazy<T>::const_pointer Lazy<T>::operator->() const
{
return get();
}
template<typename T>
typename Lazy<T>::pointer Lazy<T>::operator->()
{
return get();
}
template<typename T>
template<typename F>
void Lazy<T>::ifExists(F const& f)
{
if (item)
f(item);
}
class Queue;
class Task
{
public:
typedef std::function<void()> callback;
typedef std::vector<callback> CallbackList;
explicit Task(callback func);
void invoke();
void wait();
bool wait_for(int64_t milliseconds);
// Run callback when task is done
void then(callback func);
private:
typedef std::unique_lock<std::mutex> ScopedLock;
callback func;
CallbackList thens;
bool done;
std::mutex taskLock;
std::condition_variable taskCond;
};
// This class needs its own lifetime management with refcount because it
// needs to keep itself alive while it is expecting "then" callbacks
class WhenImpl
{
public:
WhenImpl& then(Task::callback func);
void wait();
bool wait_for(int64_t milliseconds);
template<typename Rep, typename Period>
bool wait_for(std::chrono::duration<Rep, Period> duration);
template<typename Clock, typename Duration>
bool wait_until(std::chrono::time_point<Clock, Duration> point);
// Increase expect count by one, to prevent it becoming completed
// too soon when adding multiple workitems
void beginAdding();
void endAdding();
private:
typedef std::mutex Lock;
typedef std::condition_variable Cond;
typedef std::unique_lock<Lock> ScopedLock;
template<typename ...T>
explicit WhenImpl(T& ...handles);
template<typename InputIt, typename = typename std::iterator_traits<InputIt>::iterator_category *>
WhenImpl(InputIt st, InputIt en);
friend class When;
void addref();
void release();
WhenImpl& add();
template<typename T1>
WhenImpl& add(T1& handle);
template<typename T1, typename ...T>
WhenImpl& add(T1& handle, T& ...rest);
void signal();
Lazy<Lock> whenLock;
Lazy<Cond> whenCond;
std::size_t got, expect;
std::atomic_uint refcount;
Task::CallbackList thens;
};
template<typename ...T>
WhenImpl::WhenImpl(T& ...handles)
: got(0)
, expect(sizeof...(handles))
, refcount(1)
{
add(handles...);
}
template<typename InputIt, typename>
WhenImpl::WhenImpl(InputIt st, InputIt en)
: got(0)
, expect(std::distance(st, en))
, refcount(1)
{
for ( ; st != en; ++st)
add(*st);
}
template<typename T1>
WhenImpl& WhenImpl::add(T1& handle)
{
// Keep self alive until then callback
addref();
// Request a callback when this item is completed
handle.then(std::bind(&WhenImpl::signal, this));
return *this;
}
template<typename T1, typename ...T>
WhenImpl& WhenImpl::add(T1& handle, T& ...rest)
{
add(handle);
return add(rest...);
}
class When
{
public:
template<typename ...T>
When(T&& ...handles);
template<typename InputIt, typename = typename std::iterator_traits<InputIt>::iterator_category *>
When(InputIt st, InputIt en);
When(const When& r);
When(When&& r);
~When();
When& operator=(When r);
When& then(Task::callback func);
When& wait();
bool wait_for(int64_t milliseconds);
template<typename Rep, typename Period>
bool wait_for(std::chrono::duration<Rep, Period> duration);
template<typename Clock, typename Duration>
bool wait_until(std::chrono::time_point<Clock, Duration> point);
private:
WhenImpl* ptr;
};
template<typename ...T>
When::When(T&& ...handles)
: ptr(new WhenImpl(std::forward<T>(handles)...))
{
}
template<typename InputIt, typename>
When::When(InputIt st, InputIt en)
: ptr(new WhenImpl(st, en))
{
}
template<typename Rep, typename Period>
bool When::wait_for(std::chrono::duration<Rep, Period> duration)
{
return ptr->wait_for(duration);
}
template<typename Clock, typename Duration>
bool When::wait_until(std::chrono::time_point<Clock, Duration> point)
{
return ptr->wait_until(point);
}
// RAII object that tracks whether the caller cares about a task anymore,
// and provides a way to test-for or wait-for completion
class Handle
{
public:
Handle();
explicit Handle(std::shared_ptr<Task> task);
Handle(const Handle& r);
Handle(Handle&& r);
Handle& operator=(Handle r);
void release();
bool empty() const;
Handle& then(Task::callback func);
Handle& wait();
bool wait_for(int64_t milliseconds);
template<typename Rep, typename Period>
bool wait_for(std::chrono::duration<Rep, Period> duration);
template<typename Clock, typename Duration>
bool wait_until(std::chrono::time_point<Clock, Duration> point);
private:
std::shared_ptr<Task> task;
};
class Queue
{
public:
Queue(int workerCount = -1);
~Queue();
Handle queueWork(Task::callback callback, bool highPriority = true);
static Queue &global();
static Handle run(Task::callback work, bool highPriority = true);
template<typename InputIt, typename V = typename std::iterator_traits<InputIt>::value_type>
When run(InputIt st, InputIt en, bool highPriority);
void finish();
private:
typedef std::shared_ptr<Task> TaskPointer;
typedef std::deque<TaskPointer> WorkQueue;
When runMultiple(std::function<bool(Task::callback&)> generator,
bool highPriority = false);
void start(int workerCount = -1);
void stop();
bool done;
enum FinishState {
NOTFINISHED = 0,
// finish() has been called but the queue isn't drained
FINISHING,
// queue has been drained and worker threads have exited
FINISHED
};
FinishState finished;
typedef std::mutex Lock;
typedef std::unique_lock<Lock> ScopedLock;
mutable Lock queueLock;
std::vector<std::thread> workers;
std::condition_variable notEmpty;
std::condition_variable empty;
WorkQueue queue;
bool getMoreWork(TaskPointer &item);
void workLoop();
};
template<typename Rep, typename Period>
bool Handle::wait_for(std::chrono::duration<Rep, Period> duration)
{
return wait_for(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count());
}
template<typename Clock, typename Duration>
bool Handle::wait_until(std::chrono::time_point<Clock, Duration> point)
{
return wait_for(std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - point).count());
}
static inline Handle run(Task::callback work)
{
return Queue::run(std::move(work));
}
template<typename InputIt, typename V>
When Queue::run(InputIt st, InputIt en, bool highPriority)
{
return Queue::runMultiple([&](Task::callback& work) mutable -> bool {
if (st != en)
{
work = *st;
++st;
return true;
}
return false;
}, highPriority);
}
template<typename InputIt>
static inline When run(InputIt st, InputIt en, bool highPriority = false)
{
return Queue::global().run(st, en, highPriority);
}
}
#endif // TASKQUEUE_H