|
| 1 | +//===--- ConditionVariable.h - ConditionVariable -------------- -*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Provides a system-independent condition variable, Condition. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#ifndef SWIFT_THREADING_CONDITIONVARIABLE_H |
| 18 | +#define SWIFT_THREADING_CONDITIONVARIABLE_H |
| 19 | + |
| 20 | +#include "ScopedLock.h" |
| 21 | + |
| 22 | +#include "Impl.h" |
| 23 | + |
| 24 | +namespace swift { |
| 25 | + |
| 26 | +// A condition variable. |
| 27 | +// |
| 28 | +// Note that use of this class should be minimised because there is no |
| 29 | +// automatic way to deal with priority inversions; the operating system |
| 30 | +// cannot, in general, know which thread(s) might wake a thread that is |
| 31 | +// waiting on a condition variable. |
| 32 | +// |
| 33 | +// FWIW: the reason this class doesn't use a separate Mutex instance is |
| 34 | +// that on Darwin, Mutex uses os_unfair_lock, and there is no condition |
| 35 | +// variable implementation that takes one of those. |
| 36 | +class ConditionVariable { |
| 37 | + |
| 38 | + ConditionVariable(const ConditionVariable &) = delete; |
| 39 | + ConditionVariable &operator=(const ConditionVariable &) = delete; |
| 40 | + ConditionVariable(ConditionVariable &&) = delete; |
| 41 | + ConditionVariable &operator=(ConditionVariable &&) = delete; |
| 42 | + |
| 43 | +public: |
| 44 | + /// Constructs a condition variable. |
| 45 | + ConditionVariable() { |
| 46 | + threading_impl::cond_init(Handle); |
| 47 | + } |
| 48 | + ~ConditionVariable() { |
| 49 | + threading_impl::cond_destroy(Handle); |
| 50 | + } |
| 51 | + |
| 52 | + /// Lock the associated mutex. |
| 53 | + /// |
| 54 | + /// Precondition: ConditionVariable not locked by this thread. |
| 55 | + void lock() { |
| 56 | + threading_impl::cond_lock(Handle); |
| 57 | + } |
| 58 | + |
| 59 | + /// Unlock the associated mutex. |
| 60 | + /// |
| 61 | + /// Precondition: ConditionVariable locked by this thread. |
| 62 | + void unlock() { |
| 63 | + threading_impl::cond_unlock(Handle); |
| 64 | + } |
| 65 | + |
| 66 | + /// Unblock a single thread that is blocked on this condition variable. |
| 67 | + void signal() { |
| 68 | + threading_impl::cond_signal(Handle); |
| 69 | + } |
| 70 | + |
| 71 | + /// Unblock all threads that are blocked on this condition variable. |
| 72 | + void broadcast() { |
| 73 | + threading_impl::cond_broadcast(Handle); |
| 74 | + } |
| 75 | + |
| 76 | + /// Unlock and block on this condition variable. |
| 77 | + /// |
| 78 | + /// After this method returns, the associated mutex will be locked. |
| 79 | + /// |
| 80 | + /// Precondition: ConditionVariable locked by this thread. |
| 81 | + void wait() { |
| 82 | + threading_impl::cond_wait(Handle); |
| 83 | + } |
| 84 | + |
| 85 | + /// Unlock and block on this condition variable, with a timeout. |
| 86 | + /// |
| 87 | + /// After this method returns, the associated mutex will be locked. |
| 88 | + /// |
| 89 | + /// Precondition: ConditionVariable locked by this thread. |
| 90 | + template <class Rep, class Period> |
| 91 | + bool wait(std::chrono::duration<Rep, Period> duration) { |
| 92 | + return threading_impl::cond_wait(Handle, duration); |
| 93 | + } |
| 94 | + |
| 95 | + /// Unlock and block on this condition variable, with a deadline. |
| 96 | + /// |
| 97 | + /// After this method returns, the associated mutex will be locked. |
| 98 | + /// |
| 99 | + /// Precondition: ConditionVariable locked by this thread. |
| 100 | + template <class Rep, class Period> |
| 101 | + bool waitUntil(std::chrono::time_point<std::chrono::system_clock, |
| 102 | + std::chrono::duration<Rep, Period>> deadline) { |
| 103 | + auto sysdeadline = std::chrono::time_point_cast< |
| 104 | + std::chrono::system_clock::duration>(deadline); |
| 105 | + return threading_impl::cond_wait(Handle, sysdeadline); |
| 106 | + } |
| 107 | + |
| 108 | + /// Acquires lock before calling the supplied critical section and releases |
| 109 | + /// lock on return from critical section. |
| 110 | + /// |
| 111 | + /// This call can block while waiting for the lock to become available. |
| 112 | + /// |
| 113 | + /// For example the following mutates value while holding the mutex lock. |
| 114 | + /// |
| 115 | + /// ``` |
| 116 | + /// cond.withLock([&value] { value++; }); |
| 117 | + /// ``` |
| 118 | + /// |
| 119 | + /// Precondition: ConditionVariable not locked by this thread. |
| 120 | + template <typename CriticalSection> |
| 121 | + auto withLock(CriticalSection &&criticalSection) |
| 122 | + -> decltype(std::forward<CriticalSection>(criticalSection)()) { |
| 123 | + ScopedLock guard(*this); |
| 124 | + return std::forward<CriticalSection>(criticalSection)(); |
| 125 | + } |
| 126 | + |
| 127 | + /// A stack based object that locks the supplied ConditionVariable on |
| 128 | + /// construction and unlocks it on destruction. |
| 129 | + /// |
| 130 | + /// Precondition: ConditionVariable not locked by this thread. |
| 131 | + typedef ScopedLockT<ConditionVariable, false> ScopedLock; |
| 132 | + |
| 133 | + /// A stack based object that unlocks the supplied ConditionVariable on |
| 134 | + /// construction and locks it on destruction. |
| 135 | + /// |
| 136 | + /// Precondition: ConditionVariable locked by this thread. |
| 137 | + typedef ScopedLockT<ConditionVariable, true> ScopedUnlock; |
| 138 | + |
| 139 | +private: |
| 140 | + threading_impl::cond_handle Handle; |
| 141 | +}; |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +#endif // SWIFT_THREADING_CONDITIONVARIABLE_H |
0 commit comments