2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <folly/IntrusiveList.h>
21 #include <folly/SpinLock.h>
22 #include <folly/fibers/GenericBaton.h>
30 * Like mutex but allows timed_lock in addition to lock and try_lock.
37 DCHECK(threadWaiters_.empty());
38 DCHECK(fiberWaiters_.empty());
39 DCHECK(notifiedFiber_ == nullptr);
42 TimedMutex(const TimedMutex& rhs) = delete;
43 TimedMutex& operator=(const TimedMutex& rhs) = delete;
44 TimedMutex(TimedMutex&& rhs) = delete;
45 TimedMutex& operator=(TimedMutex&& rhs) = delete;
47 // Lock the mutex. The thread / fiber is blocked until the mutex is free
50 // Lock the mutex. The thread / fiber will be blocked for a time duration.
52 // @return true if the mutex was locked, false otherwise
53 template <typename Rep, typename Period>
54 bool timed_lock(const std::chrono::duration<Rep, Period>& duration);
56 // Try to obtain lock without blocking the thread or fiber
59 // Unlock the mutex and wake up a waiter if there is one
63 enum class LockResult { SUCCESS, TIMEOUT, STOLEN };
65 template <typename WaitFunc>
66 LockResult lockHelper(WaitFunc&& waitFunc);
68 // represents a waiter waiting for the lock. The waiter waits on the
69 // baton until it is woken up by a post or timeout expires.
72 folly::IntrusiveListHook hook;
75 using MutexWaiterList = folly::IntrusiveList<MutexWaiter, &MutexWaiter::hook>;
77 folly::SpinLock lock_; //< lock to protect waiter list
78 bool locked_ = false; //< is this locked by some thread?
79 MutexWaiterList threadWaiters_; //< list of waiters
80 MutexWaiterList fiberWaiters_; //< list of waiters
81 MutexWaiter* notifiedFiber_{nullptr}; //< Fiber waiter which has been notified
87 * A readers-writer lock which allows multiple readers to hold the
88 * lock simultaneously or only one writer.
90 * NOTE: This is a reader-preferred RWLock i.e. readers are give priority
91 * when there are both readers and writers waiting to get the lock.
93 template <typename BatonType>
97 pthread_spin_init(&lock_, PTHREAD_PROCESS_PRIVATE);
101 pthread_spin_destroy(&lock_);
104 TimedRWMutex(const TimedRWMutex& rhs) = delete;
105 TimedRWMutex& operator=(const TimedRWMutex& rhs) = delete;
106 TimedRWMutex(TimedRWMutex&& rhs) = delete;
107 TimedRWMutex& operator=(TimedRWMutex&& rhs) = delete;
109 // Lock for shared access. The thread / fiber is blocked until the lock
113 // Like read_lock except the thread /fiber is blocked for a time duration
114 // @return true if locked successfully, false otherwise.
115 template <typename Rep, typename Period>
116 bool timed_read_lock(const std::chrono::duration<Rep, Period>& duration);
118 // Like read_lock but doesn't block the thread / fiber if the lock can't
120 // @return true if lock was acquired, false otherwise.
121 bool try_read_lock();
123 // Obtain an exclusive lock. The thread / fiber is blocked until the lock
127 // Like write_lock except the thread / fiber is blocked for a time duration
128 // @return true if locked successfully, false otherwise.
129 template <typename Rep, typename Period>
130 bool timed_write_lock(const std::chrono::duration<Rep, Period>& duration);
132 // Like write_lock but doesn't block the thread / fiber if the lock cant be
134 // @return true if lock was acquired, false otherwise.
135 bool try_write_lock();
137 // Wrapper for write_lock() for compatibility with Mutex
142 // Realease the lock. The thread / fiber will wake up all readers if there are
143 // any. If there are waiting writers then only one of them will be woken up.
144 // NOTE: readers are given priority over writers (see above comment)
147 // Downgrade the lock. The thread / fiber will wake up all readers if there
153 explicit ReadHolder(TimedRWMutex& lock) : lock_(&lock) {
163 ReadHolder(const ReadHolder& rhs) = delete;
164 ReadHolder& operator=(const ReadHolder& rhs) = delete;
165 ReadHolder(ReadHolder&& rhs) = delete;
166 ReadHolder& operator=(ReadHolder&& rhs) = delete;
174 explicit WriteHolder(TimedRWMutex& lock) : lock_(&lock) {
184 WriteHolder(const WriteHolder& rhs) = delete;
185 WriteHolder& operator=(const WriteHolder& rhs) = delete;
186 WriteHolder(WriteHolder&& rhs) = delete;
187 WriteHolder& operator=(WriteHolder&& rhs) = delete;
194 // invariants that must hold when the lock is not held by anyone
195 void verify_unlocked_properties() {
196 assert(readers_ == 0);
197 assert(read_waiters_.empty());
198 assert(write_waiters_.empty());
201 // Different states the lock can be in
208 typedef boost::intrusive::list_member_hook<> MutexWaiterHookType;
210 // represents a waiter waiting for the lock.
213 MutexWaiterHookType hook;
216 typedef boost::intrusive::
217 member_hook<MutexWaiter, MutexWaiterHookType, &MutexWaiter::hook>
220 typedef boost::intrusive::list<
223 boost::intrusive::constant_time_size<true>>
226 pthread_spinlock_t lock_; //< lock protecting the internal state
227 // (state_, read_waiters_, etc.)
228 State state_ = State::UNLOCKED;
230 uint32_t readers_ = 0; //< Number of readers who have the lock
232 MutexWaiterList write_waiters_; //< List of thread / fibers waiting for
235 MutexWaiterList read_waiters_; //< List of thread / fibers waiting for
241 #include "TimedMutex-inl.h"