xref: /llvm-project/libcxx/test/support/make_test_thread.h (revision 99174842aedf9aa571bdb23d07566965ff3c21e3)
156462801SLouis Dionne //===----------------------------------------------------------------------===//
256462801SLouis Dionne //
356462801SLouis Dionne // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
456462801SLouis Dionne // See https://llvm.org/LICENSE.txt for license information.
556462801SLouis Dionne // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
656462801SLouis Dionne //
756462801SLouis Dionne //===----------------------------------------------------------------------===//
8480cd780SLouis Dionne 
956462801SLouis Dionne #ifndef TEST_SUPPORT_MAKE_TEST_THREAD_H
1056462801SLouis Dionne #define TEST_SUPPORT_MAKE_TEST_THREAD_H
1156462801SLouis Dionne 
1256462801SLouis Dionne #include <thread>
1356462801SLouis Dionne #include <utility>
1456462801SLouis Dionne 
15475e1543SLouis Dionne #include "test_macros.h"
16475e1543SLouis Dionne 
1756462801SLouis Dionne namespace support {
1856462801SLouis Dionne 
19475e1543SLouis Dionne // These functions are used to mock the creation of threads within the test suite.
20475e1543SLouis Dionne //
21475e1543SLouis Dionne // This provides a vendor-friendly way of making the test suite work even on platforms
22475e1543SLouis Dionne // where the standard thread constructors don't work (e.g. embedded environments where
23475e1543SLouis Dionne // creating a thread requires additional information like setting attributes).
24475e1543SLouis Dionne //
25475e1543SLouis Dionne // Vendors can keep a downstream diff in this file to create threads however they
26475e1543SLouis Dionne // need on their platform, and the majority of the test suite will work out of the
27475e1543SLouis Dionne // box. Of course, tests that exercise the standard thread constructors won't work,
28475e1543SLouis Dionne // but any other test that only creates threads as a side effect of testing should
29475e1543SLouis Dionne // work if they use the utilities in this file.
30475e1543SLouis Dionne 
3156462801SLouis Dionne template <class F, class... Args>
3256462801SLouis Dionne std::thread make_test_thread(F&& f, Args&&... args) {
3356462801SLouis Dionne   return std::thread(std::forward<F>(f), std::forward<Args>(args)...);
3456462801SLouis Dionne }
3556462801SLouis Dionne 
36*99174842SLouis Dionne #if TEST_STD_VER >= 20
37475e1543SLouis Dionne #  ifdef _LIBCPP_VERSION
38475e1543SLouis Dionne #    define TEST_AVAILABILITY_SYNC _LIBCPP_AVAILABILITY_SYNC
39475e1543SLouis Dionne #  else
40475e1543SLouis Dionne #    define TEST_AVAILABILITY_SYNC
41475e1543SLouis Dionne #  endif
42475e1543SLouis Dionne 
43475e1543SLouis Dionne template <class F, class... Args>
44475e1543SLouis Dionne TEST_AVAILABILITY_SYNC std::jthread make_test_jthread(F&& f, Args&&... args) {
45475e1543SLouis Dionne   return std::jthread(std::forward<F>(f), std::forward<Args>(args)...);
46475e1543SLouis Dionne }
47475e1543SLouis Dionne #endif
48475e1543SLouis Dionne 
49953af0e7SLouis Dionne } // namespace support
5056462801SLouis Dionne 
5156462801SLouis Dionne #endif // TEST_SUPPORT_MAKE_TEST_THREAD_H
52