You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
- #pragma once
-
- #include <mutex>
- #include <condition_variable>
- #include <ecs/config.h>
-
- beg_namespace_ecs_core_utils
- {
-
- struct counter_blocker
- {
- private:
- std::mutex _mutex;
- std::condition_variable _cond_var;
- size_t _counter;
-
- public:
- inline counter_blocker(size_t counter) noexcept
- : _counter(counter)
- { }
-
- inline void increment() noexcept
- {
- std::lock_guard lock(_mutex);
- assert(_counter > 0);
- ++_counter;
- }
-
- inline void decrement_and_notify_one() noexcept
- {
- std::lock_guard lock(_mutex);
- assert(_counter > 0);
- --_counter;
- _cond_var.notify_one();
- }
-
- inline void decrement_and_notify_all() noexcept
- {
- std::lock_guard lock(_mutex);
- assert(_counter > 0);
- --_counter;
- _cond_var.notify_all();
- }
-
- template<typename T_func>
- inline void execute_and_wait_until_zero(T_func&& func) noexcept
- {
- func();
- std::unique_lock lock(_mutex);
- _cond_var.wait(lock, [this]{ return (_counter == 0); });
- }
- };
-
- }
- end_namespace_ecs_core_utils
|