xref: /llvm-project/libcxx/test/std/thread/thread.jthread/join.deadlock.pass.cpp (revision 121ed5c1985356436d0040dbe81bca26992b1fae)
1695138caSHui //===----------------------------------------------------------------------===//
2695138caSHui //
3695138caSHui // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4695138caSHui // See https://llvm.org/LICENSE.txt for license information.
5695138caSHui // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6695138caSHui //
7695138caSHui //===----------------------------------------------------------------------===//
8695138caSHui //
9695138caSHui // Windows cannot detect the deadlock. Instead of throwing system_error,
10695138caSHui // it would dead lock the test
11695138caSHui // UNSUPPORTED: windows
12695138caSHui 
13695138caSHui // TSAN bug: https://github.com/llvm/llvm-project/issues/66537
14695138caSHui // UNSUPPORTED: tsan
15695138caSHui 
16695138caSHui // UNSUPPORTED: no-threads
17695138caSHui // UNSUPPORTED: no-exceptions
18695138caSHui // UNSUPPORTED: c++03, c++11, c++14, c++17
19695138caSHui // XFAIL: availability-synchronization_library-missing
20695138caSHui 
21695138caSHui // void join();
22695138caSHui 
23695138caSHui #include <atomic>
24695138caSHui #include <cassert>
25695138caSHui #include <chrono>
26695138caSHui #include <concepts>
27695138caSHui #include <functional>
28695138caSHui #include <system_error>
29695138caSHui #include <thread>
30695138caSHui #include <type_traits>
31695138caSHui #include <vector>
32695138caSHui 
33*475e1543SLouis Dionne #include "make_test_thread.h"
34695138caSHui #include "test_macros.h"
35695138caSHui 
36695138caSHui int main(int, char**) {
37695138caSHui   // resource_deadlock_would_occur - if deadlock is detected or get_id() == this_thread::get_id().
38695138caSHui   {
39695138caSHui     std::function<void()> f;
40695138caSHui     std::atomic_bool start = false;
41695138caSHui     std::atomic_bool done  = false;
42695138caSHui 
43*475e1543SLouis Dionne     std::jthread jt = support::make_test_jthread([&] {
44695138caSHui       start.wait(false);
45695138caSHui       f();
46695138caSHui       done = true;
47695138caSHui       done.notify_all();
48*475e1543SLouis Dionne     });
49695138caSHui 
50695138caSHui     f = [&] {
51695138caSHui       try {
52695138caSHui         jt.join();
53695138caSHui         assert(false);
54695138caSHui       } catch (const std::system_error& err) {
55695138caSHui         assert(err.code() == std::errc::resource_deadlock_would_occur);
56695138caSHui       }
57695138caSHui     };
58695138caSHui     start = true;
59695138caSHui     start.notify_all();
60695138caSHui     done.wait(false);
61695138caSHui   }
62695138caSHui 
63695138caSHui   return 0;
64695138caSHui }
65