blob: 558f079463a4e842ae5143983408dfba27975dd7 [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 Rep, class Period>
17// bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
18
19#include <mutex>
20#include <cassert>
21
22bool try_lock_for_called = false;
23
24typedef std::chrono::milliseconds ms;
25
26struct mutex
27{
28 template <class Rep, class Period>
29 bool try_lock_for(const std::chrono::duration<Rep, Period>& rel_time)
30 {
31 assert(rel_time == ms(5));
32 try_lock_for_called = !try_lock_for_called;
33 return try_lock_for_called;
34 }
35 void unlock() {}
36};
37
38mutex m;
39
40int main()
41{
42 std::unique_lock<mutex> lk(m, std::defer_lock);
43 assert(lk.try_lock_for(ms(5)) == true);
44 assert(try_lock_for_called == true);
45 assert(lk.owns_lock() == true);
46 try
47 {
48 lk.try_lock_for(ms(5));
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_for(ms(5)) == false);
57 assert(try_lock_for_called == false);
58 assert(lk.owns_lock() == false);
59 lk.release();
60 try
61 {
62 lk.try_lock_for(ms(5));
63 assert(false);
64 }
65 catch (std::system_error& e)
66 {
67 assert(e.code().value() == EPERM);
68 }
69}