Patched to compile on Linux with --disable-threads.
authorRyan C. Gordon <icculus@icculus.org>
Sat, 01 Dec 2018 10:36:26 -0500
changeset 12436fa17d3d76431
parent 12435 261716de5769
child 12437 ab67e41946fe
Patched to compile on Linux with --disable-threads.

Fixes Bugzilla #4393.
src/core/linux/SDL_dbus.c
src/thread/pthread/SDL_systhread.c
src/video/wayland/SDL_waylanddatamanager.c
     1.1 --- a/src/core/linux/SDL_dbus.c	Tue Nov 27 15:10:26 2018 -0800
     1.2 +++ b/src/core/linux/SDL_dbus.c	Sat Dec 01 10:36:26 2018 -0500
     1.3 @@ -21,6 +21,11 @@
     1.4  #include "../../SDL_internal.h"
     1.5  #include "SDL_dbus.h"
     1.6  
     1.7 +#if !SDL_THREADS_DISABLED
     1.8 +#include <sys/time.h>
     1.9 +#include <sys/resource.h>
    1.10 +#endif
    1.11 +
    1.12  #if SDL_USE_LIBDBUS
    1.13  /* we never link directly to libdbus. */
    1.14  #include "SDL_loadso.h"
    1.15 @@ -342,6 +347,82 @@
    1.16  
    1.17      return SDL_TRUE;
    1.18  }
    1.19 +
    1.20 +#if !SDL_THREADS_DISABLED
    1.21 +/* d-bus queries to org.freedesktop.RealtimeKit1. */
    1.22 +#define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
    1.23 +#define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
    1.24 +#define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"
    1.25 +
    1.26 +static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
    1.27 +static Sint32 rtkit_min_nice_level = -20;
    1.28 +
    1.29 +static void
    1.30 +rtkit_initialize()
    1.31 +{
    1.32 +    SDL_DBusContext *dbus = SDL_DBus_GetContext();
    1.33 +
    1.34 +    /* Try getting minimum nice level: this is often greater than PRIO_MIN (-20). */
    1.35 +    if (!dbus || !SDL_DBus_QueryPropertyOnConnection(dbus->system_conn, RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MinNiceLevel",
    1.36 +                                            DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
    1.37 +        rtkit_min_nice_level = -20;
    1.38 +    }
    1.39 +}
    1.40 +
    1.41 +static SDL_bool
    1.42 +rtkit_setpriority(pid_t thread, int nice_level)
    1.43 +{
    1.44 +    Uint64 ui64 = (Uint64)thread;
    1.45 +    Sint32 si32 = (Sint32)nice_level;
    1.46 +    SDL_DBusContext *dbus = SDL_DBus_GetContext();
    1.47 +
    1.48 +    pthread_once(&rtkit_initialize_once, rtkit_initialize);
    1.49 +
    1.50 +    if (si32 < rtkit_min_nice_level)
    1.51 +        si32 = rtkit_min_nice_level;
    1.52 +
    1.53 +    if (!dbus || !SDL_DBus_CallMethodOnConnection(dbus->system_conn,
    1.54 +            RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MakeThreadHighPriority",
    1.55 +            DBUS_TYPE_UINT64, &ui64, DBUS_TYPE_INT32, &si32, DBUS_TYPE_INVALID,
    1.56 +            DBUS_TYPE_INVALID)) {
    1.57 +        return SDL_FALSE;
    1.58 +    }
    1.59 +    return SDL_TRUE;
    1.60 +}
    1.61  #endif
    1.62  
    1.63 +#endif
    1.64 +
    1.65 +/* this is a public symbol, so it has to exist even if threads are disabled. */
    1.66 +int
    1.67 +SDL_LinuxSetThreadPriority(Sint64 threadID, int priority)
    1.68 +{
    1.69 +#if SDL_THREADS_DISABLED
    1.70 +    return SDL_Unsupported();
    1.71 +#else
    1.72 +    if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) == 0) {
    1.73 +        return 0;
    1.74 +    }
    1.75 +
    1.76 +#if SDL_USE_LIBDBUS
    1.77 +    /* Note that this fails if you're trying to set high priority
    1.78 +       and you don't have root permission. BUT DON'T RUN AS ROOT!
    1.79 +
    1.80 +       You can grant the ability to increase thread priority by
    1.81 +       running the following command on your application binary:
    1.82 +          sudo setcap 'cap_sys_nice=eip' <application>
    1.83 +
    1.84 +       Let's try setting priority with RealtimeKit...
    1.85 +
    1.86 +       README and sample code at: http://git.0pointer.net/rtkit.git
    1.87 +    */
    1.88 +    if (rtkit_setpriority((pid_t)threadID, priority)) {
    1.89 +        return 0;
    1.90 +    }
    1.91 +#endif
    1.92 +
    1.93 +    return SDL_SetError("setpriority() failed");
    1.94 +#endif
    1.95 +}
    1.96 +
    1.97  /* vi: set ts=4 sw=4 expandtab: */
     2.1 --- a/src/thread/pthread/SDL_systhread.c	Tue Nov 27 15:10:26 2018 -0800
     2.2 +++ b/src/thread/pthread/SDL_systhread.c	Sat Dec 01 10:36:26 2018 -0500
     2.3 @@ -184,84 +184,6 @@
     2.4      return ((SDL_threadID) pthread_self());
     2.5  }
     2.6  
     2.7 -#if __LINUX__
     2.8 -/* d-bus queries to org.freedesktop.RealtimeKit1. */
     2.9 -#if SDL_USE_LIBDBUS
    2.10 -
    2.11 -#define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
    2.12 -#define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
    2.13 -#define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"
    2.14 -
    2.15 -static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
    2.16 -static Sint32 rtkit_min_nice_level = -20;
    2.17 -
    2.18 -static void
    2.19 -rtkit_initialize()
    2.20 -{
    2.21 -    SDL_DBusContext *dbus = SDL_DBus_GetContext();
    2.22 -
    2.23 -    /* Try getting minimum nice level: this is often greater than PRIO_MIN (-20). */
    2.24 -    if (!dbus || !SDL_DBus_QueryPropertyOnConnection(dbus->system_conn, RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MinNiceLevel",
    2.25 -                                            DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
    2.26 -        rtkit_min_nice_level = -20;
    2.27 -    }
    2.28 -}
    2.29 -
    2.30 -static SDL_bool
    2.31 -rtkit_setpriority(pid_t thread, int nice_level)
    2.32 -{
    2.33 -    Uint64 ui64 = (Uint64)thread;
    2.34 -    Sint32 si32 = (Sint32)nice_level;
    2.35 -    SDL_DBusContext *dbus = SDL_DBus_GetContext();
    2.36 -
    2.37 -    pthread_once(&rtkit_initialize_once, rtkit_initialize);
    2.38 -
    2.39 -    if (si32 < rtkit_min_nice_level)
    2.40 -        si32 = rtkit_min_nice_level;
    2.41 -
    2.42 -    if (!dbus || !SDL_DBus_CallMethodOnConnection(dbus->system_conn,
    2.43 -            RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MakeThreadHighPriority",
    2.44 -            DBUS_TYPE_UINT64, &ui64, DBUS_TYPE_INT32, &si32, DBUS_TYPE_INVALID,
    2.45 -            DBUS_TYPE_INVALID)) {
    2.46 -        return SDL_FALSE;
    2.47 -    }
    2.48 -    return SDL_TRUE;
    2.49 -}
    2.50 -
    2.51 -#else
    2.52 -
    2.53 -static SDL_bool
    2.54 -rtkit_setpriority(pid_t thread, int nice_level)
    2.55 -{
    2.56 -    return SDL_FALSE;
    2.57 -}
    2.58 -
    2.59 -#endif /* !SDL_USE_LIBDBUS */
    2.60 -
    2.61 -int
    2.62 -SDL_LinuxSetThreadPriority(Sint64 threadID, int priority)
    2.63 -{
    2.64 -    if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) < 0) {
    2.65 -        /* Note that this fails if you're trying to set high priority
    2.66 -           and you don't have root permission. BUT DON'T RUN AS ROOT!
    2.67 -
    2.68 -           You can grant the ability to increase thread priority by
    2.69 -           running the following command on your application binary:
    2.70 -               sudo setcap 'cap_sys_nice=eip' <application>
    2.71 -
    2.72 -           Let's try setting priority with RealtimeKit...
    2.73 -
    2.74 -           README and sample code at:
    2.75 -             http://git.0pointer.net/rtkit.git
    2.76 -         */
    2.77 -        if (rtkit_setpriority((pid_t)threadID, priority) == SDL_FALSE) {
    2.78 -            return SDL_SetError("setpriority() failed");
    2.79 -        }
    2.80 -    }
    2.81 -    return 0;
    2.82 -}
    2.83 -#endif /* __LINUX__ */
    2.84 -
    2.85  int
    2.86  SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
    2.87  {
     3.1 --- a/src/video/wayland/SDL_waylanddatamanager.c	Tue Nov 27 15:10:26 2018 -0800
     3.2 +++ b/src/video/wayland/SDL_waylanddatamanager.c	Sat Dec 01 10:36:26 2018 -0500
     3.3 @@ -53,7 +53,11 @@
     3.4      sigemptyset(&sig_set);
     3.5      sigaddset(&sig_set, SIGPIPE);  
     3.6  
     3.7 -    pthread_sigmask(SIG_BLOCK, &sig_set, &old_sig_set); 
     3.8 +#if SDL_THREADS_DISABLED
     3.9 +    sigprocmask(SIG_BLOCK, &sig_set, &old_sig_set);
    3.10 +#else
    3.11 +    pthread_sigmask(SIG_BLOCK, &sig_set, &old_sig_set);
    3.12 +#endif
    3.13  
    3.14      if (ready == 0) {
    3.15          bytes_written = SDL_SetError("Pipe timeout");
    3.16 @@ -70,7 +74,12 @@
    3.17      }
    3.18  
    3.19      sigtimedwait(&sig_set, 0, &zerotime);
    3.20 +
    3.21 +#if SDL_THREADS_DISABLED
    3.22 +    sigprocmask(SIG_SETMASK, &old_sig_set, NULL);
    3.23 +#else
    3.24      pthread_sigmask(SIG_SETMASK, &old_sig_set, NULL);
    3.25 +#endif
    3.26  
    3.27      return bytes_written;
    3.28  }