SDL  2.0
SDL_systhread.c File Reference
#include "../../SDL_internal.h"
#include <pthread.h>
#include <signal.h>
#include "SDL_platform.h"
#include "SDL_thread.h"
#include "SDL_hints.h"
#include "../SDL_thread_c.h"
#include "../SDL_systhread.h"
#include "SDL_assert.h"
+ Include dependency graph for SDL_systhread.c:

Go to the source code of this file.

Functions

static voidRunThread (void *data)
 
int SDL_SYS_CreateThread (SDL_Thread *thread, void *args)
 
void SDL_SYS_SetupThread (const char *name)
 
SDL_threadID SDL_ThreadID (void)
 
int SDL_SYS_SetThreadPriority (SDL_ThreadPriority priority)
 
void SDL_SYS_WaitThread (SDL_Thread *thread)
 
void SDL_SYS_DetachThread (SDL_Thread *thread)
 

Variables

static const int sig_list []
 

Function Documentation

static void* RunThread ( void data)
static

Definition at line 70 of file SDL_systhread.c.

References Android_JNI_SetupThread(), NULL, SDL_FALSE, and SDL_RunThread().

Referenced by SDL_SYS_CreateThread().

71 {
72 #ifdef __ANDROID__
74 #endif
76  return NULL;
77 }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1967
int Android_JNI_SetupThread(void)
#define NULL
Definition: begin_code.h:143
void SDL_RunThread(void *data)
Definition: SDL_thread.c:264
int SDL_SYS_CreateThread ( SDL_Thread thread,
void args 
)

Definition at line 87 of file SDL_systhread.c.

References SDL_Thread::handle, RunThread(), SDL_atoi, SDL_GetHint, SDL_HINT_THREAD_STACK_SIZE, SDL_SetError, and SDL_TRUE.

88 {
89  pthread_attr_t type;
90  const char *hint = SDL_GetHint(SDL_HINT_THREAD_STACK_SIZE);
91 
92  /* do this here before any threads exist, so there's no race condition. */
93  #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
94  if (!checked_setname) {
95  void *fn = dlsym(RTLD_DEFAULT, "pthread_setname_np");
96  #if defined(__MACOSX__) || defined(__IPHONEOS__)
97  ppthread_setname_np = (int(*)(const char*)) fn;
98  #elif defined(__LINUX__)
99  ppthread_setname_np = (int(*)(pthread_t, const char*)) fn;
100  #endif
101  checked_setname = SDL_TRUE;
102  }
103  #endif
104 
105  /* Set the thread attributes */
106  if (pthread_attr_init(&type) != 0) {
107  return SDL_SetError("Couldn't initialize pthread attributes");
108  }
109  pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
110 
111  /* If the SDL_HINT_THREAD_STACK_SIZE exists and it seems to be a positive number, use it */
112  if (hint && hint[0] >= '0' && hint[0] <= '9') {
113  const size_t stacksize = (size_t) SDL_atoi(hint);
114  if (stacksize > 0) {
115  pthread_attr_setstacksize(&type, stacksize);
116  }
117  }
118 
119  /* Create the thread and go! */
120  if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
121  return SDL_SetError("Not enough resources to create thread");
122  }
123 
124  return 0;
125 }
#define SDL_HINT_THREAD_STACK_SIZE
A string specifying SDL&#39;s threads stack size in bytes or "0" for the backend&#39;s default size...
Definition: SDL_hints.h:374
static void * RunThread(void *data)
Definition: SDL_systhread.c:70
#define SDL_GetHint
unsigned int size_t
#define SDL_atoi
SYS_ThreadHandle handle
Definition: SDL_thread_c.h:57
#define SDL_SetError
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1564
void SDL_SYS_DetachThread ( SDL_Thread thread)

Definition at line 242 of file SDL_systhread.c.

References SDL_Thread::handle.

243 {
244  pthread_detach(thread->handle);
245 }
SYS_ThreadHandle handle
Definition: SDL_thread_c.h:57
int SDL_SYS_SetThreadPriority ( SDL_ThreadPriority  priority)

Definition at line 189 of file SDL_systhread.c.

References SDL_SetError, SDL_THREAD_PRIORITY_HIGH, and SDL_THREAD_PRIORITY_LOW.

190 {
191 #if __NACL__
192  /* FIXME: Setting thread priority does not seem to be supported in NACL */
193  return 0;
194 #elif __LINUX__
195  int value;
196 
197  if (priority == SDL_THREAD_PRIORITY_LOW) {
198  value = 19;
199  } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
200  value = -20;
201  } else {
202  value = 0;
203  }
204  if (setpriority(PRIO_PROCESS, syscall(SYS_gettid), value) < 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  */
208  return SDL_SetError("setpriority() failed");
209  }
210  return 0;
211 #else
212  struct sched_param sched;
213  int policy;
214  pthread_t thread = pthread_self();
215 
216  if (pthread_getschedparam(thread, &policy, &sched) < 0) {
217  return SDL_SetError("pthread_getschedparam() failed");
218  }
219  if (priority == SDL_THREAD_PRIORITY_LOW) {
220  sched.sched_priority = sched_get_priority_min(policy);
221  } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
222  sched.sched_priority = sched_get_priority_max(policy);
223  } else {
224  int min_priority = sched_get_priority_min(policy);
225  int max_priority = sched_get_priority_max(policy);
226  sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
227  }
228  if (pthread_setschedparam(thread, policy, &sched) < 0) {
229  return SDL_SetError("pthread_setschedparam() failed");
230  }
231  return 0;
232 #endif /* linux */
233 }
GLsizei const GLfloat * value
#define SDL_SetError
void SDL_SYS_SetupThread ( const char *  name)

Definition at line 128 of file SDL_systhread.c.

References i, NULL, SDL_assert, SDL_snprintf, and sig_list.

129 {
130 #if !defined(__ANDROID__) && !defined(__NACL__)
131  int i;
132  sigset_t mask;
133 #endif /* !__ANDROID__ && !__NACL__ */
134 
135  if (name != NULL) {
136  #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
137  SDL_assert(checked_setname);
138  if (ppthread_setname_np != NULL) {
139  #if defined(__MACOSX__) || defined(__IPHONEOS__)
140  ppthread_setname_np(name);
141  #elif defined(__LINUX__)
142  ppthread_setname_np(pthread_self(), name);
143  #endif
144  }
145  #elif HAVE_PTHREAD_SETNAME_NP
146  #if defined(__NETBSD__)
147  pthread_setname_np(pthread_self(), "%s", name);
148  #else
149  pthread_setname_np(pthread_self(), name);
150  #endif
151  #elif HAVE_PTHREAD_SET_NAME_NP
152  pthread_set_name_np(pthread_self(), name);
153  #elif defined(__HAIKU__)
154  /* The docs say the thread name can't be longer than B_OS_NAME_LENGTH. */
155  char namebuf[B_OS_NAME_LENGTH];
156  SDL_snprintf(namebuf, sizeof (namebuf), "%s", name);
157  namebuf[sizeof (namebuf) - 1] = '\0';
158  rename_thread(find_thread(NULL), namebuf);
159  #endif
160  }
161 
162  /* NativeClient does not yet support signals.*/
163 #if !defined(__ANDROID__) && !defined(__NACL__)
164  /* Mask asynchronous signals for this thread */
165  sigemptyset(&mask);
166  for (i = 0; sig_list[i]; ++i) {
167  sigaddset(&mask, sig_list[i]);
168  }
169  pthread_sigmask(SIG_BLOCK, &mask, 0);
170 #endif /* !__ANDROID__ && !__NACL__ */
171 
172 
173 #ifdef PTHREAD_CANCEL_ASYNCHRONOUS
174  /* Allow ourselves to be asynchronously cancelled */
175  {
176  int oldstate;
177  pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate);
178  }
179 #endif
180 }
GLuint const GLchar * name
static const int sig_list[]
Definition: SDL_systhread.c:63
GLenum GLint GLuint mask
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
#define SDL_assert(condition)
Definition: SDL_assert.h:167
#define NULL
Definition: begin_code.h:143
#define SDL_snprintf
void SDL_SYS_WaitThread ( SDL_Thread thread)

Definition at line 236 of file SDL_systhread.c.

References SDL_Thread::handle.

237 {
238  pthread_join(thread->handle, 0);
239 }
SYS_ThreadHandle handle
Definition: SDL_thread_c.h:57
SDL_threadID SDL_ThreadID ( void  )

Get the thread identifier for the current thread.

Definition at line 183 of file SDL_systhread.c.

184 {
185  return ((SDL_threadID) pthread_self());
186 }
unsigned long SDL_threadID
Definition: SDL_thread.h:49

Variable Documentation

const int sig_list[]
static
Initial value:
= {
SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
SIGVTALRM, SIGPROF, 0
}

Definition at line 63 of file SDL_systhread.c.

Referenced by SDL_SYS_SetupThread().