SDL_systhread.c 6.5 KB

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