1
0

SDL_threadprio.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 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. #if !SDL_THREADS_DISABLED
  21. #include <sys/time.h>
  22. #include <sys/resource.h>
  23. #include <pthread.h>
  24. #include "SDL_system.h"
  25. #if SDL_USE_LIBDBUS
  26. #include "SDL_dbus.h"
  27. /* d-bus queries to org.freedesktop.RealtimeKit1. */
  28. #define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
  29. #define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
  30. #define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"
  31. static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
  32. static Sint32 rtkit_min_nice_level = -20;
  33. static void
  34. rtkit_initialize()
  35. {
  36. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  37. /* Try getting minimum nice level: this is often greater than PRIO_MIN (-20). */
  38. if (!dbus || !SDL_DBus_QueryPropertyOnConnection(dbus->system_conn, RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MinNiceLevel",
  39. DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
  40. rtkit_min_nice_level = -20;
  41. }
  42. }
  43. static SDL_bool
  44. rtkit_setpriority(pid_t thread, int nice_level)
  45. {
  46. Uint64 ui64 = (Uint64)thread;
  47. Sint32 si32 = (Sint32)nice_level;
  48. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  49. pthread_once(&rtkit_initialize_once, rtkit_initialize);
  50. if (si32 < rtkit_min_nice_level)
  51. si32 = rtkit_min_nice_level;
  52. if (!dbus || !SDL_DBus_CallMethodOnConnection(dbus->system_conn,
  53. RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MakeThreadHighPriority",
  54. DBUS_TYPE_UINT64, &ui64, DBUS_TYPE_INT32, &si32, DBUS_TYPE_INVALID,
  55. DBUS_TYPE_INVALID)) {
  56. return SDL_FALSE;
  57. }
  58. return SDL_TRUE;
  59. }
  60. #endif /* dbus */
  61. #endif /* threads */
  62. /* this is a public symbol, so it has to exist even if threads are disabled. */
  63. int
  64. SDL_LinuxSetThreadPriority(Sint64 threadID, int priority)
  65. {
  66. #if SDL_THREADS_DISABLED
  67. return SDL_Unsupported();
  68. #else
  69. if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) == 0) {
  70. return 0;
  71. }
  72. #if SDL_USE_LIBDBUS
  73. /* Note that this fails if you're trying to set high priority
  74. and you don't have root permission. BUT DON'T RUN AS ROOT!
  75. You can grant the ability to increase thread priority by
  76. running the following command on your application binary:
  77. sudo setcap 'cap_sys_nice=eip' <application>
  78. Let's try setting priority with RealtimeKit...
  79. README and sample code at: http://git.0pointer.net/rtkit.git
  80. */
  81. if (rtkit_setpriority((pid_t)threadID, priority)) {
  82. return 0;
  83. }
  84. #endif
  85. return SDL_SetError("setpriority() failed");
  86. #endif
  87. }
  88. #endif /* __LINUX__ */
  89. /* vi: set ts=4 sw=4 expandtab: */