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