sem.h 857 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #pragma once
  2. #include "defaults.h"
  3. #include <util/generic/ptr.h>
  4. // named sempahore
  5. class TSemaphore {
  6. public:
  7. TSemaphore(const char* name, ui32 maxFreeCount);
  8. ~TSemaphore();
  9. // Increase the semaphore counter.
  10. void Release() noexcept;
  11. // Keep a thread held while the semaphore counter is equal 0.
  12. void Acquire() noexcept;
  13. // Try to enter the semaphore gate. A non-blocking variant of Acquire.
  14. // Returns 'true' if the semaphore counter decreased
  15. bool TryAcquire() noexcept;
  16. private:
  17. class TImpl;
  18. THolder<TImpl> Impl_;
  19. };
  20. // unnamed semaphore, faster, than previous
  21. class TFastSemaphore {
  22. public:
  23. TFastSemaphore(ui32 maxFreeCount);
  24. ~TFastSemaphore();
  25. void Release() noexcept;
  26. void Acquire() noexcept;
  27. bool TryAcquire() noexcept;
  28. private:
  29. class TImpl;
  30. THolder<TImpl> Impl_;
  31. };