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 (SDL_Thread *thread)
 
SDL_TLSDataSDL_Generic_GetTLSData (void)
 
int SDL_Generic_SetTLSData (SDL_TLSData *data)
 

Macro Definition Documentation

◆ TLS_ALLOC_CHUNKSIZE

#define TLS_ALLOC_CHUNKSIZE   4

Definition at line 84 of file SDL_thread_c.h.

Enumeration Type Documentation

◆ SDL_ThreadState

Enumerator
SDL_THREAD_STATE_ALIVE 
SDL_THREAD_STATE_DETACHED 
SDL_THREAD_STATE_ZOMBIE 
SDL_THREAD_STATE_CLEANED 

Definition at line 47 of file SDL_thread_c.h.

48 {
SDL_ThreadState
Definition: SDL_thread_c.h:48
@ SDL_THREAD_STATE_ZOMBIE
Definition: SDL_thread_c.h:51
@ SDL_THREAD_STATE_CLEANED
Definition: SDL_thread_c.h:52
@ SDL_THREAD_STATE_ALIVE
Definition: SDL_thread_c.h:49
@ SDL_THREAD_STATE_DETACHED
Definition: SDL_thread_c.h:50

Function Documentation

◆ SDL_Generic_GetTLSData()

SDL_TLSData* SDL_Generic_GetTLSData ( void  )

Definition at line 123 of file SDL_thread.c.

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_MemoryBarrierRelease()
Definition: SDL_atomic.h:207
int SDL_SpinLock
Definition: SDL_atomic.h:89
#define SDL_MemoryBarrierAcquire()
Definition: SDL_atomic.h:208
#define SDL_AtomicLock
#define SDL_LockMutex
#define SDL_ThreadID
#define SDL_CreateMutex
#define SDL_AtomicUnlock
#define SDL_UnlockMutex
static SDL_TLSEntry * SDL_generic_TLS
Definition: SDL_thread.c:119
static SDL_mutex * SDL_generic_TLS_mutex
Definition: SDL_thread.c:118
unsigned long SDL_threadID
Definition: SDL_thread.h:49
#define NULL
Definition: begin_code.h:163
Definition: SDL_thread.c:112
SDL_TLSData * storage
Definition: SDL_thread.c:114
SDL_threadID thread
Definition: SDL_thread.c:113
struct SDL_TLSEntry * next
Definition: SDL_thread.c:115
static SDL_mutex * mutex
Definition: testlock.c:23

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

Referenced by SDL_SYS_GetTLSData().

◆ SDL_Generic_SetTLSData()

int SDL_Generic_SetTLSData ( SDL_TLSData data)

Definition at line 162 of file SDL_thread.c.

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_malloc
#define SDL_free
#define SDL_OutOfMemory()
Definition: SDL_error.h:88

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

Referenced by SDL_SYS_SetTLSData().

◆ SDL_RunThread()

void SDL_RunThread ( SDL_Thread thread)

Definition at line 261 of file SDL_thread.c.

262 {
263  void *userdata = thread->userdata;
264  int (SDLCALL * userfunc) (void *) = thread->userfunc;
265 
266  int *statusloc = &thread->status;
267 
268  /* Perform any system-dependent setup - this function may not fail */
269  SDL_SYS_SetupThread(thread->name);
270 
271  /* Get the thread id */
272  thread->threadid = SDL_ThreadID();
273 
274  /* Run the function */
275  *statusloc = userfunc(userdata);
276 
277  /* Clean up thread-local storage */
278  SDL_TLSCleanup();
279 
280  /* Mark us as ready to be joined (or detached) */
282  /* Clean up if something already detached us. */
284  if (thread->name) {
285  SDL_free(thread->name);
286  }
287  SDL_free(thread);
288  }
289  }
290 }
#define SDL_AtomicCAS
#define SDLCALL
Definition: SDL_internal.h:49
static void SDL_TLSCleanup()
Definition: SDL_thread.c:86
void SDL_SYS_SetupThread(const char *name)
Definition: SDL_systhread.c:42
void * userdata
Definition: SDL_thread_c.h:66
char * name
Definition: SDL_thread_c.h:63
SDL_threadID threadid
Definition: SDL_thread_c.h:58
int(* userfunc)(void *)
Definition: SDL_thread_c.h:65
SDL_atomic_t state
Definition: SDL_thread_c.h:61
typedef int(__stdcall *FARPROC)()

References int(), SDL_AtomicCAS, SDL_free, SDL_SYS_SetupThread(), SDL_THREAD_STATE_ALIVE, SDL_THREAD_STATE_CLEANED, SDL_THREAD_STATE_DETACHED, SDL_THREAD_STATE_ZOMBIE, SDL_ThreadID, SDL_TLSCleanup(), SDLCALL, and SDL_TLSEntry::thread.

Referenced by RunThread().