1/*
2 * LegacyClonk
3 *
4 * Copyright (c) 2023, The LegacyClonk Team and contributors
5 *
6 * Distributed under the terms of the ISC license; see accompanying file
7 * "COPYING" for details.
8 *
9 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10 * See accompanying file "TRADEMARK" for details.
11 *
12 * To redistribute this file separately, substitute the full license texts
13 * for the above references.
14 */
15
16#pragma once
17
18#ifdef _WIN32
19#include "C4Com.h"
20#endif
21
22#include <string_view>
23#include <thread>
24
25namespace C4Thread
26{
27 void SetCurrentThreadName(std::string_view name);
28
29 struct CreateOptions
30 {
31 std::string_view Name;
32
33#ifdef _WIN32
34 winrt::apartment_type ApartmentType{winrt::apartment_type::multi_threaded};
35#endif
36 };
37
38 template<typename Func, typename... Args>
39 std::thread Create(CreateOptions options, Func &&func, Args &&...args)
40 {
41 return std::thread([](const CreateOptions &options, Func &&func, Args &&...args) mutable
42 {
43 SetCurrentThreadName(options.Name);
44
45#ifdef _WIN32
46 const C4Com com{options.ApartmentType};
47#endif
48
49 std::move(func)(std::forward<Args>(args)...);
50 },
51 std::move(options), std::move(func), std::forward<Args>(args)...);
52 }
53}
54