count_down_latch.h 908 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #pragma once
  2. #include "public.h"
  3. #ifndef _linux_
  4. #include <util/system/condvar.h>
  5. #include <util/system/mutex.h>
  6. #endif
  7. namespace NYT::NThreading {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. //! A synchronization aid that allows one or more threads to wait until
  10. //! a set of operations being performed in other threads completes.
  11. /*!
  12. * See https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html
  13. */
  14. class TCountDownLatch final
  15. {
  16. public:
  17. explicit TCountDownLatch(int count);
  18. void CountDown();
  19. void Wait() const;
  20. bool TryWait() const;
  21. int GetCount() const;
  22. private:
  23. std::atomic<int> Count_;
  24. #ifndef _linux_
  25. mutable TCondVar ConditionVariable_;
  26. mutable TMutex Mutex_;
  27. #endif
  28. };
  29. ////////////////////////////////////////////////////////////////////////////////
  30. } // namespace NYT::NThreading