22 #include "../../SDL_internal.h"
27 #include <pthread_np.h>
34 #include <sys/resource.h>
35 #include <sys/syscall.h>
39 #include "../../core/linux/SDL_dbus.h"
42 #if defined(__LINUX__) || defined(__MACOSX__) || defined(__IPHONEOS__)
45 #define RTLD_DEFAULT NULL
52 #include "../SDL_thread_c.h"
53 #include "../SDL_systhread.h"
55 #include "../../core/android/SDL_android.h"
59 #include <kernel/OS.h>
67 SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
82 #if defined(__MACOSX__) || defined(__IPHONEOS__)
84 static int (*ppthread_setname_np)(
const char*) =
NULL;
85 #elif defined(__LINUX__)
87 static int (*ppthread_setname_np)(pthread_t,
const char*) =
NULL;
95 #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
96 if (!checked_setname) {
97 void *fn = dlsym(RTLD_DEFAULT,
"pthread_setname_np");
98 #if defined(__MACOSX__) || defined(__IPHONEOS__)
99 ppthread_setname_np = (int(*)(
const char*)) fn;
100 #elif defined(__LINUX__)
101 ppthread_setname_np = (int(*)(pthread_t,
const char*)) fn;
108 if (pthread_attr_init(&
type) != 0) {
109 return SDL_SetError(
"Couldn't initialize pthread attributes");
111 pthread_attr_setdetachstate(&
type, PTHREAD_CREATE_JOINABLE);
115 pthread_attr_setstacksize(&
type, (
size_t) thread->
stacksize);
120 return SDL_SetError(
"Not enough resources to create thread");
129 #if !defined(__NACL__)
135 #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
137 if (ppthread_setname_np !=
NULL) {
138 #if defined(__MACOSX__) || defined(__IPHONEOS__)
139 ppthread_setname_np(
name);
140 #elif defined(__LINUX__)
141 ppthread_setname_np(pthread_self(),
name);
144 #elif HAVE_PTHREAD_SETNAME_NP
145 #if defined(__NETBSD__)
146 pthread_setname_np(pthread_self(),
"%s",
name);
148 pthread_setname_np(pthread_self(),
name);
150 #elif HAVE_PTHREAD_SET_NAME_NP
151 pthread_set_name_np(pthread_self(),
name);
152 #elif defined(__HAIKU__)
154 char namebuf[B_OS_NAME_LENGTH];
156 namebuf[
sizeof (namebuf) - 1] =
'\0';
157 rename_thread(find_thread(
NULL), namebuf);
162 #if !defined(__NACL__)
168 pthread_sigmask(SIG_BLOCK, &
mask, 0);
172 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS
176 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
191 #define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
192 #define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
193 #define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"
195 static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
196 static Sint32 rtkit_min_nice_level = -20;
201 SDL_DBusContext *dbus = SDL_DBus_GetContext();
204 if (!dbus || !SDL_DBus_QueryPropertyOnConnection(dbus->system_conn, RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE,
"MinNiceLevel",
205 DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
206 rtkit_min_nice_level = -20;
211 rtkit_setpriority(pid_t thread,
int nice_level)
215 SDL_DBusContext *dbus = SDL_DBus_GetContext();
217 pthread_once(&rtkit_initialize_once, rtkit_initialize);
219 if (si32 < rtkit_min_nice_level)
220 si32 = rtkit_min_nice_level;
222 if (!dbus || !SDL_DBus_CallMethodOnConnection(dbus->system_conn,
223 RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE,
"MakeThreadHighPriority",
224 DBUS_TYPE_UINT64, &ui64, DBUS_TYPE_INT32, &si32, DBUS_TYPE_INVALID,
225 DBUS_TYPE_INVALID)) {
234 rtkit_setpriority(pid_t thread,
int nice_level)
244 if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) < 0) {
257 if (rtkit_setpriority((pid_t)threadID, priority) ==
SDL_FALSE) {
273 pid_t thread = syscall(SYS_gettid);
286 struct sched_param sched;
288 pthread_t thread = pthread_self();
290 if (pthread_getschedparam(thread, &
policy, &sched) != 0) {
294 sched.sched_priority = sched_get_priority_min(
policy);
296 sched.sched_priority = sched_get_priority_max(
policy);
298 int min_priority = sched_get_priority_min(
policy);
299 int max_priority = sched_get_priority_max(
policy);
300 sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
302 sched.sched_priority += ((max_priority - min_priority) / 4);
305 if (pthread_setschedparam(thread,
policy, &sched) != 0) {
315 pthread_join(thread->
handle, 0);
321 pthread_detach(thread->
handle);