Skip to content
0.2.0-dev Download ZIP
295 lines ·9.5 KB Raw
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"
22
23#include <pthread.h>
24
25#ifdef HAVE_PTHREAD_NP_H
26#include <pthread_np.h>
27#endif
28
29#ifdef HAVE_SIGNAL_H
30#include <signal.h>
31#endif
32#include <errno.h>
33
34#ifdef SDL_PLATFORM_LINUX
35#include <sys/time.h>
36#include <sys/resource.h>
37#include <sys/syscall.h>
38#include <unistd.h>
39
40#include "../../core/linux/SDL_dbus.h"
41#endif // SDL_PLATFORM_LINUX
42
43#if (defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS)) && defined(HAVE_DLOPEN)
44#include <dlfcn.h>
45#ifndef RTLD_DEFAULT
46#define RTLD_DEFAULT NULL
47#endif
48#endif
49
50#include "../SDL_thread_c.h"
51#include "../SDL_systhread.h"
52#ifdef SDL_PLATFORM_ANDROID
53#include "../../core/android/SDL_android.h"
54#endif
55
56#ifdef SDL_PLATFORM_HAIKU
57#include <kernel/OS.h>
58#endif
59
60#ifdef HAVE_SIGNAL_H
61// List of signals to mask in the subthreads
62static const int sig_list[] = {
63 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGWINCH,
64 SIGVTALRM, SIGPROF, 0
65};
66#endif
67
68static void *RunThread(void *data)
69{
70#ifdef SDL_PLATFORM_ANDROID
71 Android_JNI_SetupThread();
72#endif
73 SDL_RunThread((SDL_Thread *)data);
74 return NULL;
75}
76
77#if (defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS)) && defined(HAVE_DLOPEN)
78static bool checked_setname = false;
79static int (*ppthread_setname_np)(const char *) = NULL;
80#elif (defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID)) && defined(HAVE_DLOPEN)
81static bool checked_setname = false;
82static int (*ppthread_setname_np)(pthread_t, const char *) = NULL;
83#endif
84bool SDL_SYS_CreateThread(SDL_Thread *thread,
85 SDL_FunctionPointer pfnBeginThread,
86 SDL_FunctionPointer pfnEndThread)
87{
88 pthread_attr_t type;
89
90// do this here before any threads exist, so there's no race condition.
91#if (defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID)) && defined(HAVE_DLOPEN)
92 if (!checked_setname) {
93 void *fn = dlsym(RTLD_DEFAULT, "pthread_setname_np");
94#if defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS)
95 ppthread_setname_np = (int (*)(const char *))fn;
96#elif defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID)
97 ppthread_setname_np = (int (*)(pthread_t, const char *))fn;
98#endif
99 checked_setname = true;
100 }
101 #endif
102
103 // Set the thread attributes
104 if (pthread_attr_init(&type) != 0) {
105 return SDL_SetError("Couldn't initialize pthread attributes");
106 }
107 pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
108
109 // Set caller-requested stack size. Otherwise: use the system default.
110 if (thread->stacksize) {
111 pthread_attr_setstacksize(&type, thread->stacksize);
112 }
113
114 // Create the thread and go!
115 if (pthread_create(&thread->handle, &type, RunThread, thread) != 0) {
116 return SDL_SetError("Not enough resources to create thread");
117 }
118
119 thread->threadid = (SDL_ThreadID) thread->handle; // the SDL thread ID is just the pthread_t.
120
121 return true;
122}
123
124void SDL_SYS_SetupThread(const char *name)
125{
126#ifdef HAVE_SIGNAL_H
127 int i;
128 sigset_t mask;
129#endif
130
131 if (name) {
132#if (defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID)) && defined(HAVE_DLOPEN)
133 SDL_assert(checked_setname);
134 if (ppthread_setname_np) {
135#if defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS)
136 ppthread_setname_np(name);
137#elif defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID)
138 if (ppthread_setname_np(pthread_self(), name) == ERANGE) {
139 char namebuf[16]; // Limited to 16 char
140 SDL_strlcpy(namebuf, name, sizeof(namebuf));
141 ppthread_setname_np(pthread_self(), namebuf);
142 }
143#endif
144 }
145#elif defined(HAVE_PTHREAD_SETNAME_NP)
146#ifdef SDL_PLATFORM_NETBSD
147 pthread_setname_np(pthread_self(), "%s", name);
148#else
149 if (pthread_setname_np(pthread_self(), name) == ERANGE) {
150 char namebuf[16]; // Limited to 16 char
151 SDL_strlcpy(namebuf, name, sizeof(namebuf));
152 pthread_setname_np(pthread_self(), namebuf);
153 }
154#endif
155#elif defined(HAVE_PTHREAD_SET_NAME_NP)
156 pthread_set_name_np(pthread_self(), name);
157#elif defined(SDL_PLATFORM_HAIKU)
158 // The docs say the thread name can't be longer than B_OS_NAME_LENGTH.
159 char namebuf[B_OS_NAME_LENGTH];
160 SDL_strlcpy(namebuf, name, sizeof(namebuf));
161 rename_thread(find_thread(NULL), namebuf);
162#endif
163 }
164
165#ifdef HAVE_SIGNAL_H
166 // Mask asynchronous signals for this thread
167 sigemptyset(&mask);
168 for (i = 0; sig_list[i]; ++i) {
169 sigaddset(&mask, sig_list[i]);
170 }
171 pthread_sigmask(SIG_BLOCK, &mask, NULL);
172#endif
173
174#ifdef PTHREAD_CANCEL_ASYNCHRONOUS
175 // Allow ourselves to be asynchronously cancelled
176 {
177 int oldstate;
178 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
179 }
180#endif
181}
182
183SDL_ThreadID SDL_GetCurrentThreadID(void)
184{
185 return (SDL_ThreadID)pthread_self();
186}
187
188bool SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
189{
190#ifdef SDL_PLATFORM_RISCOS
191 // FIXME: Setting thread priority does not seem to be supported
192 return true;
193#else
194 struct sched_param sched;
195 int policy;
196 int pri_policy;
197 pthread_t thread = pthread_self();
198 const char *policyhint = SDL_GetHint(SDL_HINT_THREAD_PRIORITY_POLICY);
199 const bool timecritical_realtime_hint = SDL_GetHintBoolean(SDL_HINT_THREAD_FORCE_REALTIME_TIME_CRITICAL, false);
200
201 if (pthread_getschedparam(thread, &policy, &sched) != 0) {
202 return SDL_SetError("pthread_getschedparam() failed");
203 }
204
205 /* Higher priority levels may require changing the pthread scheduler policy
206 * for the thread. SDL will make such changes by default but there is
207 * also a hint allowing that behavior to be overridden. */
208 switch (priority) {
209 case SDL_THREAD_PRIORITY_LOW:
210 case SDL_THREAD_PRIORITY_NORMAL:
211 pri_policy = SCHED_OTHER;
212 break;
213 case SDL_THREAD_PRIORITY_HIGH:
214 case SDL_THREAD_PRIORITY_TIME_CRITICAL:
215#if defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS)
216 // Apple requires SCHED_RR for high priority threads
217 pri_policy = SCHED_RR;
218 break;
219#else
220 pri_policy = SCHED_OTHER;
221 break;
222#endif
223 default:
224 pri_policy = policy;
225 break;
226 }
227
228 if (timecritical_realtime_hint && priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
229 pri_policy = SCHED_RR;
230 }
231
232 if (policyhint) {
233 if (SDL_strcmp(policyhint, "current") == 0) {
234 // Leave current thread scheduler policy unchanged
235 } else if (SDL_strcmp(policyhint, "other") == 0) {
236 policy = SCHED_OTHER;
237 } else if (SDL_strcmp(policyhint, "rr") == 0) {
238 policy = SCHED_RR;
239 } else if (SDL_strcmp(policyhint, "fifo") == 0) {
240 policy = SCHED_FIFO;
241 } else {
242 policy = pri_policy;
243 }
244 } else {
245 policy = pri_policy;
246 }
247
248#ifdef SDL_PLATFORM_LINUX
249 {
250 pid_t linuxTid = syscall(SYS_gettid);
251 return SDL_SetLinuxThreadPriorityAndPolicy(linuxTid, priority, policy);
252 }
253#else
254 if (priority == SDL_THREAD_PRIORITY_LOW) {
255 sched.sched_priority = sched_get_priority_min(policy);
256 } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
257 sched.sched_priority = sched_get_priority_max(policy);
258 } else {
259 int min_priority = sched_get_priority_min(policy);
260 int max_priority = sched_get_priority_max(policy);
261
262#if defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS)
263 if (min_priority == 15 && max_priority == 47) {
264 // Apple has a specific set of thread priorities
265 if (priority == SDL_THREAD_PRIORITY_HIGH) {
266 sched.sched_priority = 45;
267 } else {
268 sched.sched_priority = 37;
269 }
270 } else
271#endif // SDL_PLATFORM_MACOS || SDL_PLATFORM_IOS || SDL_PLATFORM_TVOS
272 {
273 sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
274 if (priority == SDL_THREAD_PRIORITY_HIGH) {
275 sched.sched_priority += ((max_priority - min_priority) / 4);
276 }
277 }
278 }
279 if (pthread_setschedparam(thread, policy, &sched) != 0) {
280 return SDL_SetError("pthread_setschedparam() failed");
281 }
282 return true;
283#endif // linux
284#endif // #if SDL_PLATFORM_RISCOS
285}
286
287void SDL_SYS_WaitThread(SDL_Thread *thread)
288{
289 pthread_join(thread->handle, NULL);
290}
291
292void SDL_SYS_DetachThread(SDL_Thread *thread)
293{
294 pthread_detach(thread->handle);
295}