SDL 2.0
SDL_threadprio.c
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2019 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#ifdef __LINUX__
24
25#include "SDL_stdinc.h"
26
27#if !SDL_THREADS_DISABLED
28#include <sys/time.h>
29#include <sys/resource.h>
30#include <pthread.h>
31#include "SDL_system.h"
32
33#if SDL_USE_LIBDBUS
34#include "SDL_dbus.h"
35/* d-bus queries to org.freedesktop.RealtimeKit1. */
36#define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
37#define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
38#define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"
39
40static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
41static Sint32 rtkit_min_nice_level = -20;
42
43static void
44rtkit_initialize()
45{
46 SDL_DBusContext *dbus = SDL_DBus_GetContext();
47
48 /* Try getting minimum nice level: this is often greater than PRIO_MIN (-20). */
49 if (!dbus || !SDL_DBus_QueryPropertyOnConnection(dbus->system_conn, RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MinNiceLevel",
50 DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
51 rtkit_min_nice_level = -20;
52 }
53}
54
55static SDL_bool
56rtkit_setpriority(pid_t thread, int nice_level)
57{
58 Uint64 ui64 = (Uint64)thread;
59 Sint32 si32 = (Sint32)nice_level;
60 SDL_DBusContext *dbus = SDL_DBus_GetContext();
61
62 pthread_once(&rtkit_initialize_once, rtkit_initialize);
63
64 if (si32 < rtkit_min_nice_level)
65 si32 = rtkit_min_nice_level;
66
67 if (!dbus || !SDL_DBus_CallMethodOnConnection(dbus->system_conn,
68 RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MakeThreadHighPriority",
69 DBUS_TYPE_UINT64, &ui64, DBUS_TYPE_INT32, &si32, DBUS_TYPE_INVALID,
70 DBUS_TYPE_INVALID)) {
71 return SDL_FALSE;
72 }
73 return SDL_TRUE;
74}
75#endif /* dbus */
76#endif /* threads */
77
78
79/* this is a public symbol, so it has to exist even if threads are disabled. */
80int
81SDL_LinuxSetThreadPriority(Sint64 threadID, int priority)
82{
83#if SDL_THREADS_DISABLED
84 return SDL_Unsupported();
85#else
86 if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) == 0) {
87 return 0;
88 }
89
90#if SDL_USE_LIBDBUS
91 /* Note that this fails if you're trying to set high priority
92 and you don't have root permission. BUT DON'T RUN AS ROOT!
93
94 You can grant the ability to increase thread priority by
95 running the following command on your application binary:
96 sudo setcap 'cap_sys_nice=eip' <application>
97
98 Let's try setting priority with RealtimeKit...
99
100 README and sample code at: http://git.0pointer.net/rtkit.git
101 */
102 if (rtkit_setpriority((pid_t)threadID, priority)) {
103 return 0;
104 }
105#endif
106
107 return SDL_SetError("setpriority() failed");
108#endif
109}
110
111#endif /* __LINUX__ */
112
113/* vi: set ts=4 sw=4 expandtab: */
#define SDL_SetError
#define SDL_LinuxSetThreadPriority
#define SDL_Unsupported()
Definition: SDL_error.h:53
SDL_bool
Definition: SDL_stdinc.h:162
@ SDL_TRUE
Definition: SDL_stdinc.h:164
@ SDL_FALSE
Definition: SDL_stdinc.h:163
int32_t Sint32
Definition: SDL_stdinc.h:197
uint64_t Uint64
Definition: SDL_stdinc.h:216
int64_t Sint64
Definition: SDL_stdinc.h:210