File include/jlib/threads.h changed (mode: 100644) (index 6b5b199..8b5506c) |
... |
... |
namespace jl::threads { |
33 |
33 |
using Spinlock [[deprecated]] = spinlock_pth; |
using Spinlock [[deprecated]] = spinlock_pth; |
34 |
34 |
using Condition [[deprecated]] = condition_pth; |
using Condition [[deprecated]] = condition_pth; |
35 |
35 |
|
|
36 |
|
template<lock_type T> |
|
37 |
|
using Lock [[deprecated]] = typename |
|
38 |
|
std::conditional<T == lock_type::SPINLOCK,Spinlock,Mutex>::type; |
|
39 |
|
|
|
40 |
36 |
template<typename result_t> |
template<typename result_t> |
41 |
|
using thread = Thread<result_t>; |
|
42 |
|
template<lock_type T> |
|
43 |
|
using lock = Lock<T>; |
|
|
37 |
|
using thread = thread_pth<result_t>; |
44 |
38 |
using mutex = mutex_pth; |
using mutex = mutex_pth; |
45 |
39 |
using spinlock = spinlock_pth; |
using spinlock = spinlock_pth; |
46 |
40 |
using condition = condition_pth; |
using condition = condition_pth; |
|
41 |
|
|
|
42 |
|
template<lock_type T> |
|
43 |
|
using lock [[deprecated]] = typename |
|
44 |
|
std::conditional<T == lock_type::SPINLOCK,spinlock,mutex>::type; |
|
45 |
|
|
|
46 |
|
template<lock_type T> |
|
47 |
|
using Lock [[deprecated]] = lock<T>; |
47 |
48 |
} |
} |
48 |
49 |
namespace jth = jl::threads; |
namespace jth = jl::threads; |
File src/thread_pool.cpp changed (mode: 100644) (index 668fa40..f5c4d97) |
20 |
20 |
#include <jlib/rarray.h> |
#include <jlib/rarray.h> |
21 |
21 |
#include <jlib/darray.h> |
#include <jlib/darray.h> |
22 |
22 |
#include <jlib/threads.h> |
#include <jlib/threads.h> |
|
23 |
|
#ifdef __clang__ |
23 |
24 |
#pragma clang diagnostic ignored "-Wdeprecated" |
#pragma clang diagnostic ignored "-Wdeprecated" |
|
25 |
|
#elif defined __GNUC__ |
|
26 |
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
|
27 |
|
#endif |
24 |
28 |
constexpr static const unsigned int STASK_UNLOADED = -1u; |
constexpr static const unsigned int STASK_UNLOADED = -1u; |
25 |
29 |
using namespace jl; |
using namespace jl; |
26 |
30 |
using namespace jl::threads; |
using namespace jl::threads; |
|
... |
... |
struct TaskQueues { |
28 |
32 |
jl::rarray<jl::darray<Task>> queues; |
jl::rarray<jl::darray<Task>> queues; |
29 |
33 |
mutex queues_lock; |
mutex queues_lock; |
30 |
34 |
unsigned int in_progress_count; |
unsigned int in_progress_count; |
31 |
|
condition idle_condition; |
|
32 |
|
condition condition; |
|
|
35 |
|
condition wait_idle_condition; |
|
36 |
|
condition wait_task_condition; |
33 |
37 |
bool has_tasks; |
bool has_tasks; |
34 |
38 |
bool brodcast_empty; |
bool brodcast_empty; |
35 |
39 |
}; |
}; |
|
... |
... |
queues_init(TaskQueues *p, unsigned int count) { |
71 |
75 |
for (auto &queue : p->queues) |
for (auto &queue : p->queues) |
72 |
76 |
queue.init(); |
queue.init(); |
73 |
77 |
p->queues_lock.init(); |
p->queues_lock.init(); |
74 |
|
p->condition.init(); |
|
75 |
|
p->idle_condition.init(); |
|
|
78 |
|
p->wait_task_condition.init(); |
|
79 |
|
p->wait_idle_condition.init(); |
76 |
80 |
p->has_tasks = false; |
p->has_tasks = false; |
77 |
81 |
p->brodcast_empty = false; |
p->brodcast_empty = false; |
78 |
82 |
p->in_progress_count = 0; |
p->in_progress_count = 0; |
|
... |
... |
queues_destroy(TaskQueues *p) { |
83 |
87 |
for (auto &queue : p->queues) |
for (auto &queue : p->queues) |
84 |
88 |
queue.destroy(); |
queue.destroy(); |
85 |
89 |
p->queues.destroy(); |
p->queues.destroy(); |
86 |
|
p->idle_condition.destroy(); |
|
87 |
|
p->condition.destroy(); |
|
|
90 |
|
p->wait_idle_condition.destroy(); |
|
91 |
|
p->wait_task_condition.destroy(); |
88 |
92 |
} |
} |
89 |
93 |
[[nodiscard]] static bool |
[[nodiscard]] static bool |
90 |
94 |
queues_submit_task(TaskQueues *p, const Task& task, unsigned int queue_i) { |
queues_submit_task(TaskQueues *p, const Task& task, unsigned int queue_i) { |
|
... |
... |
queues_submit_task(TaskQueues *p, const Task& task, unsigned int queue_i) { |
93 |
97 |
result = p->queues[queue_i].insert(task); |
result = p->queues[queue_i].insert(task); |
94 |
98 |
if (result or not p->has_tasks) { |
if (result or not p->has_tasks) { |
95 |
99 |
p->has_tasks = true; |
p->has_tasks = true; |
96 |
|
p->condition.wake_up_threads(); |
|
|
100 |
|
p->wait_task_condition.wake_up_threads(); |
97 |
101 |
} |
} |
98 |
102 |
p->queues_lock.unlock(); |
p->queues_lock.unlock(); |
99 |
103 |
return result; |
return result; |
|
... |
... |
queues_get_task(TaskQueues *p, Task *p_dst, bool *p_stop, bool job_done) { |
107 |
111 |
while (not p->has_tasks and not *p_stop) { WAIT: |
while (not p->has_tasks and not *p_stop) { WAIT: |
108 |
112 |
if (p->brodcast_empty) { |
if (p->brodcast_empty) { |
109 |
113 |
p->brodcast_empty = false; |
p->brodcast_empty = false; |
110 |
|
p->idle_condition.wake_up_thread(); |
|
|
114 |
|
p->wait_idle_condition.wake_up_thread(); |
111 |
115 |
} |
} |
112 |
|
p->condition.wait(&p->queues_lock); |
|
|
116 |
|
p->wait_task_condition.wait(&p->queues_lock); |
113 |
117 |
} |
} |
114 |
118 |
if (*p_stop) { |
if (*p_stop) { |
115 |
119 |
continue_to_work = false; |
continue_to_work = false; |
|
... |
... |
C_T: |
271 |
275 |
static void |
static void |
272 |
276 |
pool_stop(ThreadPool::Data *p) { |
pool_stop(ThreadPool::Data *p) { |
273 |
277 |
p->stop_work = true; |
p->stop_work = true; |
274 |
|
p->task_queues.condition.wake_up_threads(); |
|
|
278 |
|
p->task_queues.wait_task_condition.wake_up_threads(); |
275 |
279 |
for (auto &th : p->threads) |
for (auto &th : p->threads) |
276 |
280 |
th.instance.join(); |
th.instance.join(); |
277 |
281 |
p->stask_queue.destroy(); |
p->stask_queue.destroy(); |
|
... |
... |
wait_idle() { |
316 |
320 |
or |
or |
317 |
321 |
p->task_queues.in_progress_count > 0) { |
p->task_queues.in_progress_count > 0) { |
318 |
322 |
p->task_queues.brodcast_empty = true; |
p->task_queues.brodcast_empty = true; |
319 |
|
p->task_queues.idle_condition.wait(&p->task_queues.queues_lock); |
|
|
323 |
|
p->task_queues.wait_idle_condition.wait(&p->task_queues.queues_lock); |
320 |
324 |
} |
} |
321 |
325 |
p->task_queues.queues_lock.unlock(); |
p->task_queues.queues_lock.unlock(); |
322 |
326 |
} |
} |