|
- #pragma once
-
- #include <mutex>
- #include <condition_variable>
-
- #include <ecs/config.h>
-
- namespace ecs {
- namespace core {
- namespace utils {
-
- /**
- * counter that blocks a given operation until it is equal zero
- */
- struct counter_blocker
- {
- protected:
- std::recursive_mutex _mutex; //!< mutex to preotect the counter
- std::condition_variable_any _cond_var; //!< conditional variable to synchronize the counter
- size_t _counter; //!< the actual counter value
-
- public:
- /**
- * constructor
- *
- * @param counter initial counter value
- */
- inline counter_blocker(size_t counter) noexcept;
-
- /**
- * increment the counter value by one
- */
- inline void increment() noexcept;
-
- /**
- * decrement the counter value by one and notify all pending threads
- */
- inline void decrement() noexcept;
-
- /**
- * execute the given function and wait until the counter is equal to zero
- *
- * @tparam T_func type of the function to execute
- *
- * @param func function to execute before waiting for the counter
- */
- template<typename T_func>
- inline void execute_and_wait(T_func&& func);
-
- /**
- * Execute the given function and wait until the counter is equal to zero.
- * Execute the given tick function at least once and repeatedly if the counter value has changed.
- *
- * @tparam T_func type of the function to execute
- * @tparam T_tick type of tick function to execute
- *
- * @param func function to execute before waiting for the counter
- * @param tick tick function to execute
- */
- template<typename T_func, typename T_tick>
- inline void execute_and_wait_tick(T_func&& func, T_tick&& tick);
- };
-
- } } }
|