SDL  2.0
SDL_systhread.c File Reference
#include "../../SDL_internal.h"
#include <pthread.h>
#include <signal.h>
#include "SDL_log.h"
#include "SDL_platform.h"
#include "SDL_thread.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

§ RunThread()

static void* RunThread ( void data)
static

Definition at line 73 of file SDL_systhread.c.

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

Referenced by SDL_SYS_CreateThread().

74 {
75 #ifdef __ANDROID__
77 #endif
79  return NULL;
80 }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
int Android_JNI_SetupThread(void)
#define NULL
Definition: begin_code.h:164
void SDL_RunThread(void *data)
Definition: SDL_thread.c:265

§ SDL_SYS_CreateThread()

int SDL_SYS_CreateThread ( SDL_Thread thread,
void args 
)

Definition at line 90 of file SDL_systhread.c.

References SDL_Thread::handle, RunThread(), SDL_SetError, SDL_TRUE, and SDL_Thread::stacksize.

91 {
92  pthread_attr_t type;
93 
94  /* do this here before any threads exist, so there's no race condition. */
95  #if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__LINUX__)
96  if (!checked_setname) {
97  void *fn = dlsym(RTLD_DEFAULT, "pthread_setname_np");
98  #if defined(__MACOSX__) || defined(__IPHONEOS__)
99  ppthread_setname_np = (int(*)(const char*)) fn;
100  #elif defined(__LINUX__)
101  ppthread_setname_np = (int(*)(pthread_t, const char*)) fn;
102  #endif
103  checked_setname = SDL_TRUE;
104  }
105  #endif
106 
107  /* Set the thread attributes */
108  if (pthread_attr_init(&type) != 0) {
109  return SDL_SetError("Couldn't initialize pthread attributes");
110  }
111  pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
112 
113  /* Set caller-requested stack size. Otherwise: use the system default. */
114  if (thread->stacksize) {
115  pthread_attr_setstacksize(&type, (size_t) thread->stacksize);
116  }
117 
118  /* Create the thread and go! */
119  if (pthread_create(&thread->handle, &type, RunThread, args) != 0) {
120  return SDL_SetError("Not enough resources to create thread");
121  }
122 
123  return 0;
124 }
static void * RunThread(void *data)
Definition: SDL_systhread.c:73
SYS_ThreadHandle handle
Definition: SDL_thread_c.h:57
#define SDL_SetError
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571
size_t stacksize
Definition: SDL_thread_c.h:62

§ SDL_SYS_DetachThread()

void SDL_SYS_DetachThread ( SDL_Thread thread)

Definition at line 319 of file SDL_systhread.c.

References SDL_Thread::handle.

320 {
321  pthread_detach(thread->handle);
322 }
SYS_ThreadHandle handle
Definition: SDL_thread_c.h:57

§ SDL_SYS_SetThreadPriority()

int SDL_SYS_SetThreadPriority ( SDL_ThreadPriority  priority)

Definition at line 266 of file SDL_systhread.c.

References SDL_LinuxSetThreadPriority, SDL_SetError, SDL_THREAD_PRIORITY_HIGH, SDL_THREAD_PRIORITY_LOW, and SDL_THREAD_PRIORITY_TIME_CRITICAL.

267 {
268 #if __NACL__
269  /* FIXME: Setting thread priority does not seem to be supported in NACL */
270  return 0;
271 #elif __LINUX__
272  int value;
273  pid_t thread = syscall(SYS_gettid);
274 
275  if (priority == SDL_THREAD_PRIORITY_LOW) {
276  value = 19;
277  } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
278  value = -10;
279  } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
280  value = -20;
281  } else {
282  value = 0;
283  }
284  return SDL_LinuxSetThreadPriority(thread, value);
285 #else
286  struct sched_param sched;
287  int policy;
288  pthread_t thread = pthread_self();
289 
290  if (pthread_getschedparam(thread, &policy, &sched) != 0) {
291  return SDL_SetError("pthread_getschedparam() failed");
292  }
293  if (priority == SDL_THREAD_PRIORITY_LOW) {
294  sched.sched_priority = sched_get_priority_min(policy);
295  } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
296  sched.sched_priority = sched_get_priority_max(policy);
297  } else {
298  int min_priority = sched_get_priority_min(policy);
299  int max_priority = sched_get_priority_max(policy);
300  sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
301  if (priority == SDL_THREAD_PRIORITY_HIGH) {
302  sched.sched_priority += ((max_priority - min_priority) / 4);
303  }
304  }
305  if (pthread_setschedparam(thread, policy, &sched) != 0) {
306  return SDL_SetError("pthread_setschedparam() failed");
307  }
308  return 0;
309 #endif /* linux */
310 }
EGLint policy
Definition: eglext.h:593
#define SDL_LinuxSetThreadPriority
GLsizei const GLfloat * value
#define SDL_SetError

§ SDL_SYS_SetupThread()

void SDL_SYS_SetupThread ( const char *  name)

Definition at line 127 of file SDL_systhread.c.

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

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

§ SDL_SYS_WaitThread()

void SDL_SYS_WaitThread ( SDL_Thread thread)

Definition at line 313 of file SDL_systhread.c.

References SDL_Thread::handle.

314 {
315  pthread_join(thread->handle, 0);
316 }
SYS_ThreadHandle handle
Definition: SDL_thread_c.h:57

§ SDL_ThreadID()

SDL_threadID SDL_ThreadID ( void  )

Get the thread identifier for the current thread.

Definition at line 182 of file SDL_systhread.c.

References SDL_FALSE, SDL_LinuxSetThreadPriority, SDL_SetError, and SDL_TRUE.

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

Variable Documentation

§ sig_list

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

Definition at line 66 of file SDL_systhread.c.

Referenced by SDL_SYS_SetupThread().