File: ThreadPool.h

package info (click to toggle)
spring 106.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,316 kB
  • sloc: cpp: 543,954; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (826 lines) | stat: -rw-r--r-- 20,844 bytes parent folder | download | duplicates (3)
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef _THREADPOOL_H
#define _THREADPOOL_H

#ifndef THREADPOOL
#include  <functional>
#include "System/Threading/SpringThreading.h"

namespace ThreadPool {
	template<class F, class... Args>
	static inline void Enqueue(F&& f, Args&&... args)
	{
		f(args ...);
	}

	static inline void AddExtJob(spring::thread&& t) { t.join(); }
	static inline void AddExtJob(std::future<void>&& f) { f.get(); }
	static inline void ClearExtJobs() {}

	static inline void SetMaximumThreadCount() {}
	static inline void SetDefaultThreadCount() {}
	static inline void SetThreadCount(int num) {}
	static inline int GetThreadNum() { return 0; }
	static inline int GetMaxThreads() { return 1; }
	static inline int GetNumThreads() { return 1; }
	static inline void NotifyWorkerThreads(bool force, bool async) {}
	static inline bool HasThreads() { return false; }

	static constexpr int MAX_THREADS = 1;
}


static inline void for_mt(int start, int end, int step, const std::function<void(const int i)>&& f)
{
	for (int i = start; i < end; i += step) {
		f(i);
	}
}

static inline void for_mt(int start, int end, const std::function<void(const int i)>&& f)
{
	for_mt(start, end, 1, std::move(f));
}

static inline void for_mt2(int start, int end, unsigned worksize, const std::function<void(const int i)>&& f)
{
	for_mt(start, end, 1, std::move(f));
}


static inline void parallel(const std::function<void()>&& f)
{
	f();
}

template<class F, class G>
static inline auto parallel_reduce(F&& f, G&& g) -> typename std::result_of<F()>::type
{
	return f();
}

#else

#include "System/TimeProfiler.h"
#include "System/Platform/Threading.h"
#include "System/Threading/SpringThreading.h"

#include  <array>
#include <vector>
#include <numeric>
#include <atomic>

#undef gt
#include <memory>

#ifdef UNITSYNC
	#undef SCOPED_MT_TIMER
	#define SCOPED_MT_TIMER(x)
#endif

class ITaskGroup;
namespace ThreadPool {
	template<class F, class... Args>
	static auto Enqueue(F&& f, Args&&... args)
	-> std::shared_ptr<std::future<typename std::result_of<F(Args...)>::type>>;

	void AddExtJob(spring::thread&& t);
	void AddExtJob(std::future<void>&& f);
	void ClearExtJobs();

	void PushTaskGroup(ITaskGroup* taskGroup);
	void PushTaskGroup(std::shared_ptr<ITaskGroup>&& taskGroup);
	void WaitForFinished(std::shared_ptr<ITaskGroup>&& taskGroup);

	template<typename T>
	inline void PushTaskGroup(std::shared_ptr<T>& taskGroup) { PushTaskGroup(std::move(std::static_pointer_cast<ITaskGroup>(taskGroup))); } //FIXME std::move? doesn't it delete the original arg?
	template<typename T>
	inline void WaitForFinished(std::shared_ptr<T>& taskGroup) { WaitForFinished(std::move(std::static_pointer_cast<ITaskGroup>(taskGroup))); }

	void SetMaximumThreadCount();
	void SetDefaultThreadCount();
	void SetThreadCount(int num);
	int GetThreadNum();
	bool HasThreads();
	int GetMaxThreads();
	int GetNumThreads();
	void NotifyWorkerThreads(bool force, bool async);

	static constexpr int MAX_THREADS = 16;
}




class ITaskGroup
{
public:
	ITaskGroup(const bool getid = true, const bool pooled = false): id(getid ? lastId.fetch_add(1) : -1u), ts(0) {
		ResetState(!pooled, pooled, false);
	}

	virtual ~ITaskGroup() {
		assert(AllowDelete());
	}

	virtual bool IsAsyncTask() const { return false; }
	virtual bool IsSliceTask() const { return false; }
	virtual bool ExecuteStep() = 0;
	virtual bool SelfDelete() const { return false; }

	uint64_t ExecuteLoop(int tid, bool wffCall) {
		const spring_time t0 = spring_now();

		while (ExecuteStep());

		const spring_time t1 = spring_now();
		const spring_time dt = t1 - t0;

		if (IsSliceTask()) {
			// inTaskQueue would be set to false prematurely by the
			// first slice to finish, let it be handled by WFF which
			// blocks until all threads are
			if (!wffCall)
				return (dt.toNanoSecsi());

			inTaskQueue.store(false);
		} else {
			// do not set this to false from WFF, defeats the purpose
			if (!wffCall) {
				assert(inTaskQueue.load());
				inTaskQueue.store(wffCall);
			}
		}

		if (SelfDelete()) {
			// store *after* the check in both branches, avoids UAF
			// async-tasks can never have a parent waiting for them
			execLoopDone.store(true);
			delete this;
		} else {
			execLoopDone.store(ExecLoopDone() || !wffCall);
		}

		return (dt.toNanoSecsi());
	}

	bool IsFinished() const { assert(remainingTasks.load() >= 0); return (remainingTasks.load(std::memory_order_relaxed) == 0); }
	bool IsInJobQueue() const { return (inTaskQueue.load(std::memory_order_relaxed)); }
	bool IsInTaskPool() const { return ((taskPoolMask.load(std::memory_order_relaxed) & (1 << 0)) != 0); }
	bool IsInPoolUse() const { return ((taskPoolMask.load(std::memory_order_relaxed) & (1 << 1)) != 0); }

	bool ExecLoopDone() const { return (execLoopDone.load(std::memory_order_relaxed)); }
	// pooled tasks are deleted only when their pool dies (on exit) which is always allowed
	bool AllowDelete() const { return (IsFinished() && ((!IsInJobQueue() && ExecLoopDone()) || IsInTaskPool())); }

	int RemainingTasks() const { return remainingTasks; }
	int WantedThread() const { return wantedThread; }

	bool WaitFor(const spring_time& rel_time) const {
		const auto end = spring_now() + rel_time;
		while (!IsFinished() && (spring_now() < end));
		return IsFinished();
	}

	uint32_t GetId() const { return id; }
	uint64_t GetDeltaTime(const spring_time t) const { return (std::max(ts.load(), uint64_t(t.toNanoSecsi())) - ts); }

	void UpdateId() { id = lastId.fetch_add(1); }
	void SetTimeStamp(const spring_time t) { ts = t.toNanoSecsi(); }

	void ResetState(bool queued, bool pooled, bool inuse) {
		remainingTasks.store(0);
		wantedThread.store(0);
		taskPoolMask.store(((1 * pooled) << 0) + ((1 * inuse) << 1));

		inTaskQueue.store(queued);
		execLoopDone.store(false);
	}

public:
	std::atomic_int remainingTasks;
	std::atomic_int wantedThread; // if 0 (default), task will be executed by an arbitrary thread
	std::atomic_int taskPoolMask; // whether this task is managed (owned) and in use by a TaskPool

	std::atomic_bool inTaskQueue; // whether this task is still in a thread's queue
	std::atomic_bool execLoopDone; // whether the thread running this task is about to exit ExecLoop

private:
	static std::atomic_uint lastId;

	std::atomic<uint32_t> id;
	std::atomic<uint64_t> ts; // timestamp (ns)
};


template<class F, class... Args>
class AsyncTask: public ITaskGroup
{
public:
	typedef  typename std::result_of<F(Args...)>::type  return_type;

	AsyncTask(F f, Args... args) : selfDelete(true) {
		task = std::make_shared<std::packaged_task<return_type()>>(std::bind(f, std::forward<Args>(args)...));
		result = std::make_shared<std::future<return_type>>(task->get_future());

		remainingTasks += 1;
	}

	bool IsAsyncTask() const override { return true; }
	bool SelfDelete() const override { return (selfDelete.load()); }
	bool ExecuteStep() override {
		// note: *never* called from WaitForFinished
		(*task)();
		remainingTasks -= 1;
		return false;
	}

	// FIXME: rethrow exceptions some time
	std::shared_ptr<std::future<return_type>> GetFuture() { assert(result->valid()); return std::move(result); }

public:
	// if true, we are not managed by a shared_ptr
	std::atomic<bool> selfDelete;

	std::shared_ptr<std::packaged_task<return_type()>> task;
	std::shared_ptr<std::future<return_type>> result;
};



template<class F, typename R = int, class... Args>
class TTaskGroup: public ITaskGroup
{
public:
	TTaskGroup(const int num = 0) : curtask(0) {
		results.reserve(num);
		tasks.reserve(num);
	}

	typedef R return_type;

	void Enqueue(F f, Args... args)
	{
		auto task = std::make_shared<std::packaged_task<return_type()>>(std::bind(f, std::forward<Args>(args)...));
		results.emplace_back(task->get_future());
		tasks.emplace_back(task);
		remainingTasks.fetch_add(1, std::memory_order_release);
	}


	bool ExecuteStep() override
	{
		const int pos = curtask.fetch_add(1, std::memory_order_relaxed);

		if (pos < tasks.size()) {
			tasks[pos]();
			remainingTasks.fetch_sub(1, std::memory_order_release);
			return true;
		}

		return false;
	}

	template<typename G>
	return_type GetResult(const G&& g) {
		return std::accumulate(results.begin(), results.end(), 0, g);
	}

public:
	std::atomic<int> curtask;
	std::vector<std::function<void()>> tasks;
	std::vector<std::future<return_type>> results;
};




template<class F, typename ...Args>
class TTaskGroup<F, void, Args...>: public ITaskGroup
{
public:
	TTaskGroup(const int num = 0) : curtask(0) {
		tasks.reserve(num);
	}

	void Enqueue(F f, Args... args)
	{
		tasks.emplace_back(std::bind(f, args...));
		remainingTasks.fetch_add(1, std::memory_order_release);
	}

	bool ExecuteStep() override
	{
		const int pos = curtask.fetch_add(1, std::memory_order_relaxed);

		if (pos < tasks.size()) {
			tasks[pos]();
			remainingTasks.fetch_sub(1, std::memory_order_release);
			return true;
		}

		return false;
	}

public:
	std::atomic<int> curtask;
	std::vector<std::function<void()>> tasks;
};


template<class F>
class TTaskGroup<F, void>: public ITaskGroup
{
public:
	TTaskGroup(const int num = 0) : curtask(0) {
		tasks.reserve(num);
	}

	void Enqueue(F f)
	{
		tasks.emplace_back(f);
		remainingTasks.fetch_add(1, std::memory_order_release);
	}

	bool ExecuteStep() override
	{
		const int pos = curtask.fetch_add(1, std::memory_order_relaxed);

		if (pos < tasks.size()) {
			tasks[pos]();
			remainingTasks.fetch_sub(1, std::memory_order_release);
			return true;
		}

		return false;
	}

public:
	std::atomic<int> curtask;
	std::vector<std::function<void()>> tasks;
};


template<typename F, typename ...Args>
class TaskGroup: public TTaskGroup<F, decltype(std::declval<F>()((std::declval<Args>())...)), Args...> {
public:
	typedef decltype(std::declval<F>()((std::declval<Args>())...)) R;

	TaskGroup(const int num = 0) : TTaskGroup<F, R, Args...>(num) {}
};




#if 0
template<typename F, typename return_type = int, typename... Args>
class TParallelTaskGroup: public TTaskGroup<F, return_type, Args...>
{
public:
	TParallelTaskGroup(const int num = 0) : TTaskGroup<F, return_type, Args...>(num) {
		uniqueTasks.fill(nullptr);
	}

	void EnqueueUnique(const int threadNum, F& f, Args... args)
	{
		auto task = std::make_shared< std::packaged_task<return_type()> >(std::bind(std::forward<F>(f), std::forward<Args>(args)...));

		this->results.emplace_back(task->get_future());
		this->remainingTasks += 1;

		// NOTE:
		//   here we want task <task> to be executed by thread <threadNum>
		//   this does not actually happen, thread i can call ExecuteStep
		//   multiple times while thread j never calls it (because any TG
		//   is pulled from the queue *once*, by a random thread) meaning
		//   we will have leftover unexecuted tasks and hang
		uniqueTasks[threadNum] = [=](){ (*task)(); };
	}

	bool ExecuteStep() override
	{
		auto& func = uniqueTasks[ThreadPool::GetThreadNum()];

		// does nothing when num=0 except return false (no change to remainingTasks)
		if (func == nullptr)
			return TTaskGroup<F, return_type, Args...>::ExecuteStep();

		// no need to make threadsafe; each thread has its own container
		func();
		func = nullptr;

		if (!this->IsFinished()) {
			this->remainingTasks -= 1;
			return true;
		}

		return false;
	}

public:
	std::array<std::function<void()>, ThreadPool::MAX_THREADS> uniqueTasks;
};
#endif


#if 0
template<typename F, typename ...Args>
class ParallelTaskGroup: public TParallelTaskGroup<F, decltype(std::declval<F>()((std::declval<Args>())...)), Args...> {
public:
	typedef decltype(std::declval<F>()((std::declval<Args>())...)) R;

	ParallelTaskGroup(const int num = 0) : TParallelTaskGroup<F, R, Args...>(num) {}
};
#endif




#if 0
template<typename F>
class Parallel2TaskGroup: public ITaskGroup
{
public:
	Parallel2TaskGroup(bool pooled) : ITaskGroup(false, pooled) {
		// uniqueTasks.fill(false);
	}


	void Enqueue(F& func)
	{
		f = func;
		remainingTasks = ThreadPool::GetNumThreads();

		uniqueTasks.fill(false);
	}

	// NOTE:
	//   as written, this can only work *by accident* if 1) the thread
	//   that executes it in DoTask has a different id than the one in
	//   WaitForFinished and 2) the pool contains at most two threads
	//   (three or more will inevitably cause a hang, same conditions
	//   as TParallelTaskGroup)
	bool ExecuteStep() override
	{
		auto& ut = uniqueTasks[ThreadPool::GetThreadNum()];

		if (!ut) {
			// no need to make threadsafe; each thread has its own container
			ut = true;
			f();

			remainingTasks -= 1;
			return (!IsFinished());
		}

		return false;
	}

public:
	std::array<bool, ThreadPool::MAX_THREADS> uniqueTasks;
	std::function<void()> f;
};

#else

template<typename F>
class Parallel2TaskGroup: public ITaskGroup
{
public:
	typedef  TTaskGroup<F, void>  ChildTaskType;

	Parallel2TaskGroup(bool pooled) : ITaskGroup(false, pooled) {}

	void Enqueue(F& func)
	{
		// note: GNT counts main so we would be short one worker
		// (final task would never be executed and hang the pool)
		remainingTasks.store(ThreadPool::GetNumThreads() - 1);

		childTasks.clear();
		childTasks.reserve(remainingTasks);

		for (int i = 0; i < remainingTasks; i++) {
			auto task = std::make_shared<ChildTaskType>(1);

			task->Enqueue(func);
			task->wantedThread.store(1 + i % (ThreadPool::GetNumThreads() - 1));

			childTasks.push_back(task);
			ThreadPool::PushTaskGroup(task);
		}
	}

	bool ExecuteStep() override
	{
		bool isFinished = true;

		for (size_t n = 0; n < childTasks.size(); n++) {
			isFinished &= childTasks[n]->AllowDelete();
		}

		if (!isFinished)
			return true;

		// P2TG's are never actually in the queue, let WFF terminate
		inTaskQueue.store(false);
		remainingTasks.store(0);

		childTasks.clear();
		return false;
	}

private:
	std::vector< std::shared_ptr<ITaskGroup> > childTasks;
};
#endif



#if 0
template<typename F>
class ForTaskGroup: public ITaskGroup
{
public:
	typedef  TTaskGroup<F, void, int>  ChildTaskType;

	ForTaskGroup(bool pooled) : ITaskGroup(false, pooled) {}

	void Enqueue(const int from, const int to, const int step, F& func)
	{
		assert(to >= from);

		remainingTasks.store((step == 1) ? (to - from) : ((to - from + step - 1) / step));

		childTasks.clear();
		childTasks.reserve(remainingTasks);

		for (int i = 0; i < remainingTasks; i++) {
			auto task = std::make_shared<ChildTaskType>(1);

			task->Enqueue(func, from + (step * i));
			task->wantedThread.store(1 + i % (ThreadPool::GetNumThreads() - 1));

			childTasks.push_back(task);
			ThreadPool::PushTaskGroup(task);
		}
	}


	bool ExecuteStep() override
	{
		bool isFinished = true;

		for (size_t n = 0; n < childTasks.size(); n++) {
			isFinished &= (childTasks[n]->IsFinished() && !childTasks[n]->IsInJobQueue());
		}

		if (!isFinished)
			return true;

		remainingTasks.store(0);
		childTasks.clear();
		return false;
	}

private:
	std::vector< std::shared_ptr<ITaskGroup> > childTasks;
};

#else

template<typename F>
class ForTaskGroup: public ITaskGroup
{
public:
	typedef  TTaskGroup<F, void, int>  ChildTaskType;

	ForTaskGroup(bool pooled) : ITaskGroup(false, pooled) {}

	void Enqueue(const int from, const int to, const int step, F& func)
	{
		assert(to >= from);

		remainingTasks.store((step == 1) ? (to - from) : ((to - from + step - 1) / step));
		ctr.store(0);

		this->from = from;
		this->to   = to;
		this->step = step;
		this->func = func;
	}

	bool IsSliceTask() const override { return true; }
	bool ExecuteStep() override
	{
		const int i = from + (step * ctr.fetch_add(1, std::memory_order_relaxed));

		if (i < to) {
			func(i);
			remainingTasks -= 1;
			return true;
		}

		return false;
	}

private:
	std::atomic<int> ctr;
	std::function<void(const int)> func;

	int from;
	int to;
	int step;
};
#endif








template <template<typename> class TG, typename F>
struct TaskPool {
	typedef TG<F> FuncTaskGroup;
	typedef std::shared_ptr<FuncTaskGroup> FuncTaskGroupPtr;

	// more than 256 nested for_mt's or parallel's should be uncommon
	std::array<FuncTaskGroupPtr, 256> tgPool;
	std::atomic_int pos = {0};

	TaskPool() {
		for (size_t i = 0; i < tgPool.size(); ++i) {
			tgPool[i] = FuncTaskGroupPtr(new FuncTaskGroup(true));
		}
	}


	FuncTaskGroupPtr GetTaskGroup() {
		auto tg = tgPool[pos.fetch_add(1) % tgPool.size()];

		assert(tg->IsFinished());
		assert(tg->IsInTaskPool());
		assert(!tg->IsInJobQueue());
		assert(!tg->IsInPoolUse());

		tg->ResetState(true, true, true);
		return tg;
	}
};








template <typename F>
static inline void for_mt(int start, int end, int step, F&& f)
{
	if (!ThreadPool::HasThreads() || ((end - start) < step)) {
		for (int i = start; i < end; i += step) {
			f(i);
		}
		return;
	}

	SCOPED_MT_TIMER("ThreadPool::AddTask");

	// static, so TaskGroup's are recycled
	static TaskPool<ForTaskGroup, F> pool;
	auto taskGroup = pool.GetTaskGroup();

	taskGroup->Enqueue(start, end, step, f);
	taskGroup->UpdateId();

	assert(taskGroup->IsInJobQueue());

	#if 0
	ThreadPool::PushTaskGroup(taskGroup);
	#else
	// store the group in all worker queues s.t. each executes a slice
	for (size_t i = 1; i < ThreadPool::GetNumThreads(); ++i) {
		taskGroup->wantedThread.store(i);
		ThreadPool::PushTaskGroup(taskGroup);
	}
	#endif

	// make calling thread also run ExecuteLoop
	ThreadPool::WaitForFinished(taskGroup);

}

template <typename F>
static inline void for_mt(int start, int end, F&& f)
{
	for_mt(start, end, 1, f);
}


template <typename F>
static inline void parallel(F&& f)
{
	if (!ThreadPool::HasThreads())
		return f();

	SCOPED_MT_TIMER("ThreadPool::AddTask");

	// static, so TaskGroup's are recycled
	static TaskPool<Parallel2TaskGroup, F> pool;
	auto taskGroup = pool.GetTaskGroup();

	taskGroup->Enqueue(f);
	taskGroup->UpdateId();

	assert(taskGroup->IsInJobQueue());

	// note: child-tasks are pushed, parent itself should not be
	// ThreadPool::PushTaskGroup(taskGroup);
	ThreadPool::WaitForFinished(taskGroup);
}


template<class F, class G>
static inline auto parallel_reduce(F&& f, G&& g) -> typename std::result_of<F()>::type
{
	if (!ThreadPool::HasThreads())
		return f();

	SCOPED_MT_TIMER("ThreadPool::AddTask");

	typedef  typename std::result_of<F()>::type  RetType;
	// typedef  typename std::shared_ptr< AsyncTask<F> >  TaskType;
	typedef           std::shared_ptr< std::future<RetType> >  FoldType;

	// std::array<TaskType, ThreadPool::MAX_THREADS> tasks;
	std::array<AsyncTask<F>*, ThreadPool::MAX_THREADS> tasks;
	std::array<FoldType, ThreadPool::MAX_THREADS> results;

	// NOTE:
	//   results become available in AsyncTask::ExecuteStep, and can allow
	//   accumulate to return (followed by tasks going out of scope) before
	//   ExecuteStep's themselves have returned --> premature task deletion
	//   if shared_ptr were used (all tasks *must* have exited ExecuteLoop)
	//
	// tasks[0] = std::move(std::make_shared<AsyncTask<F>>(std::forward<F>(f)));
	tasks[0] = new AsyncTask<F>(std::forward<F>(f));
	results[0] = std::move(tasks[0]->GetFuture());

	// first job in a reduction usually wants to run on the main thread
	tasks[0]->ExecuteLoop(0, false);

	// need to push N individual tasks; see NOTE in TParallelTaskGroup
	for (size_t i = 1, n = ThreadPool::GetNumThreads(); i < n; ++i) {
		// tasks[i] = std::move(std::make_shared<AsyncTask<F>>(std::forward<F>(f)));
		tasks[i] = new AsyncTask<F>(std::forward<F>(f));
		results[i] = std::move(tasks[i]->GetFuture());

		// tasks[i]->selfDelete.store(false);
		tasks[i]->wantedThread.store(i);

		ThreadPool::PushTaskGroup(tasks[i]);
	}

	return (std::accumulate(results.begin(), results.begin() + ThreadPool::GetNumThreads(), 0, g));
}




namespace ThreadPool {
	template<class F, class... Args>
	static inline auto Enqueue(F&& f, Args&&... args)
	-> std::shared_ptr<std::future<typename std::result_of<F(Args...)>::type>>
	{
		typedef typename std::result_of<F(Args...)>::type return_type;

		if (!ThreadPool::HasThreads()) {
			// directly process when there are no worker threads
			auto task = std::make_shared< std::packaged_task<return_type()> >(std::bind(f, args ...));
			auto fut = std::make_shared<std::future<return_type>>(task->get_future());
			(*task)();
			return fut;
		}

		// can not use shared_ptr here, make async tasks delete themselves instead
		// auto task = std::make_shared<AsyncTask<F, Args...>>(std::forward<F>(f), std::forward<Args>(args)...);
		auto task = new AsyncTask<F, Args...>(std::forward<F>(f), std::forward<Args>(args)...);
		auto fut = task->GetFuture();

		// minor hack: assume AsyncTask's will cause (heavy) disk IO
		// although these can never block the main thread, the async
		// workers might still be handed an uneven work distribution
		task->wantedThread.store(1 + task->GetId() % (ThreadPool::GetNumThreads() - 1));

		ThreadPool::PushTaskGroup(task);
		return fut;
	}
}

#endif
#endif