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 | |
| 14 | // template <class Mutex> class shared_lock; |
| 15 | |
| 16 | // shared_lock(mutex_type& m, try_to_lock_t); |
| 17 | |
| 18 | #include <shared_mutex> |
| 19 | #include <thread> |
| 20 | #include <vector> |
| 21 | #include <cstdlib> |
| 22 | #include <cassert> |
| 23 | |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 24 | #if _LIBCPP_STD_VER > 11 |
| 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::system_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 f() |
| 35 | { |
| 36 | time_point t0 = Clock::now(); |
| 37 | { |
David Majnemer | f9f95be | 2014-03-17 20:19:44 +0000 | [diff] [blame] | 38 | std::shared_lock<std::shared_timed_mutex> lk(m, std::try_to_lock); |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 39 | assert(lk.owns_lock() == false); |
| 40 | } |
| 41 | { |
David Majnemer | f9f95be | 2014-03-17 20:19:44 +0000 | [diff] [blame] | 42 | std::shared_lock<std::shared_timed_mutex> lk(m, std::try_to_lock); |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 43 | assert(lk.owns_lock() == false); |
| 44 | } |
| 45 | { |
David Majnemer | f9f95be | 2014-03-17 20:19:44 +0000 | [diff] [blame] | 46 | std::shared_lock<std::shared_timed_mutex> lk(m, std::try_to_lock); |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 47 | assert(lk.owns_lock() == false); |
| 48 | } |
| 49 | while (true) |
| 50 | { |
David Majnemer | f9f95be | 2014-03-17 20:19:44 +0000 | [diff] [blame] | 51 | std::shared_lock<std::shared_timed_mutex> lk(m, std::try_to_lock); |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 52 | if (lk.owns_lock()) |
| 53 | break; |
| 54 | } |
| 55 | time_point t1 = Clock::now(); |
| 56 | ns d = t1 - t0 - ms(250); |
| 57 | assert(d < ms(200)); // within 200ms |
| 58 | } |
| 59 | |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 60 | #endif // _LIBCPP_STD_VER > 11 |
| 61 | |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 62 | int main() |
| 63 | { |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 64 | #if _LIBCPP_STD_VER > 11 |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 65 | m.lock(); |
| 66 | std::vector<std::thread> v; |
| 67 | for (int i = 0; i < 5; ++i) |
| 68 | v.push_back(std::thread(f)); |
| 69 | std::this_thread::sleep_for(ms(250)); |
| 70 | m.unlock(); |
| 71 | for (auto& t : v) |
| 72 | t.join(); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 73 | #endif // _LIBCPP_STD_VER > 11 |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 74 | } |