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_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 69 of file SDL_systhread.c.

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

Referenced by SDL_SYS_CreateThread().

70 {
71 #ifdef __ANDROID__
73 #endif
75  return NULL;
76 }
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 86 of file SDL_systhread.c.

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

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

References SDL_Thread::handle.

242 {
243  pthread_detach(thread->handle);
244 }
SYS_ThreadHandle handle
Definition: SDL_thread_c.h:57

◆ SDL_SYS_SetThreadPriority()

int SDL_SYS_SetThreadPriority ( SDL_ThreadPriority  priority)

Definition at line 184 of file SDL_systhread.c.

References SDL_SetError, SDL_THREAD_PRIORITY_HIGH, and SDL_THREAD_PRIORITY_LOW.

185 {
186 #if __NACL__
187  /* FIXME: Setting thread priority does not seem to be supported in NACL */
188  return 0;
189 #elif __LINUX__
190  int value;
191 
192  if (priority == SDL_THREAD_PRIORITY_LOW) {
193  value = 19;
194  } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
195  value = -20;
196  } else {
197  value = 0;
198  }
199  if (setpriority(PRIO_PROCESS, syscall(SYS_gettid), value) < 0) {
200  /* Note that this fails if you're trying to set high priority
201  and you don't have root permission. BUT DON'T RUN AS ROOT!
202 
203  You can grant the ability to increase thread priority by
204  running the following command on your application binary:
205  sudo setcap 'cap_sys_nice=eip' <application>
206  */
207  return SDL_SetError("setpriority() failed");
208  }
209  return 0;
210 #else
211  struct sched_param sched;
212  int policy;
213  pthread_t thread = pthread_self();
214 
215  if (pthread_getschedparam(thread, &policy, &sched) != 0) {
216  return SDL_SetError("pthread_getschedparam() failed");
217  }
218  if (priority == SDL_THREAD_PRIORITY_LOW) {
219  sched.sched_priority = sched_get_priority_min(policy);
220  } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
221  sched.sched_priority = sched_get_priority_max(policy);
222  } else {
223  int min_priority = sched_get_priority_min(policy);
224  int max_priority = sched_get_priority_max(policy);
225  sched.sched_priority = (min_priority + (max_priority - min_priority) / 2);
226  }
227  if (pthread_setschedparam(thread, policy, &sched) != 0) {
228  return SDL_SetError("pthread_setschedparam() failed");
229  }
230  return 0;
231 #endif /* linux */
232 }
EGLint policy
Definition: eglext.h:593
GLsizei const GLfloat * value
#define SDL_SetError

◆ SDL_SYS_SetupThread()

void SDL_SYS_SetupThread ( const char *  name)

Definition at line 123 of file SDL_systhread.c.

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

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

References SDL_Thread::handle.

236 {
237  pthread_join(thread->handle, 0);
238 }
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 178 of file SDL_systhread.c.

179 {
180  return ((SDL_threadID) pthread_self());
181 }
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 62 of file SDL_systhread.c.

Referenced by SDL_SYS_SetupThread().