65 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// UNSUPPORTED: no-threads, no-exceptions10 11// ASan seems to try to create threadsm which obviously doesn't work in this test.12// UNSUPPORTED: asan, hwasan13 14// UNSUPPORTED: c++0315 16// There is no way to limit the number of threads on windows17// UNSUPPORTED: windows18 19// AIX, macOS and FreeBSD seem to limit the number of processes, not threads via RLIMIT_NPROC20// XFAIL: target={{.+}}-aix{{.*}}21// XFAIL: target={{.+}}-apple-{{.*}}22// XFAIL: freebsd23 24// z/OS does not have mechanism to limit the number of threads25// XFAIL: target={{.+}}-zos{{.*}}26 27// This test makes sure that we fail gracefully in care the thread creation fails. This is only reliably possible on28// systems that allow limiting the number of threads that can be created. See https://llvm.org/PR125428 for more details29 30#include <cassert>31#include <future>32#include <system_error>33 34#if __has_include(<sys/resource.h>)35# include <sys/resource.h>36# ifdef RLIMIT_NPROC37void force_thread_creation_failure() {38 rlimit lim = {1, 1};39 assert(setrlimit(RLIMIT_NPROC, &lim) == 0);40}41# else42# error "No known way to force only one thread being available"43# endif44#else45# error "No known way to force only one thread being available"46#endif47 48int main(int, char**) {49 force_thread_creation_failure();50 51 try {52 std::future<int> fut = std::async(std::launch::async, [] { return 1; });53 assert(false);54 } catch (const std::system_error&) {55 }56 57 try {58 std::future<void> fut = std::async(std::launch::async, [] { return; });59 assert(false);60 } catch (const std::system_error&) {61 }62 63 return 0;64}65