thread: code style

(cherry picked from commit 461a38ff1af1d749426c8e2c6f2a56e2b31c1a45)
This commit is contained in:
pionere 2022-11-29 17:30:03 +01:00 committed by Sam Lantinga
parent 8cda5102fc
commit 0b7a9a8e9f
12 changed files with 43 additions and 42 deletions

View file

@ -74,7 +74,7 @@ static void WaitAll(SDL_sem *sem)
RSemaphore sema; RSemaphore sema;
sema.SetHandle(sem->handle); sema.SetHandle(sem->handle);
sema.Wait(); sema.Wait();
while(sem->count < 0) { while (sem->count < 0) {
sema.Wait(); sema.Wait();
} }
} }
@ -96,7 +96,7 @@ SDL_CreateSemaphore(Uint32 initial_value)
void void
SDL_DestroySemaphore(SDL_sem * sem) SDL_DestroySemaphore(SDL_sem * sem)
{ {
if (sem) { if (sem != NULL) {
RSemaphore sema; RSemaphore sema;
sema.SetHandle(sem->handle); sema.SetHandle(sem->handle);
sema.Signal(sema.Count()); sema.Signal(sema.Count());

View file

@ -54,13 +54,11 @@ CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny* aPtr1, TAny*
{ {
TBuf<16> name; TBuf<16> name;
TInt status = KErrNone; TInt status = KErrNone;
do do {
{
object_count++; object_count++;
name.Format(_L("SDL_%x"), object_count); name.Format(_L("SDL_%x"), object_count);
status = aFunc(name, aPtr1, aPtr2); status = aFunc(name, aPtr1, aPtr2);
} } while (status == KErrAlreadyExists);
while(status == KErrAlreadyExists);
return status; return status;
} }
@ -73,8 +71,7 @@ SDL_SYS_CreateThread(SDL_Thread *thread)
if (status != KErrNone) { if (status != KErrNone) {
delete(((RThread*)(thread->handle))); delete(((RThread*)(thread->handle)));
thread->handle = NULL; thread->handle = NULL;
SDL_SetError("Not enough resources to create thread"); return SDL_SetError("Not enough resources to create thread");
return -1;
} }
rthread.Resume(); rthread.Resume();

View file

@ -85,7 +85,7 @@ int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
int ret; int ret;
struct timer_alarm_t alarm; struct timer_alarm_t alarm;
InitializeTimerAlarm(&alarm); InitializeTimerAlarm(&alarm);
if (sem == NULL) { if (sem == NULL) {
return SDL_InvalidParamError("sem"); return SDL_InvalidParamError("sem");
} }
@ -107,7 +107,7 @@ int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
if (ret < 0) { if (ret < 0) {
return SDL_MUTEX_TIMEDOUT; return SDL_MUTEX_TIMEDOUT;
} }
return 0; //Wait condition satisfied. return 0; // Wait condition satisfied.
} }
int SDL_SemTryWait(SDL_sem *sem) int SDL_SemTryWait(SDL_sem *sem)

View file

@ -46,7 +46,7 @@ SDL_sem *
SDL_CreateSemaphore(Uint32 initial_value) 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) { if (sem != NULL) {
if (sem_init(&sem->sem, 0, initial_value) < 0) { if (sem_init(&sem->sem, 0, initial_value) < 0) {
SDL_SetError("sem_init() failed"); SDL_SetError("sem_init() failed");
SDL_free(sem); SDL_free(sem);
@ -61,7 +61,7 @@ SDL_CreateSemaphore(Uint32 initial_value)
void void
SDL_DestroySemaphore(SDL_sem * sem) SDL_DestroySemaphore(SDL_sem * sem)
{ {
if (sem) { if (sem != NULL) {
sem_destroy(&sem->sem); sem_destroy(&sem->sem);
SDL_free(sem); SDL_free(sem);
} }
@ -180,11 +180,15 @@ Uint32
SDL_SemValue(SDL_sem * sem) SDL_SemValue(SDL_sem * sem)
{ {
int ret = 0; int ret = 0;
if (sem) {
sem_getvalue(&sem->sem, &ret); if (sem == NULL) {
if (ret < 0) { SDL_InvalidParamError("sem");
ret = 0; return 0;
} }
sem_getvalue(&sem->sem, &ret);
if (ret < 0) {
ret = 0;
} }
return (Uint32) ret; return (Uint32) ret;
} }

View file

@ -59,7 +59,7 @@ extern "C"
void void
SDL_DestroyCond(SDL_cond * cond) SDL_DestroyCond(SDL_cond * cond)
{ {
if (cond) { if (cond != NULL) {
delete cond; delete cond;
} }
} }
@ -115,11 +115,11 @@ extern "C"
int int
SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms) SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
{ {
if (!cond) { if (cond == NULL) {
return SDL_InvalidParamError("cond"); return SDL_InvalidParamError("cond");
} }
if (!mutex) { if (mutex == NULL) {
return SDL_InvalidParamError("mutex"); return SDL_InvalidParamError("mutex");
} }
@ -132,7 +132,7 @@ SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
cpp_lock.release(); cpp_lock.release();
return 0; return 0;
} else { } else {
auto wait_result = cond->cpp_cond.wait_for ( auto wait_result = cond->cpp_cond.wait_for(
cpp_lock, cpp_lock,
std::chrono::duration<Uint32, std::milli>(ms) std::chrono::duration<Uint32, std::milli>(ms)
); );

View file

@ -54,7 +54,7 @@ extern "C"
void void
SDL_DestroyMutex(SDL_mutex * mutex) SDL_DestroyMutex(SDL_mutex * mutex)
{ {
if (mutex) { if (mutex != NULL) {
delete mutex; delete mutex;
} }
} }

View file

@ -46,7 +46,7 @@ SDL_CreateCond(void)
SDL_cond *cond; SDL_cond *cond;
cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond)); cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
if (cond) { if (cond != NULL) {
cond->lock = SDL_CreateMutex(); cond->lock = SDL_CreateMutex();
cond->wait_sem = SDL_CreateSemaphore(0); cond->wait_sem = SDL_CreateSemaphore(0);
cond->wait_done = SDL_CreateSemaphore(0); cond->wait_done = SDL_CreateSemaphore(0);
@ -65,7 +65,7 @@ SDL_CreateCond(void)
void void
SDL_DestroyCond(SDL_cond * cond) SDL_DestroyCond(SDL_cond * cond)
{ {
if (cond) { if (cond != NULL) {
if (cond->wait_sem) { if (cond->wait_sem) {
SDL_DestroySemaphore(cond->wait_sem); SDL_DestroySemaphore(cond->wait_sem);
} }

View file

@ -42,7 +42,7 @@ SDL_CreateMutex(void)
/* Allocate mutex memory */ /* Allocate mutex memory */
mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex)); mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
if (mutex) { if (mutex != NULL) {
res = sceKernelCreateLwMutex( res = sceKernelCreateLwMutex(
&mutex->lock, &mutex->lock,
@ -65,7 +65,7 @@ SDL_CreateMutex(void)
void void
SDL_DestroyMutex(SDL_mutex * mutex) SDL_DestroyMutex(SDL_mutex * mutex)
{ {
if (mutex) { if (mutex != NULL) {
sceKernelDeleteLwMutex(&mutex->lock); sceKernelDeleteLwMutex(&mutex->lock);
SDL_free(mutex); SDL_free(mutex);
} }

View file

@ -80,7 +80,7 @@ void SDL_DestroySemaphore(SDL_sem *sem)
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
{ {
Uint32 *pTimeout; Uint32 *pTimeout;
unsigned int res; unsigned int res;
if (sem == NULL) { if (sem == NULL) {
return SDL_InvalidParamError("sem"); return SDL_InvalidParamError("sem");
@ -102,13 +102,13 @@ int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
} }
res = sceKernelWaitSema(sem->semid, 1, pTimeout); res = sceKernelWaitSema(sem->semid, 1, pTimeout);
switch (res) { switch (res) {
case SCE_KERNEL_OK: case SCE_KERNEL_OK:
return 0; return 0;
case SCE_KERNEL_ERROR_WAIT_TIMEOUT: case SCE_KERNEL_ERROR_WAIT_TIMEOUT:
return SDL_MUTEX_TIMEDOUT; return SDL_MUTEX_TIMEDOUT;
default: default:
return SDL_SetError("WaitForSingleObject() failed"); return SDL_SetError("WaitForSingleObject() failed");
} }
} }

View file

@ -98,7 +98,7 @@ SDL_CreateCond_cv(void)
static void static void
SDL_DestroyCond_cv(SDL_cond * cond) SDL_DestroyCond_cv(SDL_cond * cond)
{ {
if (cond) { if (cond != NULL) {
/* There are no kernel allocated resources */ /* There are no kernel allocated resources */
SDL_free(cond); SDL_free(cond);
} }

View file

@ -75,7 +75,7 @@ SDL_CreateMutex_srw(void)
static void static void
SDL_DestroyMutex_srw(SDL_mutex * mutex) SDL_DestroyMutex_srw(SDL_mutex * mutex)
{ {
if (mutex) { if (mutex != NULL) {
/* There are no kernel allocated resources */ /* There are no kernel allocated resources */
SDL_free(mutex); SDL_free(mutex);
} }
@ -177,7 +177,7 @@ SDL_CreateMutex_cs(void)
/* Allocate mutex memory */ /* Allocate mutex memory */
mutex = (SDL_mutex_cs *) SDL_malloc(sizeof(*mutex)); mutex = (SDL_mutex_cs *) SDL_malloc(sizeof(*mutex));
if (mutex) { if (mutex != NULL) {
/* Initialize */ /* Initialize */
/* On SMP systems, a non-zero spin count generally helps performance */ /* On SMP systems, a non-zero spin count generally helps performance */
#if __WINRT__ #if __WINRT__
@ -196,7 +196,7 @@ static void
SDL_DestroyMutex_cs(SDL_mutex * mutex_) SDL_DestroyMutex_cs(SDL_mutex * mutex_)
{ {
SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_; SDL_mutex_cs *mutex = (SDL_mutex_cs *)mutex_;
if (mutex) { if (mutex != NULL) {
DeleteCriticalSection(&mutex->cs); DeleteCriticalSection(&mutex->cs);
SDL_free(mutex); SDL_free(mutex);
} }

View file

@ -99,7 +99,7 @@ SDL_CreateSemaphore_atom(Uint32 initial_value)
SDL_sem_atom *sem; SDL_sem_atom *sem;
sem = (SDL_sem_atom *) SDL_malloc(sizeof(*sem)); sem = (SDL_sem_atom *) SDL_malloc(sizeof(*sem));
if (sem) { if (sem != NULL) {
sem->count = initial_value; sem->count = initial_value;
} else { } else {
SDL_OutOfMemory(); SDL_OutOfMemory();
@ -110,7 +110,7 @@ SDL_CreateSemaphore_atom(Uint32 initial_value)
static void static void
SDL_DestroySemaphore_atom(SDL_sem * sem) SDL_DestroySemaphore_atom(SDL_sem * sem)
{ {
if (sem) { if (sem != NULL) {
SDL_free(sem); SDL_free(sem);
} }
} }
@ -272,7 +272,7 @@ SDL_CreateSemaphore_kern(Uint32 initial_value)
/* Allocate sem memory */ /* Allocate sem memory */
sem = (SDL_sem_kern *) SDL_malloc(sizeof(*sem)); sem = (SDL_sem_kern *) SDL_malloc(sizeof(*sem));
if (sem) { if (sem != NULL) {
/* Create the semaphore, with max value 32K */ /* Create the semaphore, with max value 32K */
#if __WINRT__ #if __WINRT__
sem->id = CreateSemaphoreEx(NULL, initial_value, 32 * 1024, NULL, 0, SEMAPHORE_ALL_ACCESS); sem->id = CreateSemaphoreEx(NULL, initial_value, 32 * 1024, NULL, 0, SEMAPHORE_ALL_ACCESS);
@ -296,7 +296,7 @@ static void
SDL_DestroySemaphore_kern(SDL_sem * _sem) SDL_DestroySemaphore_kern(SDL_sem * _sem)
{ {
SDL_sem_kern *sem = (SDL_sem_kern *)_sem; SDL_sem_kern *sem = (SDL_sem_kern *)_sem;
if (sem) { if (sem != NULL) {
if (sem->id) { if (sem->id) {
CloseHandle(sem->id); CloseHandle(sem->id);
sem->id = 0; sem->id = 0;