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

Macro Definition Documentation

◆ TLS_ALLOC_CHUNKSIZE

#define TLS_ALLOC_CHUNKSIZE   4

Definition at line 79 of file SDL_thread_c.h.

Referenced by SDL_TLSSet().

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 45 of file SDL_thread_c.h.

Function Documentation

◆ SDL_Generic_GetTLSData()

SDL_TLSData* SDL_Generic_GetTLSData ( void  )

Definition at line 124 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().

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

◆ SDL_Generic_SetTLSData()

int SDL_Generic_SetTLSData ( SDL_TLSData data)

Definition at line 163 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().

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

◆ SDL_RunThread()

void SDL_RunThread ( void data)

Definition at line 265 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().

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