blob: 24e4d109a09735e358b99288b3153b58a9d4a771 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00009//
10// UNSUPPORTED: libcpp-has-no-threads
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000011
12// <mutex>
13
14// template <class Mutex> class unique_lock;
15
16// template <class Clock, class Duration>
17// bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
18
19#include <mutex>
20#include <cassert>
21
22bool try_lock_until_called = false;
23
24struct mutex
25{
26 template <class Clock, class Duration>
27 bool try_lock_until(const std::chrono::time_point<Clock, Duration>& abs_time)
28 {
29 typedef std::chrono::milliseconds ms;
30 assert(Clock::now() - abs_time < ms(5));
31 try_lock_until_called = !try_lock_until_called;
32 return try_lock_until_called;
33 }
34 void unlock() {}
35};
36
37mutex m;
38
39int main()
40{
Howard Hinnantf8f85212010-11-20 19:16:30 +000041 typedef std::chrono::steady_clock Clock;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042 std::unique_lock<mutex> lk(m, std::defer_lock);
43 assert(lk.try_lock_until(Clock::now()) == true);
44 assert(try_lock_until_called == true);
45 assert(lk.owns_lock() == true);
46 try
47 {
48 lk.try_lock_until(Clock::now());
49 assert(false);
50 }
51 catch (std::system_error& e)
52 {
53 assert(e.code().value() == EDEADLK);
54 }
55 lk.unlock();
56 assert(lk.try_lock_until(Clock::now()) == false);
57 assert(try_lock_until_called == false);
58 assert(lk.owns_lock() == false);
59 lk.release();
60 try
61 {
62 lk.try_lock_until(Clock::now());
63 assert(false);
64 }
65 catch (std::system_error& e)
66 {
67 assert(e.code().value() == EPERM);
68 }
69}