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 // template <class Rep, class Period>
17 // shared_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
25 #include <shared_mutex>
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
);
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.
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.
58 std::shared_lock
<std::shared_timed_mutex
> lk(m
, LongTime
);
59 assert(lk
.owns_lock() == true);
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
);
75 std::vector
<std::thread
> v
;
76 for (unsigned i
= 0; i
< Threads
; ++i
)
77 v
.push_back(support::make_test_thread(f1
));
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
);
89 std::vector
<std::thread
> v
;
90 for (unsigned i
= 0; i
< Threads
; ++i
)
91 v
.push_back(support::make_test_thread(f2
));