xref: /llvm-project/libcxx/test/std/thread/thread.jthread/join.deadlock.pass.cpp (revision 121ed5c1985356436d0040dbe81bca26992b1fae)
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 // Windows cannot detect the deadlock. Instead of throwing system_error,
10 // it would dead lock the test
11 // UNSUPPORTED: windows
12 
13 // TSAN bug: https://github.com/llvm/llvm-project/issues/66537
14 // UNSUPPORTED: tsan
15 
16 // UNSUPPORTED: no-threads
17 // UNSUPPORTED: no-exceptions
18 // UNSUPPORTED: c++03, c++11, c++14, c++17
19 // XFAIL: availability-synchronization_library-missing
20 
21 // void join();
22 
23 #include <atomic>
24 #include <cassert>
25 #include <chrono>
26 #include <concepts>
27 #include <functional>
28 #include <system_error>
29 #include <thread>
30 #include <type_traits>
31 #include <vector>
32 
33 #include "make_test_thread.h"
34 #include "test_macros.h"
35 
36 int main(int, char**) {
37   // resource_deadlock_would_occur - if deadlock is detected or get_id() == this_thread::get_id().
38   {
39     std::function<void()> f;
40     std::atomic_bool start = false;
41     std::atomic_bool done  = false;
42 
43     std::jthread jt = support::make_test_jthread([&] {
44       start.wait(false);
45       f();
46       done = true;
47       done.notify_all();
48     });
49 
50     f = [&] {
51       try {
52         jt.join();
53         assert(false);
54       } catch (const std::system_error& err) {
55         assert(err.code() == std::errc::resource_deadlock_would_occur);
56       }
57     };
58     start = true;
59     start.notify_all();
60     done.wait(false);
61   }
62 
63   return 0;
64 }
65