SDL_systhread.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #include "SDL_system.h"
  20. #include <pthread.h>
  21. #if HAVE_PTHREAD_NP_H
  22. #include <pthread_np.h>
  23. #endif
  24. #include <signal.h>
  25. #ifdef __LINUX__
  26. #include <sys/time.h>
  27. #include <sys/resource.h>
  28. #include <sys/syscall.h>
  29. #include <unistd.h>
  30. #include <errno.h>
  31. #include "../../core/linux/SDL_dbus.h"
  32. #endif /* __LINUX__ */
  33. #if defined(__LINUX__) || defined(__MACOSX__) || defined(__IPHONEOS__)
  34. #include <dlfcn.h>
  35. #ifndef RTLD_DEFAULT
  36. #define RTLD_DEFAULT NULL
  37. #endif
  38. #endif
  39. #include "SDL_log.h"
  40. #include "SDL_platform.h"
  41. #include "SDL_thread.h"
  42. #include "../SDL_thread_c.h"
  43. #include "../SDL_systhread.h"
  44. #ifdef __ANDROID__
  45. #include "../../core/android/SDL_android.h"
  46. #endif
  47. #ifdef __HAIKU__
  48. #include <kernel/OS.h>
  49. #endif
  50. #include "SDL_assert.h"
  51. #ifndef __NACL__
  52. /* List of signals to mask in the subthreads */
  53. static const int sig_list[] = {
  54. SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
  55. SIGVTALRM, SIGPROF, 0
  56. };
  57. #endif
  58. static void *
  59. RunThread(void *data)
  60. {
  61. #ifdef __ANDROID__
  62. Android_JNI_SetupThread();
  63. #endif
  64. SDL_RunThread(data);
  65. return NULL;
  66. }
  67. #if defined(__MACOSX__) || defined(__IPHONEOS__)
  68. static SDL_bool checked_setname = SDL_FALSE;
  69. static int (*ppthread_setname_np)(const char*) = NULL;
  70. #elif defined(__LINUX__)
  71. static SDL_bool checked_setname = SDL_FALSE;
  72. static int (*ppthread_setname_np)(pthread_t, const char*) = NULL;
  73. #endif
  74. int
  75. SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
  76. {
  77. pthread_attr_t type;
  78. /* do this here before any threads exist, so there's no race condition. */
  79. #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
  80. if (!checked_setname) {
  81. void *fn = dlsym(RTLD_DEFAULT, "pthread_setname_np");
  82. #if defined(__MACOSX__) || defined(__IPHONEOS__)
  83. ppthread_setname_np = (int(*)(const char*)) fn;
  84. #elif defined(__LINUX__)
  85. ppthread_setname_np = (int(*)(pthread_t, const char*)) fn;
  86. #endif
  87. checked_setname = SDL_TRUE;
  88. }
  89. #endif
  90. /* Set the thread attributes */
  91. if (pthread_attr_init(&type) != 0) {
  92. return SDL_SetError("Couldn't initialize pthread attributes");
  93. }
  94. pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
  95. /* Set caller-requested stack size. Otherwise: use the system default. */
  96. if (thread->stacksize) {
  97. pthread_attr_setstacksize(&type, (size_t) thread->stacksize);
  98. }
  99. /* Create the thread and go! */
  100. if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
  101. return SDL_SetError("Not enough resources to create thread");
  102. }
  103. return 0;
  104. }
  105. void
  106. SDL_SYS_SetupThread(const char *name)
  107. {
  108. #if !defined(__NACL__)
  109. int i;
  110. sigset_t mask;
  111. #endif /* !__NACL__ */
  112. if (name != NULL) {
  113. #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
  114. SDL_assert(checked_setname);
  115. if (ppthread_setname_np != NULL) {
  116. #if defined(__MACOSX__) || defined(__IPHONEOS__)
  117. ppthread_setname_np(name);
  118. #elif defined(__LINUX__)
  119. ppthread_setname_np(pthread_self(), name);
  120. #endif
  121. }
  122. #elif HAVE_PTHREAD_SETNAME_NP
  123. #if defined(__NETBSD__)
  124. pthread_setname_np(pthread_self(), "%s", name);
  125. #else
  126. pthread_setname_np(pthread_self(), name);
  127. #endif
  128. #elif HAVE_PTHREAD_SET_NAME_NP
  129. pthread_set_name_np(pthread_self(), name);
  130. #elif defined(__HAIKU__)
  131. /* The docs say the thread name can't be longer than B_OS_NAME_LENGTH. */
  132. char namebuf[B_OS_NAME_LENGTH];
  133. SDL_snprintf(namebuf, sizeof (namebuf), "%s", name);
  134. namebuf[sizeof (namebuf) - 1] = '\0';
  135. rename_thread(find_thread(NULL), namebuf);
  136. #endif
  137. }
  138. /* NativeClient does not yet support signals.*/
  139. #if !defined(__NACL__)
  140. /* Mask asynchronous signals for this thread */
  141. sigemptyset(&mask);
  142. for (i = 0; sig_list[i]; ++i) {
  143. sigaddset(&mask, sig_list[i]);
  144. }
  145. pthread_sigmask(SIG_BLOCK, &mask, 0);
  146. #endif /* !__NACL__ */
  147. #ifdef PTHREAD_CANCEL_ASYNCHRONOUS
  148. /* Allow ourselves to be asynchronously cancelled */
  149. {
  150. int oldstate;
  151. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
  152. }
  153. #endif
  154. }
  155. SDL_threadID
  156. SDL_ThreadID(void)
  157. {
  158. return ((SDL_threadID) pthread_self());
  159. }
  160. int
  161. SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
  162. {
  163. #if __NACL__
  164. /* FIXME: Setting thread priority does not seem to be supported in NACL */
  165. return 0;
  166. #elif __LINUX__
  167. int value;
  168. pid_t thread = syscall(SYS_gettid);
  169. if (priority == SDL_THREAD_PRIORITY_LOW) {
  170. value = 19;
  171. } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
  172. value = -10;
  173. } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  174. value = -20;
  175. } else {
  176. value = 0;
  177. }
  178. return SDL_LinuxSetThreadPriority(thread, value);
  179. #else
  180. struct sched_param sched;
  181. int policy;
  182. pthread_t thread = pthread_self();
  183. if (pthread_getschedparam(thread, &policy, &sched) != 0) {
  184. return SDL_SetError("pthread_getschedparam() failed");
  185. }
  186. if (priority == SDL_THREAD_PRIORITY_LOW) {
  187. sched.sched_priority = sched_get_priority_min(policy);
  188. } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  189. sched.sched_priority = sched_get_priority_max(policy);
  190. } else {
  191. int min_priority = sched_get_priority_min(policy);
  192. int max_priority = sched_get_priority_max(policy);
  193. sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
  194. if (priority == SDL_THREAD_PRIORITY_HIGH) {
  195. sched.sched_priority += ((max_priority - min_priority) / 4);
  196. }
  197. }
  198. if (pthread_setschedparam(thread, policy, &sched) != 0) {
  199. return SDL_SetError("pthread_setschedparam() failed");
  200. }
  201. return 0;
  202. #endif /* linux */
  203. }
  204. void
  205. SDL_SYS_WaitThread(SDL_Thread * thread)
  206. {
  207. pthread_join(thread->handle, 0);
  208. }
  209. void
  210. SDL_SYS_DetachThread(SDL_Thread * thread)
  211. {
  212. pthread_detach(thread->handle);
  213. }
  214. /* vi: set ts=4 sw=4 expandtab: */