14 #include <condition_variable> 17 #include "containers.h" 28 template <
class TMemInterface >
class base_task;
29 template <
class TMemInterface >
class base_task_graph;
30 template <
class TMemInterface >
class base_thread_pool;
31 template <
class TMemInterface >
struct base_thread;
36 template <
class TMemInterface >
class base_thread_pool
38 typedef base_thread< TMemInterface > thread_type;
39 typedef base_task_graph< TMemInterface > task_graph_type;
40 typedef base_thread_pool< TMemInterface > thread_pool;
41 typedef typename task_graph_type::task_type task_type;
42 typedef typename task_graph_type::task_queue_type task_queue_type;
43 typedef typename task_graph_type::task_memory_allocator_type task_memory_allocator_type;
44 typedef typename thread_type::thread_index_type thread_index_type;
99 void wake_up(thread_num_t _num_threads_to_wake_up =
max_num_threads, uint64_t _thread_affinity_mask = std::numeric_limits<uint64_t>::max());
128 optimization std::atomic< typename task_type::rank_type > queue_rank[task_type::num_priority][
max_num_threads];
131 template <
class TMemInterface >
138 memset(threads, 0,
sizeof(threads));
139 for (
auto &priority_queue : queue_rank)
141 for (
auto &rank : priority_queue)
153 for (uint32_t thread_idx = 0; thread_idx <
num_threads; thread_idx++)
161 std::unique_lock< std::mutex > signal(
setup.
signal);
166 for (uint32_t iterations = 0; iterations <
num_threads; ++iterations)
168 threads[iterations]->start();
177 for (uint32_t thread_idx = 0; thread_idx <
num_threads; thread_idx++)
179 threads[thread_idx]->
join();
182 for (uint32_t thread_idx = 0; thread_idx <
num_threads; thread_idx++)
184 delete threads[thread_idx];
187 memset(threads, 0,
sizeof(threads));
190 template <
class TMemInterface >
193 _num_threads_to_wake_up = std::min(
num_threads, _num_threads_to_wake_up);
194 reduce_starvation(always_different_thread_woken_up_first)
static thread_index_type next_thread_index(
this, 0);
195 for (uint32_t iterations = 0, woke_up = 0; woke_up < _num_threads_to_wake_up && iterations <
num_threads; ++next_thread_index, ++iterations)
197 if (!next_thread_index.
is_set(_thread_affinity_mask))
199 threads[next_thread_index]->
wake_up();
203 reduce_starvation(always_different_thread_woken_up_first)++ next_thread_index;
206 template <
class TMemInterface >
209 return task_scheduler::get_current_thread< typename thread_pool::thread_type >();
void wake_up(thread_num_t _num_threads_to_wake_up=max_num_threads, uint64_t _thread_affinity_mask=std::numeric_limits< uint64_t >::max())
Wakes up.
Definition: threadpool.h:191
std::mutex signal
The signal
Definition: threadpool.h:65
std::condition_variable radio
The radio
Definition: threadpool.h:69
std::atomic< uint32_t > num_working
The number working
Definition: threadpool.h:126
task_graph_type * task_graph
The task graph
Definition: threadpool.h:117
std::atomic_uint32_t thread_sync
The thread synchronize
Definition: threadpool.h:73
const thread_num_t max_num_threads
The maximum number threads
Definition: globals.h:29
void wake_up()
Wakes up.
Definition: thread.h:206
Struct setup_container
Definition: threadpool.h:60
persistent_container persistent
The persistent
Definition: taskgraph.h:273
thread_type * get_current_thread()
Gets the current thread.
Definition: threadpool.h:207
Class stl_allocator.
Definition: allocator.h:16
void join()
Joins this instance.
Definition: thread.h:211
bool is_set(thread_mask_int_t _other_mask)
Determines whether the specified other mask is set.
Definition: types.h:195
std::atomic< state_selector > request_exit
The request exit
Definition: threadpool.h:77
setup_container setup
The setup
Definition: threadpool.h:109
sub_graph_vector sub_graphs
The sub graphs
Definition: taskgraph.h:104
task_memory_allocator_type task_memory_allocator
The threads
Definition: threadpool.h:122
void stop()
Stops this instance.
Definition: threadpool.h:172
Struct base_thread
Definition: task.h:37
thread_num_t num_threads
The number threads
Definition: threadpool.h:113
void start(task_graph_type &task_graph)
Starts the specified task graph.
Definition: threadpool.h:148
state_selector
Enum state_selector
Definition: threadpool.h:50
static thread_type * create_thread(thread_pool *_pool)
Creates the thread.
Definition: thread.h:168
base_thread_pool(thread_num_t _num_threads=max_num_threads)
Initializes a new instance of the base_thread_pool class.
Definition: threadpool.h:132