cc_semaphore.h 615 B

123456789101112131415161718192021222324252627282930313233343536
  1. #pragma once
  2. #include "latch.h"
  3. template <typename TThis>
  4. class TComplexConditionSemaphore {
  5. private:
  6. TLatch Latch;
  7. public:
  8. void Updated() {
  9. if (GetThis()->TryWait()) {
  10. Latch.Unlock();
  11. }
  12. }
  13. void Wait() {
  14. while (!GetThis()->TryWait()) {
  15. Latch.Lock();
  16. if (GetThis()->TryWait()) {
  17. Latch.Unlock();
  18. return;
  19. }
  20. Latch.Wait();
  21. }
  22. }
  23. bool IsLocked() {
  24. return Latch.IsLocked();
  25. }
  26. private:
  27. TThis* GetThis() {
  28. return static_cast<TThis*>(this);
  29. }
  30. };