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 //===----------------------------------------------------------------------===// 9 // 10 // UNSUPPORTED: libcpp-has-no-threads 11 // UNSUPPORTED: c++98, c++03, c++11 12 13 // <shared_mutex> 14 15 // template <class Mutex> class shared_lock; 16 17 // void lock(); 18 19 #include <shared_mutex> 20 #include <thread> 21 #include <vector> 22 #include <cstdlib> 23 #include <cassert> 24 25 #include "test_macros.h" 26 27 std::shared_timed_mutex m; 28 29 typedef std::chrono::system_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 ms WaitTime = ms(250); 36 37 // Thread sanitizer causes more overhead and will sometimes cause this test 38 // to fail. To prevent this we give Thread sanitizer more time to complete the 39 // test. 40 #if !TEST_HAS_FEATURE(thread_sanitizer) 41 ms Tolerance = ms(25); 42 #else 43 ms Tolerance = ms(75); 44 #endif 45 46 47 void f() 48 { 49 std::shared_lock<std::shared_timed_mutex> lk(m, std::defer_lock); 50 time_point t0 = Clock::now(); 51 lk.lock(); 52 time_point t1 = Clock::now(); 53 assert(lk.owns_lock() == true); 54 ns d = t1 - t0 - WaitTime; 55 assert(d < Tolerance); // within tolerance 56 try 57 { 58 lk.lock(); 59 assert(false); 60 } 61 catch (std::system_error& e) 62 { 63 assert(e.code().value() == EDEADLK); 64 } 65 lk.unlock(); 66 lk.release(); 67 try 68 { 69 lk.lock(); 70 assert(false); 71 } 72 catch (std::system_error& e) 73 { 74 assert(e.code().value() == EPERM); 75 } 76 } 77 78 int main() 79 { 80 m.lock(); 81 std::vector<std::thread> v; 82 for (int i = 0; i < 5; ++i) 83 v.push_back(std::thread(f)); 84 std::this_thread::sleep_for(WaitTime); 85 m.unlock(); 86 for (auto& t : v) 87 t.join(); 88 } 89