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.

53 lines
1.2 KiB

  1. #pragma once
  2. #include <iostream> // TODO debug!
  3. #include <ecs/core/utils/counter_blocker.h>
  4. namespace ecs {
  5. namespace core {
  6. namespace utils {
  7. inline counter_blocker
  8. ::counter_blocker(size_t counter) noexcept
  9. : _counter(counter)
  10. { }
  11. inline void counter_blocker
  12. ::increment() noexcept
  13. {
  14. std::lock_guard lock(_mutex);
  15. assert(_counter > 0);
  16. ++_counter;
  17. }
  18. inline void counter_blocker
  19. ::decrement() noexcept
  20. {
  21. std::lock_guard lock(_mutex);
  22. assert(_counter > 0);
  23. --_counter;
  24. _cond_var.notify_all();
  25. }
  26. template<typename T_func>
  27. inline void counter_blocker
  28. ::execute_and_wait(T_func&& func)
  29. {
  30. func();
  31. std::unique_lock lock(_mutex);
  32. _cond_var.wait(lock, [this]{ return (_counter == 0); });
  33. }
  34. template<typename T_func, typename T_tick>
  35. inline void counter_blocker
  36. ::execute_and_wait_tick(T_func&& func, T_tick&& tick)
  37. {
  38. func();
  39. std::unique_lock lock(_mutex);
  40. _cond_var.wait(lock, [this, t = std::forward<T_tick>(tick)]{
  41. t();
  42. return (_counter == 0);
  43. });
  44. }
  45. } } }