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#include "C4Thread.h"
17
18#ifdef _WIN32
19#include "C4Windows.h"
20#include "StdStringEncodingConverter.h"
21#else
22#include <pthread.h>
23#endif
24
25#include <string>
26
27void C4Thread::SetCurrentThreadName(const std::string_view name)
28{
29#ifdef _WIN32
30 static auto *const setThreadDescription = reinterpret_cast<HRESULT(__stdcall *)(HANDLE, PCWSTR)>(GetProcAddress(GetModuleHandle(L"KernelBase.dll"), "SetThreadDescription"));
31
32 if (setThreadDescription)
33 {
34 setThreadDescription(GetCurrentThread(), StdStringEncodingConverter::WinAcpToUtf16(name).c_str());
35 }
36#elif defined(__linux__)
37 pthread_setname_np(target_thread: pthread_self(), name: std::string{name, 0, 15}.c_str());
38#elif defined(__APPLE__)
39 pthread_setname_np(std::string{name}.c_str());
40#endif
41}
42