Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / thread / thread.condition / notify_all_at_thread_exit_lwg3343.pass.cpp
blobffb632a5149b5ed72cb45b99a34134cbe68661cc
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: no-threads
11 // notify_all_at_thread_exit(...) requires move semantics to transfer the unique_lock.
12 // UNSUPPORTED: c++03
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>
30 #include <cassert>
31 #include <chrono>
32 #include <memory>
33 #include <mutex>
34 #include <thread>
36 int condition_variable_lock_skipped_counter = 0;
38 union X {
39 X() : cv_() {}
40 ~X() {}
41 std::condition_variable cv_;
42 unsigned char bytes_[sizeof(std::condition_variable)];
45 void test()
47 constexpr int N = 3;
49 X x;
50 std::mutex m;
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);
60 --threads_active;
61 std::notify_all_at_thread_exit(x.cv_, std::move(lk));
62 });
63 t.detach();
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
70 // unlocks `m`.
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
79 // to be improved.
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_) {
89 c = 0xcd;
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.
98 int sum = 0;
99 for (unsigned char c : x.bytes_) {
100 sum += c;
102 DoNotOptimize(sum);
103 assert(sum == (0xcd * sizeof(std::condition_variable)));
106 int main(int, char**)
108 for (int i = 0; i < 1000; ++i) {
109 test();
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);
116 return 0;