SDL_threadprio.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #ifdef __LINUX__
  20. #include "SDL_stdinc.h"
  21. #if !SDL_THREADS_DISABLED
  22. #include <sys/time.h>
  23. #include <sys/resource.h>
  24. #include <pthread.h>
  25. #include "SDL_system.h"
  26. #include "SDL_dbus.h"
  27. #if SDL_USE_LIBDBUS
  28. /* d-bus queries to org.freedesktop.RealtimeKit1. */
  29. #define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
  30. #define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
  31. #define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"
  32. static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
  33. static Sint32 rtkit_min_nice_level = -20;
  34. static void
  35. rtkit_initialize()
  36. {
  37. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  38. /* Try getting minimum nice level: this is often greater than PRIO_MIN (-20). */
  39. if (!dbus || !SDL_DBus_QueryPropertyOnConnection(dbus->system_conn, RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MinNiceLevel",
  40. DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
  41. rtkit_min_nice_level = -20;
  42. }
  43. }
  44. static SDL_bool
  45. rtkit_setpriority(pid_t thread, int nice_level)
  46. {
  47. Uint64 ui64 = (Uint64)thread;
  48. Sint32 si32 = (Sint32)nice_level;
  49. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  50. pthread_once(&rtkit_initialize_once, rtkit_initialize);
  51. if (si32 < rtkit_min_nice_level)
  52. si32 = rtkit_min_nice_level;
  53. if (!dbus || !SDL_DBus_CallMethodOnConnection(dbus->system_conn,
  54. RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MakeThreadHighPriority",
  55. DBUS_TYPE_UINT64, &ui64, DBUS_TYPE_INT32, &si32, DBUS_TYPE_INVALID,
  56. DBUS_TYPE_INVALID)) {
  57. return SDL_FALSE;
  58. }
  59. return SDL_TRUE;
  60. }
  61. #endif /* dbus */
  62. #endif /* threads */
  63. /* this is a public symbol, so it has to exist even if threads are disabled. */
  64. int
  65. SDL_LinuxSetThreadPriority(Sint64 threadID, int priority)
  66. {
  67. #if SDL_THREADS_DISABLED
  68. return SDL_Unsupported();
  69. #else
  70. if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) == 0) {
  71. return 0;
  72. }
  73. #if SDL_USE_LIBDBUS
  74. /* Note that this fails you most likely:
  75. * Have your process's scheduler incorrectly configured.
  76. See the requirements at:
  77. http://git.0pointer.net/rtkit.git/tree/README#n16
  78. * Encountered dbus/polkit security restrictions. Note
  79. that the RealtimeKit1 dbus endpoint is inaccessible
  80. over ssh connections for most common distro configs.
  81. You might want to check your local config for details:
  82. /usr/share/polkit-1/actions/org.freedesktop.RealtimeKit1.policy
  83. README and sample code at: http://git.0pointer.net/rtkit.git
  84. */
  85. if (rtkit_setpriority((pid_t)threadID, priority)) {
  86. return 0;
  87. }
  88. #endif
  89. return SDL_SetError("setpriority() failed");
  90. #endif
  91. }
  92. #endif /* __LINUX__ */
  93. /* vi: set ts=4 sw=4 expandtab: */