ST_engine  0.3-ALPHA
task_manager.hpp
1 /* This file is part of the "ST" project.
2  * You may use, distribute or modify this code under the terms
3  * of the GNU General Public License version 2.
4  * See LICENCE.txt in the root directory of the project.
5  *
6  * Author: Maxim Atanasov
7  * E-mail: maxim.atanasov@protonmail.com
8  */
9 
10 #ifndef TASK_MNGR_DEF
11 #define TASK_MNGR_DEF
12 
13 #include "../src/main/task.hpp"
14 #include "../src/main/semaphore.hpp"
15 #include <vector>
16 #include <thread>
17 #include <ST_util/atomic_queue/concurrentqueue.h>
18 
19 typedef semaphore *task_id;
20 
22 
26 class task_manager {
27 
28 private:
29  uint8_t thread_num = 0;
30  std::vector<std::thread> task_threads{};
31  std::atomic_bool run_threads{true};
32  semaphore *work_sem{};
33  moodycamel::ConcurrentQueue<ST::task *> global_task_queue;
34 
35  static int task_thread(task_manager *self);
36 
37  static void do_work(ST::task *work);
38 
39  static void start_thread(int (*thread_func)(void *), void *data);
40 
41 public:
42  explicit task_manager();
43 
44  explicit task_manager(uint8_t thread_num);
45 
46  ~task_manager();
47 
49 
50  void start_task_lockfree(ST::task *arg);
51 
52  void wait_for_task(task_id id);
53 
54  void work_wait_for_task(task_id id);
55 };
56 
57 #endif //TASK_MNGR_DEF
An object representing a task to be run by the task manager.
Definition: task.hpp:24
The Task Manager handles all things multi-threaded in the engine.
void start_task_lockfree(ST::task *arg)
void wait_for_task(task_id id)
void work_wait_for_task(task_id id)
task_id start_task(ST::task *arg)