123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- #pragma once
- #ifndef _linux_
- #include <util/system/mutex.h>
- #include <util/system/condvar.h>
- #endif
- #include <limits>
- #include <atomic>
- namespace NYT::NThreading {
- class TEventCount final
- {
- public:
- TEventCount() = default;
- TEventCount(const TEventCount&) = delete;
- TEventCount(TEventCount&&) = delete;
- class TCookie
- {
- public:
- explicit TCookie(ui32 epoch)
- : Epoch_(epoch)
- { }
- private:
- friend class TEventCount;
- ui32 Epoch_;
- };
- void NotifyOne();
- void NotifyAll();
- void NotifyMany(int count);
- TCookie PrepareWait();
- void CancelWait();
- bool Wait(TCookie cookie, TInstant deadline = TInstant::Max());
- bool Wait(TCookie cookie, TDuration timeout);
-
-
- template <class TCondition>
- bool Await(TCondition&& condition, TInstant deadline = TInstant::Max());
- template <class TCondition>
- bool Await(TCondition&& condition, TDuration timeout);
- private:
-
-
- std::atomic<ui64> Value_ = 0;
- static constexpr ui64 AddWaiter = static_cast<ui64>(1);
- static constexpr ui64 SubWaiter = static_cast<ui64>(-1);
- static constexpr ui64 EpochShift = 32;
- static constexpr ui64 AddEpoch = static_cast<ui64>(1) << EpochShift;
- static constexpr ui64 WaiterMask = AddEpoch - 1;
- #ifndef _linux_
- TCondVar ConditionVariable_;
- TMutex Mutex_;
- #endif
- };
- class TEvent final
- {
- public:
- TEvent() = default;
- TEvent(const TEvent&) = delete;
- TEvent(TEvent&&) = delete;
- void NotifyOne();
- void NotifyAll();
- bool Test() const;
- bool Wait(TInstant deadline = TInstant::Max());
- bool Wait(TDuration timeout);
- private:
- std::atomic<bool> Set_ = false;
- TEventCount EventCount_;
- };
- }
- #define EVENT_COUNT_INL_H_
- #include "event_count-inl.h"
- #undef EVENT_COUNT_INL_H_
|