| 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: libcpp-has-no-experimental-stop_token |
| 19 |
// UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 20 |
// XFAIL: availability-synchronization_library-missing |
| 21 |
|
| 22 |
// void join(); |
| 23 |
|
| 24 |
#include <atomic> |
| 25 |
#include <cassert> |
| 26 |
#include <chrono> |
| 27 |
#include <concepts> |
| 28 |
#include <functional> |
| 29 |
#include <system_error> |
| 30 |
#include <thread> |
| 31 |
#include <type_traits> |
| 32 |
#include <vector> |
| 33 |
|
| 34 |
#include "make_test_thread.h" |
| 35 |
#include "test_macros.h" |
| 36 |
|
| 37 |
int main(int, char**) { |
| 38 |
// resource_deadlock_would_occur - if deadlock is detected or get_id() == this_thread::get_id(). |
| 39 |
{ |
| 40 |
std::function<void()> f; |
| 41 |
std::atomic_bool start = false; |
| 42 |
std::atomic_bool done = false; |
| 43 |
|
| 44 |
std::jthread jt = support::make_test_jthread([&] { |
| 45 |
start.wait(false); |
| 46 |
f(); |
| 47 |
done = true; |
| 48 |
done.notify_all(); |
| 49 |
}); |
| 50 |
|
| 51 |
f = [&] { |
| 52 |
try { |
| 53 |
jt.join(); |
| 54 |
assert(false); |
| 55 |
} catch (const std::system_error& err) { |
| 56 |
assert(err.code() == std::errc::resource_deadlock_would_occur); |
| 57 |
} |
| 58 |
}; |
| 59 |
start = true; |
| 60 |
start.notify_all(); |
| 61 |
done.wait(false); |
| 62 |
} |
| 63 |
|
| 64 |
return 0; |
| 65 |
} |