Update for SDL3 coding style (#6717)

I updated .clang-format and ran clang-format 14 over the src and test directories to standardize the code base.

In general I let clang-format have it's way, and added markup to prevent formatting of code that would break or be completely unreadable if formatted.

The script I ran for the src directory is added as build-scripts/clang-format-src.sh

This fixes:
#6592
#6593
#6594
This commit is contained in:
Sam Lantinga
2022-11-30 12:51:59 -08:00
committed by GitHub
parent 14b902faca
commit 5750bcb174
781 changed files with 51659 additions and 55763 deletions

View File

@@ -37,11 +37,11 @@ extern "C" {
on success.
*/
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
extern int SDL_SYS_CreateThread(SDL_Thread * thread,
extern int SDL_SYS_CreateThread(SDL_Thread *thread,
pfnSDL_CurrentBeginThread pfnBeginThread,
pfnSDL_CurrentEndThread pfnEndThread);
#else
extern int SDL_SYS_CreateThread(SDL_Thread * thread);
extern int SDL_SYS_CreateThread(SDL_Thread *thread);
#endif
/* This function does any necessary setup in the child thread */
@@ -53,10 +53,10 @@ extern int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority);
/* This function waits for the thread to finish and frees any data
allocated by SDL_SYS_CreateThread()
*/
extern void SDL_SYS_WaitThread(SDL_Thread * thread);
extern void SDL_SYS_WaitThread(SDL_Thread *thread);
/* Mark thread as cleaned up as soon as it exits, without joining. */
extern void SDL_SYS_DetachThread(SDL_Thread * thread);
extern void SDL_SYS_DetachThread(SDL_Thread *thread);
/* Get the thread local storage for this thread */
extern SDL_TLSData *SDL_SYS_GetTLSData(void);
@@ -66,7 +66,7 @@ extern int SDL_SYS_SetTLSData(SDL_TLSData *data);
/* This is for internal SDL use, so we don't need #ifdefs everywhere. */
extern SDL_Thread *
SDL_CreateThreadInternal(int (SDLCALL * fn) (void *), const char *name,
SDL_CreateThreadInternal(int(SDLCALL *fn)(void *), const char *name,
const size_t stacksize, void *data);
/* Ends C function definitions when using C++ */

View File

@@ -26,12 +26,11 @@
#include "SDL_systhread.h"
#include "../SDL_error_c.h"
SDL_TLSID
SDL_TLSCreate()
{
static SDL_atomic_t SDL_tls_id;
return SDL_AtomicIncRef(&SDL_tls_id)+1;
return SDL_AtomicIncRef(&SDL_tls_id) + 1;
}
void *
@@ -43,11 +42,10 @@ SDL_TLSGet(SDL_TLSID id)
if (storage == NULL || id == 0 || id > storage->limit) {
return NULL;
}
return storage->array[id-1].data;
return storage->array[id - 1].data;
}
int
SDL_TLSSet(SDL_TLSID id, const void *value, void (SDLCALL *destructor)(void *))
int SDL_TLSSet(SDL_TLSID id, const void *value, void(SDLCALL *destructor)(void *))
{
SDL_TLSData *storage;
@@ -61,7 +59,7 @@ SDL_TLSSet(SDL_TLSID id, const void *value, void (SDLCALL *destructor)(void *))
oldlimit = storage ? storage->limit : 0;
newlimit = (id + TLS_ALLOC_CHUNKSIZE);
storage = (SDL_TLSData *)SDL_realloc(storage, sizeof(*storage)+(newlimit-1)*sizeof(storage->array[0]));
storage = (SDL_TLSData *)SDL_realloc(storage, sizeof(*storage) + (newlimit - 1) * sizeof(storage->array[0]));
if (storage == NULL) {
return SDL_OutOfMemory();
}
@@ -75,13 +73,12 @@ SDL_TLSSet(SDL_TLSID id, const void *value, void (SDLCALL *destructor)(void *))
}
}
storage->array[id-1].data = SDL_const_cast(void*, value);
storage->array[id-1].destructor = destructor;
storage->array[id - 1].data = SDL_const_cast(void *, value);
storage->array[id - 1].destructor = destructor;
return 0;
}
void
SDL_TLSCleanup()
void SDL_TLSCleanup()
{
SDL_TLSData *storage;
@@ -98,7 +95,6 @@ SDL_TLSCleanup()
}
}
/* This is a generic implementation of thread-local storage which doesn't
require additional OS support.
@@ -107,7 +103,8 @@ SDL_TLSCleanup()
storage this implementation should be improved to be production quality.
*/
typedef struct SDL_TLSEntry {
typedef struct SDL_TLSEntry
{
SDL_threadID thread;
SDL_TLSData *storage;
struct SDL_TLSEntry *next;
@@ -116,7 +113,6 @@ typedef struct SDL_TLSEntry {
static SDL_mutex *SDL_generic_TLS_mutex;
static SDL_TLSEntry *SDL_generic_TLS;
SDL_TLSData *
SDL_Generic_GetTLSData(void)
{
@@ -156,8 +152,7 @@ SDL_Generic_GetTLSData(void)
return storage;
}
int
SDL_Generic_SetTLSData(SDL_TLSData *storage)
int SDL_Generic_SetTLSData(SDL_TLSData *storage)
{
SDL_threadID thread = SDL_ThreadID();
SDL_TLSEntry *prev, *entry;
@@ -199,8 +194,7 @@ SDL_Generic_SetTLSData(SDL_TLSData *storage)
}
/* Non-thread-safe global error variable */
static SDL_error *
SDL_GetStaticErrBuf()
static SDL_error *SDL_GetStaticErrBuf()
{
static SDL_error SDL_global_error;
static char SDL_global_error_str[128];
@@ -210,8 +204,7 @@ SDL_GetStaticErrBuf()
}
#if !SDL_THREADS_DISABLED
static void SDLCALL
SDL_FreeErrBuf(void *data)
static void SDLCALL SDL_FreeErrBuf(void *data)
{
SDL_error *errbuf = (SDL_error *)data;
@@ -284,12 +277,10 @@ SDL_GetErrBuf(void)
#endif /* SDL_THREADS_DISABLED */
}
void
SDL_RunThread(SDL_Thread *thread)
void SDL_RunThread(SDL_Thread *thread)
{
void *userdata = thread->userdata;
int (SDLCALL * userfunc) (void *) = thread->userfunc;
int(SDLCALL * userfunc)(void *) = thread->userfunc;
int *statusloc = &thread->status;
@@ -322,27 +313,27 @@ SDL_RunThread(SDL_Thread *thread)
#undef SDL_CreateThreadWithStackSize
#endif
#if SDL_DYNAMIC_API
#define SDL_CreateThread SDL_CreateThread_REAL
#define SDL_CreateThread SDL_CreateThread_REAL
#define SDL_CreateThreadWithStackSize SDL_CreateThreadWithStackSize_REAL
#endif
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
SDL_Thread *
SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
const char *name, const size_t stacksize, void *data,
pfnSDL_CurrentBeginThread pfnBeginThread,
pfnSDL_CurrentEndThread pfnEndThread)
SDL_CreateThreadWithStackSize(int(SDLCALL *fn)(void *),
const char *name, const size_t stacksize, void *data,
pfnSDL_CurrentBeginThread pfnBeginThread,
pfnSDL_CurrentEndThread pfnEndThread)
#else
SDL_Thread *
SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
const char *name, const size_t stacksize, void *data)
SDL_CreateThreadWithStackSize(int(SDLCALL *fn)(void *),
const char *name, const size_t stacksize, void *data)
#endif
{
SDL_Thread *thread;
int ret;
/* Allocate memory for the thread info structure */
thread = (SDL_Thread *) SDL_calloc(1, sizeof(*thread));
thread = (SDL_Thread *)SDL_calloc(1, sizeof(*thread));
if (thread == NULL) {
SDL_OutOfMemory();
return NULL;
@@ -383,13 +374,13 @@ SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
DECLSPEC SDL_Thread *SDLCALL
SDL_CreateThread(int (SDLCALL * fn) (void *),
SDL_CreateThread(int(SDLCALL *fn)(void *),
const char *name, void *data,
pfnSDL_CurrentBeginThread pfnBeginThread,
pfnSDL_CurrentEndThread pfnEndThread)
#else
DECLSPEC SDL_Thread *SDLCALL
SDL_CreateThread(int (SDLCALL * fn) (void *),
SDL_CreateThread(int(SDLCALL *fn)(void *),
const char *name, void *data)
#endif
{
@@ -401,9 +392,9 @@ SDL_CreateThread(int (SDLCALL * fn) (void *),
if (stackhint != NULL) {
char *endp = NULL;
const Sint64 hintval = SDL_strtoll(stackhint, &endp, 10);
if ((*stackhint != '\0') && (*endp == '\0')) { /* a valid number? */
if (hintval > 0) { /* reject bogus values. */
stacksize = (size_t) hintval;
if ((*stackhint != '\0') && (*endp == '\0')) { /* a valid number? */
if (hintval > 0) { /* reject bogus values. */
stacksize = (size_t)hintval;
}
}
}
@@ -416,8 +407,9 @@ SDL_CreateThread(int (SDLCALL * fn) (void *),
}
SDL_Thread *
SDL_CreateThreadInternal(int (SDLCALL * fn) (void *), const char *name,
const size_t stacksize, void *data) {
SDL_CreateThreadInternal(int(SDLCALL *fn)(void *), const char *name,
const size_t stacksize, void *data)
{
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
return SDL_CreateThreadWithStackSize(fn, name, stacksize, data, NULL, NULL);
#else
@@ -426,7 +418,7 @@ SDL_CreateThreadInternal(int (SDLCALL * fn) (void *), const char *name,
}
SDL_threadID
SDL_GetThreadID(SDL_Thread * thread)
SDL_GetThreadID(SDL_Thread *thread)
{
SDL_threadID id;
@@ -439,7 +431,7 @@ SDL_GetThreadID(SDL_Thread * thread)
}
const char *
SDL_GetThreadName(SDL_Thread * thread)
SDL_GetThreadName(SDL_Thread *thread)
{
if (thread) {
return thread->name;
@@ -448,14 +440,12 @@ SDL_GetThreadName(SDL_Thread * thread)
}
}
int
SDL_SetThreadPriority(SDL_ThreadPriority priority)
int SDL_SetThreadPriority(SDL_ThreadPriority priority)
{
return SDL_SYS_SetThreadPriority(priority);
}
void
SDL_WaitThread(SDL_Thread * thread, int *status)
void SDL_WaitThread(SDL_Thread *thread, int *status)
{
if (thread) {
SDL_SYS_WaitThread(thread);
@@ -469,8 +459,7 @@ SDL_WaitThread(SDL_Thread * thread, int *status)
}
}
void
SDL_DetachThread(SDL_Thread * thread)
void SDL_DetachThread(SDL_Thread *thread)
{
if (thread == NULL) {
return;
@@ -483,9 +472,9 @@ SDL_DetachThread(SDL_Thread * thread)
/* all other states are pretty final, see where we landed. */
const int thread_state = SDL_AtomicGet(&thread->state);
if ((thread_state == SDL_THREAD_STATE_DETACHED) || (thread_state == SDL_THREAD_STATE_CLEANED)) {
return; /* already detached (you shouldn't call this twice!) */
return; /* already detached (you shouldn't call this twice!) */
} else if (thread_state == SDL_THREAD_STATE_ZOMBIE) {
SDL_WaitThread(thread, NULL); /* already done, clean it up. */
SDL_WaitThread(thread, NULL); /* already done, clean it up. */
} else {
SDL_assert(0 && "Unexpected thread state");
}

View File

@@ -23,7 +23,6 @@
#ifndef SDL_thread_c_h_
#define SDL_thread_c_h_
/* Need the definitions of SYS_ThreadHandle */
#if SDL_THREADS_DISABLED
#include "generic/SDL_systhread_c.h"
@@ -63,25 +62,27 @@ struct SDL_Thread
SDL_threadID threadid;
SYS_ThreadHandle handle;
int status;
SDL_atomic_t state; /* SDL_THREAD_STATE_* */
SDL_atomic_t state; /* SDL_THREAD_STATE_* */
SDL_error errbuf;
char *name;
size_t stacksize; /* 0 for default, >0 for user-specified stack size. */
int (SDLCALL * userfunc) (void *);
size_t stacksize; /* 0 for default, >0 for user-specified stack size. */
int(SDLCALL *userfunc)(void *);
void *userdata;
void *data;
void *endfunc; /* only used on some platforms. */
void *endfunc; /* only used on some platforms. */
};
/* This is the function called to run a thread */
extern void SDL_RunThread(SDL_Thread *thread);
/* This is the system-independent thread local storage structure */
typedef struct {
typedef struct
{
unsigned int limit;
struct {
struct
{
void *data;
void (SDLCALL *destructor)(void*);
void(SDLCALL *destructor)(void *);
} array[1];
} SDL_TLSData;

View File

@@ -26,7 +26,6 @@
implementation, written by Christopher Tate and Owen Smith. Thanks!
*/
#include "../generic/SDL_syscond_c.h"
/* If two implementations are to be compiled into SDL (the active one
@@ -57,7 +56,7 @@ SDL_CreateCond_generic(void)
{
SDL_cond_generic *cond;
cond = (SDL_cond_generic *) SDL_malloc(sizeof(SDL_cond_generic));
cond = (SDL_cond_generic *)SDL_malloc(sizeof(SDL_cond_generic));
if (cond) {
cond->lock = SDL_CreateMutex();
cond->wait_sem = SDL_CreateSemaphore(0);
@@ -74,8 +73,7 @@ SDL_CreateCond_generic(void)
}
/* Destroy a condition variable */
void
SDL_DestroyCond_generic(SDL_cond * _cond)
void SDL_DestroyCond_generic(SDL_cond *_cond)
{
SDL_cond_generic *cond = (SDL_cond_generic *)_cond;
if (cond) {
@@ -93,8 +91,7 @@ SDL_DestroyCond_generic(SDL_cond * _cond)
}
/* Restart one of the threads that are waiting on the condition variable */
int
SDL_CondSignal_generic(SDL_cond * _cond)
int SDL_CondSignal_generic(SDL_cond *_cond)
{
SDL_cond_generic *cond = (SDL_cond_generic *)_cond;
if (cond == NULL) {
@@ -118,8 +115,7 @@ SDL_CondSignal_generic(SDL_cond * _cond)
}
/* Restart all threads that are waiting on the condition variable */
int
SDL_CondBroadcast_generic(SDL_cond * _cond)
int SDL_CondBroadcast_generic(SDL_cond *_cond)
{
SDL_cond_generic *cond = (SDL_cond_generic *)_cond;
if (cond == NULL) {
@@ -173,8 +169,7 @@ Thread B:
SDL_CondSignal(cond);
SDL_UnlockMutex(lock);
*/
int
SDL_CondWaitTimeout_generic(SDL_cond * _cond, SDL_mutex * mutex, Uint32 ms)
int SDL_CondWaitTimeout_generic(SDL_cond *_cond, SDL_mutex *mutex, Uint32 ms)
{
SDL_cond_generic *cond = (SDL_cond_generic *)_cond;
int retval;
@@ -229,8 +224,7 @@ SDL_CondWaitTimeout_generic(SDL_cond * _cond, SDL_mutex * mutex, Uint32 ms)
}
/* Wait on the condition variable forever */
int
SDL_CondWait_generic(SDL_cond * cond, SDL_mutex * mutex)
int SDL_CondWait_generic(SDL_cond *cond, SDL_mutex *mutex)
{
return SDL_CondWaitTimeout_generic(cond, mutex, SDL_MUTEX_MAXWAIT);
}

View File

@@ -20,19 +20,18 @@
*/
#include "SDL_internal.h"
#ifndef SDL_syscond_generic_h_
#define SDL_syscond_generic_h_
#if SDL_THREAD_GENERIC_COND_SUFFIX
SDL_cond * SDL_CreateCond_generic(void);
void SDL_DestroyCond_generic(SDL_cond * cond);
int SDL_CondSignal_generic(SDL_cond * cond);
int SDL_CondBroadcast_generic(SDL_cond * cond);
int SDL_CondWait_generic(SDL_cond * cond, SDL_mutex * mutex);
int SDL_CondWaitTimeout_generic(SDL_cond * cond,
SDL_mutex * mutex, Uint32 ms);
SDL_cond *SDL_CreateCond_generic(void);
void SDL_DestroyCond_generic(SDL_cond *cond);
int SDL_CondSignal_generic(SDL_cond *cond);
int SDL_CondBroadcast_generic(SDL_cond *cond);
int SDL_CondWait_generic(SDL_cond *cond, SDL_mutex *mutex);
int SDL_CondWaitTimeout_generic(SDL_cond *cond,
SDL_mutex *mutex, Uint32 ms);
#endif /* SDL_THREAD_GENERIC_COND_SUFFIX */

View File

@@ -24,7 +24,6 @@
#include "SDL_systhread_c.h"
struct SDL_mutex
{
int recursive;
@@ -39,7 +38,7 @@ SDL_CreateMutex(void)
SDL_mutex *mutex;
/* Allocate mutex memory */
mutex = (SDL_mutex *) SDL_calloc(1, sizeof(*mutex));
mutex = (SDL_mutex *)SDL_calloc(1, sizeof(*mutex));
#if !SDL_THREADS_DISABLED
if (mutex) {
@@ -60,8 +59,7 @@ SDL_CreateMutex(void)
}
/* Free the mutex */
void
SDL_DestroyMutex(SDL_mutex * mutex)
void SDL_DestroyMutex(SDL_mutex *mutex)
{
if (mutex) {
if (mutex->sem) {
@@ -72,8 +70,7 @@ SDL_DestroyMutex(SDL_mutex * mutex)
}
/* Lock the mutex */
int
SDL_LockMutex(SDL_mutex * mutex)
int SDL_LockMutex(SDL_mutex *mutex)
{
#if SDL_THREADS_DISABLED
return 0;
@@ -102,8 +99,7 @@ SDL_LockMutex(SDL_mutex * mutex)
}
/* try Lock the mutex */
int
SDL_TryLockMutex(SDL_mutex * mutex)
int SDL_TryLockMutex(SDL_mutex *mutex)
{
#if SDL_THREADS_DISABLED
return 0;
@@ -135,8 +131,7 @@ SDL_TryLockMutex(SDL_mutex * mutex)
}
/* Unlock the mutex */
int
SDL_mutexV(SDL_mutex * mutex)
int SDL_mutexV(SDL_mutex *mutex)
{
#if SDL_THREADS_DISABLED
return 0;

View File

@@ -24,47 +24,41 @@
#include "SDL_systhread_c.h"
#if SDL_THREADS_DISABLED
SDL_sem *
SDL_CreateSemaphore(Uint32 initial_value)
{
SDL_SetError("SDL not built with thread support");
return (SDL_sem *) 0;
return (SDL_sem *)0;
}
void
SDL_DestroySemaphore(SDL_sem * sem)
void SDL_DestroySemaphore(SDL_sem *sem)
{
}
int
SDL_SemTryWait(SDL_sem * sem)
int SDL_SemTryWait(SDL_sem *sem)
{
return SDL_SetError("SDL not built with thread support");
}
int
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
{
return SDL_SetError("SDL not built with thread support");
}
int
SDL_SemWait(SDL_sem * sem)
int SDL_SemWait(SDL_sem *sem)
{
return SDL_SetError("SDL not built with thread support");
}
Uint32
SDL_SemValue(SDL_sem * sem)
SDL_SemValue(SDL_sem *sem)
{
return 0;
}
int
SDL_SemPost(SDL_sem * sem)
int SDL_SemPost(SDL_sem *sem)
{
return SDL_SetError("SDL not built with thread support");
}
@@ -84,7 +78,7 @@ SDL_CreateSemaphore(Uint32 initial_value)
{
SDL_sem *sem;
sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
if (sem == NULL) {
SDL_OutOfMemory();
return NULL;
@@ -105,8 +99,7 @@ SDL_CreateSemaphore(Uint32 initial_value)
/* WARNING:
You cannot call this function when another thread is using the semaphore.
*/
void
SDL_DestroySemaphore(SDL_sem * sem)
void SDL_DestroySemaphore(SDL_sem *sem)
{
if (sem) {
sem->count = 0xFFFFFFFF;
@@ -124,8 +117,7 @@ SDL_DestroySemaphore(SDL_sem * sem)
}
}
int
SDL_SemTryWait(SDL_sem * sem)
int SDL_SemTryWait(SDL_sem *sem)
{
int retval;
@@ -144,8 +136,7 @@ SDL_SemTryWait(SDL_sem * sem)
return retval;
}
int
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
{
int retval;
@@ -174,14 +165,13 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
return retval;
}
int
SDL_SemWait(SDL_sem * sem)
int SDL_SemWait(SDL_sem *sem)
{
return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
}
Uint32
SDL_SemValue(SDL_sem * sem)
SDL_SemValue(SDL_sem *sem)
{
Uint32 value;
@@ -194,8 +184,7 @@ SDL_SemValue(SDL_sem * sem)
return value;
}
int
SDL_SemPost(SDL_sem * sem)
int SDL_SemPost(SDL_sem *sem)
{
if (sem == NULL) {
return SDL_InvalidParamError("sem");

View File

@@ -25,20 +25,17 @@
#include "../SDL_systhread.h"
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
int
SDL_SYS_CreateThread(SDL_Thread * thread,
pfnSDL_CurrentBeginThread pfnBeginThread,
pfnSDL_CurrentEndThread pfnEndThread)
int SDL_SYS_CreateThread(SDL_Thread *thread,
pfnSDL_CurrentBeginThread pfnBeginThread,
pfnSDL_CurrentEndThread pfnEndThread)
#else
int
SDL_SYS_CreateThread(SDL_Thread * thread)
int SDL_SYS_CreateThread(SDL_Thread *thread)
#endif /* SDL_PASSED_BEGINTHREAD_ENDTHREAD */
{
return SDL_SetError("Threads are not supported on this platform");
}
void
SDL_SYS_SetupThread(const char *name)
void SDL_SYS_SetupThread(const char *name)
{
return;
}
@@ -49,20 +46,17 @@ SDL_ThreadID(void)
return 0;
}
int
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
{
return 0;
}
void
SDL_SYS_WaitThread(SDL_Thread * thread)
void SDL_SYS_WaitThread(SDL_Thread *thread)
{
return;
}
void
SDL_SYS_DetachThread(SDL_Thread * thread)
void SDL_SYS_DetachThread(SDL_Thread *thread)
{
return;
}

View File

@@ -22,15 +22,13 @@
#include "SDL_internal.h"
#include "../SDL_thread_c.h"
SDL_TLSData *
SDL_SYS_GetTLSData(void)
{
return SDL_Generic_GetTLSData();
}
int
SDL_SYS_SetTLSData(SDL_TLSData *data)
int SDL_SYS_SetTLSData(SDL_TLSData *data)
{
return SDL_Generic_SetTLSData(data);
}

View File

@@ -35,7 +35,7 @@ struct SDL_cond
SDL_cond *
SDL_CreateCond(void)
{
SDL_cond *cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
SDL_cond *cond = (SDL_cond *)SDL_malloc(sizeof(SDL_cond));
if (cond) {
CondVar_Init(&cond->cond_variable);
} else {
@@ -45,8 +45,7 @@ SDL_CreateCond(void)
}
/* Destroy a condition variable */
void
SDL_DestroyCond(SDL_cond *cond)
void SDL_DestroyCond(SDL_cond *cond)
{
if (cond) {
SDL_free(cond);
@@ -54,8 +53,7 @@ SDL_DestroyCond(SDL_cond *cond)
}
/* Restart one of the threads that are waiting on the condition variable */
int
SDL_CondSignal(SDL_cond *cond)
int SDL_CondSignal(SDL_cond *cond)
{
if (cond == NULL) {
return SDL_InvalidParamError("cond");
@@ -66,8 +64,7 @@ SDL_CondSignal(SDL_cond *cond)
}
/* Restart all threads that are waiting on the condition variable */
int
SDL_CondBroadcast(SDL_cond *cond)
int SDL_CondBroadcast(SDL_cond *cond)
{
if (cond == NULL) {
return SDL_InvalidParamError("cond");
@@ -98,8 +95,7 @@ Thread B:
SDL_CondSignal(cond);
SDL_UnlockMutex(lock);
*/
int
SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
{
Result res;
@@ -115,15 +111,14 @@ SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
CondVar_Wait(&cond->cond_variable, &mutex->lock.lock);
} else {
res = CondVar_WaitTimeout(&cond->cond_variable, &mutex->lock.lock,
(s64) ms * 1000000LL);
(s64)ms * 1000000LL);
}
return R_SUCCEEDED(res) ? 0 : SDL_MUTEX_TIMEDOUT;
}
/* Wait on the condition variable forever */
int
SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
{
return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
}

View File

@@ -33,7 +33,7 @@ SDL_CreateMutex(void)
SDL_mutex *mutex;
/* Allocate mutex memory */
mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex));
if (mutex) {
RecursiveLock_Init(&mutex->lock);
} else {
@@ -43,8 +43,7 @@ SDL_CreateMutex(void)
}
/* Free the mutex */
void
SDL_DestroyMutex(SDL_mutex *mutex)
void SDL_DestroyMutex(SDL_mutex *mutex)
{
if (mutex) {
SDL_free(mutex);
@@ -52,8 +51,7 @@ SDL_DestroyMutex(SDL_mutex *mutex)
}
/* Lock the mutex */
int
SDL_LockMutex(SDL_mutex *mutex)
int SDL_LockMutex(SDL_mutex *mutex)
{
if (mutex == NULL) {
return SDL_InvalidParamError("mutex");
@@ -65,8 +63,7 @@ SDL_LockMutex(SDL_mutex *mutex)
}
/* try Lock the mutex */
int
SDL_TryLockMutex(SDL_mutex *mutex)
int SDL_TryLockMutex(SDL_mutex *mutex)
{
if (mutex == NULL) {
return SDL_InvalidParamError("mutex");
@@ -76,8 +73,7 @@ SDL_TryLockMutex(SDL_mutex *mutex)
}
/* Unlock the mutex */
int
SDL_mutexV(SDL_mutex *mutex)
int SDL_mutexV(SDL_mutex *mutex)
{
if (mutex == NULL) {
return SDL_InvalidParamError("mutex");

View File

@@ -25,7 +25,6 @@
#include <3ds.h>
struct SDL_mutex
{
RecursiveLock lock;

View File

@@ -26,7 +26,6 @@
#include <3ds.h>
struct SDL_semaphore
{
LightSemaphore semaphore;
@@ -42,7 +41,7 @@ SDL_CreateSemaphore(Uint32 initial_value)
return NULL;
}
sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
if (sem == NULL) {
SDL_OutOfMemory();
return NULL;
@@ -56,16 +55,14 @@ SDL_CreateSemaphore(Uint32 initial_value)
/* WARNING:
You cannot call this function when another thread is using the semaphore.
*/
void
SDL_DestroySemaphore(SDL_sem *sem)
void SDL_DestroySemaphore(SDL_sem *sem)
{
if (sem) {
SDL_free(sem);
}
}
int
SDL_SemTryWait(SDL_sem *sem)
int SDL_SemTryWait(SDL_sem *sem)
{
if (sem == NULL) {
return SDL_InvalidParamError("sem");
@@ -74,8 +71,7 @@ SDL_SemTryWait(SDL_sem *sem)
return SDL_SemWaitTimeout(sem, 0);
}
int
SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
{
int retval;
@@ -103,8 +99,7 @@ SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
return retval;
}
int
SDL_SemWait(SDL_sem *sem)
int SDL_SemWait(SDL_sem *sem)
{
return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
}
@@ -119,8 +114,7 @@ SDL_SemValue(SDL_sem *sem)
return sem->semaphore.current_count;
}
int
SDL_SemPost(SDL_sem *sem)
int SDL_SemPost(SDL_sem *sem)
{
if (sem == NULL) {
return SDL_InvalidParamError("sem");

View File

@@ -37,10 +37,9 @@
static size_t GetStackSize(size_t requested_size);
static void
ThreadEntry(void *arg)
static void ThreadEntry(void *arg)
{
SDL_RunThread((SDL_Thread *) arg);
SDL_RunThread((SDL_Thread *)arg);
threadExit(0);
}
@@ -48,8 +47,7 @@ ThreadEntry(void *arg)
#error "SDL_PASSED_BEGINTHREAD_ENDTHREAD is not supported on N3DS"
#endif
int
SDL_SYS_CreateThread(SDL_Thread *thread)
int SDL_SYS_CreateThread(SDL_Thread *thread)
{
s32 priority = N3DS_THREAD_PRIORITY_MEDIUM;
size_t stack_size = GetStackSize(thread->stacksize);
@@ -86,8 +84,7 @@ GetStackSize(size_t requested_size)
return requested_size;
}
void
SDL_SYS_SetupThread(const char *name)
void SDL_SYS_SetupThread(const char *name)
{
return;
}
@@ -97,11 +94,10 @@ SDL_ThreadID(void)
{
u32 thread_ID = 0;
svcGetThreadId(&thread_ID, CUR_THREAD_HANDLE);
return (SDL_threadID) thread_ID;
return (SDL_threadID)thread_ID;
}
int
SDL_SYS_SetThreadPriority(SDL_ThreadPriority sdl_priority)
int SDL_SYS_SetThreadPriority(SDL_ThreadPriority sdl_priority)
{
s32 svc_priority;
switch (sdl_priority) {
@@ -120,11 +116,10 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority sdl_priority)
default:
svc_priority = N3DS_THREAD_PRIORITY_MEDIUM;
}
return (int) svcSetThreadPriority(CUR_THREAD_HANDLE, svc_priority);
return (int)svcSetThreadPriority(CUR_THREAD_HANDLE, svc_priority);
}
void
SDL_SYS_WaitThread(SDL_Thread *thread)
void SDL_SYS_WaitThread(SDL_Thread *thread)
{
Result res = threadJoin(thread->handle, U64_MAX);
@@ -137,8 +132,7 @@ SDL_SYS_WaitThread(SDL_Thread *thread)
}
}
void
SDL_SYS_DetachThread(SDL_Thread *thread)
void SDL_SYS_DetachThread(SDL_Thread *thread)
{
threadDetach(thread->handle);
}

View File

@@ -31,9 +31,9 @@ struct SDL_mutex
TInt handle;
};
extern TInt CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny*, TAny*);
extern TInt CreateUnique(TInt (*aFunc)(const TDesC &aName, TAny *, TAny *), TAny *, TAny *);
static TInt NewMutex(const TDesC& aName, TAny* aPtr1, TAny*)
static TInt NewMutex(const TDesC &aName, TAny *aPtr1, TAny *)
{
return ((RMutex *)aPtr1)->CreateGlobal(aName);
}
@@ -49,21 +49,20 @@ SDL_CreateMutex(void)
SDL_SetError("Couldn't create mutex.");
return NULL;
}
SDL_mutex* mutex = new /*(ELeave)*/ SDL_mutex;
SDL_mutex *mutex = new /*(ELeave)*/ SDL_mutex;
mutex->handle = rmutex.Handle();
return mutex;
}
/* Free the mutex */
void
SDL_DestroyMutex(SDL_mutex * mutex)
void SDL_DestroyMutex(SDL_mutex *mutex)
{
if (mutex) {
RMutex rmutex;
rmutex.SetHandle(mutex->handle);
rmutex.Signal();
rmutex.Close();
delete(mutex);
delete (mutex);
mutex = NULL;
}
}
@@ -84,8 +83,7 @@ SDL_TryLockMutex(SDL_mutex * mutex)
#endif
/* Lock the mutex */
int
SDL_LockMutex(SDL_mutex * mutex)
int SDL_LockMutex(SDL_mutex *mutex)
{
if (mutex == NULL) {
return SDL_InvalidParamError("mutex");
@@ -99,8 +97,7 @@ SDL_LockMutex(SDL_mutex * mutex)
}
/* Unlock the mutex */
int
SDL_UnlockMutex(SDL_mutex * mutex)
int SDL_UnlockMutex(SDL_mutex *mutex)
{
if (mutex == NULL) {
return SDL_InvalidParamError("mutex");

View File

@@ -24,7 +24,6 @@
#include <e32std.h>
#define SDL_MUTEX_TIMEOUT -2
struct SDL_semaphore
@@ -35,18 +34,17 @@ struct SDL_semaphore
struct TInfo
{
TInfo(TInt aTime, TInt aHandle) :
iTime(aTime), iHandle(aHandle), iVal(0) {}
TInfo(TInt aTime, TInt aHandle) : iTime(aTime), iHandle(aHandle), iVal(0) {}
TInt iTime;
TInt iHandle;
TInt iVal;
};
extern TInt CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny*, TAny*);
extern TInt CreateUnique(TInt (*aFunc)(const TDesC &aName, TAny *, TAny *), TAny *, TAny *);
static TBool RunThread(TAny* aInfo)
static TBool RunThread(TAny *aInfo)
{
TInfo* info = STATIC_CAST(TInfo*, aInfo);
TInfo *info = STATIC_CAST(TInfo *, aInfo);
User::After(info->iTime);
RSemaphore sema;
sema.SetHandle(info->iHandle);
@@ -55,15 +53,14 @@ static TBool RunThread(TAny* aInfo)
return 0;
}
static TInt
NewThread(const TDesC& aName, TAny* aPtr1, TAny* aPtr2)
static TInt NewThread(const TDesC &aName, TAny *aPtr1, TAny *aPtr2)
{
return ((RThread *)(aPtr1))->Create(aName, RunThread, KDefaultStackSize, NULL, aPtr2);
}
static TInt NewSema(const TDesC& aName, TAny* aPtr1, TAny* aPtr2)
static TInt NewSema(const TDesC &aName, TAny *aPtr1, TAny *aPtr2)
{
TInt value = *((TInt*) aPtr2);
TInt value = *((TInt *)aPtr2);
return ((RSemaphore *)aPtr1)->CreateGlobal(aName, value);
}
@@ -85,14 +82,13 @@ SDL_CreateSemaphore(Uint32 initial_value)
if (status != KErrNone) {
SDL_SetError("Couldn't create semaphore");
}
SDL_semaphore* sem = new /*(ELeave)*/ SDL_semaphore;
SDL_semaphore *sem = new /*(ELeave)*/ SDL_semaphore;
sem->handle = s.Handle();
sem->count = initial_value;
return sem;
}
void
SDL_DestroySemaphore(SDL_sem * sem)
void SDL_DestroySemaphore(SDL_sem *sem)
{
if (sem != NULL) {
RSemaphore sema;
@@ -104,8 +100,7 @@ SDL_DestroySemaphore(SDL_sem * sem)
}
}
int
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
{
if (sem == NULL) {
return SDL_InvalidParamError("sem");
@@ -117,8 +112,8 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
}
RThread thread;
TInfo* info = new (ELeave)TInfo(timeout, sem->handle);
TInt status = CreateUnique(NewThread, &thread, info);
TInfo *info = new (ELeave) TInfo(timeout, sem->handle);
TInt status = CreateUnique(NewThread, &thread, info);
if (status != KErrNone) {
return status;
@@ -135,8 +130,7 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
return info->iVal;
}
int
SDL_SemTryWait(SDL_sem *sem)
int SDL_SemTryWait(SDL_sem *sem)
{
if (sem == NULL) {
return SDL_InvalidParamError("sem");
@@ -148,14 +142,13 @@ SDL_SemTryWait(SDL_sem *sem)
return SDL_MUTEX_TIMEOUT;
}
int
SDL_SemWait(SDL_sem * sem)
int SDL_SemWait(SDL_sem *sem)
{
return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
}
Uint32
SDL_SemValue(SDL_sem * sem)
SDL_SemValue(SDL_sem *sem)
{
if (sem == NULL) {
SDL_InvalidParamError("sem");
@@ -164,8 +157,7 @@ SDL_SemValue(SDL_sem * sem)
return sem->count;
}
int
SDL_SemPost(SDL_sem * sem)
int SDL_SemPost(SDL_sem *sem)
{
if (sem == NULL) {
return SDL_InvalidParamError("sem");

View File

@@ -34,24 +34,21 @@ extern "C" {
static int object_count;
static int
RunThread(TAny* data)
static int RunThread(TAny *data)
{
SDL_RunThread((SDL_Thread*)data);
SDL_RunThread((SDL_Thread *)data);
return 0;
}
static TInt
NewThread(const TDesC& aName, TAny* aPtr1, TAny* aPtr2)
static TInt NewThread(const TDesC &aName, TAny *aPtr1, TAny *aPtr2)
{
return ((RThread *)(aPtr1))->Create(aName, RunThread, KDefaultStackSize, NULL, aPtr2);
}
int
CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny* aPtr1, TAny* aPtr2)
int CreateUnique(TInt (*aFunc)(const TDesC &aName, TAny *, TAny *), TAny *aPtr1, TAny *aPtr2)
{
TBuf<16> name;
TInt status = KErrNone;
TInt status = KErrNone;
do {
object_count++;
name.Format(_L("SDL_%x"), object_count);
@@ -60,14 +57,13 @@ CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny* aPtr1, TAny*
return status;
}
int
SDL_SYS_CreateThread(SDL_Thread *thread)
int SDL_SYS_CreateThread(SDL_Thread *thread)
{
RThread rthread;
TInt status = CreateUnique(NewThread, &rthread, thread);
if (status != KErrNone) {
delete(((RThread*)(thread->handle)));
delete (((RThread *)(thread->handle)));
thread->handle = NULL;
return SDL_SetError("Not enough resources to create thread");
}
@@ -77,8 +73,7 @@ SDL_SYS_CreateThread(SDL_Thread *thread)
return 0;
}
void
SDL_SYS_SetupThread(const char *name)
void SDL_SYS_SetupThread(const char *name)
{
return;
}
@@ -86,19 +81,17 @@ SDL_SYS_SetupThread(const char *name)
SDL_threadID
SDL_ThreadID(void)
{
RThread current;
RThread current;
TThreadId id = current.Id();
return id;
}
int
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
{
return 0;
}
void
SDL_SYS_WaitThread(SDL_Thread * thread)
void SDL_SYS_WaitThread(SDL_Thread *thread)
{
RThread t;
t.Open(thread->threadid);
@@ -110,8 +103,7 @@ SDL_SYS_WaitThread(SDL_Thread * thread)
t.Close();
}
void
SDL_SYS_DetachThread(SDL_Thread * thread)
void SDL_SYS_DetachThread(SDL_Thread *thread)
{
return;
}

View File

@@ -28,14 +28,15 @@
#include <stdlib.h>
#include <timer_alarm.h>
#include <kernel.h>
struct SDL_semaphore {
s32 semid;
struct SDL_semaphore
{
s32 semid;
};
static void usercb(struct timer_alarm_t *alarm, void *arg) {
static void usercb(struct timer_alarm_t *alarm, void *arg)
{
iReleaseWaitThread((int)arg);
}
@@ -45,12 +46,12 @@ SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
SDL_sem *sem;
ee_sema_t sema;
sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
if (sem != NULL) {
/* TODO: Figure out the limit on the maximum value. */
sema.init_count = initial_value;
sema.max_count = 255;
sema.option = 0;
sema.max_count = 255;
sema.option = 0;
sem->semid = CreateSema(&sema);
if (sem->semid < 0) {

View File

@@ -31,7 +31,8 @@
#include "../SDL_thread_c.h"
#include <kernel.h>
static void FinishThread(SDL_Thread *thread) {
static void FinishThread(SDL_Thread *thread)
{
ee_thread_status_t info;
int res;
@@ -67,18 +68,17 @@ int SDL_SYS_CreateThread(SDL_Thread *thread)
priority = status.current_priority;
}
stack_size = thread->stacksize ? ((int) thread->stacksize) : 0x1800;
stack_size = thread->stacksize ? ((int)thread->stacksize) : 0x1800;
/* Create EE Thread */
eethread.attr = 0;
eethread.option = 0;
eethread.func = &childThread;
eethread.stack = SDL_malloc(stack_size);
eethread.stack_size = stack_size;
eethread.gp_reg = &_gp;
eethread.initial_priority = priority;
thread->handle = CreateThread(&eethread);
eethread.attr = 0;
eethread.option = 0;
eethread.func = &childThread;
eethread.stack = SDL_malloc(stack_size);
eethread.stack_size = stack_size;
eethread.gp_reg = &_gp;
eethread.initial_priority = priority;
thread->handle = CreateThread(&eethread);
if (thread->handle < 0) {
return SDL_SetError("CreateThread() failed");
@@ -86,8 +86,8 @@ int SDL_SYS_CreateThread(SDL_Thread *thread)
// Prepare el semaphore for the ending function
sema.init_count = 0;
sema.max_count = 1;
sema.option = 0;
sema.max_count = 1;
sema.option = 0;
thread->endfunc = (void *)CreateSema(&sema);
return StartThread(thread->handle, thread);
@@ -100,7 +100,7 @@ void SDL_SYS_SetupThread(const char *name)
SDL_threadID SDL_ThreadID(void)
{
return (SDL_threadID) GetThreadId();
return (SDL_threadID)GetThreadId();
}
void SDL_SYS_WaitThread(SDL_Thread *thread)
@@ -129,7 +129,7 @@ int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
value = 50;
}
return ChangeThreadPriority(GetThreadId(),value);
return ChangeThreadPriority(GetThreadId(), value);
}
#endif /* SDL_THREAD_PS2 */

View File

@@ -28,7 +28,6 @@
implementation, written by Christopher Tate and Owen Smith. Thanks!
*/
struct SDL_cond
{
SDL_mutex *lock;
@@ -44,7 +43,7 @@ SDL_CreateCond(void)
{
SDL_cond *cond;
cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
cond = (SDL_cond *)SDL_malloc(sizeof(SDL_cond));
if (cond) {
cond->lock = SDL_CreateMutex();
cond->wait_sem = SDL_CreateSemaphore(0);
@@ -61,8 +60,7 @@ SDL_CreateCond(void)
}
/* Destroy a condition variable */
void
SDL_DestroyCond(SDL_cond * cond)
void SDL_DestroyCond(SDL_cond *cond)
{
if (cond) {
if (cond->wait_sem) {
@@ -79,8 +77,7 @@ SDL_DestroyCond(SDL_cond * cond)
}
/* Restart one of the threads that are waiting on the condition variable */
int
SDL_CondSignal(SDL_cond * cond)
int SDL_CondSignal(SDL_cond *cond)
{
if (cond == NULL) {
return SDL_InvalidParamError("cond");
@@ -103,8 +100,7 @@ SDL_CondSignal(SDL_cond * cond)
}
/* Restart all threads that are waiting on the condition variable */
int
SDL_CondBroadcast(SDL_cond * cond)
int SDL_CondBroadcast(SDL_cond *cond)
{
if (cond == NULL) {
return SDL_InvalidParamError("cond");
@@ -157,8 +153,7 @@ Thread B:
SDL_CondSignal(cond);
SDL_UnlockMutex(lock);
*/
int
SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
{
int retval;
@@ -212,8 +207,7 @@ SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
}
/* Wait on the condition variable forever */
int
SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
{
return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
}

View File

@@ -44,7 +44,7 @@ SDL_CreateMutex(void)
SceInt32 res = 0;
/* Allocate mutex memory */
mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex));
if (mutex) {
res = sceKernelCreateLwMutex(
@@ -52,8 +52,7 @@ SDL_CreateMutex(void)
"SDL mutex",
SCE_KERNEL_MUTEX_ATTR_RECURSIVE,
0,
NULL
);
NULL);
if (res < 0) {
SDL_SetError("Error trying to create mutex: %lx", res);
@@ -65,8 +64,7 @@ SDL_CreateMutex(void)
}
/* Free the mutex */
void
SDL_DestroyMutex(SDL_mutex * mutex)
void SDL_DestroyMutex(SDL_mutex *mutex)
{
if (mutex) {
sceKernelDeleteLwMutex(&mutex->lock);
@@ -75,8 +73,7 @@ SDL_DestroyMutex(SDL_mutex * mutex)
}
/* Try to lock the mutex */
int
SDL_TryLockMutex(SDL_mutex * mutex)
int SDL_TryLockMutex(SDL_mutex *mutex)
{
#if SDL_THREADS_DISABLED
return 0;
@@ -88,25 +85,23 @@ SDL_TryLockMutex(SDL_mutex * mutex)
res = sceKernelTryLockLwMutex(&mutex->lock, 1);
switch (res) {
case SCE_KERNEL_ERROR_OK:
return 0;
break;
case SCE_KERNEL_ERROR_WAIT_TIMEOUT:
return SDL_MUTEX_TIMEDOUT;
break;
default:
return SDL_SetError("Error trying to lock mutex: %lx", res);
break;
case SCE_KERNEL_ERROR_OK:
return 0;
break;
case SCE_KERNEL_ERROR_WAIT_TIMEOUT:
return SDL_MUTEX_TIMEDOUT;
break;
default:
return SDL_SetError("Error trying to lock mutex: %lx", res);
break;
}
return -1;
#endif /* SDL_THREADS_DISABLED */
}
/* Lock the mutex */
int
SDL_mutexP(SDL_mutex * mutex)
int SDL_mutexP(SDL_mutex *mutex)
{
#if SDL_THREADS_DISABLED
return 0;
@@ -126,8 +121,7 @@ SDL_mutexP(SDL_mutex * mutex)
}
/* Unlock the mutex */
int
SDL_mutexV(SDL_mutex * mutex)
int SDL_mutexV(SDL_mutex *mutex)
{
#if SDL_THREADS_DISABLED
return 0;

View File

@@ -27,21 +27,20 @@
#include <stdio.h>
#include <stdlib.h>
#include <pspthreadman.h>
#include <pspkerror.h>
struct SDL_semaphore {
SceUID semid;
struct SDL_semaphore
{
SceUID semid;
};
/* Create a semaphore */
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
{
SDL_sem *sem;
sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
if (sem != NULL) {
/* TODO: Figure out the limit on the maximum value. */
sem->semid = sceKernelCreateSema("SDL sema", 0, initial_value, 255, NULL);
@@ -94,18 +93,18 @@ int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
if (timeout == SDL_MUTEX_MAXWAIT) {
pTimeout = NULL;
} else {
timeout *= 1000; /* Convert to microseconds. */
timeout *= 1000; /* Convert to microseconds. */
pTimeout = &timeout;
}
res = sceKernelWaitSema(sem->semid, 1, (SceUInt *) pTimeout);
switch (res) {
case SCE_KERNEL_ERROR_OK:
return 0;
case SCE_KERNEL_ERROR_WAIT_TIMEOUT:
return SDL_MUTEX_TIMEDOUT;
default:
return SDL_SetError("sceKernelWaitSema() failed");
res = sceKernelWaitSema(sem->semid, 1, (SceUInt *)pTimeout);
switch (res) {
case SCE_KERNEL_ERROR_OK:
return 0;
case SCE_KERNEL_ERROR_WAIT_TIMEOUT:
return SDL_MUTEX_TIMEDOUT;
default:
return SDL_SetError("sceKernelWaitSema() failed");
}
}

View File

@@ -32,10 +32,9 @@
#include <pspkerneltypes.h>
#include <pspthreadman.h>
static int ThreadEntry(SceSize args, void *argp)
{
SDL_RunThread(*(SDL_Thread **) argp);
SDL_RunThread(*(SDL_Thread **)argp);
return 0;
}
@@ -51,8 +50,8 @@ int SDL_SYS_CreateThread(SDL_Thread *thread)
}
thread->handle = sceKernelCreateThread(thread->name, ThreadEntry,
priority, thread->stacksize ? ((int) thread->stacksize) : 0x8000,
PSP_THREAD_ATTR_VFPU, NULL);
priority, thread->stacksize ? ((int)thread->stacksize) : 0x8000,
PSP_THREAD_ATTR_VFPU, NULL);
if (thread->handle < 0) {
return SDL_SetError("sceKernelCreateThread() failed");
}
@@ -68,7 +67,7 @@ void SDL_SYS_SetupThread(const char *name)
SDL_threadID SDL_ThreadID(void)
{
return (SDL_threadID) sceKernelGetThreadId();
return (SDL_threadID)sceKernelGetThreadId();
}
void SDL_SYS_WaitThread(SDL_Thread *thread)
@@ -102,8 +101,7 @@ int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
value = 50;
}
return sceKernelChangeThreadPriority(sceKernelGetThreadId(),value);
return sceKernelChangeThreadPriority(sceKernelGetThreadId(), value);
}
#endif /* SDL_THREAD_PSP */

View File

@@ -39,7 +39,7 @@ SDL_CreateCond(void)
{
SDL_cond *cond;
cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
cond = (SDL_cond *)SDL_malloc(sizeof(SDL_cond));
if (cond) {
if (pthread_cond_init(&cond->cond, NULL) != 0) {
SDL_SetError("pthread_cond_init() failed");
@@ -51,8 +51,7 @@ SDL_CreateCond(void)
}
/* Destroy a condition variable */
void
SDL_DestroyCond(SDL_cond * cond)
void SDL_DestroyCond(SDL_cond *cond)
{
if (cond) {
pthread_cond_destroy(&cond->cond);
@@ -61,8 +60,7 @@ SDL_DestroyCond(SDL_cond * cond)
}
/* Restart one of the threads that are waiting on the condition variable */
int
SDL_CondSignal(SDL_cond * cond)
int SDL_CondSignal(SDL_cond *cond)
{
int retval;
@@ -78,8 +76,7 @@ SDL_CondSignal(SDL_cond * cond)
}
/* Restart all threads that are waiting on the condition variable */
int
SDL_CondBroadcast(SDL_cond * cond)
int SDL_CondBroadcast(SDL_cond *cond)
{
int retval;
@@ -94,8 +91,7 @@ SDL_CondBroadcast(SDL_cond * cond)
return retval;
}
int
SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
{
int retval;
#ifndef HAVE_CLOCK_GETTIME
@@ -123,7 +119,7 @@ SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
abstime.tv_nsec -= 1000000000;
}
tryagain:
tryagain:
retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
switch (retval) {
case EINTR:
@@ -143,8 +139,7 @@ SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
/* Wait on the condition variable, unlocking the provided mutex.
The mutex must be locked before entering this function!
*/
int
SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
{
if (cond == NULL) {
return SDL_InvalidParamError("cond");

View File

@@ -23,7 +23,6 @@
#include <errno.h>
#include <pthread.h>
#if !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX && \
!SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
#define FAKE_RECURSIVE_MUTEX 1
@@ -45,7 +44,7 @@ SDL_CreateMutex(void)
pthread_mutexattr_t attr;
/* Allocate the structure */
mutex = (SDL_mutex *) SDL_calloc(1, sizeof(*mutex));
mutex = (SDL_mutex *)SDL_calloc(1, sizeof(*mutex));
if (mutex) {
pthread_mutexattr_init(&attr);
#if SDL_THREAD_PTHREAD_RECURSIVE_MUTEX
@@ -66,8 +65,7 @@ SDL_CreateMutex(void)
return mutex;
}
void
SDL_DestroyMutex(SDL_mutex * mutex)
void SDL_DestroyMutex(SDL_mutex *mutex)
{
if (mutex) {
pthread_mutex_destroy(&mutex->id);
@@ -76,8 +74,7 @@ SDL_DestroyMutex(SDL_mutex * mutex)
}
/* Lock the mutex */
int
SDL_LockMutex(SDL_mutex * mutex)
int SDL_LockMutex(SDL_mutex *mutex)
{
#if FAKE_RECURSIVE_MUTEX
pthread_t this_thread;
@@ -111,8 +108,7 @@ SDL_LockMutex(SDL_mutex * mutex)
return 0;
}
int
SDL_TryLockMutex(SDL_mutex * mutex)
int SDL_TryLockMutex(SDL_mutex *mutex)
{
int retval;
int result;
@@ -157,8 +153,7 @@ SDL_TryLockMutex(SDL_mutex * mutex)
return retval;
}
int
SDL_UnlockMutex(SDL_mutex * mutex)
int SDL_UnlockMutex(SDL_mutex *mutex)
{
if (mutex == NULL) {
return SDL_InvalidParamError("mutex");

View File

@@ -26,7 +26,6 @@
#include <sys/time.h>
#include <time.h>
/* Wrapper around POSIX 1003.1b semaphores */
#if defined(__MACOS__) || defined(__IOS__)
@@ -43,7 +42,7 @@ struct SDL_semaphore
SDL_sem *
SDL_CreateSemaphore(Uint32 initial_value)
{
SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
SDL_sem *sem = (SDL_sem *)SDL_malloc(sizeof(SDL_sem));
if (sem != NULL) {
if (sem_init(&sem->sem, 0, initial_value) < 0) {
SDL_SetError("sem_init() failed");
@@ -56,8 +55,7 @@ SDL_CreateSemaphore(Uint32 initial_value)
return sem;
}
void
SDL_DestroySemaphore(SDL_sem * sem)
void SDL_DestroySemaphore(SDL_sem *sem)
{
if (sem != NULL) {
sem_destroy(&sem->sem);
@@ -65,8 +63,7 @@ SDL_DestroySemaphore(SDL_sem * sem)
}
}
int
SDL_SemTryWait(SDL_sem * sem)
int SDL_SemTryWait(SDL_sem *sem)
{
int retval;
@@ -80,8 +77,7 @@ SDL_SemTryWait(SDL_sem * sem)
return retval;
}
int
SDL_SemWait(SDL_sem * sem)
int SDL_SemWait(SDL_sem *sem)
{
int retval;
@@ -99,8 +95,7 @@ SDL_SemWait(SDL_sem * sem)
return retval;
}
int
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
{
int retval;
#ifdef HAVE_SEM_TIMEDWAIT
@@ -126,9 +121,9 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
#ifdef HAVE_SEM_TIMEDWAIT
/* Setup the timeout. sem_timedwait doesn't wait for
* a lapse of time, but until we reach a certain time.
* This time is now plus the timeout.
*/
* a lapse of time, but until we reach a certain time.
* This time is now plus the timeout.
*/
#ifdef HAVE_CLOCK_GETTIME
clock_gettime(CLOCK_REALTIME, &ts_timeout);
@@ -175,7 +170,7 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
}
Uint32
SDL_SemValue(SDL_sem * sem)
SDL_SemValue(SDL_sem *sem)
{
int ret = 0;
@@ -188,11 +183,10 @@ SDL_SemValue(SDL_sem * sem)
if (ret < 0) {
ret = 0;
}
return (Uint32) ret;
return (Uint32)ret;
}
int
SDL_SemPost(SDL_sem * sem)
int SDL_SemPost(SDL_sem *sem)
{
int retval;

View File

@@ -20,7 +20,6 @@
*/
#include "SDL_internal.h"
#include <pthread.h>
#if HAVE_PTHREAD_NP_H
@@ -56,54 +55,51 @@
#include <kernel/OS.h>
#endif
/* List of signals to mask in the subthreads */
static const int sig_list[] = {
SIGHUP, SIGINT, SIGQUIT, SIGPIPE, SIGALRM, SIGTERM, SIGCHLD, SIGWINCH,
SIGVTALRM, SIGPROF, 0
};
static void *
RunThread(void *data)
static void *RunThread(void *data)
{
#ifdef __ANDROID__
Android_JNI_SetupThread();
#endif
SDL_RunThread((SDL_Thread *) data);
SDL_RunThread((SDL_Thread *)data);
return NULL;
}
#if (defined(__MACOS__) || defined(__IOS__)) && defined(HAVE_DLOPEN)
static SDL_bool checked_setname = SDL_FALSE;
static int (*ppthread_setname_np)(const char*) = NULL;
static int (*ppthread_setname_np)(const char *) = NULL;
#elif defined(__LINUX__) && defined(HAVE_DLOPEN)
static SDL_bool checked_setname = SDL_FALSE;
static int (*ppthread_setname_np)(pthread_t, const char*) = NULL;
static int (*ppthread_setname_np)(pthread_t, const char *) = NULL;
#endif
int
SDL_SYS_CreateThread(SDL_Thread * thread)
int SDL_SYS_CreateThread(SDL_Thread *thread)
{
pthread_attr_t type;
/* do this here before any threads exist, so there's no race condition. */
#if (defined(__MACOS__) || defined(__IOS__) || defined(__LINUX__)) && defined(HAVE_DLOPEN)
/* do this here before any threads exist, so there's no race condition. */
#if (defined(__MACOS__) || defined(__IOS__) || defined(__LINUX__)) && defined(HAVE_DLOPEN)
if (!checked_setname) {
void *fn = dlsym(RTLD_DEFAULT, "pthread_setname_np");
#if defined(__MACOS__) || defined(__IOS__)
ppthread_setname_np = (int(*)(const char*)) fn;
#elif defined(__LINUX__)
ppthread_setname_np = (int(*)(pthread_t, const char*)) fn;
#endif
#if defined(__MACOS__) || defined(__IOS__)
ppthread_setname_np = (int (*)(const char *))fn;
#elif defined(__LINUX__)
ppthread_setname_np = (int (*)(pthread_t, const char *))fn;
#endif
checked_setname = SDL_TRUE;
}
#endif
#endif
/* Set the thread attributes */
if (pthread_attr_init(&type) != 0) {
return SDL_SetError("Couldn't initialize pthread attributes");
}
pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
/* Set caller-requested stack size. Otherwise: use the system default. */
if (thread->stacksize) {
pthread_attr_setstacksize(&type, thread->stacksize);
@@ -117,44 +113,43 @@ SDL_SYS_CreateThread(SDL_Thread * thread)
return 0;
}
void
SDL_SYS_SetupThread(const char *name)
void SDL_SYS_SetupThread(const char *name)
{
int i;
sigset_t mask;
if (name != NULL) {
#if (defined(__MACOS__) || defined(__IOS__) || defined(__LINUX__)) && defined(HAVE_DLOPEN)
#if (defined(__MACOS__) || defined(__IOS__) || defined(__LINUX__)) && defined(HAVE_DLOPEN)
SDL_assert(checked_setname);
if (ppthread_setname_np != NULL) {
#if defined(__MACOS__) || defined(__IOS__)
#if defined(__MACOS__) || defined(__IOS__)
ppthread_setname_np(name);
#elif defined(__LINUX__)
#elif defined(__LINUX__)
if (ppthread_setname_np(pthread_self(), name) == ERANGE) {
char namebuf[16]; /* Limited to 16 char */
SDL_strlcpy(namebuf, name, sizeof (namebuf));
SDL_strlcpy(namebuf, name, sizeof(namebuf));
ppthread_setname_np(pthread_self(), namebuf);
}
#endif
#endif
}
#elif HAVE_PTHREAD_SETNAME_NP
#if defined(__NETBSD__)
pthread_setname_np(pthread_self(), "%s", name);
#else
if (pthread_setname_np(pthread_self(), name) == ERANGE) {
char namebuf[16]; /* Limited to 16 char */
SDL_strlcpy(namebuf, name, sizeof (namebuf));
pthread_setname_np(pthread_self(), namebuf);
}
#endif
#elif HAVE_PTHREAD_SET_NAME_NP
pthread_set_name_np(pthread_self(), name);
#elif defined(__HAIKU__)
/* The docs say the thread name can't be longer than B_OS_NAME_LENGTH. */
char namebuf[B_OS_NAME_LENGTH];
SDL_strlcpy(namebuf, name, sizeof (namebuf));
rename_thread(find_thread(NULL), namebuf);
#endif
#elif HAVE_PTHREAD_SETNAME_NP
#if defined(__NETBSD__)
pthread_setname_np(pthread_self(), "%s", name);
#else
if (pthread_setname_np(pthread_self(), name) == ERANGE) {
char namebuf[16]; /* Limited to 16 char */
SDL_strlcpy(namebuf, name, sizeof(namebuf));
pthread_setname_np(pthread_self(), namebuf);
}
#endif
#elif HAVE_PTHREAD_SET_NAME_NP
pthread_set_name_np(pthread_self(), name);
#elif defined(__HAIKU__)
/* The docs say the thread name can't be longer than B_OS_NAME_LENGTH. */
char namebuf[B_OS_NAME_LENGTH];
SDL_strlcpy(namebuf, name, sizeof(namebuf));
rename_thread(find_thread(NULL), namebuf);
#endif
}
/* Mask asynchronous signals for this thread */
@@ -179,8 +174,7 @@ SDL_ThreadID(void)
return (SDL_threadID)pthread_self();
}
int
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
{
#if __RISCOS__
/* FIXME: Setting thread priority does not seem to be supported */
@@ -279,14 +273,12 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
#endif /* #if __RISCOS__ */
}
void
SDL_SYS_WaitThread(SDL_Thread * thread)
void SDL_SYS_WaitThread(SDL_Thread *thread)
{
pthread_join(thread->handle, 0);
}
void
SDL_SYS_DetachThread(SDL_Thread * thread)
void SDL_SYS_DetachThread(SDL_Thread *thread)
{
pthread_detach(thread->handle);
}

View File

@@ -24,7 +24,6 @@
#include <pthread.h>
#define INVALID_PTHREAD_KEY ((pthread_key_t)-1)
static pthread_key_t thread_local_storage = INVALID_PTHREAD_KEY;
@@ -54,8 +53,7 @@ SDL_SYS_GetTLSData(void)
return (SDL_TLSData *)pthread_getspecific(thread_local_storage);
}
int
SDL_SYS_SetTLSData(SDL_TLSData *data)
int SDL_SYS_SetTLSData(SDL_TLSData *data)
{
if (generic_local_storage) {
return SDL_Generic_SetTLSData(data);

View File

@@ -36,15 +36,14 @@ struct SDL_cond
};
/* Create a condition variable */
extern "C"
SDL_cond *
extern "C" SDL_cond *
SDL_CreateCond(void)
{
/* Allocate and initialize the condition variable */
try {
SDL_cond * cond = new SDL_cond;
SDL_cond *cond = new SDL_cond;
return cond;
} catch (std::system_error & ex) {
} catch (std::system_error &ex) {
SDL_SetError("unable to create a C++ condition variable: code=%d; %s", ex.code(), ex.what());
return NULL;
} catch (std::bad_alloc &) {
@@ -54,9 +53,8 @@ SDL_CreateCond(void)
}
/* Destroy a condition variable */
extern "C"
void
SDL_DestroyCond(SDL_cond * cond)
extern "C" void
SDL_DestroyCond(SDL_cond *cond)
{
if (cond != NULL) {
delete cond;
@@ -64,9 +62,8 @@ SDL_DestroyCond(SDL_cond * cond)
}
/* Restart one of the threads that are waiting on the condition variable */
extern "C"
int
SDL_CondSignal(SDL_cond * cond)
extern "C" int
SDL_CondSignal(SDL_cond *cond)
{
if (cond == NULL) {
return SDL_InvalidParamError("cond");
@@ -77,9 +74,8 @@ SDL_CondSignal(SDL_cond * cond)
}
/* Restart all threads that are waiting on the condition variable */
extern "C"
int
SDL_CondBroadcast(SDL_cond * cond)
extern "C" int
SDL_CondBroadcast(SDL_cond *cond)
{
if (cond == NULL) {
return SDL_InvalidParamError("cond");
@@ -110,9 +106,8 @@ Thread B:
SDL_CondSignal(cond);
SDL_UnlockMutex(lock);
*/
extern "C"
int
SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
extern "C" int
SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
{
if (cond == NULL) {
return SDL_InvalidParamError("cond");
@@ -126,15 +121,13 @@ SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
std::unique_lock<std::recursive_mutex> cpp_lock(mutex->cpp_mutex, std::adopt_lock_t());
if (ms == SDL_MUTEX_MAXWAIT) {
cond->cpp_cond.wait(
cpp_lock
);
cpp_lock);
cpp_lock.release();
return 0;
} else {
auto wait_result = cond->cpp_cond.wait_for(
cpp_lock,
std::chrono::duration<Uint32, std::milli>(ms)
);
std::chrono::duration<Uint32, std::milli>(ms));
cpp_lock.release();
if (wait_result == std::cv_status::timeout) {
return SDL_MUTEX_TIMEDOUT;
@@ -142,15 +135,14 @@ SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
return 0;
}
}
} catch (std::system_error & ex) {
} catch (std::system_error &ex) {
return SDL_SetError("unable to wait on a C++ condition variable: code=%d; %s", ex.code(), ex.what());
}
}
/* Wait on the condition variable forever */
extern "C"
int
SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
extern "C" int
SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
{
return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
}

View File

@@ -29,17 +29,15 @@ extern "C" {
#include "SDL_sysmutex_c.h"
#include <Windows.h>
/* Create a mutex */
extern "C"
SDL_mutex *
extern "C" SDL_mutex *
SDL_CreateMutex(void)
{
/* Allocate and initialize the mutex */
try {
SDL_mutex * mutex = new SDL_mutex;
SDL_mutex *mutex = new SDL_mutex;
return mutex;
} catch (std::system_error & ex) {
} catch (std::system_error &ex) {
SDL_SetError("unable to create a C++ mutex: code=%d; %s", ex.code(), ex.what());
return NULL;
} catch (std::bad_alloc &) {
@@ -49,9 +47,8 @@ SDL_CreateMutex(void)
}
/* Free the mutex */
extern "C"
void
SDL_DestroyMutex(SDL_mutex * mutex)
extern "C" void
SDL_DestroyMutex(SDL_mutex *mutex)
{
if (mutex != NULL) {
delete mutex;
@@ -59,9 +56,8 @@ SDL_DestroyMutex(SDL_mutex * mutex)
}
/* Lock the semaphore */
extern "C"
int
SDL_mutexP(SDL_mutex * mutex)
extern "C" int
SDL_mutexP(SDL_mutex *mutex)
{
if (mutex == NULL) {
return SDL_InvalidParamError("mutex");
@@ -70,14 +66,13 @@ SDL_mutexP(SDL_mutex * mutex)
try {
mutex->cpp_mutex.lock();
return 0;
} catch (std::system_error & ex) {
} catch (std::system_error &ex) {
return SDL_SetError("unable to lock a C++ mutex: code=%d; %s", ex.code(), ex.what());
}
}
/* TryLock the mutex */
int
SDL_TryLockMutex(SDL_mutex * mutex)
int SDL_TryLockMutex(SDL_mutex *mutex)
{
int retval = 0;
if (mutex == NULL) {
@@ -91,9 +86,8 @@ SDL_TryLockMutex(SDL_mutex * mutex)
}
/* Unlock the mutex */
extern "C"
int
SDL_mutexV(SDL_mutex * mutex)
extern "C" int
SDL_mutexV(SDL_mutex *mutex)
{
if (mutex == NULL) {
return SDL_InvalidParamError("mutex");

View File

@@ -35,30 +35,27 @@ extern "C" {
#include <Windows.h>
#endif
static void
RunThread(void *args)
static void RunThread(void *args)
{
SDL_RunThread((SDL_Thread *) args);
SDL_RunThread((SDL_Thread *)args);
}
extern "C"
int
SDL_SYS_CreateThread(SDL_Thread * thread)
extern "C" int
SDL_SYS_CreateThread(SDL_Thread *thread)
{
try {
// !!! FIXME: no way to set a thread stack size here.
std::thread cpp_thread(RunThread, thread);
thread->handle = (void *) new std::thread(std::move(cpp_thread));
thread->handle = (void *)new std::thread(std::move(cpp_thread));
return 0;
} catch (std::system_error & ex) {
} catch (std::system_error &ex) {
return SDL_SetError("unable to start a C++ thread: code=%d; %s", ex.code(), ex.what());
} catch (std::bad_alloc &) {
return SDL_OutOfMemory();
}
}
extern "C"
void
extern "C" void
SDL_SYS_SetupThread(const char *name)
{
// Make sure a thread ID gets assigned ASAP, for debugging purposes:
@@ -66,8 +63,7 @@ SDL_SYS_SetupThread(const char *name)
return;
}
extern "C"
SDL_threadID
extern "C" SDL_threadID
SDL_ThreadID(void)
{
#ifdef __WINRT__
@@ -88,8 +84,7 @@ SDL_ThreadID(void)
#endif
}
extern "C"
int
extern "C" int
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
{
#ifdef __WINRT__
@@ -115,16 +110,15 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
#endif
}
extern "C"
void
SDL_SYS_WaitThread(SDL_Thread * thread)
extern "C" void
SDL_SYS_WaitThread(SDL_Thread *thread)
{
if ( ! thread) {
if (!thread) {
return;
}
try {
std::thread * cpp_thread = (std::thread *) thread->handle;
std::thread *cpp_thread = (std::thread *)thread->handle;
if (cpp_thread->joinable()) {
cpp_thread->join();
}
@@ -135,16 +129,15 @@ SDL_SYS_WaitThread(SDL_Thread * thread)
}
}
extern "C"
void
SDL_SYS_DetachThread(SDL_Thread * thread)
extern "C" void
SDL_SYS_DetachThread(SDL_Thread *thread)
{
if ( ! thread) {
if (!thread) {
return;
}
try {
std::thread * cpp_thread = (std::thread *) thread->handle;
std::thread *cpp_thread = (std::thread *)thread->handle;
if (cpp_thread->joinable()) {
cpp_thread->detach();
}
@@ -155,15 +148,13 @@ SDL_SYS_DetachThread(SDL_Thread * thread)
}
}
extern "C"
SDL_TLSData *
extern "C" SDL_TLSData *
SDL_SYS_GetTLSData(void)
{
return SDL_Generic_GetTLSData();
}
extern "C"
int
extern "C" int
SDL_SYS_SetTLSData(SDL_TLSData *data)
{
return SDL_Generic_SetTLSData(data);

View File

@@ -21,6 +21,6 @@
#include "SDL_internal.h"
/* For a thread handle, use a void pointer to a std::thread */
typedef void * SYS_ThreadHandle;
typedef void *SYS_ThreadHandle;
/* vi: set ts=4 sw=4 expandtab: */

View File

@@ -28,7 +28,6 @@
implementation, written by Christopher Tate and Owen Smith. Thanks!
*/
struct SDL_cond
{
SDL_mutex *lock;
@@ -44,7 +43,7 @@ SDL_CreateCond(void)
{
SDL_cond *cond;
cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
cond = (SDL_cond *)SDL_malloc(sizeof(SDL_cond));
if (cond != NULL) {
cond->lock = SDL_CreateMutex();
cond->wait_sem = SDL_CreateSemaphore(0);
@@ -61,8 +60,7 @@ SDL_CreateCond(void)
}
/* Destroy a condition variable */
void
SDL_DestroyCond(SDL_cond * cond)
void SDL_DestroyCond(SDL_cond *cond)
{
if (cond != NULL) {
if (cond->wait_sem) {
@@ -79,8 +77,7 @@ SDL_DestroyCond(SDL_cond * cond)
}
/* Restart one of the threads that are waiting on the condition variable */
int
SDL_CondSignal(SDL_cond * cond)
int SDL_CondSignal(SDL_cond *cond)
{
if (cond == NULL) {
return SDL_InvalidParamError("cond");
@@ -103,8 +100,7 @@ SDL_CondSignal(SDL_cond * cond)
}
/* Restart all threads that are waiting on the condition variable */
int
SDL_CondBroadcast(SDL_cond * cond)
int SDL_CondBroadcast(SDL_cond *cond)
{
if (cond == NULL) {
return SDL_InvalidParamError("cond");
@@ -157,8 +153,7 @@ Thread B:
SDL_CondSignal(cond);
SDL_UnlockMutex(lock);
*/
int
SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
{
int retval;
@@ -212,8 +207,7 @@ SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
}
/* Wait on the condition variable forever */
int
SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
{
return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
}

View File

@@ -40,7 +40,7 @@ SDL_CreateMutex(void)
SceInt32 res = 0;
/* Allocate mutex memory */
mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex));
if (mutex != NULL) {
res = sceKernelCreateLwMutex(
@@ -48,8 +48,7 @@ SDL_CreateMutex(void)
"SDL mutex",
SCE_KERNEL_MUTEX_ATTR_RECURSIVE,
0,
NULL
);
NULL);
if (res < 0) {
SDL_SetError("Error trying to create mutex: %x", res);
@@ -61,8 +60,7 @@ SDL_CreateMutex(void)
}
/* Free the mutex */
void
SDL_DestroyMutex(SDL_mutex * mutex)
void SDL_DestroyMutex(SDL_mutex *mutex)
{
if (mutex != NULL) {
sceKernelDeleteLwMutex(&mutex->lock);
@@ -71,8 +69,7 @@ SDL_DestroyMutex(SDL_mutex * mutex)
}
/* Try to lock the mutex */
int
SDL_TryLockMutex(SDL_mutex * mutex)
int SDL_TryLockMutex(SDL_mutex *mutex)
{
#if SDL_THREADS_DISABLED
return 0;
@@ -84,25 +81,23 @@ SDL_TryLockMutex(SDL_mutex * mutex)
res = sceKernelTryLockLwMutex(&mutex->lock, 1);
switch (res) {
case SCE_KERNEL_OK:
return 0;
break;
case SCE_KERNEL_ERROR_MUTEX_FAILED_TO_OWN:
return SDL_MUTEX_TIMEDOUT;
break;
default:
return SDL_SetError("Error trying to lock mutex: %x", res);
break;
case SCE_KERNEL_OK:
return 0;
break;
case SCE_KERNEL_ERROR_MUTEX_FAILED_TO_OWN:
return SDL_MUTEX_TIMEDOUT;
break;
default:
return SDL_SetError("Error trying to lock mutex: %x", res);
break;
}
return -1;
#endif /* SDL_THREADS_DISABLED */
}
/* Lock the mutex */
int
SDL_mutexP(SDL_mutex * mutex)
int SDL_mutexP(SDL_mutex *mutex)
{
#if SDL_THREADS_DISABLED
return 0;
@@ -122,8 +117,7 @@ SDL_mutexP(SDL_mutex * mutex)
}
/* Unlock the mutex */
int
SDL_mutexV(SDL_mutex * mutex)
int SDL_mutexV(SDL_mutex *mutex)
{
#if SDL_THREADS_DISABLED
return 0;

View File

@@ -27,22 +27,21 @@
#include <stdio.h>
#include <stdlib.h>
#include <psp2/types.h>
#include <psp2/kernel/error.h>
#include <psp2/kernel/threadmgr.h>
struct SDL_semaphore {
SceUID semid;
struct SDL_semaphore
{
SceUID semid;
};
/* Create a semaphore */
SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
{
SDL_sem *sem;
sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
if (sem != NULL) {
/* TODO: Figure out the limit on the maximum value. */
sem->semid = sceKernelCreateSema("SDL sema", 0, initial_value, 255, NULL);
@@ -95,7 +94,7 @@ int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
if (timeout == SDL_MUTEX_MAXWAIT) {
pTimeout = NULL;
} else {
timeout *= 1000; /* Convert to microseconds. */
timeout *= 1000; /* Convert to microseconds. */
pTimeout = &timeout;
}

View File

@@ -32,19 +32,19 @@
#include <psp2/types.h>
#include <psp2/kernel/threadmgr.h>
#define VITA_THREAD_STACK_SIZE_MIN 0x1000 // 4KiB
#define VITA_THREAD_STACK_SIZE_MAX 0x2000000 // 32MiB
#define VITA_THREAD_STACK_SIZE_DEFAULT 0x10000 // 64KiB
#define VITA_THREAD_NAME_MAX 32
#define VITA_THREAD_STACK_SIZE_MIN 0x1000 // 4KiB
#define VITA_THREAD_STACK_SIZE_MAX 0x2000000 // 32MiB
#define VITA_THREAD_STACK_SIZE_DEFAULT 0x10000 // 64KiB
#define VITA_THREAD_NAME_MAX 32
#define VITA_THREAD_PRIORITY_LOW 191
#define VITA_THREAD_PRIORITY_NORMAL 160
#define VITA_THREAD_PRIORITY_HIGH 112
#define VITA_THREAD_PRIORITY_LOW 191
#define VITA_THREAD_PRIORITY_NORMAL 160
#define VITA_THREAD_PRIORITY_HIGH 112
#define VITA_THREAD_PRIORITY_TIME_CRITICAL 64
static int ThreadEntry(SceSize args, void *argp)
{
SDL_RunThread(*(SDL_Thread **) argp);
SDL_RunThread(*(SDL_Thread **)argp);
return 0;
}
@@ -72,11 +72,11 @@ int SDL_SYS_CreateThread(SDL_Thread *thread)
thread->handle = sceKernelCreateThread(
thread_name, // name
ThreadEntry, // function to run
0, // priority. 0 means priority of calling thread
stack_size, // stack size
0, // attibutes. always 0
0, // cpu affinity mask. 0 = all CPUs
NULL // opt. always NULL
0, // priority. 0 means priority of calling thread
stack_size, // stack size
0, // attibutes. always 0
0, // cpu affinity mask. 0 = all CPUs
NULL // opt. always NULL
);
if (thread->handle < 0) {
@@ -94,7 +94,7 @@ void SDL_SYS_SetupThread(const char *name)
SDL_threadID SDL_ThreadID(void)
{
return (SDL_threadID) sceKernelGetThreadId();
return (SDL_threadID)sceKernelGetThreadId();
}
void SDL_SYS_WaitThread(SDL_Thread *thread)
@@ -112,23 +112,22 @@ int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
{
int value = VITA_THREAD_PRIORITY_NORMAL;
switch(priority) {
case SDL_THREAD_PRIORITY_LOW:
value = VITA_THREAD_PRIORITY_LOW;
break;
case SDL_THREAD_PRIORITY_NORMAL:
value = VITA_THREAD_PRIORITY_NORMAL;
break;
case SDL_THREAD_PRIORITY_HIGH:
value = VITA_THREAD_PRIORITY_HIGH;
break;
case SDL_THREAD_PRIORITY_TIME_CRITICAL:
value = VITA_THREAD_PRIORITY_TIME_CRITICAL;
break;
switch (priority) {
case SDL_THREAD_PRIORITY_LOW:
value = VITA_THREAD_PRIORITY_LOW;
break;
case SDL_THREAD_PRIORITY_NORMAL:
value = VITA_THREAD_PRIORITY_NORMAL;
break;
case SDL_THREAD_PRIORITY_HIGH:
value = VITA_THREAD_PRIORITY_HIGH;
break;
case SDL_THREAD_PRIORITY_TIME_CRITICAL:
value = VITA_THREAD_PRIORITY_TIME_CRITICAL;
break;
}
return sceKernelChangeThreadPriority(0, value);
}
#endif /* SDL_THREAD_VITA */

View File

@@ -20,11 +20,10 @@
*/
#include "SDL_internal.h"
#include "../generic/SDL_syscond_c.h"
#include "SDL_sysmutex_c.h"
typedef SDL_cond * (*pfnSDL_CreateCond)(void);
typedef SDL_cond *(*pfnSDL_CreateCond)(void);
typedef void (*pfnSDL_DestroyCond)(SDL_cond *);
typedef int (*pfnSDL_CondSignal)(SDL_cond *);
typedef int (*pfnSDL_CondBroadcast)(SDL_cond *);
@@ -33,39 +32,42 @@ typedef int (*pfnSDL_CondWaitTimeout)(SDL_cond *, SDL_mutex *, Uint32);
typedef struct SDL_cond_impl_t
{
pfnSDL_CreateCond Create;
pfnSDL_DestroyCond Destroy;
pfnSDL_CondSignal Signal;
pfnSDL_CondBroadcast Broadcast;
pfnSDL_CondWait Wait;
pfnSDL_CondWaitTimeout WaitTimeout;
pfnSDL_CreateCond Create;
pfnSDL_DestroyCond Destroy;
pfnSDL_CondSignal Signal;
pfnSDL_CondBroadcast Broadcast;
pfnSDL_CondWait Wait;
pfnSDL_CondWaitTimeout WaitTimeout;
} SDL_cond_impl_t;
/* Implementation will be chosen at runtime based on available Kernel features */
static SDL_cond_impl_t SDL_cond_impl_active = {0};
static SDL_cond_impl_t SDL_cond_impl_active = { 0 };
/**
* Native Windows Condition Variable (SRW Locks)
*/
#ifndef CONDITION_VARIABLE_INIT
#define CONDITION_VARIABLE_INIT {0}
typedef struct CONDITION_VARIABLE {
#define CONDITION_VARIABLE_INIT \
{ \
0 \
}
typedef struct CONDITION_VARIABLE
{
PVOID Ptr;
} CONDITION_VARIABLE, *PCONDITION_VARIABLE;
#endif
#if __WINRT__
#define pWakeConditionVariable WakeConditionVariable
#define pWakeAllConditionVariable WakeAllConditionVariable
#define pWakeConditionVariable WakeConditionVariable
#define pWakeAllConditionVariable WakeAllConditionVariable
#define pSleepConditionVariableSRW SleepConditionVariableSRW
#define pSleepConditionVariableCS SleepConditionVariableCS
#define pSleepConditionVariableCS SleepConditionVariableCS
#else
typedef VOID(WINAPI *pfnWakeConditionVariable)(PCONDITION_VARIABLE);
typedef VOID(WINAPI *pfnWakeAllConditionVariable)(PCONDITION_VARIABLE);
typedef BOOL(WINAPI *pfnSleepConditionVariableSRW)(PCONDITION_VARIABLE, PSRWLOCK, DWORD, ULONG);
typedef BOOL(WINAPI* pfnSleepConditionVariableCS)(PCONDITION_VARIABLE, PCRITICAL_SECTION, DWORD);
typedef BOOL(WINAPI *pfnSleepConditionVariableCS)(PCONDITION_VARIABLE, PCRITICAL_SECTION, DWORD);
static pfnWakeConditionVariable pWakeConditionVariable = NULL;
static pfnWakeAllConditionVariable pWakeAllConditionVariable = NULL;
@@ -78,14 +80,12 @@ typedef struct SDL_cond_cv
CONDITION_VARIABLE cond;
} SDL_cond_cv;
static SDL_cond *
SDL_CreateCond_cv(void)
static SDL_cond *SDL_CreateCond_cv(void)
{
SDL_cond_cv *cond;
/* Relies on CONDITION_VARIABLE_INIT == 0. */
cond = (SDL_cond_cv *) SDL_calloc(1, sizeof(*cond));
cond = (SDL_cond_cv *)SDL_calloc(1, sizeof(*cond));
if (cond == NULL) {
SDL_OutOfMemory();
}
@@ -93,8 +93,7 @@ SDL_CreateCond_cv(void)
return (SDL_cond *)cond;
}
static void
SDL_DestroyCond_cv(SDL_cond * cond)
static void SDL_DestroyCond_cv(SDL_cond *cond)
{
if (cond != NULL) {
/* There are no kernel allocated resources */
@@ -102,8 +101,7 @@ SDL_DestroyCond_cv(SDL_cond * cond)
}
}
static int
SDL_CondSignal_cv(SDL_cond * _cond)
static int SDL_CondSignal_cv(SDL_cond *_cond)
{
SDL_cond_cv *cond = (SDL_cond_cv *)_cond;
if (cond == NULL) {
@@ -115,8 +113,7 @@ SDL_CondSignal_cv(SDL_cond * _cond)
return 0;
}
static int
SDL_CondBroadcast_cv(SDL_cond * _cond)
static int SDL_CondBroadcast_cv(SDL_cond *_cond)
{
SDL_cond_cv *cond = (SDL_cond_cv *)_cond;
if (cond == NULL) {
@@ -128,8 +125,7 @@ SDL_CondBroadcast_cv(SDL_cond * _cond)
return 0;
}
static int
SDL_CondWaitTimeout_cv(SDL_cond * _cond, SDL_mutex * _mutex, Uint32 ms)
static int SDL_CondWaitTimeout_cv(SDL_cond *_cond, SDL_mutex *_mutex, Uint32 ms)
{
SDL_cond_cv *cond = (SDL_cond_cv *)_cond;
DWORD timeout;
@@ -145,7 +141,7 @@ SDL_CondWaitTimeout_cv(SDL_cond * _cond, SDL_mutex * _mutex, Uint32 ms)
if (ms == SDL_MUTEX_MAXWAIT) {
timeout = INFINITE;
} else {
timeout = (DWORD) ms;
timeout = (DWORD)ms;
}
if (SDL_mutex_impl_active.Type == SDL_MUTEX_SRW) {
@@ -192,13 +188,12 @@ SDL_CondWaitTimeout_cv(SDL_cond * _cond, SDL_mutex * _mutex, Uint32 ms)
return ret;
}
static int
SDL_CondWait_cv(SDL_cond * cond, SDL_mutex * mutex) {
static int SDL_CondWait_cv(SDL_cond *cond, SDL_mutex *mutex)
{
return SDL_CondWaitTimeout_cv(cond, mutex, SDL_MUTEX_MAXWAIT);
}
static const SDL_cond_impl_t SDL_cond_impl_cv =
{
static const SDL_cond_impl_t SDL_cond_impl_cv = {
&SDL_CreateCond_cv,
&SDL_DestroyCond_cv,
&SDL_CondSignal_cv,
@@ -211,8 +206,7 @@ static const SDL_cond_impl_t SDL_cond_impl_cv =
* Generic Condition Variable implementation using SDL_mutex and SDL_sem
*/
static const SDL_cond_impl_t SDL_cond_impl_generic =
{
static const SDL_cond_impl_t SDL_cond_impl_generic = {
&SDL_CreateCond_generic,
&SDL_DestroyCond_generic,
&SDL_CondSignal_generic,
@@ -221,13 +215,12 @@ static const SDL_cond_impl_t SDL_cond_impl_generic =
&SDL_CondWaitTimeout_generic,
};
SDL_cond *
SDL_CreateCond(void)
{
if (SDL_cond_impl_active.Create == NULL) {
/* Default to generic implementation, works with all mutex implementations */
const SDL_cond_impl_t * impl = &SDL_cond_impl_generic;
const SDL_cond_impl_t *impl = &SDL_cond_impl_generic;
if (SDL_mutex_impl_active.Type == SDL_MUTEX_INVALID) {
/* The mutex implementation isn't decided yet, trigger it */
@@ -247,10 +240,10 @@ SDL_CreateCond(void)
{
HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
if (kernel32) {
pWakeConditionVariable = (pfnWakeConditionVariable) GetProcAddress(kernel32, "WakeConditionVariable");
pWakeAllConditionVariable = (pfnWakeAllConditionVariable) GetProcAddress(kernel32, "WakeAllConditionVariable");
pSleepConditionVariableSRW = (pfnSleepConditionVariableSRW) GetProcAddress(kernel32, "SleepConditionVariableSRW");
pSleepConditionVariableCS = (pfnSleepConditionVariableCS) GetProcAddress(kernel32, "SleepConditionVariableCS");
pWakeConditionVariable = (pfnWakeConditionVariable)GetProcAddress(kernel32, "WakeConditionVariable");
pWakeAllConditionVariable = (pfnWakeAllConditionVariable)GetProcAddress(kernel32, "WakeAllConditionVariable");
pSleepConditionVariableSRW = (pfnSleepConditionVariableSRW)GetProcAddress(kernel32, "SleepConditionVariableSRW");
pSleepConditionVariableCS = (pfnSleepConditionVariableCS)GetProcAddress(kernel32, "SleepConditionVariableCS");
if (pWakeConditionVariable && pWakeAllConditionVariable && pSleepConditionVariableSRW && pSleepConditionVariableCS) {
/* Use the Windows provided API */
impl = &SDL_cond_impl_cv;
@@ -264,32 +257,27 @@ SDL_CreateCond(void)
return SDL_cond_impl_active.Create();
}
void
SDL_DestroyCond(SDL_cond * cond)
void SDL_DestroyCond(SDL_cond *cond)
{
SDL_cond_impl_active.Destroy(cond);
}
int
SDL_CondSignal(SDL_cond * cond)
int SDL_CondSignal(SDL_cond *cond)
{
return SDL_cond_impl_active.Signal(cond);
}
int
SDL_CondBroadcast(SDL_cond * cond)
int SDL_CondBroadcast(SDL_cond *cond)
{
return SDL_cond_impl_active.Broadcast(cond);
}
int
SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
{
return SDL_cond_impl_active.WaitTimeout(cond, mutex, ms);
}
int
SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
{
return SDL_cond_impl_active.Wait(cond, mutex);
}

View File

@@ -30,14 +30,10 @@
* which are chosen at runtime.
*/
#include "SDL_sysmutex_c.h"
/* Implementation will be chosen at runtime based on available Kernel features */
SDL_mutex_impl_t SDL_mutex_impl_active = {0};
SDL_mutex_impl_t SDL_mutex_impl_active = { 0 };
/**
* Implementation based on Slim Reader/Writer (SRW) Locks for Win 7 and newer.
@@ -45,8 +41,8 @@ SDL_mutex_impl_t SDL_mutex_impl_active = {0};
#if __WINRT__
/* Functions are guaranteed to be available */
#define pReleaseSRWLockExclusive ReleaseSRWLockExclusive
#define pAcquireSRWLockExclusive AcquireSRWLockExclusive
#define pReleaseSRWLockExclusive ReleaseSRWLockExclusive
#define pAcquireSRWLockExclusive AcquireSRWLockExclusive
#define pTryAcquireSRWLockExclusive TryAcquireSRWLockExclusive
#else
typedef VOID(WINAPI *pfnReleaseSRWLockExclusive)(PSRWLOCK);
@@ -57,13 +53,12 @@ static pfnAcquireSRWLockExclusive pAcquireSRWLockExclusive = NULL;
static pfnTryAcquireSRWLockExclusive pTryAcquireSRWLockExclusive = NULL;
#endif
static SDL_mutex *
SDL_CreateMutex_srw(void)
static SDL_mutex *SDL_CreateMutex_srw(void)
{
SDL_mutex_srw *mutex;
/* Relies on SRWLOCK_INIT == 0. */
mutex = (SDL_mutex_srw *) SDL_calloc(1, sizeof(*mutex));
mutex = (SDL_mutex_srw *)SDL_calloc(1, sizeof(*mutex));
if (mutex == NULL) {
SDL_OutOfMemory();
}
@@ -71,8 +66,7 @@ SDL_CreateMutex_srw(void)
return (SDL_mutex *)mutex;
}
static void
SDL_DestroyMutex_srw(SDL_mutex * mutex)
static void SDL_DestroyMutex_srw(SDL_mutex *mutex)
{
if (mutex != NULL) {
/* There are no kernel allocated resources */
@@ -80,8 +74,7 @@ SDL_DestroyMutex_srw(SDL_mutex * mutex)
}
}
static int
SDL_LockMutex_srw(SDL_mutex * _mutex)
static int SDL_LockMutex_srw(SDL_mutex *_mutex)
{
SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
DWORD this_thread;
@@ -106,8 +99,7 @@ SDL_LockMutex_srw(SDL_mutex * _mutex)
return 0;
}
static int
SDL_TryLockMutex_srw(SDL_mutex * _mutex)
static int SDL_TryLockMutex_srw(SDL_mutex *_mutex)
{
SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
DWORD this_thread;
@@ -132,8 +124,7 @@ SDL_TryLockMutex_srw(SDL_mutex * _mutex)
return retval;
}
static int
SDL_UnlockMutex_srw(SDL_mutex * _mutex)
static int SDL_UnlockMutex_srw(SDL_mutex *_mutex)
{
SDL_mutex_srw *mutex = (SDL_mutex_srw *)_mutex;
@@ -153,8 +144,7 @@ SDL_UnlockMutex_srw(SDL_mutex * _mutex)
return 0;
}
static const SDL_mutex_impl_t SDL_mutex_impl_srw =
{
static const SDL_mutex_impl_t SDL_mutex_impl_srw = {
&SDL_CreateMutex_srw,
&SDL_DestroyMutex_srw,
&SDL_LockMutex_srw,
@@ -163,19 +153,17 @@ static const SDL_mutex_impl_t SDL_mutex_impl_srw =
SDL_MUTEX_SRW,
};
/**
* Fallback Mutex implementation using Critical Sections (before Win 7)
*/
/* Create a mutex */
static SDL_mutex *
SDL_CreateMutex_cs(void)
static SDL_mutex *SDL_CreateMutex_cs(void)
{
SDL_mutex_cs *mutex;
/* Allocate mutex memory */
mutex = (SDL_mutex_cs *) SDL_malloc(sizeof(*mutex));
mutex = (SDL_mutex_cs *)SDL_malloc(sizeof(*mutex));
if (mutex != NULL) {
/* Initialize */
/* On SMP systems, a non-zero spin count generally helps performance */
@@ -191,8 +179,7 @@ SDL_CreateMutex_cs(void)
}
/* Free the mutex */
static void
SDL_DestroyMutex_cs(SDL_mutex * mutex_)
static void SDL_DestroyMutex_cs(SDL_mutex *mutex_)
{
SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
if (mutex != NULL) {
@@ -202,8 +189,7 @@ SDL_DestroyMutex_cs(SDL_mutex * mutex_)
}
/* Lock the mutex */
static int
SDL_LockMutex_cs(SDL_mutex * mutex_)
static int SDL_LockMutex_cs(SDL_mutex *mutex_)
{
SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
if (mutex == NULL) {
@@ -215,8 +201,7 @@ SDL_LockMutex_cs(SDL_mutex * mutex_)
}
/* TryLock the mutex */
static int
SDL_TryLockMutex_cs(SDL_mutex * mutex_)
static int SDL_TryLockMutex_cs(SDL_mutex *mutex_)
{
SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
int retval = 0;
@@ -231,8 +216,7 @@ SDL_TryLockMutex_cs(SDL_mutex * mutex_)
}
/* Unlock the mutex */
static int
SDL_UnlockMutex_cs(SDL_mutex * mutex_)
static int SDL_UnlockMutex_cs(SDL_mutex *mutex_)
{
SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
if (mutex == NULL) {
@@ -243,8 +227,7 @@ SDL_UnlockMutex_cs(SDL_mutex * mutex_)
return 0;
}
static const SDL_mutex_impl_t SDL_mutex_impl_cs =
{
static const SDL_mutex_impl_t SDL_mutex_impl_cs = {
&SDL_CreateMutex_cs,
&SDL_DestroyMutex_cs,
&SDL_LockMutex_cs,
@@ -253,7 +236,6 @@ static const SDL_mutex_impl_t SDL_mutex_impl_cs =
SDL_MUTEX_CS,
};
/**
* Runtime selection and redirection
*/
@@ -263,7 +245,7 @@ SDL_CreateMutex(void)
{
if (SDL_mutex_impl_active.Create == NULL) {
/* Default to fallback implementation */
const SDL_mutex_impl_t * impl = &SDL_mutex_impl_cs;
const SDL_mutex_impl_t *impl = &SDL_mutex_impl_cs;
if (!SDL_GetHintBoolean(SDL_HINT_WINDOWS_FORCE_MUTEX_CRITICAL_SECTIONS, SDL_FALSE)) {
#if __WINRT__
@@ -274,10 +256,10 @@ SDL_CreateMutex(void)
HMODULE kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
if (kernel32) {
/* Requires Vista: */
pReleaseSRWLockExclusive = (pfnReleaseSRWLockExclusive) GetProcAddress(kernel32, "ReleaseSRWLockExclusive");
pAcquireSRWLockExclusive = (pfnAcquireSRWLockExclusive) GetProcAddress(kernel32, "AcquireSRWLockExclusive");
pReleaseSRWLockExclusive = (pfnReleaseSRWLockExclusive)GetProcAddress(kernel32, "ReleaseSRWLockExclusive");
pAcquireSRWLockExclusive = (pfnAcquireSRWLockExclusive)GetProcAddress(kernel32, "AcquireSRWLockExclusive");
/* Requires 7: */
pTryAcquireSRWLockExclusive = (pfnTryAcquireSRWLockExclusive) GetProcAddress(kernel32, "TryAcquireSRWLockExclusive");
pTryAcquireSRWLockExclusive = (pfnTryAcquireSRWLockExclusive)GetProcAddress(kernel32, "TryAcquireSRWLockExclusive");
if (pReleaseSRWLockExclusive && pAcquireSRWLockExclusive && pTryAcquireSRWLockExclusive) {
impl = &SDL_mutex_impl_srw;
}
@@ -291,23 +273,23 @@ SDL_CreateMutex(void)
return SDL_mutex_impl_active.Create();
}
void
SDL_DestroyMutex(SDL_mutex * mutex) {
void SDL_DestroyMutex(SDL_mutex *mutex)
{
SDL_mutex_impl_active.Destroy(mutex);
}
int
SDL_LockMutex(SDL_mutex * mutex) {
int SDL_LockMutex(SDL_mutex *mutex)
{
return SDL_mutex_impl_active.Lock(mutex);
}
int
SDL_TryLockMutex(SDL_mutex * mutex) {
int SDL_TryLockMutex(SDL_mutex *mutex)
{
return SDL_mutex_impl_active.TryLock(mutex);
}
int
SDL_UnlockMutex(SDL_mutex * mutex) {
int SDL_UnlockMutex(SDL_mutex *mutex)
{
return SDL_mutex_impl_active.Unlock(mutex);
}

View File

@@ -22,8 +22,7 @@
#include "../../core/windows/SDL_windows.h"
typedef SDL_mutex * (*pfnSDL_CreateMutex)(void);
typedef SDL_mutex *(*pfnSDL_CreateMutex)(void);
typedef int (*pfnSDL_LockMutex)(SDL_mutex *);
typedef int (*pfnSDL_TryLockMutex)(SDL_mutex *);
typedef int (*pfnSDL_UnlockMutex)(SDL_mutex *);
@@ -38,21 +37,24 @@ typedef enum
typedef struct SDL_mutex_impl_t
{
pfnSDL_CreateMutex Create;
pfnSDL_CreateMutex Create;
pfnSDL_DestroyMutex Destroy;
pfnSDL_LockMutex Lock;
pfnSDL_LockMutex Lock;
pfnSDL_TryLockMutex TryLock;
pfnSDL_UnlockMutex Unlock;
pfnSDL_UnlockMutex Unlock;
/* Needed by SDL_cond: */
SDL_MutexType Type;
SDL_MutexType Type;
} SDL_mutex_impl_t;
extern SDL_mutex_impl_t SDL_mutex_impl_active;
#ifndef SRWLOCK_INIT
#define SRWLOCK_INIT {0}
typedef struct _SRWLOCK {
#define SRWLOCK_INIT \
{ \
0 \
}
typedef struct _SRWLOCK
{
PVOID Ptr;
} SRWLOCK, *PSRWLOCK;
#endif

View File

@@ -31,12 +31,11 @@
* Faster due to significantly less context switches.
* Requires Windows 8 or newer.
* which are chosen at runtime.
*/
*/
#include "../../core/windows/SDL_windows.h"
typedef SDL_sem * (*pfnSDL_CreateSemaphore)(Uint32);
typedef SDL_sem *(*pfnSDL_CreateSemaphore)(Uint32);
typedef void (*pfnSDL_DestroySemaphore)(SDL_sem *);
typedef int (*pfnSDL_SemWaitTimeout)(SDL_sem *, Uint32);
typedef int (*pfnSDL_SemTryWait)(SDL_sem *);
@@ -46,18 +45,17 @@ typedef int (*pfnSDL_SemPost)(SDL_sem *);
typedef struct SDL_semaphore_impl_t
{
pfnSDL_CreateSemaphore Create;
pfnSDL_CreateSemaphore Create;
pfnSDL_DestroySemaphore Destroy;
pfnSDL_SemWaitTimeout WaitTimeout;
pfnSDL_SemTryWait TryWait;
pfnSDL_SemWait Wait;
pfnSDL_SemValue Value;
pfnSDL_SemPost Post;
pfnSDL_SemWaitTimeout WaitTimeout;
pfnSDL_SemTryWait TryWait;
pfnSDL_SemWait Wait;
pfnSDL_SemValue Value;
pfnSDL_SemPost Post;
} SDL_sem_impl_t;
/* Implementation will be chosen at runtime based on available Kernel features */
static SDL_sem_impl_t SDL_sem_impl_active = {0};
static SDL_sem_impl_t SDL_sem_impl_active = { 0 };
/**
* Atomic + WaitOnAddress implementation
@@ -67,7 +65,7 @@ static SDL_sem_impl_t SDL_sem_impl_active = {0};
/* https://www.microsoft.com/en-us/download/details.aspx?id=47328 */
#if (HAVE_WINAPIFAMILY_H) && defined(WINAPI_FAMILY_PHONE_APP)
#define SDL_WINAPI_FAMILY_PHONE (WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP)
#define SDL_WINAPI_FAMILY_PHONE (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
#else
#define SDL_WINAPI_FAMILY_PHONE 0
#endif
@@ -75,10 +73,10 @@ static SDL_sem_impl_t SDL_sem_impl_active = {0};
#if !SDL_WINAPI_FAMILY_PHONE
#if __WINRT__
/* Functions are guaranteed to be available */
#define pWaitOnAddress WaitOnAddress
#define pWaitOnAddress WaitOnAddress
#define pWakeByAddressSingle WakeByAddressSingle
#else
typedef BOOL(WINAPI *pfnWaitOnAddress)(volatile VOID*, PVOID, SIZE_T, DWORD);
typedef BOOL(WINAPI *pfnWaitOnAddress)(volatile VOID *, PVOID, SIZE_T, DWORD);
typedef VOID(WINAPI *pfnWakeByAddressSingle)(PVOID);
static pfnWaitOnAddress pWaitOnAddress = NULL;
@@ -90,12 +88,11 @@ typedef struct SDL_semaphore_atom
LONG count;
} SDL_sem_atom;
static SDL_sem *
SDL_CreateSemaphore_atom(Uint32 initial_value)
static SDL_sem *SDL_CreateSemaphore_atom(Uint32 initial_value)
{
SDL_sem_atom *sem;
sem = (SDL_sem_atom *) SDL_malloc(sizeof(*sem));
sem = (SDL_sem_atom *)SDL_malloc(sizeof(*sem));
if (sem != NULL) {
sem->count = initial_value;
} else {
@@ -104,16 +101,14 @@ SDL_CreateSemaphore_atom(Uint32 initial_value)
return (SDL_sem *)sem;
}
static void
SDL_DestroySemaphore_atom(SDL_sem * sem)
static void SDL_DestroySemaphore_atom(SDL_sem *sem)
{
if (sem != NULL) {
SDL_free(sem);
}
}
static int
SDL_SemTryWait_atom(SDL_sem * _sem)
static int SDL_SemTryWait_atom(SDL_sem *_sem)
{
SDL_sem_atom *sem = (SDL_sem_atom *)_sem;
LONG count;
@@ -134,8 +129,7 @@ SDL_SemTryWait_atom(SDL_sem * _sem)
return SDL_MUTEX_TIMEDOUT;
}
static int
SDL_SemWait_atom(SDL_sem * _sem)
static int SDL_SemWait_atom(SDL_sem *_sem)
{
SDL_sem_atom *sem = (SDL_sem_atom *)_sem;
LONG count;
@@ -159,8 +153,7 @@ SDL_SemWait_atom(SDL_sem * _sem)
}
}
static int
SDL_SemWaitTimeout_atom(SDL_sem * _sem, Uint32 timeout)
static int SDL_SemWaitTimeout_atom(SDL_sem *_sem, Uint32 timeout)
{
SDL_sem_atom *sem = (SDL_sem_atom *)_sem;
LONG count;
@@ -181,7 +174,7 @@ SDL_SemWaitTimeout_atom(SDL_sem * _sem, Uint32 timeout)
* need to recalculate the effective timeout before every wait
*/
now = SDL_GetTicks();
deadline = now + (DWORD) timeout;
deadline = now + (DWORD)timeout;
for (;;) {
count = sem->count;
@@ -210,8 +203,7 @@ SDL_SemWaitTimeout_atom(SDL_sem * _sem, Uint32 timeout)
}
}
static Uint32
SDL_SemValue_atom(SDL_sem * _sem)
static Uint32 SDL_SemValue_atom(SDL_sem *_sem)
{
SDL_sem_atom *sem = (SDL_sem_atom *)_sem;
@@ -223,8 +215,7 @@ SDL_SemValue_atom(SDL_sem * _sem)
return (Uint32)sem->count;
}
static int
SDL_SemPost_atom(SDL_sem * _sem)
static int SDL_SemPost_atom(SDL_sem *_sem)
{
SDL_sem_atom *sem = (SDL_sem_atom *)_sem;
@@ -238,8 +229,7 @@ SDL_SemPost_atom(SDL_sem * _sem)
return 0;
}
static const SDL_sem_impl_t SDL_sem_impl_atom =
{
static const SDL_sem_impl_t SDL_sem_impl_atom = {
&SDL_CreateSemaphore_atom,
&SDL_DestroySemaphore_atom,
&SDL_SemWaitTimeout_atom,
@@ -250,7 +240,6 @@ static const SDL_sem_impl_t SDL_sem_impl_atom =
};
#endif /* !SDL_WINAPI_FAMILY_PHONE */
/**
* Fallback Semaphore implementation using Kernel Semaphores
*/
@@ -262,13 +251,12 @@ typedef struct SDL_semaphore_kern
} SDL_sem_kern;
/* Create a semaphore */
static SDL_sem *
SDL_CreateSemaphore_kern(Uint32 initial_value)
static SDL_sem *SDL_CreateSemaphore_kern(Uint32 initial_value)
{
SDL_sem_kern *sem;
/* Allocate sem memory */
sem = (SDL_sem_kern *) SDL_malloc(sizeof(*sem));
sem = (SDL_sem_kern *)SDL_malloc(sizeof(*sem));
if (sem != NULL) {
/* Create the semaphore, with max value 32K */
#if __WINRT__
@@ -289,8 +277,7 @@ SDL_CreateSemaphore_kern(Uint32 initial_value)
}
/* Free the semaphore */
static void
SDL_DestroySemaphore_kern(SDL_sem * _sem)
static void SDL_DestroySemaphore_kern(SDL_sem *_sem)
{
SDL_sem_kern *sem = (SDL_sem_kern *)_sem;
if (sem != NULL) {
@@ -302,8 +289,7 @@ SDL_DestroySemaphore_kern(SDL_sem * _sem)
}
}
static int
SDL_SemWaitTimeout_kern(SDL_sem * _sem, Uint32 timeout)
static int SDL_SemWaitTimeout_kern(SDL_sem *_sem, Uint32 timeout)
{
SDL_sem_kern *sem = (SDL_sem_kern *)_sem;
int retval;
@@ -316,7 +302,7 @@ SDL_SemWaitTimeout_kern(SDL_sem * _sem, Uint32 timeout)
if (timeout == SDL_MUTEX_MAXWAIT) {
dwMilliseconds = INFINITE;
} else {
dwMilliseconds = (DWORD) timeout;
dwMilliseconds = (DWORD)timeout;
}
switch (WaitForSingleObjectEx(sem->id, dwMilliseconds, FALSE)) {
case WAIT_OBJECT_0:
@@ -333,21 +319,18 @@ SDL_SemWaitTimeout_kern(SDL_sem * _sem, Uint32 timeout)
return retval;
}
static int
SDL_SemTryWait_kern(SDL_sem * sem)
static int SDL_SemTryWait_kern(SDL_sem *sem)
{
return SDL_SemWaitTimeout_kern(sem, 0);
}
static int
SDL_SemWait_kern(SDL_sem * sem)
static int SDL_SemWait_kern(SDL_sem *sem)
{
return SDL_SemWaitTimeout_kern(sem, SDL_MUTEX_MAXWAIT);
}
/* Returns the current count of the semaphore */
static Uint32
SDL_SemValue_kern(SDL_sem * _sem)
static Uint32 SDL_SemValue_kern(SDL_sem *_sem)
{
SDL_sem_kern *sem = (SDL_sem_kern *)_sem;
if (sem == NULL) {
@@ -357,8 +340,7 @@ SDL_SemValue_kern(SDL_sem * _sem)
return (Uint32)sem->count;
}
static int
SDL_SemPost_kern(SDL_sem * _sem)
static int SDL_SemPost_kern(SDL_sem *_sem)
{
SDL_sem_kern *sem = (SDL_sem_kern *)_sem;
if (sem == NULL) {
@@ -371,14 +353,13 @@ SDL_SemPost_kern(SDL_sem * _sem)
*/
InterlockedIncrement(&sem->count);
if (ReleaseSemaphore(sem->id, 1, NULL) == FALSE) {
InterlockedDecrement(&sem->count); /* restore */
InterlockedDecrement(&sem->count); /* restore */
return SDL_SetError("ReleaseSemaphore() failed");
}
return 0;
}
static const SDL_sem_impl_t SDL_sem_impl_kern =
{
static const SDL_sem_impl_t SDL_sem_impl_kern = {
&SDL_CreateSemaphore_kern,
&SDL_DestroySemaphore_kern,
&SDL_SemWaitTimeout_kern,
@@ -388,7 +369,6 @@ static const SDL_sem_impl_t SDL_sem_impl_kern =
&SDL_SemPost_kern,
};
/**
* Runtime selection and redirection
*/
@@ -398,7 +378,7 @@ SDL_CreateSemaphore(Uint32 initial_value)
{
if (SDL_sem_impl_active.Create == NULL) {
/* Default to fallback implementation */
const SDL_sem_impl_t * impl = &SDL_sem_impl_kern;
const SDL_sem_impl_t *impl = &SDL_sem_impl_kern;
#if !SDL_WINAPI_FAMILY_PHONE
if (!SDL_GetHintBoolean(SDL_HINT_WINDOWS_FORCE_SEMAPHORE_KERNEL, SDL_FALSE)) {
@@ -415,8 +395,8 @@ SDL_CreateSemaphore(Uint32 initial_value)
HMODULE synch120 = GetModuleHandle(TEXT("api-ms-win-core-synch-l1-2-0.dll"));
if (synch120) {
/* Try to load required functions provided by Win 8 or newer */
pWaitOnAddress = (pfnWaitOnAddress) GetProcAddress(synch120, "WaitOnAddress");
pWakeByAddressSingle = (pfnWakeByAddressSingle) GetProcAddress(synch120, "WakeByAddressSingle");
pWaitOnAddress = (pfnWaitOnAddress)GetProcAddress(synch120, "WaitOnAddress");
pWakeByAddressSingle = (pfnWakeByAddressSingle)GetProcAddress(synch120, "WakeByAddressSingle");
if (pWaitOnAddress && pWakeByAddressSingle) {
impl = &SDL_sem_impl_atom;
@@ -432,38 +412,33 @@ SDL_CreateSemaphore(Uint32 initial_value)
return SDL_sem_impl_active.Create(initial_value);
}
void
SDL_DestroySemaphore(SDL_sem * sem)
void SDL_DestroySemaphore(SDL_sem *sem)
{
SDL_sem_impl_active.Destroy(sem);
}
int
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
{
return SDL_sem_impl_active.WaitTimeout(sem, timeout);
}
int
SDL_SemTryWait(SDL_sem * sem)
int SDL_SemTryWait(SDL_sem *sem)
{
return SDL_sem_impl_active.TryWait(sem);
}
int
SDL_SemWait(SDL_sem * sem)
int SDL_SemWait(SDL_sem *sem)
{
return SDL_sem_impl_active.Wait(sem);
}
Uint32
SDL_SemValue(SDL_sem * sem)
SDL_SemValue(SDL_sem *sem)
{
return SDL_sem_impl_active.Value(sem);
}
int
SDL_SemPost(SDL_sem * sem)
int SDL_SemPost(SDL_sem *sem)
{
return SDL_sem_impl_active.Post(sem);
}

View File

@@ -38,28 +38,26 @@
/* Cygwin gcc-3 ... MingW64 (even with a i386 host) does this like MSVC. */
#if (defined(__MINGW32__) && (__GNUC__ < 4))
typedef unsigned long (__cdecl *pfnSDL_CurrentBeginThread) (void *, unsigned,
unsigned (__stdcall *func)(void *), void *arg,
unsigned, unsigned *threadID);
typedef void (__cdecl *pfnSDL_CurrentEndThread)(unsigned code);
typedef unsigned long(__cdecl *pfnSDL_CurrentBeginThread)(void *, unsigned,
unsigned(__stdcall *func)(void *), void *arg,
unsigned, unsigned *threadID);
typedef void(__cdecl *pfnSDL_CurrentEndThread)(unsigned code);
#else
typedef uintptr_t(__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
unsigned (__stdcall *
func) (void
*),
void *arg, unsigned,
unsigned *threadID);
typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
typedef uintptr_t(__cdecl *pfnSDL_CurrentBeginThread)(void *, unsigned,
unsigned(__stdcall *
func)(void
*),
void *arg, unsigned,
unsigned *threadID);
typedef void(__cdecl *pfnSDL_CurrentEndThread)(unsigned code);
#endif
#endif /* !SDL_PASSED_BEGINTHREAD_ENDTHREAD */
static DWORD
RunThread(void *data)
static DWORD RunThread(void *data)
{
SDL_Thread *thread = (SDL_Thread *) data;
pfnSDL_CurrentEndThread pfnEndThread = (pfnSDL_CurrentEndThread) thread->endfunc;
SDL_Thread *thread = (SDL_Thread *)data;
pfnSDL_CurrentEndThread pfnEndThread = (pfnSDL_CurrentEndThread)thread->endfunc;
SDL_RunThread(thread);
if (pfnEndThread != NULL) {
pfnEndThread(0);
@@ -67,33 +65,28 @@ RunThread(void *data)
return 0;
}
static DWORD WINAPI
RunThreadViaCreateThread(LPVOID data)
static DWORD WINAPI RunThreadViaCreateThread(LPVOID data)
{
return RunThread(data);
return RunThread(data);
}
static unsigned __stdcall
RunThreadViaBeginThreadEx(void *data)
static unsigned __stdcall RunThreadViaBeginThreadEx(void *data)
{
return (unsigned)RunThread(data);
return (unsigned)RunThread(data);
}
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
int
SDL_SYS_CreateThread(SDL_Thread * thread,
pfnSDL_CurrentBeginThread pfnBeginThread,
pfnSDL_CurrentEndThread pfnEndThread)
int SDL_SYS_CreateThread(SDL_Thread *thread,
pfnSDL_CurrentBeginThread pfnBeginThread,
pfnSDL_CurrentEndThread pfnEndThread)
{
#elif defined(__CYGWIN__) || defined(__WINRT__)
int
SDL_SYS_CreateThread(SDL_Thread * thread)
int SDL_SYS_CreateThread(SDL_Thread *thread)
{
pfnSDL_CurrentBeginThread pfnBeginThread = NULL;
pfnSDL_CurrentEndThread pfnEndThread = NULL;
#else
int
SDL_SYS_CreateThread(SDL_Thread * thread)
int SDL_SYS_CreateThread(SDL_Thread *thread)
{
pfnSDL_CurrentBeginThread pfnBeginThread = (pfnSDL_CurrentBeginThread)_beginthreadex;
pfnSDL_CurrentEndThread pfnEndThread = (pfnSDL_CurrentEndThread)_endthreadex;
@@ -106,10 +99,9 @@ SDL_SYS_CreateThread(SDL_Thread * thread)
/* thread->stacksize == 0 means "system default", same as win32 expects */
if (pfnBeginThread) {
unsigned threadid = 0;
thread->handle = (SYS_ThreadHandle)
((size_t) pfnBeginThread(NULL, (unsigned int) thread->stacksize,
RunThreadViaBeginThreadEx,
thread, flags, &threadid));
thread->handle = (SYS_ThreadHandle)((size_t)pfnBeginThread(NULL, (unsigned int)thread->stacksize,
RunThreadViaBeginThreadEx,
thread, flags, &threadid));
} else {
DWORD threadid = 0;
thread->handle = CreateThread(NULL, thread->stacksize,
@@ -122,31 +114,29 @@ SDL_SYS_CreateThread(SDL_Thread * thread)
return 0;
}
#pragma pack(push,8)
#pragma pack(push, 8)
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; /* must be 0x1000 */
LPCSTR szName; /* pointer to name (in user addr space) */
DWORD dwType; /* must be 0x1000 */
LPCSTR szName; /* pointer to name (in user addr space) */
DWORD dwThreadID; /* thread ID (-1=caller thread) */
DWORD dwFlags; /* reserved for future use, must be zero */
DWORD dwFlags; /* reserved for future use, must be zero */
} THREADNAME_INFO;
#pragma pack(pop)
typedef HRESULT(WINAPI *pfnSetThreadDescription)(HANDLE, PCWSTR);
typedef HRESULT (WINAPI *pfnSetThreadDescription)(HANDLE, PCWSTR);
void
SDL_SYS_SetupThread(const char *name)
void SDL_SYS_SetupThread(const char *name)
{
if (name != NULL) {
#ifndef __WINRT__ /* !!! FIXME: There's no LoadLibrary() in WinRT; don't know if SetThreadDescription is available there at all at the moment. */
#ifndef __WINRT__ /* !!! FIXME: There's no LoadLibrary() in WinRT; don't know if SetThreadDescription is available there at all at the moment. */
static pfnSetThreadDescription pSetThreadDescription = NULL;
static HMODULE kernel32 = 0;
if (!kernel32) {
kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
if (kernel32) {
pSetThreadDescription = (pfnSetThreadDescription) GetProcAddress(kernel32, "SetThreadDescription");
pSetThreadDescription = (pfnSetThreadDescription)GetProcAddress(kernel32, "SetThreadDescription");
}
}
@@ -157,7 +147,7 @@ SDL_SYS_SetupThread(const char *name)
SDL_free(strw);
}
}
#endif
#endif
/* Presumably some version of Visual Studio will understand SetThreadDescription(),
but we still need to deal with older OSes and debuggers. Set it with the arcane
@@ -175,11 +165,11 @@ SDL_SYS_SetupThread(const char *name)
SDL_zero(inf);
inf.dwType = 0x1000;
inf.szName = name;
inf.dwThreadID = (DWORD) -1;
inf.dwThreadID = (DWORD)-1;
inf.dwFlags = 0;
/* The debugger catches this, renames the thread, continues on. */
RaiseException(0x406D1388, 0, sizeof(inf) / sizeof(ULONG), (const ULONG_PTR*) &inf);
RaiseException(0x406D1388, 0, sizeof(inf) / sizeof(ULONG), (const ULONG_PTR *)&inf);
}
}
}
@@ -190,8 +180,7 @@ SDL_ThreadID(void)
return (SDL_threadID)GetCurrentThreadId();
}
int
SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
{
int value;
@@ -210,15 +199,13 @@ SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
return 0;
}
void
SDL_SYS_WaitThread(SDL_Thread * thread)
void SDL_SYS_WaitThread(SDL_Thread *thread)
{
WaitForSingleObjectEx(thread->handle, INFINITE, FALSE);
CloseHandle(thread->handle);
}
void
SDL_SYS_DetachThread(SDL_Thread * thread)
void SDL_SYS_DetachThread(SDL_Thread *thread)
{
CloseHandle(thread->handle);
}

View File

@@ -31,7 +31,7 @@
#include <fibersapi.h>
#ifndef TLS_OUT_OF_INDEXES
#define TLS_OUT_OF_INDEXES FLS_OUT_OF_INDEXES
#define TLS_OUT_OF_INDEXES FLS_OUT_OF_INDEXES
#endif
#define TlsAlloc() FlsAlloc(NULL)
@@ -66,8 +66,7 @@ SDL_SYS_GetTLSData(void)
return (SDL_TLSData *)TlsGetValue(thread_local_storage);
}
int
SDL_SYS_SetTLSData(SDL_TLSData *data)
int SDL_SYS_SetTLSData(SDL_TLSData *data)
{
if (generic_local_storage) {
return SDL_Generic_SetTLSData(data);