SDL  2.0
SDL_thread_c.h File Reference
#include "../SDL_internal.h"
#include "SDL_thread.h"
#include "generic/SDL_systhread_c.h"
#include "../SDL_error_c.h"
+ Include dependency graph for SDL_thread_c.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SDL_Thread
 
struct  SDL_TLSData
 

Macros

#define TLS_ALLOC_CHUNKSIZE   4
 

Enumerations

enum  SDL_ThreadState {
  SDL_THREAD_STATE_ALIVE,
  SDL_THREAD_STATE_DETACHED,
  SDL_THREAD_STATE_ZOMBIE,
  SDL_THREAD_STATE_CLEANED
}
 

Functions

void SDL_RunThread (void *data)
 
SDL_TLSDataSDL_Generic_GetTLSData ()
 
int SDL_Generic_SetTLSData (SDL_TLSData *data)
 

Macro Definition Documentation

#define TLS_ALLOC_CHUNKSIZE   4

Definition at line 78 of file SDL_thread_c.h.

Referenced by SDL_TLSSet().

Enumeration Type Documentation

Enumerator
SDL_THREAD_STATE_ALIVE 
SDL_THREAD_STATE_DETACHED 
SDL_THREAD_STATE_ZOMBIE 
SDL_THREAD_STATE_CLEANED 

Definition at line 45 of file SDL_thread_c.h.

Function Documentation

SDL_TLSData* SDL_Generic_GetTLSData ( )

Definition at line 123 of file SDL_thread.c.

References mutex, SDL_TLSEntry::next, NULL, SDL_AtomicLock, SDL_AtomicUnlock, SDL_CreateMutex, SDL_LockMutex, SDL_MemoryBarrierAcquire, SDL_MemoryBarrierRelease, SDL_ThreadID, SDL_UnlockMutex, SDL_TLSEntry::storage, and SDL_TLSEntry::thread.

Referenced by SDL_SYS_GetTLSData().

124 {
125  SDL_threadID thread = SDL_ThreadID();
126  SDL_TLSEntry *entry;
127  SDL_TLSData *storage = NULL;
128 
129 #if !SDL_THREADS_DISABLED
130  if (!SDL_generic_TLS_mutex) {
131  static SDL_SpinLock tls_lock;
132  SDL_AtomicLock(&tls_lock);
133  if (!SDL_generic_TLS_mutex) {
137  if (!SDL_generic_TLS_mutex) {
138  SDL_AtomicUnlock(&tls_lock);
139  return NULL;
140  }
141  }
142  SDL_AtomicUnlock(&tls_lock);
143  }
144 #endif /* SDL_THREADS_DISABLED */
145 
148  for (entry = SDL_generic_TLS; entry; entry = entry->next) {
149  if (entry->thread == thread) {
150  storage = entry->storage;
151  break;
152  }
153  }
154 #if !SDL_THREADS_DISABLED
156 #endif
157 
158  return storage;
159 }
#define SDL_ThreadID
SDL_TLSData * storage
Definition: SDL_thread.c:114
#define SDL_LockMutex
static SDL_mutex * SDL_generic_TLS_mutex
Definition: SDL_thread.c:118
#define SDL_AtomicLock
#define SDL_MemoryBarrierRelease()
Definition: SDL_atomic.h:180
#define SDL_CreateMutex
SDL_threadID thread
Definition: SDL_thread.c:113
static SDL_mutex * mutex
Definition: testlock.c:23
#define SDL_MemoryBarrierAcquire()
Definition: SDL_atomic.h:181
struct SDL_TLSEntry * next
Definition: SDL_thread.c:115
#define SDL_AtomicUnlock
Definition: SDL_thread.c:112
#define NULL
Definition: begin_code.h:143
static SDL_TLSEntry * SDL_generic_TLS
Definition: SDL_thread.c:119
#define SDL_UnlockMutex
int SDL_SpinLock
Definition: SDL_atomic.h:89
unsigned long SDL_threadID
Definition: SDL_thread.h:49
int SDL_Generic_SetTLSData ( SDL_TLSData data)

Definition at line 162 of file SDL_thread.c.

References SDL_TLSEntry::next, NULL, SDL_free(), SDL_generic_TLS, SDL_LockMutex, SDL_malloc, SDL_OutOfMemory, SDL_ThreadID, SDL_UnlockMutex, SDL_TLSEntry::storage, and SDL_TLSEntry::thread.

Referenced by SDL_SYS_SetTLSData().

163 {
164  SDL_threadID thread = SDL_ThreadID();
165  SDL_TLSEntry *prev, *entry;
166 
167  /* SDL_Generic_GetTLSData() is always called first, so we can assume SDL_generic_TLS_mutex */
169  prev = NULL;
170  for (entry = SDL_generic_TLS; entry; entry = entry->next) {
171  if (entry->thread == thread) {
172  if (storage) {
173  entry->storage = storage;
174  } else {
175  if (prev) {
176  prev->next = entry->next;
177  } else {
178  SDL_generic_TLS = entry->next;
179  }
180  SDL_free(entry);
181  }
182  break;
183  }
184  prev = entry;
185  }
186  if (!entry) {
187  entry = (SDL_TLSEntry *)SDL_malloc(sizeof(*entry));
188  if (entry) {
189  entry->thread = thread;
190  entry->storage = storage;
191  entry->next = SDL_generic_TLS;
192  SDL_generic_TLS = entry;
193  }
194  }
196 
197  if (!entry) {
198  return SDL_OutOfMemory();
199  }
200  return 0;
201 }
#define SDL_ThreadID
SDL_TLSData * storage
Definition: SDL_thread.c:114
#define SDL_LockMutex
static SDL_mutex * SDL_generic_TLS_mutex
Definition: SDL_thread.c:118
SDL_threadID thread
Definition: SDL_thread.c:113
struct SDL_TLSEntry * next
Definition: SDL_thread.c:115
void SDL_free(void *mem)
Definition: SDL_thread.c:112
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
static SDL_TLSEntry * SDL_generic_TLS
Definition: SDL_thread.c:119
#define SDL_UnlockMutex
#define SDL_malloc
unsigned long SDL_threadID
Definition: SDL_thread.h:49
void SDL_RunThread ( void data)

Definition at line 264 of file SDL_thread.c.

References thread_args::data, thread_args::func, thread_args::info, SDL_AtomicCAS, SDL_free(), SDL_SemPost, SDL_SYS_SetupThread(), SDL_THREAD_STATE_ALIVE, SDL_THREAD_STATE_CLEANED, SDL_THREAD_STATE_DETACHED, SDL_THREAD_STATE_ZOMBIE, SDL_ThreadID, SDL_TLSCleanup(), SDLCALL, SDL_TLSEntry::thread, and thread_args::wait.

Referenced by RunThread().

265 {
266  thread_args *args = (thread_args *) data;
267  int (SDLCALL * userfunc) (void *) = args->func;
268  void *userdata = args->data;
269  SDL_Thread *thread = args->info;
270  int *statusloc = &thread->status;
271 
272  /* Perform any system-dependent setup - this function may not fail */
273  SDL_SYS_SetupThread(thread->name);
274 
275  /* Get the thread id */
276  thread->threadid = SDL_ThreadID();
277 
278  /* Wake up the parent thread */
279  SDL_SemPost(args->wait);
280 
281  /* Run the function */
282  *statusloc = userfunc(userdata);
283 
284  /* Clean up thread-local storage */
285  SDL_TLSCleanup();
286 
287  /* Mark us as ready to be joined (or detached) */
289  /* Clean up if something already detached us. */
291  if (thread->name) {
292  SDL_free(thread->name);
293  }
294  SDL_free(thread);
295  }
296  }
297 }
#define SDL_ThreadID
#define SDL_AtomicCAS
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1967
#define SDL_SemPost
void SDL_free(void *mem)
static void SDL_TLSCleanup()
Definition: SDL_thread.c:86
int(* func)(void *)
Definition: SDL_thread.c:257
SDL_Thread * info
Definition: SDL_thread.c:259
SDL_sem * wait
Definition: SDL_thread.c:260
void SDL_SYS_SetupThread(const char *name)
Definition: SDL_systhread.c:42
void * data
Definition: SDL_thread.c:258
#define SDLCALL
Definition: SDL_internal.h:31