Whoops, forgot to add a new source file. :/
authorRyan C. Gordon <icculus@icculus.org>
Wed, 05 Dec 2018 17:53:38 -0500
changeset 124605d56e3a2a3e4
parent 12459 34266b6e578d
child 12461 14205d7f7715
Whoops, forgot to add a new source file. :/
src/core/linux/SDL_threadprio.c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/core/linux/SDL_threadprio.c	Wed Dec 05 17:53:38 2018 -0500
     1.3 @@ -0,0 +1,111 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +#include "../../SDL_internal.h"
    1.25 +
    1.26 +#ifdef __LINUX__
    1.27 +
    1.28 +#if !SDL_THREADS_DISABLED
    1.29 +#include <sys/time.h>
    1.30 +#include <sys/resource.h>
    1.31 +#include <pthread.h>
    1.32 +#include "SDL_system.h"
    1.33 +
    1.34 +#if SDL_USE_LIBDBUS
    1.35 +#include "SDL_dbus.h"
    1.36 +/* d-bus queries to org.freedesktop.RealtimeKit1. */
    1.37 +#define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
    1.38 +#define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
    1.39 +#define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"
    1.40 +
    1.41 +static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
    1.42 +static Sint32 rtkit_min_nice_level = -20;
    1.43 +
    1.44 +static void
    1.45 +rtkit_initialize()
    1.46 +{
    1.47 +    SDL_DBusContext *dbus = SDL_DBus_GetContext();
    1.48 +
    1.49 +    /* Try getting minimum nice level: this is often greater than PRIO_MIN (-20). */
    1.50 +    if (!dbus || !SDL_DBus_QueryPropertyOnConnection(dbus->system_conn, RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MinNiceLevel",
    1.51 +                                            DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
    1.52 +        rtkit_min_nice_level = -20;
    1.53 +    }
    1.54 +}
    1.55 +
    1.56 +static SDL_bool
    1.57 +rtkit_setpriority(pid_t thread, int nice_level)
    1.58 +{
    1.59 +    Uint64 ui64 = (Uint64)thread;
    1.60 +    Sint32 si32 = (Sint32)nice_level;
    1.61 +    SDL_DBusContext *dbus = SDL_DBus_GetContext();
    1.62 +
    1.63 +    pthread_once(&rtkit_initialize_once, rtkit_initialize);
    1.64 +
    1.65 +    if (si32 < rtkit_min_nice_level)
    1.66 +        si32 = rtkit_min_nice_level;
    1.67 +
    1.68 +    if (!dbus || !SDL_DBus_CallMethodOnConnection(dbus->system_conn,
    1.69 +            RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MakeThreadHighPriority",
    1.70 +            DBUS_TYPE_UINT64, &ui64, DBUS_TYPE_INT32, &si32, DBUS_TYPE_INVALID,
    1.71 +            DBUS_TYPE_INVALID)) {
    1.72 +        return SDL_FALSE;
    1.73 +    }
    1.74 +    return SDL_TRUE;
    1.75 +}
    1.76 +#endif /* dbus */
    1.77 +#endif /* threads */
    1.78 +
    1.79 +
    1.80 +/* this is a public symbol, so it has to exist even if threads are disabled. */
    1.81 +int
    1.82 +SDL_LinuxSetThreadPriority(Sint64 threadID, int priority)
    1.83 +{
    1.84 +#if SDL_THREADS_DISABLED
    1.85 +    return SDL_Unsupported();
    1.86 +#else
    1.87 +    if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) == 0) {
    1.88 +        return 0;
    1.89 +    }
    1.90 +
    1.91 +#if SDL_USE_LIBDBUS
    1.92 +    /* Note that this fails if you're trying to set high priority
    1.93 +       and you don't have root permission. BUT DON'T RUN AS ROOT!
    1.94 +
    1.95 +       You can grant the ability to increase thread priority by
    1.96 +       running the following command on your application binary:
    1.97 +          sudo setcap 'cap_sys_nice=eip' <application>
    1.98 +
    1.99 +       Let's try setting priority with RealtimeKit...
   1.100 +
   1.101 +       README and sample code at: http://git.0pointer.net/rtkit.git
   1.102 +    */
   1.103 +    if (rtkit_setpriority((pid_t)threadID, priority)) {
   1.104 +        return 0;
   1.105 +    }
   1.106 +#endif
   1.107 +
   1.108 +    return SDL_SetError("setpriority() failed");
   1.109 +#endif
   1.110 +}
   1.111 +
   1.112 +#endif  /* __LINUX__ */
   1.113 +
   1.114 +/* vi: set ts=4 sw=4 expandtab: */