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