26 using namespace folly;
28 static constexpr
auto one_ms = std::chrono::milliseconds(1);
38 EXPECT_EQ(this->lock_state, CurrentLockState::UNLOCKED);
39 this->lock_state = CurrentLockState::UNIQUE;
42 EXPECT_EQ(this->lock_state, CurrentLockState::UNIQUE);
43 this->lock_state = CurrentLockState::UNLOCKED;
46 EXPECT_EQ(this->lock_state, CurrentLockState::UNLOCKED);
47 this->lock_state = CurrentLockState::SHARED;
50 EXPECT_EQ(this->lock_state, CurrentLockState::SHARED);
51 this->lock_state = CurrentLockState::UNLOCKED;
54 EXPECT_EQ(this->lock_state, CurrentLockState::UNLOCKED);
59 this->lock_state = CurrentLockState::UNLOCKED;
64 this->lock_state = CurrentLockState::UNIQUE;
67 EXPECT_EQ(this->lock_state, CurrentLockState::UNIQUE);
71 EXPECT_EQ(this->lock_state, CurrentLockState::UNIQUE);
72 this->lock_state = CurrentLockState::SHARED;
76 this->lock_state = CurrentLockState::SHARED;
79 template <
class Rep,
class Period>
81 EXPECT_EQ(this->lock_state, CurrentLockState::UNLOCKED);
82 this->lock_state = CurrentLockState::UNIQUE;
86 template <
class Rep,
class Period>
88 EXPECT_EQ(this->lock_state, CurrentLockState::UNLOCKED);
93 template <
class Rep,
class Period>
95 const std::chrono::duration<Rep, Period>&) {
97 this->lock_state = CurrentLockState::UNIQUE;
109 static_assert(!traits::is_timed,
"std:mutex is not a timed lock");
110 static_assert(!traits::is_shared,
"std:mutex is not a shared lock");
111 static_assert(!traits::is_upgrade,
"std::mutex is not an upgradable lock");
115 traits::unlock(mutex);
120 static_assert(traits::is_timed,
"folly::SharedMutex is a timed lock");
121 static_assert(traits::is_shared,
"folly::SharedMutex is a shared lock");
122 static_assert(traits::is_upgrade,
"folly::SharedMutex is an upgradable lock");
126 traits::unlock(mutex);
128 traits::lock_shared(mutex);
129 traits::lock_shared(mutex);
130 traits::unlock_shared(mutex);
131 traits::unlock_shared(mutex);
133 traits::lock_upgrade(mutex);
134 traits::unlock_upgrade(mutex);
137 traits::lock_upgrade(mutex);
138 traits::unlock_upgrade_and_lock(mutex);
139 bool gotLock = traits::try_lock_for(mutex,
one_ms);
140 EXPECT_FALSE(gotLock) <<
"Should not have been able to acquire an exclusive " 141 "lock after upgrading to an exclusive lock";
142 gotLock = traits::try_lock_upgrade_for(mutex,
one_ms);
143 EXPECT_FALSE(gotLock) <<
"Should not have been able to acquire an upgrade " 144 "lock after upgrading to an exclusive lock";
145 gotLock = traits::try_lock_shared_for(mutex,
one_ms);
146 EXPECT_FALSE(gotLock) <<
"Should not have been able to acquire a shared " 147 "lock after upgrading to an exclusive lock";
148 traits::unlock(mutex);
150 traits::lock_upgrade(mutex);
151 traits::unlock_upgrade_and_lock_shared(mutex);
152 gotLock = traits::try_lock_for(mutex,
one_ms);
153 EXPECT_FALSE(gotLock) <<
"Should not have been able to acquire an exclusive " 154 "mutex after downgrading from an upgrade to a " 156 traits::unlock_shared(mutex);
159 gotLock = traits::try_lock_for(mutex,
one_ms);
160 EXPECT_FALSE(gotLock) <<
"Should not have been able to acquire an exclusive " 161 "lock after acquiring an exclusive lock";
162 gotLock = traits::try_lock_upgrade_for(mutex,
one_ms);
163 EXPECT_FALSE(gotLock) <<
"Should not have been able to acquire an upgrade " 164 "lock after acquiring an exclusive lock";
165 gotLock = traits::try_lock_shared_for(mutex,
one_ms);
166 EXPECT_FALSE(gotLock) <<
"Should not have been able to acquire a shared " 167 "lock after acquiring an exclusive lock";
168 traits::unlock_and_lock_upgrade(mutex);
169 gotLock = traits::try_lock_for(mutex,
one_ms);
170 EXPECT_FALSE(gotLock) <<
"Should not have been able to acquire an exclusive " 171 "lock after downgrading to an upgrade lock";
172 traits::unlock_upgrade(mutex);
175 traits::unlock_and_lock_shared(mutex);
176 gotLock = traits::try_lock_for(mutex,
one_ms);
177 EXPECT_FALSE(gotLock) <<
"Should not have been able to acquire an exclusive " 178 "lock after downgrading to a shared lock";
179 traits::unlock_shared(mutex);
184 static_assert(!traits::is_timed,
"folly::SpinLock is not a timed lock");
185 static_assert(!traits::is_shared,
"folly::SpinLock is not a shared lock");
187 !traits::is_upgrade,
"folly::SpinLock is not an upgradable lock");
191 traits::unlock(mutex);
196 static_assert(!traits::is_timed,
"folly::RWSpinLock is not a timed lock");
197 static_assert(traits::is_shared,
"folly::RWSpinLock is a shared lock");
198 static_assert(traits::is_upgrade,
"folly::RWSpinLock is an upgradable lock");
202 traits::unlock(mutex);
204 traits::lock_shared(mutex);
205 traits::lock_shared(mutex);
206 traits::unlock_shared(mutex);
207 traits::unlock_shared(mutex);
212 static_assert(!traits::is_timed,
"boost::mutex is not a timed lock");
213 static_assert(!traits::is_shared,
"boost::mutex is not a shared lock");
214 static_assert(!traits::is_upgrade,
"boost::mutex is not an upgradable lock");
218 traits::unlock(mutex);
224 !traits::is_timed,
"boost::recursive_mutex is not a timed lock");
226 !traits::is_shared,
"boost::recursive_mutex is not a shared lock");
228 !traits::is_upgrade,
"boost::recursive_mutex is not an upgradable lock");
230 boost::recursive_mutex
mutex;
233 traits::unlock(mutex);
234 traits::unlock(mutex);
237 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES 240 static_assert(traits::is_timed,
"std::timed_mutex is a timed lock");
241 static_assert(!traits::is_shared,
"std::timed_mutex is not a shared lock");
243 !traits::is_upgrade,
"std::timed_mutex is not an upgradable lock");
245 std::timed_mutex
mutex;
247 bool gotLock = traits::try_lock_for(mutex, std::chrono::milliseconds(1));
248 EXPECT_FALSE(gotLock) <<
"should not have been able to acquire the " 249 <<
"timed_mutex a second time";
250 traits::unlock(mutex);
255 static_assert(traits::is_timed,
"std::recursive_timed_mutex is a timed lock");
257 !traits::is_shared,
"std::recursive_timed_mutex is not a shared lock");
260 "std::recursive_timed_mutex is not an upgradable lock");
262 std::recursive_timed_mutex
mutex;
264 auto gotLock = traits::try_lock_for(mutex, std::chrono::milliseconds(10));
265 EXPECT_TRUE(gotLock) <<
"should have been able to acquire the " 266 <<
"recursive_timed_mutex a second time";
267 traits::unlock(mutex);
268 traits::unlock(mutex);
273 static_assert(traits::is_timed,
"boost::shared_mutex is a timed lock");
274 static_assert(traits::is_shared,
"boost::shared_mutex is a shared lock");
276 traits::is_upgrade,
"boost::shared_mutex is an upgradable lock");
278 boost::shared_mutex
mutex;
280 auto gotLock = traits::try_lock_for(mutex, std::chrono::milliseconds(1));
281 EXPECT_FALSE(gotLock) <<
"should not have been able to acquire the " 282 <<
"shared_mutex a second time";
283 gotLock = traits::try_lock_shared_for(mutex, std::chrono::milliseconds(1));
284 EXPECT_FALSE(gotLock) <<
"should not have been able to acquire the " 285 <<
"shared_mutex a second time";
286 traits::unlock(mutex);
288 traits::lock_shared(mutex);
289 gotLock = traits::try_lock_for(mutex, std::chrono::milliseconds(1));
290 EXPECT_FALSE(gotLock) <<
"should not have been able to acquire the " 291 <<
"shared_mutex a second time";
292 gotLock = traits::try_lock_shared_for(mutex, std::chrono::milliseconds(10));
293 EXPECT_TRUE(gotLock) <<
"should have been able to acquire the " 294 <<
"shared_mutex a second time in shared mode";
295 traits::unlock_shared(mutex);
296 traits::unlock_shared(mutex);
298 #endif // FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES 310 mutex.unlock_upgrade();
311 mutex.lock_upgrade();
314 mutex.lock_upgrade();
322 mutex.unlock_upgrade();
323 mutex.lock_upgrade();
326 mutex.lock_upgrade();
328 mutex.unlock_shared();
334 mutex.unlock_shared();
338 EXPECT_EQ(mutex.lock_state, Mutex::CurrentLockState::UNLOCKED);
349 EXPECT_TRUE(gotLock) <<
"Should have been able to acquire the fake mutex";
352 mutex.lock_upgrade();
355 <<
"Should have been able to upgrade from upgrade to unique";
360 EXPECT_TRUE(gotLock) <<
"Should have been able to downgrade from exclusive " 362 mutex.unlock_upgrade();
364 mutex.lock_upgrade();
366 EXPECT_TRUE(gotLock) <<
"Should have been able to downgrade from upgrade to " 368 mutex.unlock_shared();
372 EXPECT_TRUE(gotLock) <<
"Should have been able to downgrade from exclusive " 374 mutex.unlock_shared();
detail::UnlockPolicyExclusive< LockTraits > UnlockPolicy
static bool try_lock_for(Mutex &mutex, const std::chrono::duration< Rep, Period > &timeout)
static void unlock(Mutex &mutex)
void unlock_and_lock_upgrade()
void unlock_upgrade_and_lock()
static std::true_type lock(Mutex &mutex)
#define EXPECT_EQ(val1, val2)
static bool try_lock_for(Mutex &mutex, const std::chrono::duration< Rep, Period > &timeout)
static void unlock(Mutex &mutex)
void unlock_upgrade_and_lock_shared()
static void unlock(Mutex &mutex)
—— Concurrent Priority Queue Implementation ——
void unlock_and_lock_shared()
static bool try_lock_for(Mutex &mutex, const std::chrono::duration< Rep, Period > &)
bool try_lock_for(const std::chrono::duration< Rep, Period > &)
static std::true_type lock(Mutex &mutex)
static bool try_lock_for(Mutex &mutex, const std::chrono::duration< Rep, Period > &)
static std::true_type lock(Mutex &mutex)
static bool try_lock_for(Mutex &mutex, const std::chrono::duration< Rep, Period > &)
static std::true_type lock(Mutex &mutex)
auto lock(SynchronizedLocker...lockersIn) -> std::tuple< typename SynchronizedLocker::LockedPtr... >
detail::UnlockPolicyUpgrade< LockTraits > UnlockPolicy
bool try_unlock_upgrade_and_lock_for(const std::chrono::duration< Rep, Period > &)
#define EXPECT_TRUE(condition)
detail::UnlockPolicyShared< LockTraits > UnlockPolicy
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
#define EXPECT_FALSE(condition)
static constexpr auto one_ms
static std::true_type lock(Mutex &mutex)
static void std_mutex(size_t numOps, size_t numThreads)
bool try_lock_upgrade_for(const std::chrono::duration< Rep, Period > &)
TEST(SequencedExecutor, CPUThreadPoolExecutor)