1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-threads
10 // UNSUPPORTED: c++03, c++11
12 // UNSUPPORTED: availability-shared_mutex-missing
18 // class shared_timed_mutex;
20 // void lock_shared();
27 #include <shared_mutex>
31 #include "make_test_thread.h"
32 #include "test_macros.h"
34 std::shared_timed_mutex m
;
36 typedef std::chrono::system_clock Clock
;
37 typedef Clock::time_point time_point
;
38 typedef Clock::duration duration
;
39 typedef std::chrono::milliseconds ms
;
40 typedef std::chrono::nanoseconds ns
;
42 std::atomic
<unsigned> countDown
;
43 time_point readerStart
; // Protected by the above mutex 'm'
44 time_point writerStart
; // Protected by the above mutex 'm'
46 ms WaitTime
= ms(250);
48 void readerMustWait() {
51 time_point t1
= Clock::now();
52 time_point t0
= readerStart
;
54 assert(t0
.time_since_epoch() > ms(0));
55 assert(t1
- t0
>= WaitTime
);
64 void writerMustWait() {
67 time_point t1
= Clock::now();
68 time_point t0
= writerStart
;
70 assert(t0
.time_since_epoch() > ms(0));
71 assert(t1
- t0
>= WaitTime
);
78 countDown
.store(threads
);
80 std::vector
<std::thread
> v
;
81 for (int i
= 0; i
< threads
; ++i
)
82 v
.push_back(support::make_test_thread(readerMustWait
));
84 std::this_thread::yield();
85 readerStart
= Clock::now();
86 std::this_thread::sleep_for(WaitTime
);
91 countDown
.store(threads
+ 1);
94 t
= support::make_test_thread(reader
);
95 std::thread q
= support::make_test_thread(writerMustWait
);
97 std::this_thread::yield();
98 writerStart
= Clock::now();
99 std::this_thread::sleep_for(WaitTime
);