blob: 9816e57f692a26a45ffb99e90b3c367117f8014b [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// class timed_mutex;
15
16// template <class Rep, class Period>
17// shared_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
18
19#include <shared_mutex>
20#include <thread>
21#include <vector>
22#include <cstdlib>
23#include <cassert>
24
Dan Albert1d4a1ed2016-05-25 22:36:09 -070025#if _LIBCPP_STD_VER > 11
Howard Hinnantba898e42013-09-21 01:49:28 +000026
David Majnemerf9f95be2014-03-17 20:19:44 +000027std::shared_timed_mutex m;
Howard Hinnantba898e42013-09-21 01:49:28 +000028
29typedef std::chrono::steady_clock Clock;
30typedef Clock::time_point time_point;
31typedef Clock::duration duration;
32typedef std::chrono::milliseconds ms;
33typedef std::chrono::nanoseconds ns;
34
35void f1()
36{
37 time_point t0 = Clock::now();
Dan Albert1d4a1ed2016-05-25 22:36:09 -070038 std::shared_lock<std::shared_timed_mutex> lk(m, ms(300));
Howard Hinnantba898e42013-09-21 01:49:28 +000039 assert(lk.owns_lock() == true);
40 time_point t1 = Clock::now();
Dan Albert1d4a1ed2016-05-25 22:36:09 -070041 ns d = t1 - t0 - ms(250);
42 assert(d < ms(50)); // within 50ms
Howard Hinnantba898e42013-09-21 01:49:28 +000043}
44
45void f2()
46{
47 time_point t0 = Clock::now();
Dan Albert1d4a1ed2016-05-25 22:36:09 -070048 std::shared_lock<std::shared_timed_mutex> lk(m, ms(250));
Howard Hinnantba898e42013-09-21 01:49:28 +000049 assert(lk.owns_lock() == false);
50 time_point t1 = Clock::now();
Dan Albert1d4a1ed2016-05-25 22:36:09 -070051 ns d = t1 - t0 - ms(250);
52 assert(d < ms(50)); // within 50ms
Howard Hinnantba898e42013-09-21 01:49:28 +000053}
54
Dan Albert1d4a1ed2016-05-25 22:36:09 -070055#endif // _LIBCPP_STD_VER > 11
56
Howard Hinnantba898e42013-09-21 01:49:28 +000057int main()
58{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070059#if _LIBCPP_STD_VER > 11
Howard Hinnantba898e42013-09-21 01:49:28 +000060 {
61 m.lock();
62 std::vector<std::thread> v;
63 for (int i = 0; i < 5; ++i)
64 v.push_back(std::thread(f1));
Dan Albert1d4a1ed2016-05-25 22:36:09 -070065 std::this_thread::sleep_for(ms(250));
Howard Hinnantba898e42013-09-21 01:49:28 +000066 m.unlock();
67 for (auto& t : v)
68 t.join();
69 }
70 {
71 m.lock();
72 std::vector<std::thread> v;
73 for (int i = 0; i < 5; ++i)
74 v.push_back(std::thread(f2));
Dan Albert1d4a1ed2016-05-25 22:36:09 -070075 std::this_thread::sleep_for(ms(300));
Howard Hinnantba898e42013-09-21 01:49:28 +000076 m.unlock();
77 for (auto& t : v)
78 t.join();
79 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -070080#endif // _LIBCPP_STD_VER > 11
Howard Hinnantba898e42013-09-21 01:49:28 +000081}