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 // The fix of LWG3343 is done in the dylib. That means Apple backdeployment
15 // targets remain broken. Due to the nature of the test, testing on a broken
16 // system does not guarantee that the test fails, so the test can't use XFAIL.
17 // UNSUPPORTED: stdlib=apple-libc++ && target={{.+}}-apple-macosx10.{{.+}}
18 // UNSUPPORTED: stdlib=apple-libc++ && target={{.+}}-apple-macosx11.{{.+}}
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;
41 std::condition_variable cv_
;
42 unsigned char bytes_
[sizeof(std::condition_variable
)];
51 int threads_active
= N
;
53 for (int i
= 0; i
< N
; ++i
) {
54 std::thread t
= support::make_test_thread([&] {
55 // Emulate work being done.
56 std::this_thread::sleep_for(std::chrono::milliseconds(1));
58 // Signal thread completion.
59 std::unique_lock
<std::mutex
> lk(m
);
61 std::notify_all_at_thread_exit(x
.cv_
, std::move(lk
));
66 // Wait until all threads complete, i.e. until they've all
67 // decremented `threads_active` and then unlocked `m` at thread exit.
68 // It is possible that this `wait` may spuriously wake up,
69 // but it won't be able to continue until the last thread
72 std::unique_lock
<std::mutex
> lk(m
);
73 // Due to OS scheduling the workers might have terminated when this
74 // code is reached. In that case the wait will not sleep and the call
75 // to notify_all_at_thread_exit has no effect; the condition variable
76 // will not be used here.
78 // Keep track of how often that happens, if too often the test needs
80 if(threads_active
== 0)
81 ++condition_variable_lock_skipped_counter
;
82 x
.cv_
.wait(lk
, [&]() { return threads_active
== 0; });
85 // Destroy the condition_variable and shred the bytes.
86 // Simulate reusing the memory for something else.
87 x
.cv_
.~condition_variable();
88 for (unsigned char& c
: x
.bytes_
) {
92 DoNotOptimize(x
.bytes_
);
94 // Check that the bytes still have the same value we just wrote to them.
95 // If any thread wrongly unlocked `m` before calling cv.notify_all(), and
96 // cv.notify_all() writes to the memory of the cv, then we have a chance
97 // to detect the problem here.
99 for (unsigned char c
: x
.bytes_
) {
103 assert(sum
== (0xcd * sizeof(std::condition_variable
)));
106 int main(int, char**)
108 for (int i
= 0; i
< 1000; ++i
) {
112 // The threshold is arbitrary, it just makes sure the notification is
113 // tested a reasonable number of times.
114 assert(condition_variable_lock_skipped_counter
< 250);