[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / libcxx / test / std / thread / thread.mutex / thread.lock / thread.lock.shared / thread.lock.shared.cons / mutex.pass.cpp
blob4940041bcf9656d5d6d7fecc82688a48789fe1e7
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // UNSUPPORTED: no-threads
10 // UNSUPPORTED: c++03, c++11
11 // ALLOW_RETRIES: 2
13 // <shared_mutex>
15 // template <class Mutex> class shared_lock;
17 // explicit shared_lock(mutex_type& m);
19 // template<class _Mutex> shared_lock(shared_lock<_Mutex>)
20 // -> shared_lock<_Mutex>; // C++17
22 #include <cassert>
23 #include <chrono>
24 #include <cstdlib>
25 #include <shared_mutex>
26 #include <thread>
27 #include <vector>
29 #include "make_test_thread.h"
30 #include "test_macros.h"
32 typedef std::chrono::system_clock Clock;
33 typedef Clock::time_point time_point;
34 typedef Clock::duration duration;
35 typedef std::chrono::milliseconds ms;
36 typedef std::chrono::nanoseconds ns;
38 ms WaitTime = ms(250);
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_IS_EXECUTED_IN_A_SLOW_ENVIRONMENT)
44 ms Tolerance = ms(50);
45 #else
46 ms Tolerance = ms(50 * 5);
47 #endif
49 std::shared_timed_mutex m;
51 void f()
53 time_point t0 = Clock::now();
54 time_point t1;
56 std::shared_lock<std::shared_timed_mutex> ul(m);
57 t1 = Clock::now();
59 ns d = t1 - t0 - WaitTime;
60 assert(d < Tolerance); // within tolerance
63 void g()
65 time_point t0 = Clock::now();
66 time_point t1;
68 std::shared_lock<std::shared_timed_mutex> ul(m);
69 t1 = Clock::now();
71 ns d = t1 - t0;
72 assert(d < Tolerance); // within tolerance
75 int main(int, char**)
77 std::vector<std::thread> v;
79 m.lock();
80 for (int i = 0; i < 5; ++i)
81 v.push_back(support::make_test_thread(f));
82 std::this_thread::sleep_for(WaitTime);
83 m.unlock();
84 for (auto& t : v)
85 t.join();
88 m.lock_shared();
89 for (auto& t : v)
90 t = support::make_test_thread(g);
91 std::thread q = support::make_test_thread(f);
92 std::this_thread::sleep_for(WaitTime);
93 m.unlock_shared();
94 for (auto& t : v)
95 t.join();
96 q.join();
99 #if TEST_STD_VER >= 17
100 std::shared_lock sl(m);
101 static_assert((std::is_same<decltype(sl), std::shared_lock<decltype(m)>>::value), "" );
102 #endif
104 return 0;