Home | History | Annotate | Download | only in thread.lock.shared.cons
      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 // class shared_timed_mutex;
     16 
     17 // template <class Clock, class Duration>
     18 //   shared_lock(mutex_type& m, 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 
     26 #include "test_macros.h"
     27 
     28 std::shared_timed_mutex m;
     29 
     30 typedef std::chrono::steady_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(50);
     43 #else
     44 ms Tolerance = ms(50 * 5);
     45 #endif
     46 
     47 void f1()
     48 {
     49     time_point t0 = Clock::now();
     50     std::shared_lock<std::shared_timed_mutex> lk(m, Clock::now() + WaitTime + Tolerance);
     51     assert(lk.owns_lock() == true);
     52     time_point t1 = Clock::now();
     53     ns d = t1 - t0 - WaitTime;
     54     assert(d < Tolerance);  // within 50ms
     55 }
     56 
     57 void f2()
     58 {
     59     time_point t0 = Clock::now();
     60     std::shared_lock<std::shared_timed_mutex> lk(m, Clock::now() + WaitTime);
     61     assert(lk.owns_lock() == false);
     62     time_point t1 = Clock::now();
     63     ns d = t1 - t0 - WaitTime;
     64     assert(d < Tolerance);  // within 50ms
     65 }
     66 
     67 int main()
     68 {
     69     {
     70         m.lock();
     71         std::vector<std::thread> v;
     72         for (int i = 0; i < 5; ++i)
     73             v.push_back(std::thread(f1));
     74         std::this_thread::sleep_for(WaitTime);
     75         m.unlock();
     76         for (auto& t : v)
     77             t.join();
     78     }
     79     {
     80         m.lock();
     81         std::vector<std::thread> v;
     82         for (int i = 0; i < 5; ++i)
     83             v.push_back(std::thread(f2));
     84         std::this_thread::sleep_for(WaitTime + Tolerance);
     85         m.unlock();
     86         for (auto& t : v)
     87             t.join();
     88     }
     89 }
     90