brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.5 KiB · c6eb069 Raw
64 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// Windows cannot detect the deadlock. Instead of throwing system_error,10// it would dead lock the test11// UNSUPPORTED: windows12 13// TSAN bug: https://llvm.org/PR6653714// UNSUPPORTED: tsan15 16// UNSUPPORTED: no-threads17// UNSUPPORTED: no-exceptions18// UNSUPPORTED: c++03, c++11, c++14, c++1719 20// void join();21 22#include <atomic>23#include <cassert>24#include <chrono>25#include <concepts>26#include <functional>27#include <system_error>28#include <thread>29#include <type_traits>30#include <vector>31 32#include "make_test_thread.h"33#include "test_macros.h"34 35int main(int, char**) {36  // resource_deadlock_would_occur - if deadlock is detected or get_id() == this_thread::get_id().37  {38    std::function<void()> f;39    std::atomic_bool start = false;40    std::atomic_bool done  = false;41 42    std::jthread jt = support::make_test_jthread([&] {43      start.wait(false);44      f();45      done = true;46      done.notify_all();47    });48 49    f = [&] {50      try {51        jt.join();52        assert(false);53      } catch (const std::system_error& err) {54        assert(err.code() == std::errc::resource_deadlock_would_occur);55      }56    };57    start = true;58    start.notify_all();59    done.wait(false);60  }61 62  return 0;63}64