src/core/linux/SDL_threadprio.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 04 Jan 2019 22:01:14 -0800
changeset 12503 806492103856
parent 12460 5d56e3a2a3e4
child 12868 71ddabe9ad73
permissions -rw-r--r--
Updated copyright for 2019
icculus@12460
     1
/*
icculus@12460
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
icculus@12460
     4
icculus@12460
     5
  This software is provided 'as-is', without any express or implied
icculus@12460
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@12460
     7
  arising from the use of this software.
icculus@12460
     8
icculus@12460
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@12460
    10
  including commercial applications, and to alter it and redistribute it
icculus@12460
    11
  freely, subject to the following restrictions:
icculus@12460
    12
icculus@12460
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@12460
    14
     claim that you wrote the original software. If you use this software
icculus@12460
    15
     in a product, an acknowledgment in the product documentation would be
icculus@12460
    16
     appreciated but is not required.
icculus@12460
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@12460
    18
     misrepresented as being the original software.
icculus@12460
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@12460
    20
*/
icculus@12460
    21
#include "../../SDL_internal.h"
icculus@12460
    22
icculus@12460
    23
#ifdef __LINUX__
icculus@12460
    24
icculus@12460
    25
#if !SDL_THREADS_DISABLED
icculus@12460
    26
#include <sys/time.h>
icculus@12460
    27
#include <sys/resource.h>
icculus@12460
    28
#include <pthread.h>
icculus@12460
    29
#include "SDL_system.h"
icculus@12460
    30
icculus@12460
    31
#if SDL_USE_LIBDBUS
icculus@12460
    32
#include "SDL_dbus.h"
icculus@12460
    33
/* d-bus queries to org.freedesktop.RealtimeKit1. */
icculus@12460
    34
#define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
icculus@12460
    35
#define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
icculus@12460
    36
#define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"
icculus@12460
    37
icculus@12460
    38
static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
icculus@12460
    39
static Sint32 rtkit_min_nice_level = -20;
icculus@12460
    40
icculus@12460
    41
static void
icculus@12460
    42
rtkit_initialize()
icculus@12460
    43
{
icculus@12460
    44
    SDL_DBusContext *dbus = SDL_DBus_GetContext();
icculus@12460
    45
icculus@12460
    46
    /* Try getting minimum nice level: this is often greater than PRIO_MIN (-20). */
icculus@12460
    47
    if (!dbus || !SDL_DBus_QueryPropertyOnConnection(dbus->system_conn, RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MinNiceLevel",
icculus@12460
    48
                                            DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
icculus@12460
    49
        rtkit_min_nice_level = -20;
icculus@12460
    50
    }
icculus@12460
    51
}
icculus@12460
    52
icculus@12460
    53
static SDL_bool
icculus@12460
    54
rtkit_setpriority(pid_t thread, int nice_level)
icculus@12460
    55
{
icculus@12460
    56
    Uint64 ui64 = (Uint64)thread;
icculus@12460
    57
    Sint32 si32 = (Sint32)nice_level;
icculus@12460
    58
    SDL_DBusContext *dbus = SDL_DBus_GetContext();
icculus@12460
    59
icculus@12460
    60
    pthread_once(&rtkit_initialize_once, rtkit_initialize);
icculus@12460
    61
icculus@12460
    62
    if (si32 < rtkit_min_nice_level)
icculus@12460
    63
        si32 = rtkit_min_nice_level;
icculus@12460
    64
icculus@12460
    65
    if (!dbus || !SDL_DBus_CallMethodOnConnection(dbus->system_conn,
icculus@12460
    66
            RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MakeThreadHighPriority",
icculus@12460
    67
            DBUS_TYPE_UINT64, &ui64, DBUS_TYPE_INT32, &si32, DBUS_TYPE_INVALID,
icculus@12460
    68
            DBUS_TYPE_INVALID)) {
icculus@12460
    69
        return SDL_FALSE;
icculus@12460
    70
    }
icculus@12460
    71
    return SDL_TRUE;
icculus@12460
    72
}
icculus@12460
    73
#endif /* dbus */
icculus@12460
    74
#endif /* threads */
icculus@12460
    75
icculus@12460
    76
icculus@12460
    77
/* this is a public symbol, so it has to exist even if threads are disabled. */
icculus@12460
    78
int
icculus@12460
    79
SDL_LinuxSetThreadPriority(Sint64 threadID, int priority)
icculus@12460
    80
{
icculus@12460
    81
#if SDL_THREADS_DISABLED
icculus@12460
    82
    return SDL_Unsupported();
icculus@12460
    83
#else
icculus@12460
    84
    if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) == 0) {
icculus@12460
    85
        return 0;
icculus@12460
    86
    }
icculus@12460
    87
icculus@12460
    88
#if SDL_USE_LIBDBUS
icculus@12460
    89
    /* Note that this fails if you're trying to set high priority
icculus@12460
    90
       and you don't have root permission. BUT DON'T RUN AS ROOT!
icculus@12460
    91
icculus@12460
    92
       You can grant the ability to increase thread priority by
icculus@12460
    93
       running the following command on your application binary:
icculus@12460
    94
          sudo setcap 'cap_sys_nice=eip' <application>
icculus@12460
    95
icculus@12460
    96
       Let's try setting priority with RealtimeKit...
icculus@12460
    97
icculus@12460
    98
       README and sample code at: http://git.0pointer.net/rtkit.git
icculus@12460
    99
    */
icculus@12460
   100
    if (rtkit_setpriority((pid_t)threadID, priority)) {
icculus@12460
   101
        return 0;
icculus@12460
   102
    }
icculus@12460
   103
#endif
icculus@12460
   104
icculus@12460
   105
    return SDL_SetError("setpriority() failed");
icculus@12460
   106
#endif
icculus@12460
   107
}
icculus@12460
   108
icculus@12460
   109
#endif  /* __LINUX__ */
icculus@12460
   110
icculus@12460
   111
/* vi: set ts=4 sw=4 expandtab: */