Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 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 Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 9 | // |
| 10 | // UNSUPPORTED: libcpp-has-no-threads |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 11 | |
| 12 | // <shared_mutex> |
| 13 | |
David Majnemer | f9f95be | 2014-03-17 20:19:44 +0000 | [diff] [blame] | 14 | // class shared_timed_mutex; |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 15 | |
| 16 | // template <class Clock, class Duration> |
| 17 | // shared_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time); |
| 18 | |
| 19 | #include <shared_mutex> |
| 20 | #include <thread> |
| 21 | #include <vector> |
| 22 | #include <cstdlib> |
| 23 | #include <cassert> |
| 24 | |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 25 | #if _LIBCPP_STD_VER > 11 |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 26 | |
David Majnemer | f9f95be | 2014-03-17 20:19:44 +0000 | [diff] [blame] | 27 | std::shared_timed_mutex m; |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 28 | |
| 29 | typedef std::chrono::steady_clock Clock; |
| 30 | typedef Clock::time_point time_point; |
| 31 | typedef Clock::duration duration; |
| 32 | typedef std::chrono::milliseconds ms; |
| 33 | typedef std::chrono::nanoseconds ns; |
| 34 | |
| 35 | void f1() |
| 36 | { |
| 37 | time_point t0 = Clock::now(); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 38 | std::shared_lock<std::shared_timed_mutex> lk(m, Clock::now() + ms(300)); |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 39 | assert(lk.owns_lock() == true); |
| 40 | time_point t1 = Clock::now(); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 41 | ns d = t1 - t0 - ms(250); |
| 42 | assert(d < ns(50000000)); // within 50ms |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void f2() |
| 46 | { |
| 47 | time_point t0 = Clock::now(); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 48 | std::shared_lock<std::shared_timed_mutex> lk(m, Clock::now() + ms(250)); |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 49 | assert(lk.owns_lock() == false); |
| 50 | time_point t1 = Clock::now(); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 51 | ns d = t1 - t0 - ms(250); |
| 52 | assert(d < ms(50)); // within 50ms |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 55 | #endif // _LIBCPP_STD_VER > 11 |
| 56 | |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 57 | int main() |
| 58 | { |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 59 | #if _LIBCPP_STD_VER > 11 |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 60 | { |
| 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 Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 65 | std::this_thread::sleep_for(ms(250)); |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 66 | 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 Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 75 | std::this_thread::sleep_for(ms(300)); |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 76 | m.unlock(); |
| 77 | for (auto& t : v) |
| 78 | t.join(); |
| 79 | } |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 80 | #endif // _LIBCPP_STD_VER > 11 |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 81 | } |