1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3
4#include "thread.h"
5
6#include "dbg.h"
7#include "windows.h"
8
9#if defined(CONF_FAMILY_UNIX)
10#include <pthread.h>
11#include <sched.h>
12
13#include <cstdlib>
14
15#if defined(CONF_PLATFORM_MACOS)
16#if defined(__MAC_10_10) && __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_10
17#include <pthread/qos.h>
18#endif
19#endif
20
21#if defined(CONF_PLATFORM_MACOS) || defined(CONF_PLATFORM_IOS)
22#include <mach/task_policy.h>
23#include <mach/thread_act.h>
24#include <mach/thread_policy.h>
25#elif defined(CONF_PLATFORM_LINUX) || defined(CONF_PLATFORM_ANDROID)
26#include <sys/prctl.h>
27#endif
28
29#if defined(CONF_PLATFORM_EMSCRIPTEN)
30#include <emscripten/emscripten.h>
31#endif
32
33#elif defined(CONF_FAMILY_WINDOWS)
34#include <windows.h>
35#else
36#error NOT IMPLEMENTED
37#endif
38
39struct THREAD_RUN
40{
41 void (*threadfunc)(void *);
42 void *u;
43};
44
45#if defined(CONF_FAMILY_UNIX)
46static void *thread_run(void *user)
47#elif defined(CONF_FAMILY_WINDOWS)
48static DWORD __stdcall thread_run(void *user)
49#else
50#error not implemented
51#endif
52{
53#if defined(CONF_FAMILY_WINDOWS)
54 CWindowsComLifecycle WindowsComLifecycle(false);
55#endif
56 struct THREAD_RUN *data = (THREAD_RUN *)user;
57 void (*threadfunc)(void *) = data->threadfunc;
58 void *u = data->u;
59 free(ptr: data);
60 threadfunc(u);
61#if defined(CONF_FAMILY_UNIX)
62 return nullptr;
63#elif defined(CONF_FAMILY_WINDOWS)
64 return 0;
65#else
66#error not implemented
67#endif
68}
69
70void *thread_init(void (*threadfunc)(void *), void *u, const char *name)
71{
72 struct THREAD_RUN *data = (THREAD_RUN *)malloc(size: sizeof(*data));
73 data->threadfunc = threadfunc;
74 data->u = u;
75#if defined(CONF_FAMILY_UNIX)
76 {
77 pthread_attr_t attr;
78 dbg_assert(pthread_attr_init(&attr) == 0, "pthread_attr_init failure");
79#if defined(CONF_PLATFORM_MACOS) && defined(__MAC_10_10) && __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_10
80 dbg_assert(pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INTERACTIVE, 0) == 0, "pthread_attr_set_qos_class_np failure");
81#endif
82 pthread_t id;
83 dbg_assert(pthread_create(&id, &attr, thread_run, data) == 0, "pthread_create failure");
84#if defined(CONF_PLATFORM_EMSCRIPTEN)
85 // Return control to the browser's main thread to allow the pthread to be started,
86 // otherwise we deadlock when waiting for a thread immediately after starting it.
87 emscripten_sleep(0);
88#endif
89 return (void *)id;
90 }
91#elif defined(CONF_FAMILY_WINDOWS)
92 HANDLE thread = CreateThread(nullptr, 0, thread_run, data, 0, nullptr);
93 dbg_assert(thread != nullptr, "CreateThread failure");
94 HMODULE kernel_base_handle;
95 if(GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, L"KernelBase.dll", &kernel_base_handle))
96 {
97 // Intentional
98#ifdef __MINGW32__
99#pragma GCC diagnostic push
100#pragma GCC diagnostic ignored "-Wcast-function-type"
101#endif
102 auto set_thread_description_function = reinterpret_cast<HRESULT(WINAPI *)(HANDLE, PCWSTR)>(GetProcAddress(kernel_base_handle, "SetThreadDescription"));
103#ifdef __MINGW32__
104#pragma GCC diagnostic pop
105#endif
106 if(set_thread_description_function)
107 set_thread_description_function(thread, windows_utf8_to_wide(name).c_str());
108 }
109 return thread;
110#else
111#error not implemented
112#endif
113}
114
115void thread_wait(void *thread)
116{
117#if defined(CONF_PLATFORM_EMSCRIPTEN)
118 // TODO: Remove this workaround when https://github.com/emscripten-core/emscripten/issues/9910 is fixed.
119 while(true)
120 {
121 const int join_result = pthread_tryjoin_np((pthread_t)thread, nullptr);
122 if(join_result == 0)
123 {
124 break;
125 }
126 dbg_assert(join_result == EBUSY, "pthread_tryjoin_np failure");
127 // Busy waiting so we can periodically yield control to browser's
128 // main thread because blocking on the main thread is very bad.
129 emscripten_sleep(10);
130 }
131#elif defined(CONF_FAMILY_UNIX)
132 dbg_assert(pthread_join((pthread_t)thread, nullptr) == 0, "pthread_join failure");
133#elif defined(CONF_FAMILY_WINDOWS)
134 dbg_assert(WaitForSingleObject((HANDLE)thread, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failure");
135 dbg_assert(CloseHandle(thread), "CloseHandle failure");
136#else
137#error not implemented
138#endif
139}
140
141void thread_yield()
142{
143#if defined(CONF_FAMILY_UNIX)
144 dbg_assert(sched_yield() == 0, "sched_yield failure");
145#elif defined(CONF_FAMILY_WINDOWS)
146 Sleep(0);
147#else
148#error not implemented
149#endif
150}
151
152void thread_request_precise_wakeups()
153{
154#if defined(CONF_PLATFORM_MACOS) || defined(CONF_PLATFORM_IOS)
155 // Not asserted, the system is free to decline the policy.
156 thread_latency_qos_policy_data_t policy = {LATENCY_QOS_TIER_0};
157 thread_policy_set(pthread_mach_thread_np(pthread_self()), THREAD_LATENCY_QOS_POLICY, (thread_policy_t)&policy, THREAD_LATENCY_QOS_POLICY_COUNT);
158#elif defined(CONF_PLATFORM_LINUX) || defined(CONF_PLATFORM_ANDROID)
159 dbg_assert(prctl(PR_SET_TIMERSLACK, 1) == 0, "prctl(PR_SET_TIMERSLACK) failure");
160#endif
161}
162
163void thread_detach(void *thread)
164{
165#if defined(CONF_FAMILY_UNIX)
166 dbg_assert(pthread_detach((pthread_t)thread) == 0, "pthread_detach failure");
167#elif defined(CONF_FAMILY_WINDOWS)
168 dbg_assert(CloseHandle(thread), "CloseHandle failure");
169#else
170#error not implemented
171#endif
172}
173