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_TLSData * | SDL_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

enum 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.

{
SDL_THREAD_STATE_ALIVE,
SDL_THREAD_STATE_DETACHED,
SDL_THREAD_STATE_ZOMBIE,
SDL_THREAD_STATE_CLEANED,
} SDL_ThreadState;
SDL_THREAD_STATE_ZOMBIE
Definition: SDL_thread_c.h:49
SDL_THREAD_STATE_DETACHED
Definition: SDL_thread_c.h:48
SDL_THREAD_STATE_CLEANED
Definition: SDL_thread_c.h:50
SDL_THREAD_STATE_ALIVE
Definition: SDL_thread_c.h:47
SDL_ThreadState
SDL_ThreadState
Definition: SDL_thread_c.h:45

## 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().

{
SDL_threadID thread = SDL_ThreadID();
SDL_TLSEntry *entry;
SDL_TLSData *storage = NULL;
#if !SDL_THREADS_DISABLED
if (!SDL_generic_TLS_mutex) {
static SDL_SpinLock tls_lock;
SDL_AtomicLock(&tls_lock);
if (!SDL_generic_TLS_mutex) {
SDL_mutex *mutex = SDL_CreateMutex();
SDL_MemoryBarrierRelease();
SDL_generic_TLS_mutex = mutex;
if (!SDL_generic_TLS_mutex) {
SDL_AtomicUnlock(&tls_lock);
return NULL;
}
}
SDL_AtomicUnlock(&tls_lock);
}
#endif /* SDL_THREADS_DISABLED */
SDL_MemoryBarrierAcquire();
SDL_LockMutex(SDL_generic_TLS_mutex);
for (entry = SDL_generic_TLS; entry; entry = entry->next) {
if (entry->thread == thread) {
storage = entry->storage;
break;
}
}
#if !SDL_THREADS_DISABLED
SDL_UnlockMutex(SDL_generic_TLS_mutex);
#endif
return storage;
}
SDL_ThreadID
#define SDL_ThreadID
Definition: SDL_dynapi_overrides.h:475
SDL_TLSEntry::storage
SDL_TLSData * storage
Definition: SDL_thread.c:115
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:260
SDL_generic_TLS_mutex
static SDL_mutex * SDL_generic_TLS_mutex
Definition: SDL_thread.c:119
SDL_AtomicLock
#define SDL_AtomicLock
Definition: SDL_dynapi_overrides.h:64
SDL_MemoryBarrierRelease
#define SDL_MemoryBarrierRelease()
Definition: SDL_atomic.h:189
SDL_CreateMutex
#define SDL_CreateMutex
Definition: SDL_dynapi_overrides.h:259
SDL_TLSData
Definition: SDL_thread_c.h:70
SDL_TLSEntry::thread
SDL_threadID thread
Definition: SDL_thread.c:114
mutex
static SDL_mutex * mutex
Definition: testlock.c:23
SDL_MemoryBarrierAcquire
#define SDL_MemoryBarrierAcquire()
Definition: SDL_atomic.h:190
SDL_TLSEntry::next
struct SDL_TLSEntry * next
Definition: SDL_thread.c:116
SDL_mutex
Definition: SDL_sysmutex.c:29
SDL_AtomicUnlock
#define SDL_AtomicUnlock
Definition: SDL_dynapi_overrides.h:65
SDL_TLSEntry
Definition: SDL_thread.c:113
NULL
#define NULL
Definition: begin_code.h:164
SDL_generic_TLS
static SDL_TLSEntry * SDL_generic_TLS
Definition: SDL_thread.c:120
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:262
SDL_SpinLock
int SDL_SpinLock
Definition: SDL_atomic.h:89
SDL_threadID
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().

{
SDL_threadID thread = SDL_ThreadID();
SDL_TLSEntry *prev, *entry;
/* SDL_Generic_GetTLSData() is always called first, so we can assume SDL_generic_TLS_mutex */
SDL_LockMutex(SDL_generic_TLS_mutex);
prev = NULL;
for (entry = SDL_generic_TLS; entry; entry = entry->next) {
if (entry->thread == thread) {
if (storage) {
entry->storage = storage;
} else {
if (prev) {
prev->next = entry->next;
} else {
SDL_generic_TLS = entry->next;
}
SDL_free(entry);
}
break;
}
prev = entry;
}
if (!entry) {
entry = (SDL_TLSEntry *)SDL_malloc(sizeof(*entry));
if (entry) {
entry->thread = thread;
entry->storage = storage;
entry->next = SDL_generic_TLS;
SDL_generic_TLS = entry;
}
}
SDL_UnlockMutex(SDL_generic_TLS_mutex);
if (!entry) {
return SDL_OutOfMemory();
}
return 0;
}
SDL_ThreadID
#define SDL_ThreadID
Definition: SDL_dynapi_overrides.h:475
SDL_TLSEntry::storage
SDL_TLSData * storage
Definition: SDL_thread.c:115
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:260
SDL_generic_TLS_mutex
static SDL_mutex * SDL_generic_TLS_mutex
Definition: SDL_thread.c:119
SDL_TLSEntry::thread
SDL_threadID thread
Definition: SDL_thread.c:114
SDL_TLSEntry::next
struct SDL_TLSEntry * next
Definition: SDL_thread.c:116
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_TLSEntry
Definition: SDL_thread.c:113
NULL
#define NULL
Definition: begin_code.h:164
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_generic_TLS
static SDL_TLSEntry * SDL_generic_TLS
Definition: SDL_thread.c:120
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:262
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
SDL_threadID
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().

{
thread_args *args = (thread_args *) data;
int (SDLCALL * userfunc) (void *) = args->func;
void *userdata = args->data;
SDL_Thread *thread = args->info;
int *statusloc = &thread->status;
/* Perform any system-dependent setup - this function may not fail */
SDL_SYS_SetupThread(thread->name);
/* Get the thread id */
thread->threadid = SDL_ThreadID();
/* Wake up the parent thread */
SDL_SemPost(args->wait);
/* Run the function */
*statusloc = userfunc(userdata);
/* Clean up thread-local storage */
SDL_TLSCleanup();
/* Mark us as ready to be joined (or detached) */
if (!SDL_AtomicCAS(&thread->state, SDL_THREAD_STATE_ALIVE, SDL_THREAD_STATE_ZOMBIE)) {
/* Clean up if something already detached us. */
if (SDL_AtomicCAS(&thread->state, SDL_THREAD_STATE_DETACHED, SDL_THREAD_STATE_CLEANED)) {
if (thread->name) {
SDL_free(thread->name);
}
SDL_free(thread);
}
}
}
SDL_ThreadID
#define SDL_ThreadID
Definition: SDL_dynapi_overrides.h:475
SDL_THREAD_STATE_ZOMBIE
Definition: SDL_thread_c.h:49
SDL_THREAD_STATE_DETACHED
Definition: SDL_thread_c.h:48
SDL_AtomicCAS
#define SDL_AtomicCAS
Definition: SDL_dynapi_overrides.h:66
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
thread_args
Definition: SDL_thread.c:256
SDL_Thread::status
int status
Definition: SDL_thread_c.h:58
SDL_THREAD_STATE_CLEANED
Definition: SDL_thread_c.h:50
SDL_SemPost
#define SDL_SemPost
Definition: SDL_dynapi_overrides.h:269
SDL_THREAD_STATE_ALIVE
Definition: SDL_thread_c.h:47
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_TLSCleanup
static void SDL_TLSCleanup()
Definition: SDL_thread.c:87
thread_args::func
int(* func)(void *)
Definition: SDL_thread.c:258
thread_args::info
SDL_Thread * info
Definition: SDL_thread.c:260
thread_args::wait
SDL_sem * wait
Definition: SDL_thread.c:261
SDL_SYS_SetupThread
void SDL_SYS_SetupThread(const char *name)
Definition: SDL_systhread.c:42
SDL_Thread
Definition: SDL_thread_c.h:54
thread_args::data
void * data
Definition: SDL_thread.c:259
SDLCALL
#define SDLCALL
Definition: SDL_internal.h:45