SDL  2.0
SDL_threadprio.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 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_error.h"
26 #include "SDL_stdinc.h"
27 
28 #if !SDL_THREADS_DISABLED
29 #include <sys/time.h>
30 #include <sys/resource.h>
31 #include <pthread.h>
32 #include "SDL_system.h"
33 
34 #include "SDL_dbus.h"
35 
36 #if SDL_USE_LIBDBUS
37 /* d-bus queries to org.freedesktop.RealtimeKit1. */
38 #define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
39 #define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
40 #define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"
41 
42 static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
43 static Sint32 rtkit_min_nice_level = -20;
44 
45 static void
46 rtkit_initialize()
47 {
48  SDL_DBusContext *dbus = SDL_DBus_GetContext();
49 
50  /* Try getting minimum nice level: this is often greater than PRIO_MIN (-20). */
51  if (!dbus || !SDL_DBus_QueryPropertyOnConnection(dbus->system_conn, RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MinNiceLevel",
52  DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
53  rtkit_min_nice_level = -20;
54  }
55 }
56 
57 static SDL_bool
58 rtkit_setpriority(pid_t thread, int nice_level)
59 {
60  Uint64 ui64 = (Uint64)thread;
61  Sint32 si32 = (Sint32)nice_level;
62  SDL_DBusContext *dbus = SDL_DBus_GetContext();
63 
64  pthread_once(&rtkit_initialize_once, rtkit_initialize);
65 
66  if (si32 < rtkit_min_nice_level)
67  si32 = rtkit_min_nice_level;
68 
69  if (!dbus || !SDL_DBus_CallMethodOnConnection(dbus->system_conn,
70  RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MakeThreadHighPriority",
71  DBUS_TYPE_UINT64, &ui64, DBUS_TYPE_INT32, &si32, DBUS_TYPE_INVALID,
72  DBUS_TYPE_INVALID)) {
73  return SDL_FALSE;
74  }
75  return SDL_TRUE;
76 }
77 #endif /* dbus */
78 #endif /* threads */
79 
80 
81 /* this is a public symbol, so it has to exist even if threads are disabled. */
82 int
83 SDL_LinuxSetThreadPriority(Sint64 threadID, int priority)
84 {
85 #if SDL_THREADS_DISABLED
86  return SDL_Unsupported();
87 #else
88  if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) == 0) {
89  return 0;
90  }
91 
92 #if SDL_USE_LIBDBUS
93  /* Note that this fails you most likely:
94  * Have your process's scheduler incorrectly configured.
95  See the requirements at:
96  http://git.0pointer.net/rtkit.git/tree/README#n16
97  * Encountered dbus/polkit security restrictions. Note
98  that the RealtimeKit1 dbus endpoint is inaccessible
99  over ssh connections for most common distro configs.
100  You might want to check your local config for details:
101  /usr/share/polkit-1/actions/org.freedesktop.RealtimeKit1.policy
102 
103  README and sample code at: http://git.0pointer.net/rtkit.git
104  */
105  if (rtkit_setpriority((pid_t)threadID, priority)) {
106  return 0;
107  }
108 #endif
109 
110  return SDL_SetError("setpriority() failed");
111 #endif
112 }
113 
114 #endif /* __LINUX__ */
115 
116 /* vi: set ts=4 sw=4 expandtab: */
Sint32
int32_t Sint32
Definition: SDL_stdinc.h:197
Sint64
int64_t Sint64
Definition: SDL_stdinc.h:210
SDL_error.h
SDL_LinuxSetThreadPriority
#define SDL_LinuxSetThreadPriority
Definition: SDL_dynapi_overrides.h:673
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
SDL_dbus.h
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
Uint64
uint64_t Uint64
Definition: SDL_stdinc.h:216
SDL_system.h
SDL_stdinc.h
SDL_Unsupported
#define SDL_Unsupported()
Definition: SDL_error.h:53
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:161