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
16 // class shared_timed_mutex;
18 // void lock_shared();
25 #include <shared_mutex>
29 #include "make_test_thread.h"
30 #include "test_macros.h"
32 std::shared_timed_mutex m
;
34 typedef std::chrono::system_clock Clock
;
35 typedef Clock::time_point time_point
;
36 typedef Clock::duration duration
;
37 typedef std::chrono::milliseconds ms
;
38 typedef std::chrono::nanoseconds ns
;
40 std::atomic
<unsigned> countDown
;
41 time_point readerStart
; // Protected by the above mutex 'm'
42 time_point writerStart
; // Protected by the above mutex 'm'
44 ms WaitTime
= ms(250);
46 void readerMustWait() {
49 time_point t1
= Clock::now();
50 time_point t0
= readerStart
;
52 assert(t0
.time_since_epoch() > ms(0));
53 assert(t1
- t0
>= WaitTime
);
62 void writerMustWait() {
65 time_point t1
= Clock::now();
66 time_point t0
= writerStart
;
68 assert(t0
.time_since_epoch() > ms(0));
69 assert(t1
- t0
>= WaitTime
);
76 countDown
.store(threads
);
78 std::vector
<std::thread
> v
;
79 for (int i
= 0; i
< threads
; ++i
)
80 v
.push_back(support::make_test_thread(readerMustWait
));
82 std::this_thread::yield();
83 readerStart
= Clock::now();
84 std::this_thread::sleep_for(WaitTime
);
89 countDown
.store(threads
+ 1);
92 t
= support::make_test_thread(reader
);
93 std::thread q
= support::make_test_thread(writerMustWait
);
95 std::this_thread::yield();
96 writerStart
= Clock::now();
97 std::this_thread::sleep_for(WaitTime
);