N3659: Shared locking in C++ Revision 2, c++1y only

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@191127 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Howard Hinnant 2013-09-21 01:49:28 +00:00
parent 8127758024
commit ba898e4208
39 changed files with 2300 additions and 32 deletions

View File

@ -20,16 +20,6 @@
#pragma GCC system_header
#endif
#ifdef _LIBCPP_SHARED_LOCK
namespace ting {
template <class _Mutex> class shared_lock;
template <class _Mutex> 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<mutex_type>&&, try_to_lock_t);
template <class _Clock, class _Duration>
unique_lock(ting::shared_lock<mutex_type>&&,
const chrono::time_point<_Clock, _Duration>&);
template <class _Rep, class _Period>
unique_lock(ting::shared_lock<mutex_type>&&,
const chrono::duration<_Rep, _Period>&);
explicit unique_lock(ting::upgrade_lock<mutex_type>&&);
unique_lock(ting::upgrade_lock<mutex_type>&&, try_to_lock_t);
template <class _Clock, class _Duration>
unique_lock(ting::upgrade_lock<mutex_type>&&,
const chrono::time_point<_Clock, _Duration>&);
template <class _Rep, class _Period>
unique_lock(ting::upgrade_lock<mutex_type>&&,
const chrono::duration<_Rep, _Period>&);
#endif // _LIBCPP_SHARED_LOCK
#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
void lock();

419
include/shared_mutex Normal file
View File

@ -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 <class Rep, class Period>
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
template <class Clock, class Duration>
bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
void unlock();
// Shared ownership
void lock_shared(); // blocking
bool try_lock_shared();
template <class Rep, class Period>
bool
try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time);
template <class Clock, class Duration>
bool
try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time);
void unlock_shared();
};
template <class Mutex>
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 <class Clock, class Duration>
shared_lock(mutex_type& m,
const chrono::time_point<Clock, Duration>& abs_time);
template <class Rep, class Period>
shared_lock(mutex_type& m,
const chrono::duration<Rep, Period>& 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 <class Rep, class Period>
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
template <class Clock, class Duration>
bool try_lock_until(const chrono::time_point<Clock, Duration>& 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 <class Mutex>
void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& 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 <class _Rep, class _Period>
_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 <class _Clock, class _Duration>
bool
try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time);
void unlock();
// Shared ownership
void lock_shared();
bool try_lock_shared();
template <class _Rep, class _Period>
_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 <class _Clock, class _Duration>
bool
try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& __abs_time);
void unlock_shared();
};
template <class _Clock, class _Duration>
bool
shared_mutex::try_lock_until(
const chrono::time_point<_Clock, _Duration>& __abs_time)
{
unique_lock<mutex> __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 <class _Clock, class _Duration>
bool
shared_mutex::try_lock_shared_until(
const chrono::time_point<_Clock, _Duration>& __abs_time)
{
unique_lock<mutex> __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 _Mutex>
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 <class _Clock, class _Duration>
_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 <class _Rep, class _Period>
_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 <class Rep, class Period>
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
template <class Clock, class Duration>
bool try_lock_until(const chrono::time_point<Clock, Duration>& 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 <class _Mutex>
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 <class _Mutex>
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 <class _Mutex>
template <class _Rep, class _Period>
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 <class _Mutex>
template <class _Clock, class _Duration>
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 <class _Mutex>
void
shared_lock<_Mutex>::unlock()
{
if (!__owns_)
__throw_system_error(EPERM, "shared_lock::unlock: not locked");
__m_->unlock_shared();
__owns_ = false;
}
template <class _Mutex>
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

101
src/shared_mutex.cpp Normal file
View File

@ -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<mutex> 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<mutex> lk(__mut_);
if (__state_ == 0)
{
__state_ = __write_entered_;
return true;
}
return false;
}
void
shared_mutex::unlock()
{
lock_guard<mutex> _(__mut_);
__state_ = 0;
__gate1_.notify_all();
}
// Shared ownership
void
shared_mutex::lock_shared()
{
unique_lock<mutex> 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<mutex> 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<mutex> _(__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

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// shared_lock& operator=(shared_lock const&) = delete;
#include <shared_mutex>
#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<std::shared_mutex> lk0(m0);
std::shared_lock<std::shared_mutex> lk1(m1);
lk1 = lk0;
#else
# error
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// shared_lock(shared_lock const&) = delete;
#include <shared_mutex>
#if _LIBCPP_STD_VER > 11
std::shared_mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<std::shared_mutex> lk0(m);
std::shared_lock<std::shared_mutex> lk = lk0;
#else
# error
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// shared_lock();
#include <shared_mutex>
#include <cassert>
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<std::shared_mutex> ul;
assert(!ul.owns_lock());
assert(ul.mutex() == nullptr);
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// shared_lock& operator=(shared_lock&& u);
#include <shared_mutex>
#include <cassert>
#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<std::shared_mutex> lk0(m0);
std::shared_lock<std::shared_mutex> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// shared_lock(shared_lock&& u);
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
std::shared_mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<std::shared_mutex> lk0(m);
std::shared_lock<std::shared_mutex> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// explicit shared_lock(mutex_type& m);
#include <shared_mutex>
#include <thread>
#include <vector>
#include <cstdlib>
#include <cassert>
#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<std::shared_mutex> 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<std::shared_mutex> 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<std::thread> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// shared_lock(mutex_type& m, adopt_lock_t);
#include <shared_mutex>
#include <cassert>
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_mutex m;
m.lock();
std::shared_lock<std::shared_mutex> lk(m, std::adopt_lock);
assert(lk.mutex() == &m);
assert(lk.owns_lock() == true);
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// shared_lock(mutex_type& m, defer_lock_t);
#include <shared_mutex>
#include <cassert>
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_mutex m;
std::shared_lock<std::shared_mutex> lk(m, std::defer_lock);
assert(lk.mutex() == &m);
assert(lk.owns_lock() == false);
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// class timed_mutex;
// template <class Rep, class Period>
// shared_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
#include <shared_mutex>
#include <thread>
#include <vector>
#include <cstdlib>
#include <cassert>
#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<std::shared_mutex> 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<std::shared_mutex> 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<std::thread> 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<std::thread> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// class shared_mutex;
// template <class Clock, class Duration>
// shared_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
#include <shared_mutex>
#include <thread>
#include <vector>
#include <cstdlib>
#include <cassert>
#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<std::shared_mutex> 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<std::shared_mutex> 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<std::thread> 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<std::thread> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// shared_lock(mutex_type& m, try_to_lock_t);
#include <shared_mutex>
#include <thread>
#include <vector>
#include <cstdlib>
#include <cassert>
#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<std::shared_mutex> lk(m, std::try_to_lock);
assert(lk.owns_lock() == false);
}
{
std::shared_lock<std::shared_mutex> lk(m, std::try_to_lock);
assert(lk.owns_lock() == false);
}
{
std::shared_lock<std::shared_mutex> lk(m, std::try_to_lock);
assert(lk.owns_lock() == false);
}
while (true)
{
std::shared_lock<std::shared_mutex> 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<std::thread> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// void lock();
#include <shared_mutex>
#include <thread>
#include <vector>
#include <cstdlib>
#include <cassert>
#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<std::shared_mutex> 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<std::thread> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// bool try_lock();
#include <shared_mutex>
#include <cassert>
#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<mutex> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// template <class Rep, class Period>
// bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
bool try_lock_for_called = false;
typedef std::chrono::milliseconds ms;
struct mutex
{
template <class Rep, class Period>
bool try_lock_shared_for(const std::chrono::duration<Rep, Period>& 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<mutex> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// template <class Clock, class Duration>
// bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
bool try_lock_until_called = false;
struct mutex
{
template <class Clock, class Duration>
bool try_lock_shared_until(const std::chrono::time_point<Clock, Duration>& 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<mutex> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// void unlock();
#include <shared_mutex>
#include <cassert>
#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<mutex> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// void swap(shared_lock& u) noexcept;
#include <shared_mutex>
#include <cassert>
#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<mutex> lk1(m);
std::shared_lock<mutex> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// template <class Mutex>
// void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;
#include <shared_mutex>
#include <cassert>
#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<mutex> lk1(m);
std::shared_lock<mutex> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// mutex_type* release() noexcept;
#include <shared_mutex>
#include <cassert>
#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<mutex> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// mutex_type *mutex() const noexcept;
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
std::shared_mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<std::shared_mutex> lk0;
assert(lk0.mutex() == nullptr);
std::shared_lock<std::shared_mutex> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// explicit operator bool() const noexcept;
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
std::shared_mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<std::shared_mutex> lk0;
assert(static_cast<bool>(lk0) == false);
std::shared_lock<std::shared_mutex> lk1(m);
assert(static_cast<bool>(lk1) == true);
lk1.unlock();
assert(static_cast<bool>(lk1) == false);
static_assert(noexcept(static_cast<bool>(lk0)), "explicit operator bool() must be noexcept");
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex> class shared_lock;
// bool owns_lock() const noexcept;
#include <shared_mutex>
#include <cassert>
#if _LIBCPP_STD_VER > 11
std::shared_mutex m;
#endif // _LIBCPP_STD_VER > 11
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_lock<std::shared_mutex> lk0;
assert(lk0.owns_lock() == false);
std::shared_lock<std::shared_mutex> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// template <class Mutex>
// class shared_lock
// {
// public:
// typedef Mutex mutex_type;
// ...
// };
#include <shared_mutex>
#include <type_traits>
int main()
{
#if _LIBCPP_STD_VER > 11
static_assert((std::is_same<std::shared_lock<std::mutex>::mutex_type,
std::mutex>::value), "");
#endif // _LIBCPP_STD_VER > 11
}

View File

@ -11,7 +11,7 @@
// template <class Mutex> class unique_lock;
// void swap(unique_lock& u);
// mutex_type* release() noexcept;
#include <mutex>
#include <cassert>

View File

@ -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()
{
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// class shared_mutex;
// shared_mutex& operator=(const shared_mutex&) = delete;
#include <shared_mutex>
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_mutex m0;
std::shared_mutex m1;
m1 = m0;
#else
# error
#endif
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// class shared_mutex;
// shared_mutex(const shared_mutex&) = delete;
#include <shared_mutex>
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_mutex m0;
std::shared_mutex m1(m0);
#else
# error
#endif
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// class shared_mutex;
// shared_mutex();
#include <shared_mutex>
int main()
{
#if _LIBCPP_STD_VER > 11
std::shared_mutex m;
#endif
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// class shared_mutex;
// void lock();
#include <shared_mutex>
#include <thread>
#include <cstdlib>
#include <cassert>
#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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// class shared_mutex;
// void lock_shared();
#include <shared_mutex>
#include <thread>
#include <vector>
#include <cstdlib>
#include <cassert>
#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<std::thread> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// class shared_mutex;
// bool try_lock();
#include <shared_mutex>
#include <thread>
#include <cstdlib>
#include <cassert>
#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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// class shared_mutex;
// template <class Rep, class Period>
// bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
#include <shared_mutex>
#include <thread>
#include <cstdlib>
#include <cassert>
#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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// class shared_mutex;
// bool try_lock_shared();
#include <shared_mutex>
#include <thread>
#include <vector>
#include <cstdlib>
#include <cassert>
#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<std::thread> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// class shared_mutex;
// template <class Rep, class Period>
// bool try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time);
#include <shared_mutex>
#include <thread>
#include <vector>
#include <cstdlib>
#include <cassert>
#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<std::thread> 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<std::thread> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// class shared_mutex;
// template <class Clock, class Duration>
// bool try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time);
#include <shared_mutex>
#include <thread>
#include <vector>
#include <cstdlib>
#include <cassert>
#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<std::thread> 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<std::thread> 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
}

View File

@ -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.
//
//===----------------------------------------------------------------------===//
// <shared_mutex>
// class shared_mutex;
// template <class Clock, class Duration>
// bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
#include <shared_mutex>
#include <thread>
#include <cstdlib>
#include <cassert>
#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
}