Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
TaskPool.h
Go to the documentation of this file.
1/*
2Copyright (c) 2018 DIVIDE-Studio
3Copyright (c) 2009 Ionut Cava
4
5This file is part of DIVIDE Framework.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software
9and associated documentation files (the "Software"), to deal in the Software
10without restriction,
11including without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense,
13and/or sell copies of the Software, and to permit persons to whom the
14Software is furnished to do so,
15subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED,
22INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23PARTICULAR PURPOSE AND NONINFRINGEMENT.
24IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25DAMAGES OR OTHER LIABILITY,
26WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27IN CONNECTION WITH THE SOFTWARE
28OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30*/
31
32#pragma once
33#ifndef DVD_TASK_POOL_H_
34#define DVD_TASK_POOL_H_
35
37
38namespace Divide {
39
41{
49 bool _waitForFinish = true;
51 bool _useCurrentThread = true;
53 bool _allowPoolIdle = true;
55 bool _allowRunInIdle = true;
56};
57
58using PoolTask = DELEGATE_STD<bool, bool/*threadWaitingCall*/>;
59
60class TaskPool final : public GUIDWrapper {
61public:
62 public:
63
64 constexpr static bool IsBlocking = true;
65
66 explicit TaskPool(std::string_view workerName);
67 ~TaskPool();
68
69 bool init(size_t threadCount, const DELEGATE<void, const std::thread::id&>& onThreadCreate = {});
70 void shutdown();
71
72 static Task* AllocateTask(Task* parentTask, DELEGATE<void, Task&>&& func, bool allowedInIdle ) noexcept;
73
75 size_t flushCallbackQueue();
76 void waitForAllTasks(bool flushCallbacks);
77
80 void threadWaiting();
81
82
83 // Add a new task to the pool's queue
84 bool addTask( PoolTask&& job );
85
86 // Reinitializes the thread pool (joins and closes out all existing threads first)
87 void init();
88 // Join all of the threads and block until all running tasks have completed.
89 void join();
90
91 // Wait for all running jobs to finish
92 void wait() const noexcept;
93
94 void executeOneTask( bool isIdleCall );
95
96 PROPERTY_R( vector<std::thread>, threads );
97
98
99 private:
100 //ToDo: replace all friend class declarations with attorneys -Ionut;
101 friend struct Task;
102 friend void Start(Task& task, TaskPool& pool, const TaskPriority priority, const DELEGATE<void>& onCompletionFunction);
103 friend void Wait(const Task& task, TaskPool& pool);
104 friend void Parallel_For(TaskPool& pool, const ParallelForDescriptor& descriptor);
105
106 void taskStarted(Task& task);
107 void taskCompleted(Task& task);
108
109 bool enqueue(Task& task, TaskPriority priority, const DELEGATE<void>& onCompletionFunction);
110 bool runRealTime(Task& task, const DELEGATE<void>& onCompletionFunction );
111
112 bool deque( bool isIdleCall, PoolTask& taskOut );
113 void waitForTask(const Task& task);
114
115 private:
116 const string _threadNamePrefix;
117
119 {
121 U32 _taskID = U32_MAX;
122 };
123
125 eastl::fixed_vector<CallbackEntry, 1 << 9, true> _taskCallbacks;
126
127 moodycamel::ConcurrentQueue<U32> _threadedCallbackBuffer{};
128
129 using QueueType = std::conditional_t<IsBlocking, moodycamel::BlockingConcurrentQueue<PoolTask>, moodycamel::ConcurrentQueue<PoolTask>>;
131
133 std::condition_variable _taskFinishedCV;
135
136 std::atomic_uint _runningTaskCount = 0u;
137 std::atomic_size_t _activeThreads{ 0u };
138
139 bool _isRunning{ true };
140};
141
142template<class Predicate>
143Task* CreateTask(Predicate&& threadedFunction, bool allowedInIdle = true);
144
145template<class Predicate>
146Task* CreateTask(Task* parentTask, Predicate&& threadedFunction, bool allowedInIdle = true);
147
148void Parallel_For(TaskPool& pool, const ParallelForDescriptor& descriptor, const DELEGATE<void, const Task*, U32/*start*/, U32/*end*/>& cbk);
149
150} //namespace Divide
151
152#endif //DVD_TASK_POOL_H_
153
154#include "TaskPool.inl"
#define PROPERTY_R(...)
Convenience method to add a class member with public read access but protected write access.
Utility class that adds basic GUID management to objects.
Definition: GUIDWrapper.h:44
static Task * AllocateTask(Task *parentTask, DELEGATE< void, Task & > &&func, bool allowedInIdle) noexcept
Definition: TaskPool.cpp:290
eastl::fixed_vector< CallbackEntry, 1<< 9, true > _taskCallbacks
Definition: TaskPool.h:125
bool enqueue(Task &task, TaskPriority priority, const DELEGATE< void > &onCompletionFunction)
Definition: TaskPool.cpp:83
void threadWaiting()
Definition: TaskPool.cpp:341
void waitForTask(const Task &task)
Definition: TaskPool.cpp:184
std::atomic_size_t _activeThreads
Definition: TaskPool.h:137
const string _threadNamePrefix
Definition: TaskPool.h:116
Mutex _taskFinishedMutex
Definition: TaskPool.h:132
friend void Start(Task &task, TaskPool &pool, const TaskPriority priority, const DELEGATE< void > &onCompletionFunction)
Definition: Task.cpp:9
std::condition_variable _taskFinishedCV
Definition: TaskPool.h:133
moodycamel::ConcurrentQueue< U32 > _threadedCallbackBuffer
Definition: TaskPool.h:127
DELEGATE< void, const std::thread::id & > _threadCreateCbk
Definition: TaskPool.h:134
void executeOneTask(bool isIdleCall)
Definition: TaskPool.cpp:383
size_t flushCallbackQueue()
Returns the number of callbacks processed.
Definition: TaskPool.cpp:201
static constexpr bool IsBlocking
Definition: TaskPool.h:64
std::conditional_t< IsBlocking, moodycamel::BlockingConcurrentQueue< PoolTask >, moodycamel::ConcurrentQueue< PoolTask > > QueueType
Definition: TaskPool.h:129
friend void Parallel_For(TaskPool &pool, const ParallelForDescriptor &descriptor)
void wait() const noexcept
Definition: TaskPool.cpp:364
QueueType _queue
Definition: TaskPool.h:130
void waitForAllTasks(bool flushCallbacks)
Definition: TaskPool.cpp:246
bool deque(bool isIdleCall, PoolTask &taskOut)
Definition: TaskPool.cpp:393
std::atomic_uint _runningTaskCount
Definition: TaskPool.h:136
friend void Wait(const Task &task, TaskPool &pool)
Definition: Task.cpp:20
void taskCompleted(Task &task)
Definition: TaskPool.cpp:271
SharedMutex _taskCallbacksLock
Definition: TaskPool.h:124
bool addTask(PoolTask &&job)
Definition: TaskPool.cpp:378
bool runRealTime(Task &task, const DELEGATE< void > &onCompletionFunction)
Definition: TaskPool.cpp:155
void taskStarted(Task &task)
Definition: TaskPool.cpp:266
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
DELEGATE_STD< Ret, Args... > DELEGATE
DELEGATE_STD< bool, bool > PoolTask
Definition: TaskPool.h:58
std::function< Ret(Args...) > DELEGATE_STD
std::mutex Mutex
Definition: SharedMutex.h:40
Task * CreateTask(Predicate &&threadedFunction, bool allowedInIdle=true)
Definition: TaskPool.inl:45
std::shared_mutex SharedMutex
Definition: SharedMutex.h:43
eastl::vector< Type > vector
Definition: Vector.h:42
TaskPriority
Definition: Task.h:41
constexpr U32 U32_MAX
void Parallel_For(TaskPool &pool, const ParallelForDescriptor &descriptor, const DELEGATE< void, const Task *, U32, U32 > &cbk)
Definition: TaskPool.cpp:428
uint32_t U32
bool _allowPoolIdle
If true, we'll inform the thread pool to execute other tasks while waiting for the all async tasks to...
Definition: TaskPool.h:53
U32 _partitionSize
How many elements should we process per async task.
Definition: TaskPool.h:45
bool _useCurrentThread
If true, we'll process a for partition on the calling thread.
Definition: TaskPool.h:51
bool _waitForFinish
If this is false, the Parallel_For call won't block the current thread.
Definition: TaskPool.h:49
TaskPriority _priority
Each async task will start with the same priority specified here.
Definition: TaskPool.h:47
U32 _iterCount
For loop iteration count.
Definition: TaskPool.h:43
bool _allowRunInIdle
If true, async tasks can be invoked from other task's idle callbacks.
Definition: TaskPool.h:55
Definition: TaskPool.h:119
DELEGATE< void > _cbk
Definition: TaskPool.h:120