|
- #pragma once
-
- #include <mutex>
- #include <thread>
- #include <condition_variable>
- #include <ecs/config.h>
-
- #include "./task_queue.h"
- #include "../movable_atomic.h"
-
- namespace ecs {
- namespace core {
- namespace utils {
-
- /**
- * worker thread to execute tasks of thread pool in
- */
- struct thread_pool_worker
- {
- private:
- /**
- * state of the worker
- */
- enum class state
- {
- uninizialized, //!< the thread was not initialized yes
- running, //!< the thread is running normally
- stopped, //!< the thread was intended to stop execution
- finished, //!< the thread has stopped execution
- };
-
- using atomic_state = movable_atomic<state>;
-
- std::thread _thread; //!< actual thread object to use for execution
- std::mutex _mutex; //!< mutex to protext the conditional
- std::condition_variable _conditional; //!< condition variable to inform about new tasks
- concurrent_task_queue& _all_tasks; //!< queue to poll new tasks from
- task_queue _own_tasks; //!< queue of tasks that are assigned to this specific worker
- atomic_state _state; //!< current state of the worker
- size_t _thread_id; //!< index of the thread inside the thread pool
-
- private:
- /**
- * thread worker method
- */
- void run();
-
- /**
- * pop the next task from either the own task list or the general task list
- *
- * @param t parameter to store dequeued task in
- * @param timeout timeout to wait for new tasks
- *
- * @return TRUE if a task was dequeued, FALSE otherwise
- */
- template<typename T_rep, typename T_period>
- inline bool pop(task& t, const std::chrono::duration<T_rep, T_period>& timeout);
-
- public:
- /**
- * constructor
- *
- * @param queue task queue to poll tasks from
- */
- inline thread_pool_worker(concurrent_task_queue& all_tasks, size_t thread_id) noexcept;
-
- inline thread_pool_worker(thread_pool_worker&&) = default;
- inline thread_pool_worker(const thread_pool_worker&) = delete;
-
- inline thread_pool_worker& operator=(thread_pool_worker&&) = default;
- inline thread_pool_worker& operator=(const thread_pool_worker&) = delete;
-
- /**
- * start the worker
- *
- * @tparam T_counter type of the counter
- *
- * @param remaining_inits number of remaining thread initialization (is decremented by this thread)
- */
- template<typename T_counter>
- inline void start(T_counter& remaining_inits);
-
- /**
- * stop the thread execution
- */
- inline void stop() noexcept;
-
- /**
- * wait for the thread to finish
- */
- inline void join() noexcept;
-
- /**
- * check if the thread has stopped execution
- *
- * @retval TRUE if the thread has stopped execution
- * @retval FALSE if the thread is still running
- */
- inline bool finished() const noexcept;
-
- /**
- * enqueue a new task to execute inside this worker thread
- *
- * @tparam T_task type of the task
- *
- * @param task task to execute
- */
- template<typename T_task>
- inline void post(T_task&& task);
-
- /**
- * inform the task about new tasks in the concurent task list
- */
- inline void signal();
-
- };
-
- } } }
|