[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_duration.pass.cpp
blobe5c8caf4a07adc762947e8e03ffbacbfbdef3633
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
12 // <shared_mutex>
14 // class timed_mutex;
16 // template <class Rep, class Period>
17 // shared_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
19 #include <thread>
21 #include <atomic>
22 #include <cassert>
23 #include <chrono>
24 #include <cstdlib>
25 #include <shared_mutex>
26 #include <vector>
28 #include "make_test_thread.h"
29 #include "test_macros.h"
31 std::shared_timed_mutex m;
33 typedef std::chrono::steady_clock Clock;
34 typedef Clock::time_point time_point;
35 typedef Clock::duration duration;
36 typedef std::chrono::milliseconds ms;
37 typedef std::chrono::nanoseconds ns;
39 ms LongTime = ms(5000);
40 ms ShortTime = ms(50);
42 static const unsigned Threads = 5;
44 std::atomic<unsigned> CountDown(Threads);
46 void f1()
48 // Preemptive scheduling means that one cannot make assumptions about when
49 // code executes and therefore we cannot assume anything about when the mutex
50 // starts waiting relative to code in the main thread. We can however prove
51 // that a timeout occurred and that implies that this code is waiting.
52 // See f2() below.
54 // Nevertheless, we should at least try to ensure that the mutex waits and
55 // therefore we use an atomic variable to signal to the main thread that this
56 // code is just a few instructions away from waiting.
57 --CountDown;
58 std::shared_lock<std::shared_timed_mutex> lk(m, LongTime);
59 assert(lk.owns_lock() == true);
62 void f2()
64 time_point t0 = Clock::now();
65 std::shared_lock<std::shared_timed_mutex> lk(m, ShortTime);
66 time_point t1 = Clock::now();
67 assert(lk.owns_lock() == false);
68 assert(t1 - t0 >= ShortTime);
71 int main(int, char**)
74 m.lock();
75 std::vector<std::thread> v;
76 for (unsigned i = 0; i < Threads; ++i)
77 v.push_back(support::make_test_thread(f1));
78 while (CountDown > 0)
79 std::this_thread::yield();
80 // Give one more chance for threads to block and wait for the mutex.
81 std::this_thread::yield();
82 std::this_thread::sleep_for(ShortTime);
83 m.unlock();
84 for (auto& t : v)
85 t.join();
88 m.lock();
89 std::vector<std::thread> v;
90 for (unsigned i = 0; i < Threads; ++i)
91 v.push_back(support::make_test_thread(f2));
92 for (auto& t : v)
93 t.join();
94 m.unlock();
97 return 0;