SDL_systhread.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #include <pthread.h>
  20. #if HAVE_PTHREAD_NP_H
  21. #include <pthread_np.h>
  22. #endif
  23. #include <signal.h>
  24. #ifdef __LINUX__
  25. #include <sys/time.h>
  26. #include <sys/resource.h>
  27. #include <sys/syscall.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include "../../core/linux/SDL_dbus.h"
  31. #endif /* __LINUX__ */
  32. #if defined(__LINUX__) || defined(__MACOSX__) || defined(__IPHONEOS__)
  33. #include <dlfcn.h>
  34. #ifndef RTLD_DEFAULT
  35. #define RTLD_DEFAULT NULL
  36. #endif
  37. #endif
  38. #include "SDL_log.h"
  39. #include "SDL_platform.h"
  40. #include "SDL_thread.h"
  41. #include "../SDL_thread_c.h"
  42. #include "../SDL_systhread.h"
  43. #ifdef __ANDROID__
  44. #include "../../core/android/SDL_android.h"
  45. #endif
  46. #ifdef __HAIKU__
  47. #include <kernel/OS.h>
  48. #endif
  49. #include "SDL_assert.h"
  50. #ifndef __NACL__
  51. /* List of signals to mask in the subthreads */
  52. static const int sig_list[] = {
  53. SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
  54. SIGVTALRM, SIGPROF, 0
  55. };
  56. #endif
  57. static void *
  58. RunThread(void *data)
  59. {
  60. #ifdef __ANDROID__
  61. Android_JNI_SetupThread();
  62. #endif
  63. SDL_RunThread(data);
  64. return NULL;
  65. }
  66. #if defined(__MACOSX__) || defined(__IPHONEOS__)
  67. static SDL_bool checked_setname = SDL_FALSE;
  68. static int (*ppthread_setname_np)(const char*) = NULL;
  69. #elif defined(__LINUX__)
  70. static SDL_bool checked_setname = SDL_FALSE;
  71. static int (*ppthread_setname_np)(pthread_t, const char*) = NULL;
  72. #endif
  73. int
  74. SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
  75. {
  76. pthread_attr_t type;
  77. /* do this here before any threads exist, so there's no race condition. */
  78. #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
  79. if (!checked_setname) {
  80. void *fn = dlsym(RTLD_DEFAULT, "pthread_setname_np");
  81. #if defined(__MACOSX__) || defined(__IPHONEOS__)
  82. ppthread_setname_np = (int(*)(const char*)) fn;
  83. #elif defined(__LINUX__)
  84. ppthread_setname_np = (int(*)(pthread_t, const char*)) fn;
  85. #endif
  86. checked_setname = SDL_TRUE;
  87. }
  88. #endif
  89. /* Set the thread attributes */
  90. if (pthread_attr_init(&type) != 0) {
  91. return SDL_SetError("Couldn't initialize pthread attributes");
  92. }
  93. pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
  94. /* Set caller-requested stack size. Otherwise: use the system default. */
  95. if (thread->stacksize) {
  96. pthread_attr_setstacksize(&type, (size_t) thread->stacksize);
  97. }
  98. /* Create the thread and go! */
  99. if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
  100. return SDL_SetError("Not enough resources to create thread");
  101. }
  102. return 0;
  103. }
  104. void
  105. SDL_SYS_SetupThread(const char *name)
  106. {
  107. #if !defined(__NACL__)
  108. int i;
  109. sigset_t mask;
  110. #endif /* !__NACL__ */
  111. if (name != NULL) {
  112. #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
  113. SDL_assert(checked_setname);
  114. if (ppthread_setname_np != NULL) {
  115. #if defined(__MACOSX__) || defined(__IPHONEOS__)
  116. ppthread_setname_np(name);
  117. #elif defined(__LINUX__)
  118. ppthread_setname_np(pthread_self(), name);
  119. #endif
  120. }
  121. #elif HAVE_PTHREAD_SETNAME_NP
  122. #if defined(__NETBSD__)
  123. pthread_setname_np(pthread_self(), "%s", name);
  124. #else
  125. pthread_setname_np(pthread_self(), name);
  126. #endif
  127. #elif HAVE_PTHREAD_SET_NAME_NP
  128. pthread_set_name_np(pthread_self(), name);
  129. #elif defined(__HAIKU__)
  130. /* The docs say the thread name can't be longer than B_OS_NAME_LENGTH. */
  131. char namebuf[B_OS_NAME_LENGTH];
  132. SDL_snprintf(namebuf, sizeof (namebuf), "%s", name);
  133. namebuf[sizeof (namebuf) - 1] = '\0';
  134. rename_thread(find_thread(NULL), namebuf);
  135. #endif
  136. }
  137. /* NativeClient does not yet support signals.*/
  138. #if !defined(__NACL__)
  139. /* Mask asynchronous signals for this thread */
  140. sigemptyset(&mask);
  141. for (i = 0; sig_list[i]; ++i) {
  142. sigaddset(&mask, sig_list[i]);
  143. }
  144. pthread_sigmask(SIG_BLOCK, &mask, 0);
  145. #endif /* !__NACL__ */
  146. #ifdef PTHREAD_CANCEL_ASYNCHRONOUS
  147. /* Allow ourselves to be asynchronously cancelled */
  148. {
  149. int oldstate;
  150. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
  151. }
  152. #endif
  153. }
  154. SDL_threadID
  155. SDL_ThreadID(void)
  156. {
  157. return ((SDL_threadID) pthread_self());
  158. }
  159. #if __LINUX__
  160. /* d-bus queries to org.freedesktop.RealtimeKit1. */
  161. #if SDL_USE_LIBDBUS
  162. #define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
  163. #define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
  164. #define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"
  165. static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
  166. static Sint32 rtkit_min_nice_level = -20;
  167. static void
  168. rtkit_initialize()
  169. {
  170. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  171. /* Try getting minimum nice level: this is often greater than PRIO_MIN (-20). */
  172. if (!dbus || !SDL_DBus_QueryPropertyOnConnection(dbus->system_conn, RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MinNiceLevel",
  173. DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
  174. rtkit_min_nice_level = -20;
  175. }
  176. }
  177. static SDL_bool
  178. rtkit_setpriority(pid_t thread, int nice_level)
  179. {
  180. Uint64 ui64 = (Uint64)thread;
  181. Sint32 si32 = (Sint32)nice_level;
  182. SDL_DBusContext *dbus = SDL_DBus_GetContext();
  183. pthread_once(&rtkit_initialize_once, rtkit_initialize);
  184. if (si32 < rtkit_min_nice_level)
  185. si32 = rtkit_min_nice_level;
  186. if (!dbus || !SDL_DBus_CallMethodOnConnection(dbus->system_conn,
  187. RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MakeThreadHighPriority",
  188. DBUS_TYPE_UINT64, &ui64, DBUS_TYPE_INT32, &si32, DBUS_TYPE_INVALID,
  189. DBUS_TYPE_INVALID)) {
  190. return SDL_FALSE;
  191. }
  192. return SDL_TRUE;
  193. }
  194. #else
  195. static SDL_bool
  196. rtkit_setpriority(pid_t thread, int nice_level)
  197. {
  198. return SDL_FALSE;
  199. }
  200. #endif /* !SDL_USE_LIBDBUS */
  201. int
  202. SDL_LinuxSetThreadPriority(Sint64 threadID, int priority)
  203. {
  204. if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) < 0) {
  205. /* Note that this fails if you're trying to set high priority
  206. and you don't have root permission. BUT DON'T RUN AS ROOT!
  207. You can grant the ability to increase thread priority by
  208. running the following command on your application binary:
  209. sudo setcap 'cap_sys_nice=eip' <application>
  210. Let's try setting priority with RealtimeKit...
  211. README and sample code at:
  212. http://git.0pointer.net/rtkit.git
  213. */
  214. if (rtkit_setpriority((pid_t)threadID, priority) == SDL_FALSE) {
  215. return SDL_SetError("setpriority() failed");
  216. }
  217. }
  218. return 0;
  219. }
  220. #endif /* __LINUX__ */
  221. int
  222. SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
  223. {
  224. #if __NACL__
  225. /* FIXME: Setting thread priority does not seem to be supported in NACL */
  226. return 0;
  227. #elif __LINUX__
  228. int value;
  229. pid_t thread = syscall(SYS_gettid);
  230. if (priority == SDL_THREAD_PRIORITY_LOW) {
  231. value = 19;
  232. } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
  233. value = -10;
  234. } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  235. value = -20;
  236. } else {
  237. value = 0;
  238. }
  239. return SDL_LinuxSetThreadPriority(thread, value);
  240. #else
  241. struct sched_param sched;
  242. int policy;
  243. pthread_t thread = pthread_self();
  244. if (pthread_getschedparam(thread, &policy, &sched) != 0) {
  245. return SDL_SetError("pthread_getschedparam() failed");
  246. }
  247. if (priority == SDL_THREAD_PRIORITY_LOW) {
  248. sched.sched_priority = sched_get_priority_min(policy);
  249. } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  250. sched.sched_priority = sched_get_priority_max(policy);
  251. } else {
  252. int min_priority = sched_get_priority_min(policy);
  253. int max_priority = sched_get_priority_max(policy);
  254. sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
  255. if (priority == SDL_THREAD_PRIORITY_HIGH) {
  256. sched.sched_priority += ((max_priority - min_priority) / 4);
  257. }
  258. }
  259. if (pthread_setschedparam(thread, policy, &sched) != 0) {
  260. return SDL_SetError("pthread_setschedparam() failed");
  261. }
  262. return 0;
  263. #endif /* linux */
  264. }
  265. void
  266. SDL_SYS_WaitThread(SDL_Thread * thread)
  267. {
  268. pthread_join(thread->handle, 0);
  269. }
  270. void
  271. SDL_SYS_DetachThread(SDL_Thread * thread)
  272. {
  273. pthread_detach(thread->handle);
  274. }
  275. /* vi: set ts=4 sw=4 expandtab: */