Debian Codegraph @

libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/lock.pass.cpp

raw
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
12 // ALLOW_RETRIES: 3
13
14 // <shared_mutex>
15
16 // class shared_timed_mutex;
17
18 // void lock();
19
20 #include <atomic>
21 #include <cassert>
22 #include <chrono>
23 #include <cstdlib>
24 #include <shared_mutex>
25 #include <thread>
26
27 #include "make_test_thread.h"
28 #include "test_macros.h"
29
30 std::shared_timed_mutex m;
31
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;
37
38 std::atomic<bool> ready(false);
39 time_point start;
40
41 ms WaitTime = ms(250);
42
43 void f()
44 {
45 ready.store(true);
46 m.lock();
47 time_point t0 = start;
48 time_point t1 = Clock::now();
49 m.unlock();
50 assert(t0.time_since_epoch() > ms(0));
51 assert(t1 - t0 >= WaitTime);
52 }
53
54 int main(int, char**)
55 {
56 m.lock();
57 std::thread t = support::make_test_thread(f);
58 while (!ready)
59 std::this_thread::yield();
60 start = Clock::now();
61 std::this_thread::sleep_for(WaitTime);
62 m.unlock();
63 t.join();
64
65 return 0;
66 }