diff options
Diffstat (limited to 'libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique')
23 files changed, 1031 insertions, 0 deletions
diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_assign.fail.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_assign.fail.cpp new file mode 100644 index 00000000000..4f477449d6a --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_assign.fail.cpp @@ -0,0 +1,31 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// <mutex> + +// template <class Mutex> class unique_lock; + +// unique_lock& operator=(unique_lock const&) = delete; + +#include <mutex> +#include <cassert> + +std::mutex m0; +std::mutex m1; + +int main() +{ + std::unique_lock<std::mutex> lk0(m0); + std::unique_lock<std::mutex> lk1(m1); + lk1 = lk0; + assert(lk1.mutex() == &m0); + assert(lk1.owns_lock() == true); + assert(lk0.mutex() == nullptr); + assert(lk0.owns_lock() == false); +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_ctor.fail.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_ctor.fail.cpp new file mode 100644 index 00000000000..4888fe90d92 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_ctor.fail.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. +// +//===----------------------------------------------------------------------===// + +// <mutex> + +// template <class Mutex> class unique_lock; + +// unique_lock(unique_lock const&) = delete; + +#include <mutex> +#include <cassert> + +std::mutex m; + +int main() +{ + std::unique_lock<std::mutex> lk0(m); + std::unique_lock<std::mutex> lk = lk0; + assert(lk.mutex() == &m); + assert(lk.owns_lock() == true); + assert(lk0.mutex() == nullptr); + assert(lk0.owns_lock() == false); +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/default.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/default.pass.cpp new file mode 100644 index 00000000000..a49bc507171 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// unique_lock(); + +#include <mutex> +#include <cassert> + +int main() +{ + std::unique_lock<std::mutex> ul; + assert(!ul.owns_lock()); + assert(ul.mutex() == nullptr); +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_assign.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_assign.pass.cpp new file mode 100644 index 00000000000..4dff853088a --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_assign.pass.cpp @@ -0,0 +1,35 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// unique_lock& operator=(unique_lock&& u); + +#include <mutex> +#include <cassert> + +std::mutex m0; +std::mutex m1; + +int main() +{ +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + std::unique_lock<std::mutex> lk0(m0); + std::unique_lock<std::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_HAS_NO_RVALUE_REFERENCES +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_ctor.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_ctor.pass.cpp new file mode 100644 index 00000000000..aa640ee6d74 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// unique_lock(unique_lock&& u); + +#include <mutex> +#include <cassert> + +std::mutex m; + +int main() +{ +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + std::unique_lock<std::mutex> lk0(m); + std::unique_lock<std::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_HAS_NO_RVALUE_REFERENCES +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex.pass.cpp new file mode 100644 index 00000000000..1f7217a8300 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex.pass.cpp @@ -0,0 +1,50 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// explicit unique_lock(mutex_type& m); + +#include <mutex> +#include <thread> +#include <cstdlib> +#include <cassert> + +std::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::unique_lock<std::mutex> ul(m); + t1 = Clock::now(); + } + ns d = t1 - t0 - ms(250); + assert(d < ms(50)); // within 50ms +} + +int main() +{ + m.lock(); + std::thread t(f); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + t.join(); +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_adopt_lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_adopt_lock.pass.cpp new file mode 100644 index 00000000000..9c3a7b6505a --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// unique_lock(mutex_type& m, adopt_lock_t); + +#include <mutex> +#include <cassert> + +int main() +{ + std::mutex m; + m.lock(); + std::unique_lock<std::mutex> lk(m, std::adopt_lock); + assert(lk.mutex() == &m); + assert(lk.owns_lock() == true); +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_defer_lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_defer_lock.pass.cpp new file mode 100644 index 00000000000..bf622311f01 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// unique_lock(mutex_type& m, defer_lock_t); + +#include <mutex> +#include <cassert> + +int main() +{ + std::mutex m; + std::unique_lock<std::mutex> lk(m, std::defer_lock); + assert(lk.mutex() == &m); + assert(lk.owns_lock() == false); +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_duration.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_duration.pass.cpp new file mode 100644 index 00000000000..33e400b0da8 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_duration.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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// class timed_mutex; + +// template <class Rep, class Period> +// unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time); + +#include <mutex> +#include <thread> +#include <cstdlib> +#include <cassert> + +std::timed_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::unique_lock<std::timed_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::unique_lock<std::timed_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 +} + +int main() +{ + { + 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(); + } +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_time_point.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_time_point.pass.cpp new file mode 100644 index 00000000000..2ead6709773 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_time_point.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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// class timed_mutex; + +// template <class Clock, class Duration> +// unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time); + +#include <mutex> +#include <thread> +#include <cstdlib> +#include <cassert> + +std::timed_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::unique_lock<std::timed_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::unique_lock<std::timed_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 +} + +int main() +{ + { + 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(); + } +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_try_to_lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_try_to_lock.pass.cpp new file mode 100644 index 00000000000..cea58c554a9 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_try_to_lock.pass.cpp @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// unique_lock(mutex_type& m, try_to_lock_t); + +#include <mutex> +#include <thread> +#include <cstdlib> +#include <cassert> + +std::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::unique_lock<std::mutex> lk(m, std::try_to_lock); + assert(lk.owns_lock() == false); + } + { + std::unique_lock<std::mutex> lk(m, std::try_to_lock); + assert(lk.owns_lock() == false); + } + { + std::unique_lock<std::mutex> lk(m, std::try_to_lock); + assert(lk.owns_lock() == false); + } + while (true) + { + std::unique_lock<std::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 +} + +int main() +{ + m.lock(); + std::thread t(f); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + t.join(); +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/lock.pass.cpp new file mode 100644 index 00000000000..f5408df9819 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/lock.pass.cpp @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// void lock(); + +#include <mutex> +#include <thread> +#include <cstdlib> +#include <cassert> + +std::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::unique_lock<std::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); + } +} + +int main() +{ + m.lock(); + std::thread t(f); + std::this_thread::sleep_for(ms(250)); + m.unlock(); + t.join(); +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock.pass.cpp new file mode 100644 index 00000000000..bd88577f0a2 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock.pass.cpp @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// bool try_lock(); + +#include <mutex> +#include <cassert> + +bool try_lock_called = false; + +struct mutex +{ + bool try_lock() + { + try_lock_called = !try_lock_called; + return try_lock_called; + } + void unlock() {} +}; + +mutex m; + +int main() +{ + std::unique_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); + } +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_for.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_for.pass.cpp new file mode 100644 index 00000000000..558f079463a --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_for.pass.cpp @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// template <class Rep, class Period> +// bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); + +#include <mutex> +#include <cassert> + +bool try_lock_for_called = false; + +typedef std::chrono::milliseconds ms; + +struct mutex +{ + template <class Rep, class Period> + bool try_lock_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() {} +}; + +mutex m; + +int main() +{ + std::unique_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); + } +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_until.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_until.pass.cpp new file mode 100644 index 00000000000..24e4d109a09 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/try_lock_until.pass.cpp @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// template <class Clock, class Duration> +// bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); + +#include <mutex> +#include <cassert> + +bool try_lock_until_called = false; + +struct mutex +{ + template <class Clock, class Duration> + bool try_lock_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() {} +}; + +mutex m; + +int main() +{ + typedef std::chrono::steady_clock Clock; + std::unique_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); + } +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/unlock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/unlock.pass.cpp new file mode 100644 index 00000000000..bbabfc41df1 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/unlock.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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// void unlock(); + +#include <mutex> +#include <cassert> + +bool unlock_called = false; + +struct mutex +{ + void lock() {} + void unlock() {unlock_called = true;} +}; + +mutex m; + +int main() +{ + std::unique_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); + } +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/member_swap.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/member_swap.pass.cpp new file mode 100644 index 00000000000..598d53a65c8 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/member_swap.pass.cpp @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// void swap(unique_lock& u); + +#include <mutex> +#include <cassert> + +struct mutex +{ + void lock() {} + void unlock() {} +}; + +mutex m; + +int main() +{ + std::unique_lock<mutex> lk1(m); + std::unique_lock<mutex> lk2; + lk1.swap(lk2); + assert(lk1.mutex() == nullptr); + assert(lk1.owns_lock() == false); + assert(lk2.mutex() == &m); + assert(lk2.owns_lock() == true); +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/nonmember_swap.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/nonmember_swap.pass.cpp new file mode 100644 index 00000000000..3fc8c28f507 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/nonmember_swap.pass.cpp @@ -0,0 +1,39 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// template <class Mutex> +// void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y); + +#include <mutex> +#include <cassert> + +struct mutex +{ + void lock() {} + void unlock() {} +}; + +mutex m; + +int main() +{ + std::unique_lock<mutex> lk1(m); + std::unique_lock<mutex> lk2; + swap(lk1, lk2); + assert(lk1.mutex() == nullptr); + assert(lk1.owns_lock() == false); + assert(lk2.mutex() == &m); + assert(lk2.owns_lock() == true); +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/release.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/release.pass.cpp new file mode 100644 index 00000000000..89c28e6be73 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.mod/release.pass.cpp @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// mutex_type* release() noexcept; + +#include <mutex> +#include <cassert> + +struct mutex +{ + static int lock_count; + static int unlock_count; + void lock() {++lock_count;} + void unlock() {++unlock_count;} +}; + +int mutex::lock_count = 0; +int mutex::unlock_count = 0; + +mutex m; + +int main() +{ + std::unique_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); +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.obs/mutex.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.obs/mutex.pass.cpp new file mode 100644 index 00000000000..bc1e3e5eb77 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.obs/mutex.pass.cpp @@ -0,0 +1,31 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// mutex_type *mutex() const; + +#include <mutex> +#include <cassert> + +std::mutex m; + +int main() +{ + std::unique_lock<std::mutex> lk0; + assert(lk0.mutex() == nullptr); + std::unique_lock<std::mutex> lk1(m); + assert(lk1.mutex() == &m); + lk1.unlock(); + assert(lk1.mutex() == &m); +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.obs/op_bool.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.obs/op_bool.pass.cpp new file mode 100644 index 00000000000..7004ac0092e --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.obs/op_bool.pass.cpp @@ -0,0 +1,31 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// explicit operator bool() const; + +#include <mutex> +#include <cassert> + +std::mutex m; + +int main() +{ + std::unique_lock<std::mutex> lk0; + assert(static_cast<bool>(lk0) == false); + std::unique_lock<std::mutex> lk1(m); + assert(static_cast<bool>(lk1) == true); + lk1.unlock(); + assert(static_cast<bool>(lk1) == false); +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.obs/owns_lock.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.obs/owns_lock.pass.cpp new file mode 100644 index 00000000000..f53af35ff28 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.obs/owns_lock.pass.cpp @@ -0,0 +1,31 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> class unique_lock; + +// bool owns_lock() const; + +#include <mutex> +#include <cassert> + +std::mutex m; + +int main() +{ + std::unique_lock<std::mutex> lk0; + assert(lk0.owns_lock() == false); + std::unique_lock<std::mutex> lk1(m); + assert(lk1.owns_lock() == true); + lk1.unlock(); + assert(lk1.owns_lock() == false); +} diff --git a/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/types.pass.cpp b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/types.pass.cpp new file mode 100644 index 00000000000..f8bcb6d0d22 --- /dev/null +++ b/libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/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. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads + +// <mutex> + +// template <class Mutex> +// class unique_lock +// { +// public: +// typedef Mutex mutex_type; +// ... +// }; + +#include <mutex> +#include <type_traits> + +int main() +{ + static_assert((std::is_same<std::unique_lock<std::mutex>::mutex_type, + std::mutex>::value), ""); +} |