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: c++03
10// UNSUPPORTED: no-threads
11
12// <mutex>
13
14// class recursive_timed_mutex;
15
16// template <class Rep, class Period>
17// bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
18
19#include <mutex>
20#include <atomic>
21#include <cassert>
22#include <chrono>
23#include <thread>
24
25#include "make_test_thread.h"
26
27bool is_lockable(std::recursive_timed_mutex& m) {
28 bool did_lock;
29 std::thread t = support::make_test_thread([&] {
30 did_lock = m.try_lock();
31 if (did_lock)
32 m.unlock(); // undo side effects
33 });
34 t.join();
35
36 return did_lock;
37}
38
39template <class Function>
40std::chrono::microseconds measure(Function f) {
41 std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
42 f();
43 std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now();
44 return std::chrono::duration_cast<std::chrono::microseconds>(d: end - start);
45}
46
47int main(int, char**) {
48 // Try to lock a mutex that is not locked yet. This should succeed immediately.
49 {
50 std::recursive_timed_mutex m;
51 bool succeeded = m.try_lock_for(rtime: std::chrono::milliseconds(1));
52 assert(succeeded);
53 m.unlock();
54 }
55
56 // Lock a mutex that is already locked by this thread. This should succeed immediately and the mutex
57 // should only be unlocked after a matching number of calls to unlock() on the same thread.
58 {
59 std::recursive_timed_mutex m;
60 int lock_count = 0;
61 for (int i = 0; i != 10; ++i) {
62 assert(m.try_lock_for(std::chrono::milliseconds(1)));
63 ++lock_count;
64 }
65 while (lock_count != 0) {
66 assert(!is_lockable(m));
67 m.unlock();
68 --lock_count;
69 }
70 assert(is_lockable(m));
71 }
72
73 // Try to lock an already-locked mutex for a long enough amount of time and succeed.
74 // This is technically flaky, but we use such long durations that it should pass even
75 // in slow or contended environments.
76 {
77 std::chrono::milliseconds const wait_time(500);
78 std::chrono::milliseconds const tolerance = wait_time * 3;
79 std::atomic<bool> ready(false);
80
81 std::recursive_timed_mutex m;
82 m.lock();
83
84 std::thread t = support::make_test_thread([&] {
85 auto elapsed = measure([&] {
86 ready = true;
87 bool succeeded = m.try_lock_for(wait_time);
88 assert(succeeded);
89 m.unlock();
90 });
91
92 // Ensure we didn't wait significantly longer than our timeout. This is technically
93 // flaky and non-conforming because an implementation is free to block for arbitrarily
94 // long, but any decent quality implementation should pass this test.
95 assert(elapsed - wait_time < tolerance);
96 });
97
98 // Wait for the thread to be ready to take the lock before we unlock it from here, otherwise
99 // there's a high chance that we're not testing the "locking an already locked" mutex use case.
100 // There is still technically a race condition here.
101 while (!ready)
102 /* spin */;
103 std::this_thread::sleep_for(rtime: wait_time / 5);
104
105 m.unlock(); // this should allow the thread to lock 'm'
106 t.join();
107 }
108
109 // Try to lock an already-locked mutex for a short amount of time and fail.
110 // Again, this is technically flaky but we use such long durations that it should work.
111 {
112 std::chrono::milliseconds const wait_time(10);
113 std::chrono::milliseconds const tolerance(750); // in case the thread we spawned goes to sleep or something
114
115 std::recursive_timed_mutex m;
116 m.lock();
117
118 std::thread t = support::make_test_thread([&] {
119 auto elapsed = measure([&] {
120 bool succeeded = m.try_lock_for(wait_time);
121 assert(!succeeded);
122 });
123
124 // Ensure we failed within some bounded time.
125 assert(elapsed - wait_time < tolerance);
126 });
127
128 t.join();
129
130 m.unlock();
131 }
132
133 return 0;
134}
135

source code of libcxx/test/std/thread/thread.mutex/thread.mutex.requirements/thread.timedmutex.requirements/thread.timedmutex.recursive/try_lock_for.pass.cpp