blob: 5867465a662679232dddf474148398f827ce7196 [file] [log] [blame]
Howard Hinnantba898e42013-09-21 01:49:28 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00009//
10// UNSUPPORTED: libcpp-has-no-threads
Howard Hinnantba898e42013-09-21 01:49:28 +000011
12// <shared_mutex>
13
14// template <class Mutex> class shared_lock;
15
16// template <class Rep, class Period>
17// bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
18
19#include <shared_mutex>
20#include <cassert>
21
Dan Albert1d4a1ed2016-05-25 22:36:09 -070022#if _LIBCPP_STD_VER > 11
23
Howard Hinnantba898e42013-09-21 01:49:28 +000024bool try_lock_for_called = false;
25
26typedef std::chrono::milliseconds ms;
27
28struct mutex
29{
30 template <class Rep, class Period>
31 bool try_lock_shared_for(const std::chrono::duration<Rep, Period>& rel_time)
32 {
33 assert(rel_time == ms(5));
34 try_lock_for_called = !try_lock_for_called;
35 return try_lock_for_called;
36 }
37 void unlock_shared() {}
38};
39
40mutex m;
41
Dan Albert1d4a1ed2016-05-25 22:36:09 -070042#endif // _LIBCPP_STD_VER > 11
43
Howard Hinnantba898e42013-09-21 01:49:28 +000044int main()
45{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070046#if _LIBCPP_STD_VER > 11
Howard Hinnantba898e42013-09-21 01:49:28 +000047 std::shared_lock<mutex> lk(m, std::defer_lock);
48 assert(lk.try_lock_for(ms(5)) == true);
49 assert(try_lock_for_called == true);
50 assert(lk.owns_lock() == true);
51 try
52 {
53 lk.try_lock_for(ms(5));
54 assert(false);
55 }
56 catch (std::system_error& e)
57 {
58 assert(e.code().value() == EDEADLK);
59 }
60 lk.unlock();
61 assert(lk.try_lock_for(ms(5)) == false);
62 assert(try_lock_for_called == false);
63 assert(lk.owns_lock() == false);
64 lk.release();
65 try
66 {
67 lk.try_lock_for(ms(5));
68 assert(false);
69 }
70 catch (std::system_error& e)
71 {
72 assert(e.code().value() == EPERM);
73 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -070074#endif // _LIBCPP_STD_VER > 11
Howard Hinnantba898e42013-09-21 01:49:28 +000075}