|
5 | 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
6 | 6 | //
|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 |
| -// |
| 8 | + |
9 | 9 | // UNSUPPORTED: no-threads
|
10 | 10 | // UNSUPPORTED: c++03, c++11
|
11 |
| -// ALLOW_RETRIES: 2 |
12 | 11 |
|
13 | 12 | // <shared_mutex>
|
14 | 13 |
|
15 | 14 | // template <class Mutex> class shared_lock;
|
16 | 15 |
|
17 | 16 | // void lock();
|
18 | 17 |
|
| 18 | +#include <atomic> |
19 | 19 | #include <cassert>
|
20 |
| -#include <chrono> |
21 |
| -#include <cstdlib> |
22 |
| -#include <mutex> |
| 20 | +#include <mutex> // std::defer_lock |
23 | 21 | #include <shared_mutex>
|
24 | 22 | #include <system_error>
|
25 | 23 | #include <thread>
|
|
28 | 26 | #include "make_test_thread.h"
|
29 | 27 | #include "test_macros.h"
|
30 | 28 |
|
31 |
| -std::shared_timed_mutex m; |
| 29 | +struct Monitor { |
| 30 | + bool lock_shared_called = false; |
| 31 | + bool unlock_shared_called = false; |
| 32 | +}; |
32 | 33 |
|
33 |
| -typedef std::chrono::system_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; |
| 34 | +struct TrackedMutex { |
| 35 | + Monitor* monitor = nullptr; |
38 | 36 |
|
39 |
| -ms WaitTime = ms(250); |
| 37 | + void lock_shared() { |
| 38 | + if (monitor != nullptr) |
| 39 | + monitor->lock_shared_called = true; |
| 40 | + } |
| 41 | + void unlock_shared() { |
| 42 | + if (monitor != nullptr) |
| 43 | + monitor->unlock_shared_called = true; |
| 44 | + } |
| 45 | +}; |
40 | 46 |
|
41 |
| -// Thread sanitizer causes more overhead and will sometimes cause this test |
42 |
| -// to fail. To prevent this we give Thread sanitizer more time to complete the |
43 |
| -// test. |
44 |
| -#if !defined(TEST_IS_EXECUTED_IN_A_SLOW_ENVIRONMENT) |
45 |
| -ms Tolerance = ms(25); |
46 |
| -#else |
47 |
| -ms Tolerance = ms(25 * 5); |
48 |
| -#endif |
| 47 | +template <class Mutex> |
| 48 | +void test() { |
| 49 | + // Basic sanity test |
| 50 | + { |
| 51 | + Mutex mutex; |
| 52 | + std::vector<std::thread> threads; |
| 53 | + std::atomic<bool> ready(false); |
| 54 | + for (int i = 0; i != 5; ++i) { |
| 55 | + threads.push_back(support::make_test_thread([&] { |
| 56 | + while (!ready) { |
| 57 | + // spin |
| 58 | + } |
49 | 59 |
|
| 60 | + std::shared_lock<Mutex> lock(mutex, std::defer_lock); |
| 61 | + lock.lock(); |
| 62 | + assert(lock.owns_lock()); |
| 63 | + })); |
| 64 | + } |
| 65 | + |
| 66 | + ready = true; |
| 67 | + for (auto& t : threads) |
| 68 | + t.join(); |
| 69 | + } |
50 | 70 |
|
51 |
| -void f() |
52 |
| -{ |
53 |
| - std::shared_lock<std::shared_timed_mutex> lk(m, std::defer_lock); |
54 |
| - time_point t0 = Clock::now(); |
55 |
| - lk.lock(); |
56 |
| - time_point t1 = Clock::now(); |
57 |
| - assert(lk.owns_lock() == true); |
58 |
| - ns d = t1 - t0 - WaitTime; |
59 |
| - assert(d < Tolerance); // within tolerance |
| 71 | + // Try locking the same shared_lock again in the same thread. This should throw an exception. |
| 72 | + { |
| 73 | + Mutex mutex; |
| 74 | + std::shared_lock<Mutex> lock(mutex, std::defer_lock); |
| 75 | + lock.lock(); |
| 76 | + assert(lock.owns_lock()); |
60 | 77 | #ifndef TEST_HAS_NO_EXCEPTIONS
|
61 |
| - try |
62 |
| - { |
63 |
| - lk.lock(); |
64 |
| - assert(false); |
65 |
| - } |
66 |
| - catch (std::system_error& e) |
67 |
| - { |
68 |
| - assert(e.code().value() == EDEADLK); |
| 78 | + try { |
| 79 | + lock.lock(); |
| 80 | + assert(false); |
| 81 | + } catch (std::system_error const& e) { |
| 82 | + assert(e.code() == std::errc::resource_deadlock_would_occur); |
69 | 83 | }
|
70 | 84 | #endif
|
71 |
| - lk.unlock(); |
72 |
| - lk.release(); |
| 85 | + } |
| 86 | + |
| 87 | + // Try locking a shared_lock that isn't associated to any mutex. This should throw an exception. |
| 88 | + { |
| 89 | + std::shared_lock<Mutex> lock; // no associated mutex |
73 | 90 | #ifndef TEST_HAS_NO_EXCEPTIONS
|
74 |
| - try |
75 |
| - { |
76 |
| - lk.lock(); |
77 |
| - assert(false); |
78 |
| - } |
79 |
| - catch (std::system_error& e) |
80 |
| - { |
81 |
| - assert(e.code().value() == EPERM); |
| 91 | + try { |
| 92 | + lock.lock(); |
| 93 | + assert(false); |
| 94 | + } catch (std::system_error const& e) { |
| 95 | + assert(e.code() == std::errc::operation_not_permitted); |
82 | 96 | }
|
83 | 97 | #endif
|
| 98 | + } |
84 | 99 | }
|
85 | 100 |
|
86 |
| -int main(int, char**) |
87 |
| -{ |
88 |
| - m.lock(); |
89 |
| - std::vector<std::thread> v; |
90 |
| - for (int i = 0; i < 5; ++i) |
91 |
| - v.push_back(support::make_test_thread(f)); |
92 |
| - std::this_thread::sleep_for(WaitTime); |
93 |
| - m.unlock(); |
94 |
| - for (auto& t : v) |
95 |
| - t.join(); |
| 101 | +int main(int, char**) { |
| 102 | +#if TEST_STD_VER >= 17 |
| 103 | + test<std::shared_mutex>(); |
| 104 | +#endif |
| 105 | + test<std::shared_timed_mutex>(); |
| 106 | + test<TrackedMutex>(); |
| 107 | + |
| 108 | + // Use shared_lock with a dummy mutex class that tracks whether each |
| 109 | + // operation has been called or not. |
| 110 | + { |
| 111 | + Monitor monitor; |
| 112 | + TrackedMutex mutex{&monitor}; |
| 113 | + |
| 114 | + std::shared_lock<TrackedMutex> lock(mutex, std::defer_lock); |
| 115 | + lock.lock(); |
| 116 | + assert(monitor.lock_shared_called); |
| 117 | + assert(lock.owns_lock()); |
| 118 | + |
| 119 | + lock.unlock(); |
| 120 | + assert(monitor.unlock_shared_called); |
| 121 | + } |
96 | 122 |
|
97 | 123 | return 0;
|
98 | 124 | }
|
0 commit comments