SDL_systhread.c 6.1 KB

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