xref: /llvm-project/libcxx/test/support/make_test_thread.h (revision 99174842aedf9aa571bdb23d07566965ff3c21e3)
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 #ifndef TEST_SUPPORT_MAKE_TEST_THREAD_H
10 #define TEST_SUPPORT_MAKE_TEST_THREAD_H
11 
12 #include <thread>
13 #include <utility>
14 
15 #include "test_macros.h"
16 
17 namespace support {
18 
19 // These functions are used to mock the creation of threads within the test suite.
20 //
21 // This provides a vendor-friendly way of making the test suite work even on platforms
22 // where the standard thread constructors don't work (e.g. embedded environments where
23 // creating a thread requires additional information like setting attributes).
24 //
25 // Vendors can keep a downstream diff in this file to create threads however they
26 // need on their platform, and the majority of the test suite will work out of the
27 // box. Of course, tests that exercise the standard thread constructors won't work,
28 // but any other test that only creates threads as a side effect of testing should
29 // work if they use the utilities in this file.
30 
31 template <class F, class... Args>
32 std::thread make_test_thread(F&& f, Args&&... args) {
33   return std::thread(std::forward<F>(f), std::forward<Args>(args)...);
34 }
35 
36 #if TEST_STD_VER >= 20
37 #  ifdef _LIBCPP_VERSION
38 #    define TEST_AVAILABILITY_SYNC _LIBCPP_AVAILABILITY_SYNC
39 #  else
40 #    define TEST_AVAILABILITY_SYNC
41 #  endif
42 
43 template <class F, class... Args>
44 TEST_AVAILABILITY_SYNC std::jthread make_test_jthread(F&& f, Args&&... args) {
45   return std::jthread(std::forward<F>(f), std::forward<Args>(args)...);
46 }
47 #endif
48 
49 } // namespace support
50 
51 #endif // TEST_SUPPORT_MAKE_TEST_THREAD_H
52