SDL_systhread.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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 "SDL_system.h"
  20. #include "SDL_hints.h"
  21. #include <pthread.h>
  22. #if HAVE_PTHREAD_NP_H
  23. #include <pthread_np.h>
  24. #endif
  25. #include <signal.h>
  26. #include <errno.h>
  27. #ifdef __LINUX__
  28. #include <sys/time.h>
  29. #include <sys/resource.h>
  30. #include <sys/syscall.h>
  31. #include <unistd.h>
  32. #include "../../core/linux/SDL_dbus.h"
  33. #endif /* __LINUX__ */
  34. #if (defined(__LINUX__) || defined(__MACOSX__) || defined(__IPHONEOS__)) && defined(HAVE_DLOPEN)
  35. #include <dlfcn.h>
  36. #ifndef RTLD_DEFAULT
  37. #define RTLD_DEFAULT NULL
  38. #endif
  39. #endif
  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. #ifndef __NACL__
  50. /* List of signals to mask in the subthreads */
  51. static const int sig_list[] = {
  52. SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
  53. SIGVTALRM, SIGPROF, 0
  54. };
  55. #endif
  56. static void *
  57. RunThread(void *data)
  58. {
  59. #ifdef __ANDROID__
  60. Android_JNI_SetupThread();
  61. #endif
  62. SDL_RunThread((SDL_Thread *) data);
  63. return NULL;
  64. }
  65. #if (defined(__MACOSX__) || defined(__IPHONEOS__)) && defined(HAVE_DLOPEN)
  66. static SDL_bool checked_setname = SDL_FALSE;
  67. static int (*ppthread_setname_np)(const char*) = NULL;
  68. #elif defined(__LINUX__) && defined(HAVE_DLOPEN)
  69. static SDL_bool checked_setname = SDL_FALSE;
  70. static int (*ppthread_setname_np)(pthread_t, const char*) = NULL;
  71. #endif
  72. int
  73. SDL_SYS_CreateThread(SDL_Thread * thread)
  74. {
  75. pthread_attr_t type;
  76. /* do this here before any threads exist, so there's no race condition. */
  77. #if (defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)) && defined(HAVE_DLOPEN)
  78. if (!checked_setname) {
  79. void *fn = dlsym(RTLD_DEFAULT, "pthread_setname_np");
  80. #if defined(__MACOSX__) || defined(__IPHONEOS__)
  81. ppthread_setname_np = (int(*)(const char*)) fn;
  82. #elif defined(__LINUX__)
  83. ppthread_setname_np = (int(*)(pthread_t, const char*)) fn;
  84. #endif
  85. checked_setname = SDL_TRUE;
  86. }
  87. #endif
  88. /* Set the thread attributes */
  89. if (pthread_attr_init(&type) != 0) {
  90. return SDL_SetError("Couldn't initialize pthread attributes");
  91. }
  92. pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
  93. /* Set caller-requested stack size. Otherwise: use the system default. */
  94. if (thread->stacksize) {
  95. pthread_attr_setstacksize(&type, thread->stacksize);
  96. }
  97. /* Create the thread and go! */
  98. if (pthread_create(&thread->handle, &type, RunThread, thread) != 0) {
  99. return SDL_SetError("Not enough resources to create thread");
  100. }
  101. return 0;
  102. }
  103. void
  104. SDL_SYS_SetupThread(const char *name)
  105. {
  106. #if !defined(__NACL__)
  107. int i;
  108. sigset_t mask;
  109. #endif /* !__NACL__ */
  110. if (name != NULL) {
  111. #if (defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)) && defined(HAVE_DLOPEN)
  112. SDL_assert(checked_setname);
  113. if (ppthread_setname_np != NULL) {
  114. #if defined(__MACOSX__) || defined(__IPHONEOS__)
  115. ppthread_setname_np(name);
  116. #elif defined(__LINUX__)
  117. if (ppthread_setname_np(pthread_self(), name) == ERANGE) {
  118. char namebuf[16]; /* Limited to 16 char */
  119. SDL_strlcpy(namebuf, name, sizeof (namebuf));
  120. ppthread_setname_np(pthread_self(), namebuf);
  121. }
  122. #endif
  123. }
  124. #elif HAVE_PTHREAD_SETNAME_NP
  125. #if defined(__NETBSD__)
  126. pthread_setname_np(pthread_self(), "%s", name);
  127. #else
  128. if (pthread_setname_np(pthread_self(), name) == ERANGE) {
  129. char namebuf[16]; /* Limited to 16 char */
  130. SDL_strlcpy(namebuf, name, sizeof (namebuf));
  131. pthread_setname_np(pthread_self(), namebuf);
  132. }
  133. #endif
  134. #elif HAVE_PTHREAD_SET_NAME_NP
  135. pthread_set_name_np(pthread_self(), name);
  136. #elif defined(__HAIKU__)
  137. /* The docs say the thread name can't be longer than B_OS_NAME_LENGTH. */
  138. char namebuf[B_OS_NAME_LENGTH];
  139. SDL_strlcpy(namebuf, name, sizeof (namebuf));
  140. rename_thread(find_thread(NULL), namebuf);
  141. #endif
  142. }
  143. /* NativeClient does not yet support signals.*/
  144. #if !defined(__NACL__)
  145. /* Mask asynchronous signals for this thread */
  146. sigemptyset(&mask);
  147. for (i = 0; sig_list[i]; ++i) {
  148. sigaddset(&mask, sig_list[i]);
  149. }
  150. pthread_sigmask(SIG_BLOCK, &mask, 0);
  151. #endif /* !__NACL__ */
  152. #ifdef PTHREAD_CANCEL_ASYNCHRONOUS
  153. /* Allow ourselves to be asynchronously cancelled */
  154. {
  155. int oldstate;
  156. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
  157. }
  158. #endif
  159. }
  160. SDL_threadID
  161. SDL_ThreadID(void)
  162. {
  163. return ((SDL_threadID) pthread_self());
  164. }
  165. int
  166. SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
  167. {
  168. #if __NACL__ || __RISCOS__ || __OS2__
  169. /* FIXME: Setting thread priority does not seem to be supported in NACL */
  170. return 0;
  171. #else
  172. struct sched_param sched;
  173. int policy;
  174. int pri_policy;
  175. pthread_t thread = pthread_self();
  176. const char *policyhint = SDL_GetHint(SDL_HINT_THREAD_PRIORITY_POLICY);
  177. const SDL_bool timecritical_realtime_hint = SDL_GetHintBoolean(SDL_HINT_THREAD_FORCE_REALTIME_TIME_CRITICAL, SDL_FALSE);
  178. if (pthread_getschedparam(thread, &policy, &sched) != 0) {
  179. return SDL_SetError("pthread_getschedparam() failed");
  180. }
  181. /* Higher priority levels may require changing the pthread scheduler policy
  182. * for the thread. SDL will make such changes by default but there is
  183. * also a hint allowing that behavior to be overridden. */
  184. switch (priority) {
  185. case SDL_THREAD_PRIORITY_LOW:
  186. case SDL_THREAD_PRIORITY_NORMAL:
  187. pri_policy = SCHED_OTHER;
  188. break;
  189. case SDL_THREAD_PRIORITY_HIGH:
  190. case SDL_THREAD_PRIORITY_TIME_CRITICAL:
  191. #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__TVOS__)
  192. /* Apple requires SCHED_RR for high priority threads */
  193. pri_policy = SCHED_RR;
  194. break;
  195. #else
  196. pri_policy = SCHED_OTHER;
  197. break;
  198. #endif
  199. default:
  200. pri_policy = policy;
  201. break;
  202. }
  203. if (timecritical_realtime_hint && priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  204. pri_policy = SCHED_RR;
  205. }
  206. if (policyhint) {
  207. if (SDL_strcmp(policyhint, "current") == 0) {
  208. /* Leave current thread scheduler policy unchanged */
  209. } else if (SDL_strcmp(policyhint, "other") == 0) {
  210. policy = SCHED_OTHER;
  211. } else if (SDL_strcmp(policyhint, "rr") == 0) {
  212. policy = SCHED_RR;
  213. } else if (SDL_strcmp(policyhint, "fifo") == 0) {
  214. policy = SCHED_FIFO;
  215. } else {
  216. policy = pri_policy;
  217. }
  218. } else {
  219. policy = pri_policy;
  220. }
  221. #if __LINUX__
  222. {
  223. pid_t linuxTid = syscall(SYS_gettid);
  224. return SDL_LinuxSetThreadPriorityAndPolicy(linuxTid, priority, policy);
  225. }
  226. #else
  227. if (priority == SDL_THREAD_PRIORITY_LOW) {
  228. sched.sched_priority = sched_get_priority_min(policy);
  229. } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  230. sched.sched_priority = sched_get_priority_max(policy);
  231. } else {
  232. int min_priority = sched_get_priority_min(policy);
  233. int max_priority = sched_get_priority_max(policy);
  234. #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__TVOS__)
  235. if (min_priority == 15 && max_priority == 47) {
  236. /* Apple has a specific set of thread priorities */
  237. if (priority == SDL_THREAD_PRIORITY_HIGH) {
  238. sched.sched_priority = 45;
  239. } else {
  240. sched.sched_priority = 37;
  241. }
  242. } else
  243. #endif /* __MACOSX__ || __IPHONEOS__ || __TVOS__ */
  244. {
  245. sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
  246. if (priority == SDL_THREAD_PRIORITY_HIGH) {
  247. sched.sched_priority += ((max_priority - min_priority) / 4);
  248. }
  249. }
  250. }
  251. if (pthread_setschedparam(thread, policy, &sched) != 0) {
  252. return SDL_SetError("pthread_setschedparam() failed");
  253. }
  254. return 0;
  255. #endif /* linux */
  256. #endif /* #if __NACL__ || __RISCOS__ */
  257. }
  258. void
  259. SDL_SYS_WaitThread(SDL_Thread * thread)
  260. {
  261. pthread_join(thread->handle, 0);
  262. }
  263. void
  264. SDL_SYS_DetachThread(SDL_Thread * thread)
  265. {
  266. pthread_detach(thread->handle);
  267. }
  268. /* vi: set ts=4 sw=4 expandtab: */