diff --git a/include/__mutex_base b/include/__mutex_base index a73e400e6..d4023a64f 100644 --- a/include/__mutex_base +++ b/include/__mutex_base @@ -20,16 +20,6 @@ #pragma GCC system_header #endif -#ifdef _LIBCPP_SHARED_LOCK - -namespace ting { -template class shared_lock; -template class upgrade_lock; -} - -#endif // _LIBCPP_SHARED_LOCK - - _LIBCPP_BEGIN_NAMESPACE_STD class _LIBCPP_TYPE_VIS mutex @@ -162,27 +152,6 @@ public: return *this; } -#ifdef _LIBCPP_SHARED_LOCK - - unique_lock(ting::shared_lock&&, try_to_lock_t); - template - unique_lock(ting::shared_lock&&, - const chrono::time_point<_Clock, _Duration>&); - template - unique_lock(ting::shared_lock&&, - const chrono::duration<_Rep, _Period>&); - - explicit unique_lock(ting::upgrade_lock&&); - unique_lock(ting::upgrade_lock&&, try_to_lock_t); - template - unique_lock(ting::upgrade_lock&&, - const chrono::time_point<_Clock, _Duration>&); - template - unique_lock(ting::upgrade_lock&&, - const chrono::duration<_Rep, _Period>&); - -#endif // _LIBCPP_SHARED_LOCK - #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES void lock(); diff --git a/include/shared_mutex b/include/shared_mutex new file mode 100644 index 000000000..5b1f53aaf --- /dev/null +++ b/include/shared_mutex @@ -0,0 +1,419 @@ +// -*- C++ -*- +//===------------------------ shared_mutex --------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_SHARED_MUTEX +#define _LIBCPP_SHARED_MUTEX + +/* + shared_mutex synopsis + +// C++1y + +namespace std +{ + +class shared_mutex +{ +public: + shared_mutex(); + ~shared_mutex(); + + shared_mutex(const shared_mutex&) = delete; + shared_mutex& operator=(const shared_mutex&) = delete; + + // Exclusive ownership + void lock(); // blocking + bool try_lock(); + template + bool try_lock_for(const chrono::duration& rel_time); + template + bool try_lock_until(const chrono::time_point& abs_time); + void unlock(); + + // Shared ownership + void lock_shared(); // blocking + bool try_lock_shared(); + template + bool + try_lock_shared_for(const chrono::duration& rel_time); + template + bool + try_lock_shared_until(const chrono::time_point& abs_time); + void unlock_shared(); +}; + +template +class shared_lock +{ +public: + typedef Mutex mutex_type; + + // Shared locking + shared_lock() noexcept; + explicit shared_lock(mutex_type& m); // blocking + shared_lock(mutex_type& m, defer_lock_t) noexcept; + shared_lock(mutex_type& m, try_to_lock_t); + shared_lock(mutex_type& m, adopt_lock_t); + template + shared_lock(mutex_type& m, + const chrono::time_point& abs_time); + template + shared_lock(mutex_type& m, + const chrono::duration& rel_time); + ~shared_lock(); + + shared_lock(shared_lock const&) = delete; + shared_lock& operator=(shared_lock const&) = delete; + + shared_lock(shared_lock&& u) noexcept; + shared_lock& operator=(shared_lock&& u) noexcept; + + void lock(); // blocking + bool try_lock(); + template + bool try_lock_for(const chrono::duration& rel_time); + template + bool try_lock_until(const chrono::time_point& abs_time); + void unlock(); + + // Setters + void swap(shared_lock& u) noexcept; + mutex_type* release() noexcept; + + // Getters + bool owns_lock() const noexcept; + explicit operator bool () const noexcept; + mutex_type* mutex() const noexcept; +}; + +template + void swap(shared_lock& x, shared_lock& y) noexcept; + +} // std + +*/ + +#include <__config> + +#if _LIBCPP_STD_VER > 11 || defined(_LIBCPP_BUILDING_SHARED_MUTEX) + +#include <__mutex_base> + +#include <__undef_min_max> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +class _LIBCPP_TYPE_VIS shared_mutex +{ + mutex __mut_; + condition_variable __gate1_; + condition_variable __gate2_; + unsigned __state_; + + static const unsigned __write_entered_ = 1U << (sizeof(unsigned)*__CHAR_BIT__ - 1); + static const unsigned __n_readers_ = ~__write_entered_; +public: + shared_mutex(); + _LIBCPP_INLINE_VISIBILITY ~shared_mutex() = default; + + shared_mutex(const shared_mutex&) = delete; + shared_mutex& operator=(const shared_mutex&) = delete; + + // Exclusive ownership + void lock(); + bool try_lock(); + template + _LIBCPP_INLINE_VISIBILITY + bool + try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time) + { + return try_lock_until(chrono::steady_clock::now() + __rel_time); + } + template + bool + try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time); + void unlock(); + + // Shared ownership + void lock_shared(); + bool try_lock_shared(); + template + _LIBCPP_INLINE_VISIBILITY + bool + try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rel_time) + { + return try_lock_shared_until(chrono::steady_clock::now() + __rel_time); + } + template + bool + try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time); + void unlock_shared(); +}; + +template +bool +shared_mutex::try_lock_until( + const chrono::time_point<_Clock, _Duration>& __abs_time) +{ + unique_lock __lk(__mut_); + if (__state_ & __write_entered_) + { + while (true) + { + cv_status __status = __gate1_.wait_until(__lk, __abs_time); + if ((__state_ & __write_entered_) == 0) + break; + if (__status == cv_status::timeout) + return false; + } + } + __state_ |= __write_entered_; + if (__state_ & __n_readers_) + { + while (true) + { + cv_status __status = __gate2_.wait_until(__lk, __abs_time); + if ((__state_ & __n_readers_) == 0) + break; + if (__status == cv_status::timeout) + { + __state_ &= ~__write_entered_; + return false; + } + } + } + return true; +} + +template +bool +shared_mutex::try_lock_shared_until( + const chrono::time_point<_Clock, _Duration>& __abs_time) +{ + unique_lock __lk(__mut_); + if ((__state_ & __write_entered_) || (__state_ & __n_readers_) == __n_readers_) + { + while (true) + { + cv_status status = __gate1_.wait_until(__lk, __abs_time); + if ((__state_ & __write_entered_) == 0 && + (__state_ & __n_readers_) < __n_readers_) + break; + if (status == cv_status::timeout) + return false; + } + } + unsigned __num_readers = (__state_ & __n_readers_) + 1; + __state_ &= ~__n_readers_; + __state_ |= __num_readers; + return true; +} + +template +class shared_lock +{ +public: + typedef _Mutex mutex_type; + +private: + mutex_type* __m_; + bool __owns_; + +public: + _LIBCPP_INLINE_VISIBILITY + shared_lock() noexcept + : __m_(nullptr), + __owns_(false) + {} + + _LIBCPP_INLINE_VISIBILITY + explicit shared_lock(mutex_type& __m) + : __m_(&__m), + __owns_(true) + {__m_->lock_shared();} + + _LIBCPP_INLINE_VISIBILITY + shared_lock(mutex_type& __m, defer_lock_t) noexcept + : __m_(&__m), + __owns_(false) + {} + + _LIBCPP_INLINE_VISIBILITY + shared_lock(mutex_type& __m, try_to_lock_t) + : __m_(&__m), + __owns_(__m.try_lock_shared()) + {} + + _LIBCPP_INLINE_VISIBILITY + shared_lock(mutex_type& __m, adopt_lock_t) + : __m_(&__m), + __owns_(true) + {} + + template + _LIBCPP_INLINE_VISIBILITY + shared_lock(mutex_type& __m, + const chrono::time_point<_Clock, _Duration>& __abs_time) + : __m_(&__m), + __owns_(__m.try_lock_shared_until(__abs_time)) + {} + + template + _LIBCPP_INLINE_VISIBILITY + shared_lock(mutex_type& __m, + const chrono::duration<_Rep, _Period>& __rel_time) + : __m_(&__m), + __owns_(__m.try_lock_shared_for(__rel_time)) + {} + + _LIBCPP_INLINE_VISIBILITY + ~shared_lock() + { + if (__owns_) + __m_->unlock_shared(); + } + + shared_lock(shared_lock const&) = delete; + shared_lock& operator=(shared_lock const&) = delete; + + _LIBCPP_INLINE_VISIBILITY + shared_lock(shared_lock&& __u) noexcept + : __m_(__u.__m_), + __owns_(__u.__owns_) + { + __u.__m_ = nullptr; + __u.__owns_ = false; + } + + _LIBCPP_INLINE_VISIBILITY + shared_lock& operator=(shared_lock&& __u) noexcept + { + if (__owns_) + __m_->unlock_shared(); + __m_ = nullptr; + __owns_ = false; + __m_ = __u.__m_; + __owns_ = __u.__owns_; + __u.__m_ = nullptr; + __u.__owns_ = false; + return *this; + } + + void lock(); + bool try_lock(); + template + bool try_lock_for(const chrono::duration& rel_time); + template + bool try_lock_until(const chrono::time_point& abs_time); + void unlock(); + + // Setters + _LIBCPP_INLINE_VISIBILITY + void swap(shared_lock& __u) noexcept + { + _VSTD::swap(__m_, __u.__m_); + _VSTD::swap(__owns_, __u.__owns_); + } + + _LIBCPP_INLINE_VISIBILITY + mutex_type* release() noexcept + { + mutex_type* __m = __m_; + __m_ = nullptr; + __owns_ = false; + return __m; + } + + // Getters + _LIBCPP_INLINE_VISIBILITY + bool owns_lock() const noexcept {return __owns_;} + + _LIBCPP_INLINE_VISIBILITY + explicit operator bool () const noexcept {return __owns_;} + + _LIBCPP_INLINE_VISIBILITY + mutex_type* mutex() const noexcept {return __m_;} +}; + +template +void +shared_lock<_Mutex>::lock() +{ + if (__m_ == nullptr) + __throw_system_error(EPERM, "shared_lock::lock: references null mutex"); + if (__owns_) + __throw_system_error(EDEADLK, "shared_lock::lock: already locked"); + __m_->lock_shared(); + __owns_ = true; +} + +template +bool +shared_lock<_Mutex>::try_lock() +{ + if (__m_ == nullptr) + __throw_system_error(EPERM, "shared_lock::try_lock: references null mutex"); + if (__owns_) + __throw_system_error(EDEADLK, "shared_lock::try_lock: already locked"); + __owns_ = __m_->try_lock_shared(); + return __owns_; +} + +template +template +bool +shared_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d) +{ + if (__m_ == nullptr) + __throw_system_error(EPERM, "shared_lock::try_lock_for: references null mutex"); + if (__owns_) + __throw_system_error(EDEADLK, "shared_lock::try_lock_for: already locked"); + __owns_ = __m_->try_lock_shared_for(__d); + return __owns_; +} + +template +template +bool +shared_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) +{ + if (__m_ == nullptr) + __throw_system_error(EPERM, "shared_lock::try_lock_until: references null mutex"); + if (__owns_) + __throw_system_error(EDEADLK, "shared_lock::try_lock_until: already locked"); + __owns_ = __m_->try_lock_shared_until(__t); + return __owns_; +} + +template +void +shared_lock<_Mutex>::unlock() +{ + if (!__owns_) + __throw_system_error(EPERM, "shared_lock::unlock: not locked"); + __m_->unlock_shared(); + __owns_ = false; +} + +template +inline _LIBCPP_INLINE_VISIBILITY +void +swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) noexcept + {__x.swap(__y);} + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER > 11 + +#endif // _LIBCPP_SHARED_MUTEX diff --git a/src/shared_mutex.cpp b/src/shared_mutex.cpp new file mode 100644 index 000000000..5fb22e445 --- /dev/null +++ b/src/shared_mutex.cpp @@ -0,0 +1,101 @@ +//===---------------------- shared_mutex.cpp ------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#define _LIBCPP_BUILDING_SHARED_MUTEX +#include "shared_mutex" + +_LIBCPP_BEGIN_NAMESPACE_STD + +shared_mutex::shared_mutex() + : __state_(0) +{ +} + +// Exclusive ownership + +void +shared_mutex::lock() +{ + unique_lock lk(__mut_); + while (__state_ & __write_entered_) + __gate1_.wait(lk); + __state_ |= __write_entered_; + while (__state_ & __n_readers_) + __gate2_.wait(lk); +} + +bool +shared_mutex::try_lock() +{ + unique_lock lk(__mut_); + if (__state_ == 0) + { + __state_ = __write_entered_; + return true; + } + return false; +} + +void +shared_mutex::unlock() +{ + lock_guard _(__mut_); + __state_ = 0; + __gate1_.notify_all(); +} + +// Shared ownership + +void +shared_mutex::lock_shared() +{ + unique_lock lk(__mut_); + while ((__state_ & __write_entered_) || (__state_ & __n_readers_) == __n_readers_) + __gate1_.wait(lk); + unsigned num_readers = (__state_ & __n_readers_) + 1; + __state_ &= ~__n_readers_; + __state_ |= num_readers; +} + +bool +shared_mutex::try_lock_shared() +{ + unique_lock lk(__mut_); + unsigned num_readers = __state_ & __n_readers_; + if (!(__state_ & __write_entered_) && num_readers != __n_readers_) + { + ++num_readers; + __state_ &= ~__n_readers_; + __state_ |= num_readers; + return true; + } + return false; +} + +void +shared_mutex::unlock_shared() +{ + lock_guard _(__mut_); + unsigned num_readers = (__state_ & __n_readers_) - 1; + __state_ &= ~__n_readers_; + __state_ |= num_readers; + if (__state_ & __write_entered_) + { + if (num_readers == 0) + __gate2_.notify_one(); + } + else + { + if (num_readers == __n_readers_ - 1) + __gate1_.notify_one(); + } +} + + +_LIBCPP_END_NAMESPACE_STD diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/copy_assign.fail.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/copy_assign.fail.cpp new file mode 100644 index 000000000..0bd347d0b --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/copy_assign.fail.cpp @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// shared_lock& operator=(shared_lock const&) = delete; + +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m0; +std::shared_mutex m1; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_lock lk0(m0); + std::shared_lock lk1(m1); + lk1 = lk0; +#else +# error +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/copy_ctor.fail.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/copy_ctor.fail.cpp new file mode 100644 index 000000000..66cec9ec5 --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/copy_ctor.fail.cpp @@ -0,0 +1,30 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// shared_lock(shared_lock const&) = delete; + +#include + +#if _LIBCPP_STD_VER > 11 +std::shared_mutex m; +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_lock lk0(m); + std::shared_lock lk = lk0; +#else +# error +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/default.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/default.pass.cpp new file mode 100644 index 000000000..2adef5f76 --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/default.pass.cpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// shared_lock(); + +#include +#include + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_lock ul; + assert(!ul.owns_lock()); + assert(ul.mutex() == nullptr); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/move_assign.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/move_assign.pass.cpp new file mode 100644 index 000000000..1fcc98fce --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/move_assign.pass.cpp @@ -0,0 +1,37 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// shared_lock& operator=(shared_lock&& u); + +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m0; +std::shared_mutex m1; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_lock lk0(m0); + std::shared_lock lk1(m1); + lk1 = std::move(lk0); + assert(lk1.mutex() == &m0); + assert(lk1.owns_lock() == true); + assert(lk0.mutex() == nullptr); + assert(lk0.owns_lock() == false); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/move_ctor.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/move_ctor.pass.cpp new file mode 100644 index 000000000..7e801da20 --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/move_ctor.pass.cpp @@ -0,0 +1,33 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// shared_lock(shared_lock&& u); + +#include +#include + +#if _LIBCPP_STD_VER > 11 +std::shared_mutex m; +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_lock lk0(m); + std::shared_lock lk = std::move(lk0); + assert(lk.mutex() == &m); + assert(lk.owns_lock() == true); + assert(lk0.mutex() == nullptr); + assert(lk0.owns_lock() == false); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex.pass.cpp new file mode 100644 index 000000000..fd1dad426 --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex.pass.cpp @@ -0,0 +1,79 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// explicit shared_lock(mutex_type& m); + +#include +#include +#include +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +typedef std::chrono::system_clock Clock; +typedef Clock::time_point time_point; +typedef Clock::duration duration; +typedef std::chrono::milliseconds ms; +typedef std::chrono::nanoseconds ns; + +void f() +{ + time_point t0 = Clock::now(); + time_point t1; + { + std::shared_lock ul(m); + t1 = Clock::now(); + } + ns d = t1 - t0 - ms(250); + assert(d < ms(50)); // within 50ms +} + +void g() +{ + time_point t0 = Clock::now(); + time_point t1; + { + std::shared_lock ul(m); + t1 = Clock::now(); + } + ns d = t1 - t0; + assert(d < ms(50)); // within 50ms +} + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + m.lock(); + std::vector v; + for (int i = 0; i < 5; ++i) + v.push_back(std::thread(f)); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + for (auto& t : v) + t.join(); + m.lock_shared(); + for (auto& t : v) + t = std::thread(g); + std::thread q(f); + std::this_thread::sleep_for(ms(250)); + m.unlock_shared(); + for (auto& t : v) + t.join(); + q.join(); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_adopt_lock.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_adopt_lock.pass.cpp new file mode 100644 index 000000000..9c4400555 --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_adopt_lock.pass.cpp @@ -0,0 +1,28 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// shared_lock(mutex_type& m, adopt_lock_t); + +#include +#include + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_mutex m; + m.lock(); + std::shared_lock lk(m, std::adopt_lock); + assert(lk.mutex() == &m); + assert(lk.owns_lock() == true); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_defer_lock.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_defer_lock.pass.cpp new file mode 100644 index 000000000..2f2247a0e --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_defer_lock.pass.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// shared_lock(mutex_type& m, defer_lock_t); + +#include +#include + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_mutex m; + std::shared_lock lk(m, std::defer_lock); + assert(lk.mutex() == &m); + assert(lk.owns_lock() == false); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_duration.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_duration.pass.cpp new file mode 100644 index 000000000..f3798af9c --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_duration.pass.cpp @@ -0,0 +1,79 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class timed_mutex; + +// template +// shared_lock(mutex_type& m, const chrono::duration& rel_time); + +#include +#include +#include +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +typedef std::chrono::steady_clock Clock; +typedef Clock::time_point time_point; +typedef Clock::duration duration; +typedef std::chrono::milliseconds ms; +typedef std::chrono::nanoseconds ns; + +void f1() +{ + time_point t0 = Clock::now(); + std::shared_lock lk(m, ms(300)); + assert(lk.owns_lock() == true); + time_point t1 = Clock::now(); + ns d = t1 - t0 - ms(250); + assert(d < ms(50)); // within 50ms +} + +void f2() +{ + time_point t0 = Clock::now(); + std::shared_lock lk(m, ms(250)); + assert(lk.owns_lock() == false); + time_point t1 = Clock::now(); + ns d = t1 - t0 - ms(250); + assert(d < ms(50)); // within 50ms +} + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + { + m.lock(); + std::vector v; + for (int i = 0; i < 5; ++i) + v.push_back(std::thread(f1)); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + for (auto& t : v) + t.join(); + } + { + m.lock(); + std::vector v; + for (int i = 0; i < 5; ++i) + v.push_back(std::thread(f2)); + std::this_thread::sleep_for(ms(300)); + m.unlock(); + for (auto& t : v) + t.join(); + } +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_time_point.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_time_point.pass.cpp new file mode 100644 index 000000000..44eaee22c --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_time_point.pass.cpp @@ -0,0 +1,79 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_mutex; + +// template +// shared_lock(mutex_type& m, const chrono::time_point& abs_time); + +#include +#include +#include +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +typedef std::chrono::steady_clock Clock; +typedef Clock::time_point time_point; +typedef Clock::duration duration; +typedef std::chrono::milliseconds ms; +typedef std::chrono::nanoseconds ns; + +void f1() +{ + time_point t0 = Clock::now(); + std::shared_lock lk(m, Clock::now() + ms(300)); + assert(lk.owns_lock() == true); + time_point t1 = Clock::now(); + ns d = t1 - t0 - ms(250); + assert(d < ns(50000000)); // within 50ms +} + +void f2() +{ + time_point t0 = Clock::now(); + std::shared_lock lk(m, Clock::now() + ms(250)); + assert(lk.owns_lock() == false); + time_point t1 = Clock::now(); + ns d = t1 - t0 - ms(250); + assert(d < ms(50)); // within 50ms +} + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + { + m.lock(); + std::vector v; + for (int i = 0; i < 5; ++i) + v.push_back(std::thread(f1)); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + for (auto& t : v) + t.join(); + } + { + m.lock(); + std::vector v; + for (int i = 0; i < 5; ++i) + v.push_back(std::thread(f2)); + std::this_thread::sleep_for(ms(300)); + m.unlock(); + for (auto& t : v) + t.join(); + } +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_try_to_lock.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_try_to_lock.pass.cpp new file mode 100644 index 000000000..9dae3f96b --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.cons/mutex_try_to_lock.pass.cpp @@ -0,0 +1,72 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// shared_lock(mutex_type& m, try_to_lock_t); + +#include +#include +#include +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +typedef std::chrono::system_clock Clock; +typedef Clock::time_point time_point; +typedef Clock::duration duration; +typedef std::chrono::milliseconds ms; +typedef std::chrono::nanoseconds ns; + +void f() +{ + time_point t0 = Clock::now(); + { + std::shared_lock lk(m, std::try_to_lock); + assert(lk.owns_lock() == false); + } + { + std::shared_lock lk(m, std::try_to_lock); + assert(lk.owns_lock() == false); + } + { + std::shared_lock lk(m, std::try_to_lock); + assert(lk.owns_lock() == false); + } + while (true) + { + std::shared_lock lk(m, std::try_to_lock); + if (lk.owns_lock()) + break; + } + time_point t1 = Clock::now(); + ns d = t1 - t0 - ms(250); + assert(d < ms(200)); // within 200ms +} + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + m.lock(); + std::vector v; + for (int i = 0; i < 5; ++i) + v.push_back(std::thread(f)); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + for (auto& t : v) + t.join(); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/lock.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/lock.pass.cpp new file mode 100644 index 000000000..c6617a519 --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/lock.pass.cpp @@ -0,0 +1,77 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// void lock(); + +#include +#include +#include +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +typedef std::chrono::system_clock Clock; +typedef Clock::time_point time_point; +typedef Clock::duration duration; +typedef std::chrono::milliseconds ms; +typedef std::chrono::nanoseconds ns; + +void f() +{ + std::shared_lock lk(m, std::defer_lock); + time_point t0 = Clock::now(); + lk.lock(); + time_point t1 = Clock::now(); + assert(lk.owns_lock() == true); + ns d = t1 - t0 - ms(250); + assert(d < ms(25)); // within 25ms + try + { + lk.lock(); + assert(false); + } + catch (std::system_error& e) + { + assert(e.code().value() == EDEADLK); + } + lk.unlock(); + lk.release(); + try + { + lk.lock(); + assert(false); + } + catch (std::system_error& e) + { + assert(e.code().value() == EPERM); + } +} + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + m.lock(); + std::vector v; + for (int i = 0; i < 5; ++i) + v.push_back(std::thread(f)); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + for (auto& t : v) + t.join(); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock.pass.cpp new file mode 100644 index 000000000..d65fbfafe --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock.pass.cpp @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// bool try_lock(); + +#include +#include + +#if _LIBCPP_STD_VER > 11 + +bool try_lock_called = false; + +struct mutex +{ + bool try_lock_shared() + { + try_lock_called = !try_lock_called; + return try_lock_called; + } + void unlock_shared() {} +}; + +mutex m; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_lock lk(m, std::defer_lock); + assert(lk.try_lock() == true); + assert(try_lock_called == true); + assert(lk.owns_lock() == true); + try + { + lk.try_lock(); + assert(false); + } + catch (std::system_error& e) + { + assert(e.code().value() == EDEADLK); + } + lk.unlock(); + assert(lk.try_lock() == false); + assert(try_lock_called == false); + assert(lk.owns_lock() == false); + lk.release(); + try + { + lk.try_lock(); + assert(false); + } + catch (std::system_error& e) + { + assert(e.code().value() == EPERM); + } +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock_for.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock_for.pass.cpp new file mode 100644 index 000000000..d076e5296 --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock_for.pass.cpp @@ -0,0 +1,73 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// template +// bool try_lock_for(const chrono::duration& rel_time); + +#include +#include + +#if _LIBCPP_STD_VER > 11 + +bool try_lock_for_called = false; + +typedef std::chrono::milliseconds ms; + +struct mutex +{ + template + bool try_lock_shared_for(const std::chrono::duration& rel_time) + { + assert(rel_time == ms(5)); + try_lock_for_called = !try_lock_for_called; + return try_lock_for_called; + } + void unlock_shared() {} +}; + +mutex m; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_lock lk(m, std::defer_lock); + assert(lk.try_lock_for(ms(5)) == true); + assert(try_lock_for_called == true); + assert(lk.owns_lock() == true); + try + { + lk.try_lock_for(ms(5)); + assert(false); + } + catch (std::system_error& e) + { + assert(e.code().value() == EDEADLK); + } + lk.unlock(); + assert(lk.try_lock_for(ms(5)) == false); + assert(try_lock_for_called == false); + assert(lk.owns_lock() == false); + lk.release(); + try + { + lk.try_lock_for(ms(5)); + assert(false); + } + catch (std::system_error& e) + { + assert(e.code().value() == EPERM); + } +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock_until.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock_until.pass.cpp new file mode 100644 index 000000000..8efffc460 --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/try_lock_until.pass.cpp @@ -0,0 +1,73 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// template +// bool try_lock_until(const chrono::time_point& abs_time); + +#include +#include + +#if _LIBCPP_STD_VER > 11 + +bool try_lock_until_called = false; + +struct mutex +{ + template + bool try_lock_shared_until(const std::chrono::time_point& abs_time) + { + typedef std::chrono::milliseconds ms; + assert(Clock::now() - abs_time < ms(5)); + try_lock_until_called = !try_lock_until_called; + return try_lock_until_called; + } + void unlock_shared() {} +}; + +mutex m; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + typedef std::chrono::steady_clock Clock; + std::shared_lock lk(m, std::defer_lock); + assert(lk.try_lock_until(Clock::now()) == true); + assert(try_lock_until_called == true); + assert(lk.owns_lock() == true); + try + { + lk.try_lock_until(Clock::now()); + assert(false); + } + catch (std::system_error& e) + { + assert(e.code().value() == EDEADLK); + } + lk.unlock(); + assert(lk.try_lock_until(Clock::now()) == false); + assert(try_lock_until_called == false); + assert(lk.owns_lock() == false); + lk.release(); + try + { + lk.try_lock_until(Clock::now()); + assert(false); + } + catch (std::system_error& e) + { + assert(e.code().value() == EPERM); + } +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/unlock.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/unlock.pass.cpp new file mode 100644 index 000000000..9e4b3c8b9 --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.locking/unlock.pass.cpp @@ -0,0 +1,60 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// void unlock(); + +#include +#include + +#if _LIBCPP_STD_VER > 11 + +bool unlock_called = false; + +struct mutex +{ + void lock_shared() {} + void unlock_shared() {unlock_called = true;} +}; + +mutex m; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_lock lk(m); + lk.unlock(); + assert(unlock_called == true); + assert(lk.owns_lock() == false); + try + { + lk.unlock(); + assert(false); + } + catch (std::system_error& e) + { + assert(e.code().value() == EPERM); + } + lk.release(); + try + { + lk.unlock(); + assert(false); + } + catch (std::system_error& e) + { + assert(e.code().value() == EPERM); + } +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/member_swap.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/member_swap.pass.cpp new file mode 100644 index 000000000..6a7a39e96 --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/member_swap.pass.cpp @@ -0,0 +1,43 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// void swap(shared_lock& u) noexcept; + +#include +#include + +#if _LIBCPP_STD_VER > 11 + +struct mutex +{ + void lock_shared() {} + void unlock_shared() {} +}; + +mutex m; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_lock lk1(m); + std::shared_lock lk2; + lk1.swap(lk2); + assert(lk1.mutex() == nullptr); + assert(lk1.owns_lock() == false); + assert(lk2.mutex() == &m); + assert(lk2.owns_lock() == true); + static_assert(noexcept(lk1.swap(lk2)), "member swap must be noexcept"); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/nonmember_swap.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/nonmember_swap.pass.cpp new file mode 100644 index 000000000..03bf5e89b --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/nonmember_swap.pass.cpp @@ -0,0 +1,44 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// template +// void swap(shared_lock& x, shared_lock& y) noexcept; + +#include +#include + +#if _LIBCPP_STD_VER > 11 + +struct mutex +{ + void lock_shared() {} + void unlock_shared() {} +}; + +mutex m; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_lock lk1(m); + std::shared_lock lk2; + swap(lk1, lk2); + assert(lk1.mutex() == nullptr); + assert(lk1.owns_lock() == false); + assert(lk2.mutex() == &m); + assert(lk2.owns_lock() == true); + static_assert(noexcept(swap(lk1, lk2)), "non-member swap must be noexcept"); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/release.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/release.pass.cpp new file mode 100644 index 000000000..a836b35df --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.mod/release.pass.cpp @@ -0,0 +1,51 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// mutex_type* release() noexcept; + +#include +#include + +#if _LIBCPP_STD_VER > 11 + +struct mutex +{ + static int lock_count; + static int unlock_count; + void lock_shared() {++lock_count;} + void unlock_shared() {++unlock_count;} +}; + +int mutex::lock_count = 0; +int mutex::unlock_count = 0; + +mutex m; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_lock lk(m); + assert(lk.mutex() == &m); + assert(lk.owns_lock() == true); + assert(mutex::lock_count == 1); + assert(mutex::unlock_count == 0); + assert(lk.release() == &m); + assert(lk.mutex() == nullptr); + assert(lk.owns_lock() == false); + assert(mutex::lock_count == 1); + assert(mutex::unlock_count == 0); + static_assert(noexcept(lk.release()), "release must be noexcept"); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.obs/mutex.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.obs/mutex.pass.cpp new file mode 100644 index 000000000..927e9d1d4 --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.obs/mutex.pass.cpp @@ -0,0 +1,36 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// mutex_type *mutex() const noexcept; + +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_lock lk0; + assert(lk0.mutex() == nullptr); + std::shared_lock lk1(m); + assert(lk1.mutex() == &m); + lk1.unlock(); + assert(lk1.mutex() == &m); + static_assert(noexcept(lk0.mutex()), "mutex() must be noexcept"); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.obs/op_bool.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.obs/op_bool.pass.cpp new file mode 100644 index 000000000..901ccbc15 --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.obs/op_bool.pass.cpp @@ -0,0 +1,36 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// explicit operator bool() const noexcept; + +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_lock lk0; + assert(static_cast(lk0) == false); + std::shared_lock lk1(m); + assert(static_cast(lk1) == true); + lk1.unlock(); + assert(static_cast(lk1) == false); + static_assert(noexcept(static_cast(lk0)), "explicit operator bool() must be noexcept"); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.obs/owns_lock.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.obs/owns_lock.pass.cpp new file mode 100644 index 000000000..ca4715b67 --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.obs/owns_lock.pass.cpp @@ -0,0 +1,36 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class shared_lock; + +// bool owns_lock() const noexcept; + +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_lock lk0; + assert(lk0.owns_lock() == false); + std::shared_lock lk1(m); + assert(lk1.owns_lock() == true); + lk1.unlock(); + assert(lk1.owns_lock() == false); + static_assert(noexcept(lk0.owns_lock()), "owns_lock must be noexcept"); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.shared/types.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.shared/types.pass.cpp new file mode 100644 index 000000000..353735739 --- /dev/null +++ b/test/thread/thread.mutex/thread.lock/thread.lock.shared/types.pass.cpp @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template +// class shared_lock +// { +// public: +// typedef Mutex mutex_type; +// ... +// }; + +#include +#include + +int main() +{ +#if _LIBCPP_STD_VER > 11 + static_assert((std::is_same::mutex_type, + std::mutex>::value), ""); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/release.pass.cpp b/test/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/release.pass.cpp index 0efe7d64d..4252de151 100644 --- a/test/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/release.pass.cpp +++ b/test/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/release.pass.cpp @@ -11,7 +11,7 @@ // template class unique_lock; -// void swap(unique_lock& u); +// mutex_type* release() noexcept; #include #include diff --git a/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/nothing_to_do.pass.cpp b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/nothing_to_do.pass.cpp new file mode 100644 index 000000000..b58f5c55b --- /dev/null +++ b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/nothing_to_do.pass.cpp @@ -0,0 +1,12 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +int main() +{ +} diff --git a/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/assign.fail.cpp b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/assign.fail.cpp new file mode 100644 index 000000000..1405a22c5 --- /dev/null +++ b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/assign.fail.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_mutex; + +// shared_mutex& operator=(const shared_mutex&) = delete; + +#include + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_mutex m0; + std::shared_mutex m1; + m1 = m0; +#else +# error +#endif +} diff --git a/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/copy.fail.cpp b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/copy.fail.cpp new file mode 100644 index 000000000..7e699232a --- /dev/null +++ b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/copy.fail.cpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_mutex; + +// shared_mutex(const shared_mutex&) = delete; + +#include + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_mutex m0; + std::shared_mutex m1(m0); +#else +# error +#endif +} diff --git a/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/default.pass.cpp b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/default.pass.cpp new file mode 100644 index 000000000..060fb9243 --- /dev/null +++ b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/default.pass.cpp @@ -0,0 +1,23 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_mutex; + +// shared_mutex(); + +#include + +int main() +{ +#if _LIBCPP_STD_VER > 11 + std::shared_mutex m; +#endif +} diff --git a/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/lock.pass.cpp b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/lock.pass.cpp new file mode 100644 index 000000000..2fd780081 --- /dev/null +++ b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/lock.pass.cpp @@ -0,0 +1,52 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_mutex; + +// void lock(); + +#include +#include +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +typedef std::chrono::system_clock Clock; +typedef Clock::time_point time_point; +typedef Clock::duration duration; +typedef std::chrono::milliseconds ms; +typedef std::chrono::nanoseconds ns; + +void f() +{ + time_point t0 = Clock::now(); + m.lock(); + time_point t1 = Clock::now(); + m.unlock(); + ns d = t1 - t0 - ms(250); + assert(d < ms(50)); // within 50ms +} + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + m.lock(); + std::thread t(f); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + t.join(); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/lock_shared.pass.cpp b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/lock_shared.pass.cpp new file mode 100644 index 000000000..6fe1b4469 --- /dev/null +++ b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/lock_shared.pass.cpp @@ -0,0 +1,75 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_mutex; + +// void lock_shared(); + +#include +#include +#include +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +typedef std::chrono::system_clock Clock; +typedef Clock::time_point time_point; +typedef Clock::duration duration; +typedef std::chrono::milliseconds ms; +typedef std::chrono::nanoseconds ns; + +void f() +{ + time_point t0 = Clock::now(); + m.lock_shared(); + time_point t1 = Clock::now(); + m.unlock_shared(); + ns d = t1 - t0 - ms(250); + assert(d < ms(50)); // within 50ms +} + +void g() +{ + time_point t0 = Clock::now(); + m.lock_shared(); + time_point t1 = Clock::now(); + m.unlock_shared(); + ns d = t1 - t0; + assert(d < ms(50)); // within 50ms +} + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + m.lock(); + std::vector v; + for (int i = 0; i < 5; ++i) + v.push_back(std::thread(f)); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + for (auto& t : v) + t.join(); + m.lock_shared(); + for (auto& t : v) + t = std::thread(g); + std::thread q(f); + std::this_thread::sleep_for(ms(250)); + m.unlock_shared(); + for (auto& t : v) + t.join(); + q.join(); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock.pass.cpp b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock.pass.cpp new file mode 100644 index 000000000..86aa0c4e5 --- /dev/null +++ b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock.pass.cpp @@ -0,0 +1,56 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_mutex; + +// bool try_lock(); + +#include +#include +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +typedef std::chrono::system_clock Clock; +typedef Clock::time_point time_point; +typedef Clock::duration duration; +typedef std::chrono::milliseconds ms; +typedef std::chrono::nanoseconds ns; + +void f() +{ + time_point t0 = Clock::now(); + assert(!m.try_lock()); + assert(!m.try_lock()); + assert(!m.try_lock()); + while(!m.try_lock()) + ; + time_point t1 = Clock::now(); + m.unlock(); + ns d = t1 - t0 - ms(250); + assert(d < ms(200)); // within 200ms +} + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + m.lock(); + std::thread t(f); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + t.join(); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock_for.pass.cpp b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock_for.pass.cpp new file mode 100644 index 000000000..156035996 --- /dev/null +++ b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock_for.pass.cpp @@ -0,0 +1,71 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_mutex; + +// template +// bool try_lock_for(const chrono::duration& rel_time); + +#include +#include +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +typedef std::chrono::steady_clock Clock; +typedef Clock::time_point time_point; +typedef Clock::duration duration; +typedef std::chrono::milliseconds ms; +typedef std::chrono::nanoseconds ns; + +void f1() +{ + time_point t0 = Clock::now(); + assert(m.try_lock_for(ms(300)) == true); + time_point t1 = Clock::now(); + m.unlock(); + ns d = t1 - t0 - ms(250); + assert(d < ms(50)); // within 50ms +} + +void f2() +{ + time_point t0 = Clock::now(); + assert(m.try_lock_for(ms(250)) == false); + time_point t1 = Clock::now(); + ns d = t1 - t0 - ms(250); + assert(d < ms(50)); // within 50ms +} + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + { + m.lock(); + std::thread t(f1); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + t.join(); + } + { + m.lock(); + std::thread t(f2); + std::this_thread::sleep_for(ms(300)); + m.unlock(); + t.join(); + } +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock_shared.pass.cpp b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock_shared.pass.cpp new file mode 100644 index 000000000..da859ab99 --- /dev/null +++ b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock_shared.pass.cpp @@ -0,0 +1,60 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_mutex; + +// bool try_lock_shared(); + +#include +#include +#include +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +typedef std::chrono::system_clock Clock; +typedef Clock::time_point time_point; +typedef Clock::duration duration; +typedef std::chrono::milliseconds ms; +typedef std::chrono::nanoseconds ns; + +void f() +{ + time_point t0 = Clock::now(); + assert(!m.try_lock_shared()); + assert(!m.try_lock_shared()); + assert(!m.try_lock_shared()); + while(!m.try_lock_shared()) + ; + time_point t1 = Clock::now(); + m.unlock_shared(); + ns d = t1 - t0 - ms(250); + assert(d < ms(200)); // within 200ms +} + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + m.lock(); + std::vector v; + for (int i = 0; i < 5; ++i) + v.push_back(std::thread(f)); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + for (auto& t : v) + t.join(); +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock_shared_for.pass.cpp b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock_shared_for.pass.cpp new file mode 100644 index 000000000..b1d6e4c6a --- /dev/null +++ b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock_shared_for.pass.cpp @@ -0,0 +1,78 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_mutex; + +// template +// bool try_lock_shared_for(const chrono::duration& rel_time); + +#include +#include +#include +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +typedef std::chrono::steady_clock Clock; +typedef Clock::time_point time_point; +typedef Clock::duration duration; +typedef std::chrono::milliseconds ms; +typedef std::chrono::nanoseconds ns; + +void f1() +{ + time_point t0 = Clock::now(); + assert(m.try_lock_shared_for(ms(300)) == true); + time_point t1 = Clock::now(); + m.unlock_shared(); + ns d = t1 - t0 - ms(250); + assert(d < ms(50)); // within 50ms +} + +void f2() +{ + time_point t0 = Clock::now(); + assert(m.try_lock_shared_for(ms(250)) == false); + time_point t1 = Clock::now(); + ns d = t1 - t0 - ms(250); + assert(d < ms(50)); // within 50ms +} + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + { + m.lock(); + std::vector v; + for (int i = 0; i < 5; ++i) + v.push_back(std::thread(f1)); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + for (auto& t : v) + t.join(); + } + { + m.lock(); + std::vector v; + for (int i = 0; i < 5; ++i) + v.push_back(std::thread(f2)); + std::this_thread::sleep_for(ms(300)); + m.unlock(); + for (auto& t : v) + t.join(); + } +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock_shared_until.pass.cpp b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock_shared_until.pass.cpp new file mode 100644 index 000000000..3dbf0c7bd --- /dev/null +++ b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock_shared_until.pass.cpp @@ -0,0 +1,78 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_mutex; + +// template +// bool try_lock_shared_until(const chrono::time_point& abs_time); + +#include +#include +#include +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +typedef std::chrono::steady_clock Clock; +typedef Clock::time_point time_point; +typedef Clock::duration duration; +typedef std::chrono::milliseconds ms; +typedef std::chrono::nanoseconds ns; + +void f1() +{ + time_point t0 = Clock::now(); + assert(m.try_lock_shared_until(Clock::now() + ms(300)) == true); + time_point t1 = Clock::now(); + m.unlock_shared(); + ns d = t1 - t0 - ms(250); + assert(d < ms(50)); // within 50ms +} + +void f2() +{ + time_point t0 = Clock::now(); + assert(m.try_lock_shared_until(Clock::now() + ms(250)) == false); + time_point t1 = Clock::now(); + ns d = t1 - t0 - ms(250); + assert(d < ms(50)); // within 50ms +} + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + { + m.lock(); + std::vector v; + for (int i = 0; i < 5; ++i) + v.push_back(std::thread(f1)); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + for (auto& t : v) + t.join(); + } + { + m.lock(); + std::vector v; + for (int i = 0; i < 5; ++i) + v.push_back(std::thread(f2)); + std::this_thread::sleep_for(ms(300)); + m.unlock(); + for (auto& t : v) + t.join(); + } +#endif // _LIBCPP_STD_VER > 11 +} diff --git a/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock_until.pass.cpp b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock_until.pass.cpp new file mode 100644 index 000000000..44f91f0af --- /dev/null +++ b/test/thread/thread.mutex/thread.mutex.requirements/thread.sharedmutex.requirements/thread.sharedmutex.class/try_lock_until.pass.cpp @@ -0,0 +1,71 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// class shared_mutex; + +// template +// bool try_lock_until(const chrono::time_point& abs_time); + +#include +#include +#include +#include + +#if _LIBCPP_STD_VER > 11 + +std::shared_mutex m; + +typedef std::chrono::steady_clock Clock; +typedef Clock::time_point time_point; +typedef Clock::duration duration; +typedef std::chrono::milliseconds ms; +typedef std::chrono::nanoseconds ns; + +void f1() +{ + time_point t0 = Clock::now(); + assert(m.try_lock_until(Clock::now() + ms(300)) == true); + time_point t1 = Clock::now(); + m.unlock(); + ns d = t1 - t0 - ms(250); + assert(d < ms(50)); // within 50ms +} + +void f2() +{ + time_point t0 = Clock::now(); + assert(m.try_lock_until(Clock::now() + ms(250)) == false); + time_point t1 = Clock::now(); + ns d = t1 - t0 - ms(250); + assert(d < ms(50)); // within 50ms +} + +#endif // _LIBCPP_STD_VER > 11 + +int main() +{ +#if _LIBCPP_STD_VER > 11 + { + m.lock(); + std::thread t(f1); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + t.join(); + } + { + m.lock(); + std::thread t(f2); + std::this_thread::sleep_for(ms(300)); + m.unlock(); + t.join(); + } +#endif // _LIBCPP_STD_VER > 11 +}