|
- #pragma once
-
- #include <iostream> // TODO debug!
- #include <ecs/core/utils/counter_blocker.h>
-
- namespace ecs {
- namespace core {
- namespace utils {
-
- inline counter_blocker
- ::counter_blocker(size_t counter) noexcept
- : _counter(counter)
- { }
-
- inline void counter_blocker
- ::increment() noexcept
- {
- std::lock_guard lock(_mutex);
- assert(_counter > 0);
- ++_counter;
- }
-
- inline void counter_blocker
- ::decrement() noexcept
- {
- std::lock_guard lock(_mutex);
- assert(_counter > 0);
- --_counter;
- _cond_var.notify_all();
- }
-
- template<typename T_func>
- inline void counter_blocker
- ::execute_and_wait(T_func&& func)
- {
- func();
- std::unique_lock lock(_mutex);
- _cond_var.wait(lock, [this]{ return (_counter == 0); });
- }
-
- template<typename T_func, typename T_tick>
- inline void counter_blocker
- ::execute_and_wait_tick(T_func&& func, T_tick&& tick)
- {
- func();
- std::unique_lock lock(_mutex);
- _cond_var.wait(lock, [this, t = std::forward<T_tick>(tick)]{
- t();
- return (_counter == 0);
- });
- }
-
- } } }
|