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
11 // notify_all_at_thread_exit(...) requires move semantics to transfer the unique_lock.
14 // This test requires the fix for LWG3343 (64fc3cd), which is done in the dylib
15 // and landed in LLVM 16. Due to the nature of the test, testing on a broken
16 // system does not guarantee that the test fails, so we use UNSUPPORTED instead
18 // UNSUPPORTED: using-built-library-before-llvm-16
20 // This is a regression test for LWG3343.
22 // <condition_variable>
24 // void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
26 #include "make_test_thread.h"
27 #include "test_macros.h"
29 #include <condition_variable>
36 int condition_variable_lock_skipped_counter
= 0;
39 // MSVC warning C4583: 'X::cv_': destructor is not implicitly called
40 TEST_MSVC_DIAGNOSTIC_IGNORED(4583)
45 std::condition_variable cv_
;
46 unsigned char bytes_
[sizeof(std::condition_variable
)];
57 int threads_active
= N
;
59 for (int i
= 0; i
< N
; ++i
) {
60 std::thread t
= support::make_test_thread([&] {
61 // Emulate work being done.
62 std::this_thread::sleep_for(std::chrono::milliseconds(1));
64 // Signal thread completion.
65 std::unique_lock
<std::mutex
> lk(m
);
67 std::notify_all_at_thread_exit(x
.cv_
, std::move(lk
));
72 // Wait until all threads complete, i.e. until they've all
73 // decremented `threads_active` and then unlocked `m` at thread exit.
74 // It is possible that this `wait` may spuriously wake up,
75 // but it won't be able to continue until the last thread
78 std::unique_lock
<std::mutex
> lk(m
);
79 // Due to OS scheduling the workers might have terminated when this
80 // code is reached. In that case the wait will not sleep and the call
81 // to notify_all_at_thread_exit has no effect; the condition variable
82 // will not be used here.
84 // Keep track of how often that happens, if too often the test needs
86 if(threads_active
== 0)
87 ++condition_variable_lock_skipped_counter
;
88 x
.cv_
.wait(lk
, [&]() { return threads_active
== 0; });
91 // Destroy the condition_variable and shred the bytes.
92 // Simulate reusing the memory for something else.
93 x
.cv_
.~condition_variable();
94 for (unsigned char& c
: x
.bytes_
) {
98 DoNotOptimize(x
.bytes_
);
100 // Check that the bytes still have the same value we just wrote to them.
101 // If any thread wrongly unlocked `m` before calling cv.notify_all(), and
102 // cv.notify_all() writes to the memory of the cv, then we have a chance
103 // to detect the problem here.
105 for (unsigned char c
: x
.bytes_
) {
109 assert(sum
== (0xcd * sizeof(std::condition_variable
)));
112 int main(int, char**)
114 for (int i
= 0; i
< 1000; ++i
) {
118 // The threshold is arbitrary, it just makes sure the notification is
119 // tested a reasonable number of times.
120 assert(condition_variable_lock_skipped_counter
< 250);