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 |
Marshall Clow | f54ca46 | 2015-06-11 21:47:39 +0000 | [diff] [blame] | 11 | // UNSUPPORTED: c++03, c++98, c++11 |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 12 | |
| 13 | // <shared_mutex> |
| 14 | |
David Majnemer | f9f95be | 2014-03-17 20:19:44 +0000 | [diff] [blame] | 15 | // class shared_timed_mutex; |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 16 | |
| 17 | // template <class Clock, class Duration> |
| 18 | // bool try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time); |
| 19 | |
| 20 | #include <shared_mutex> |
| 21 | #include <thread> |
| 22 | #include <vector> |
| 23 | #include <cstdlib> |
| 24 | #include <cassert> |
| 25 | |
David Majnemer | f9f95be | 2014-03-17 20:19:44 +0000 | [diff] [blame] | 26 | std::shared_timed_mutex m; |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 27 | |
| 28 | typedef std::chrono::steady_clock Clock; |
| 29 | typedef Clock::time_point time_point; |
| 30 | typedef Clock::duration duration; |
| 31 | typedef std::chrono::milliseconds ms; |
| 32 | typedef std::chrono::nanoseconds ns; |
| 33 | |
| 34 | void f1() |
| 35 | { |
| 36 | time_point t0 = Clock::now(); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 37 | assert(m.try_lock_shared_until(Clock::now() + ms(300)) == true); |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 38 | time_point t1 = Clock::now(); |
| 39 | m.unlock_shared(); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 40 | ns d = t1 - t0 - ms(250); |
| 41 | assert(d < ms(50)); // within 50ms |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void f2() |
| 45 | { |
| 46 | time_point t0 = Clock::now(); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 47 | assert(m.try_lock_shared_until(Clock::now() + ms(250)) == false); |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 48 | time_point t1 = Clock::now(); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 49 | ns d = t1 - t0 - ms(250); |
| 50 | assert(d < ms(50)); // within 50ms |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 53 | int main() |
| 54 | { |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 55 | { |
| 56 | m.lock(); |
| 57 | std::vector<std::thread> v; |
| 58 | for (int i = 0; i < 5; ++i) |
| 59 | v.push_back(std::thread(f1)); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 60 | std::this_thread::sleep_for(ms(250)); |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 61 | m.unlock(); |
| 62 | for (auto& t : v) |
| 63 | t.join(); |
| 64 | } |
| 65 | { |
| 66 | m.lock(); |
| 67 | std::vector<std::thread> v; |
| 68 | for (int i = 0; i < 5; ++i) |
| 69 | v.push_back(std::thread(f2)); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 70 | std::this_thread::sleep_for(ms(300)); |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 71 | m.unlock(); |
| 72 | for (auto& t : v) |
| 73 | t.join(); |
| 74 | } |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 75 | } |