|
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, c++14
|
11 | 11 |
|
12 |
| -// ALLOW_RETRIES: 2 |
13 |
| - |
14 | 12 | // <shared_mutex>
|
15 | 13 |
|
16 | 14 | // class shared_mutex;
|
17 | 15 |
|
18 | 16 | // void lock();
|
19 | 17 |
|
| 18 | +#include <shared_mutex> |
| 19 | +#include <atomic> |
20 | 20 | #include <cassert>
|
21 | 21 | #include <chrono>
|
22 |
| -#include <cstdlib> |
23 |
| -#include <shared_mutex> |
24 | 22 | #include <thread>
|
| 23 | +#include <vector> |
25 | 24 |
|
26 | 25 | #include "make_test_thread.h"
|
27 | 26 | #include "test_macros.h"
|
28 | 27 |
|
29 |
| -std::shared_mutex m; |
| 28 | +int main(int, char**) { |
| 29 | + // Exclusive-lock a mutex that is not locked yet. This should succeed. |
| 30 | + { |
| 31 | + std::shared_mutex m; |
| 32 | + m.lock(); |
| 33 | + m.unlock(); |
| 34 | + } |
30 | 35 |
|
31 |
| -typedef std::chrono::system_clock Clock; |
32 |
| -typedef Clock::time_point time_point; |
33 |
| -typedef Clock::duration duration; |
34 |
| -typedef std::chrono::milliseconds ms; |
35 |
| -typedef std::chrono::nanoseconds ns; |
| 36 | + // Exclusive-lock a mutex that is already locked exclusively. This should block until it is unlocked. |
| 37 | + { |
| 38 | + std::atomic<bool> ready(false); |
| 39 | + std::shared_mutex m; |
| 40 | + m.lock(); |
| 41 | + std::atomic<bool> is_locked_from_main(true); |
36 | 42 |
|
37 |
| -ms WaitTime = ms(250); |
| 43 | + std::thread t = support::make_test_thread([&] { |
| 44 | + ready = true; |
| 45 | + m.lock(); |
| 46 | + assert(!is_locked_from_main); |
| 47 | + m.unlock(); |
| 48 | + }); |
38 | 49 |
|
39 |
| -// Thread sanitizer causes more overhead and will sometimes cause this test |
40 |
| -// to fail. To prevent this we give Thread sanitizer more time to complete the |
41 |
| -// test. |
42 |
| -#if !defined(TEST_IS_EXECUTED_IN_A_SLOW_ENVIRONMENT) |
43 |
| -ms Tolerance = ms(50); |
44 |
| -#else |
45 |
| -ms Tolerance = ms(50 * 5); |
46 |
| -#endif |
| 50 | + while (!ready) |
| 51 | + /* spin */; |
47 | 52 |
|
48 |
| -void f() |
49 |
| -{ |
50 |
| - time_point t0 = Clock::now(); |
51 |
| - m.lock(); |
52 |
| - time_point t1 = Clock::now(); |
| 53 | + // We would rather signal this after we unlock, but that would create a race condition. |
| 54 | + // We instead signal it before we unlock, which means that it's technically possible for the thread |
| 55 | + // to take the lock while we're still holding it and for the test to still pass. |
| 56 | + is_locked_from_main = false; |
53 | 57 | m.unlock();
|
54 |
| - ns d = t1 - t0 - WaitTime; |
55 |
| - assert(d < Tolerance); // within tolerance |
56 |
| -} |
57 | 58 |
|
58 |
| -int main(int, char**) |
59 |
| -{ |
60 |
| - m.lock(); |
61 |
| - std::thread t = support::make_test_thread(f); |
62 |
| - std::this_thread::sleep_for(WaitTime); |
63 |
| - m.unlock(); |
64 | 59 | t.join();
|
| 60 | + } |
| 61 | + |
| 62 | + // Exclusive-lock a mutex that is already share-locked. This should block until it is unlocked. |
| 63 | + { |
| 64 | + std::atomic<bool> ready(false); |
| 65 | + std::shared_mutex m; |
| 66 | + m.lock_shared(); |
| 67 | + std::atomic<bool> is_locked_from_main(true); |
| 68 | + |
| 69 | + std::thread t = support::make_test_thread([&] { |
| 70 | + ready = true; |
| 71 | + m.lock(); |
| 72 | + assert(!is_locked_from_main); |
| 73 | + m.unlock(); |
| 74 | + }); |
| 75 | + |
| 76 | + while (!ready) |
| 77 | + /* spin */; |
| 78 | + |
| 79 | + // We would rather signal this after we unlock, but that would create a race condition. |
| 80 | + // We instead signal it before we unlock, which means that it's technically possible for |
| 81 | + // the thread to take the lock while we're still holding it and for the test to still pass. |
| 82 | + is_locked_from_main = false; |
| 83 | + m.unlock_shared(); |
| 84 | + |
| 85 | + t.join(); |
| 86 | + } |
| 87 | + |
| 88 | + // Make sure that at most one thread can acquire the mutex concurrently. |
| 89 | + { |
| 90 | + std::atomic<int> counter = 0; |
| 91 | + std::shared_mutex mutex; |
| 92 | + |
| 93 | + std::vector<std::thread> threads; |
| 94 | + for (int i = 0; i != 10; ++i) { |
| 95 | + threads.push_back(support::make_test_thread([&] { |
| 96 | + mutex.lock(); |
| 97 | + counter++; |
| 98 | + assert(counter == 1); |
| 99 | + counter--; |
| 100 | + mutex.unlock(); |
| 101 | + })); |
| 102 | + } |
| 103 | + |
| 104 | + for (auto& t : threads) |
| 105 | + t.join(); |
| 106 | + } |
65 | 107 |
|
66 | 108 | return 0;
|
67 | 109 | }
|
0 commit comments