Skip to content
Browse files

Whoops, forgot to add a new source file. :/

  • Loading branch information
icculus committed Dec 5, 2018
1 parent 6ed76ae commit c7b713714a52dac2a499a1840d2d066dacabf973
Showing with 111 additions and 0 deletions.
  1. +111 −0 src/core/linux/SDL_threadprio.c
@@ -0,0 +1,111 @@
Simple DirectMedia Layer
Copyright (C) 1997-2018 Sam Lantinga <>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
#include "../../SDL_internal.h"

#ifdef __LINUX__

#include <sys/time.h>
#include <sys/resource.h>
#include <pthread.h>
#include "SDL_system.h"

#include "SDL_dbus.h"
/* d-bus queries to org.freedesktop.RealtimeKit1. */
#define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
#define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
#define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"

static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
static Sint32 rtkit_min_nice_level = -20;

static void
SDL_DBusContext *dbus = SDL_DBus_GetContext();

/* Try getting minimum nice level: this is often greater than PRIO_MIN (-20). */
if (!dbus || !SDL_DBus_QueryPropertyOnConnection(dbus->system_conn, RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MinNiceLevel",
DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
rtkit_min_nice_level = -20;

static SDL_bool
rtkit_setpriority(pid_t thread, int nice_level)
Uint64 ui64 = (Uint64)thread;
Sint32 si32 = (Sint32)nice_level;
SDL_DBusContext *dbus = SDL_DBus_GetContext();

pthread_once(&rtkit_initialize_once, rtkit_initialize);

if (si32 < rtkit_min_nice_level)
si32 = rtkit_min_nice_level;

if (!dbus || !SDL_DBus_CallMethodOnConnection(dbus->system_conn,
return SDL_FALSE;
return SDL_TRUE;
#endif /* dbus */
#endif /* threads */

/* this is a public symbol, so it has to exist even if threads are disabled. */
SDL_LinuxSetThreadPriority(Sint64 threadID, int priority)
return SDL_Unsupported();
if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) == 0) {
return 0;

/* Note that this fails if you're trying to set high priority
and you don't have root permission. BUT DON'T RUN AS ROOT!
You can grant the ability to increase thread priority by
running the following command on your application binary:
sudo setcap 'cap_sys_nice=eip' <application>
Let's try setting priority with RealtimeKit...
README and sample code at:
if (rtkit_setpriority((pid_t)threadID, priority)) {
return 0;

return SDL_SetError("setpriority() failed");

#endif /* __LINUX__ */

/* vi: set ts=4 sw=4 expandtab: */

0 comments on commit c7b7137

Please sign in to comment.