code formatting

This commit is contained in:
dborth 2009-05-29 15:28:11 +00:00
parent 771aa952a5
commit 63262e4da3
4 changed files with 94 additions and 79 deletions

View file

@ -44,21 +44,26 @@ SDL_cond * SDL_CreateCond(void)
SDL_cond *cond;
cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
if ( cond ) {
if (LWP_CondInit (&(cond->cond))<0) {
if (cond)
{
if (LWP_CondInit(&(cond->cond)) < 0)
{
SDL_DestroyCond(cond);
cond = NULL;
}
} else {
}
else
{
SDL_OutOfMemory();
}
return(cond);
return (cond);
}
/* Destroy a condition variable */
void SDL_DestroyCond(SDL_cond *cond)
{
if ( cond ) {
if (cond)
{
LWP_CondDestroy(cond->cond);
SDL_free(cond);
}
@ -67,45 +72,47 @@ void SDL_DestroyCond(SDL_cond *cond)
/* Restart one of the threads that are waiting on the condition variable */
int SDL_CondSignal(SDL_cond *cond)
{
if ( ! cond ) {
if (!cond)
{
SDL_SetError("Passed a NULL condition variable");
return -1;
}
return LWP_CondSignal(cond->cond)==0?0:-1;
return LWP_CondSignal(cond->cond) == 0 ? 0 : -1;
}
/* Restart all threads that are waiting on the condition variable */
int SDL_CondBroadcast(SDL_cond *cond)
{
if ( ! cond ) {
if (!cond)
{
SDL_SetError("Passed a NULL condition variable");
return -1;
}
return LWP_CondBroadcast(cond->cond)==0?0:-1;
return LWP_CondBroadcast(cond->cond) == 0 ? 0 : -1;
}
/* Wait on the condition variable for at most 'ms' milliseconds.
The mutex must be locked before entering this function!
The mutex is unlocked during the wait, and locked again after the wait.
The mutex must be locked before entering this function!
The mutex is unlocked during the wait, and locked again after the wait.
Typical use:
Typical use:
Thread A:
SDL_LockMutex(lock);
while ( ! condition ) {
SDL_CondWait(cond);
}
SDL_UnlockMutex(lock);
Thread A:
SDL_LockMutex(lock);
while ( ! condition ) {
SDL_CondWait(cond);
}
SDL_UnlockMutex(lock);
Thread B:
SDL_LockMutex(lock);
...
condition = true;
...
SDL_UnlockMutex(lock);
Thread B:
SDL_LockMutex(lock);
...
condition = true;
...
SDL_UnlockMutex(lock);
*/
extern int clock_gettime(struct timespec *tp);
@ -115,26 +122,27 @@ int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
struct timespec now;
struct timespec abstime;
if ( ! cond ) {
if (!cond)
{
SDL_SetError("Passed a NULL condition variable");
return -1;
}
clock_gettime(&now);
abstime.tv_sec = now.tv_sec + (ms/1000);
abstime.tv_nsec = (now.tv_nsec + (ms%1000) * 1000) * 1000;
if ( abstime.tv_nsec > 1000000000 ) {
abstime.tv_sec += 1;
abstime.tv_nsec -= 1000000000;
}
abstime.tv_sec = now.tv_sec + (ms / 1000);
abstime.tv_nsec = (now.tv_nsec + (ms % 1000) * 1000) * 1000;
if (abstime.tv_nsec > 1000000000)
{
abstime.tv_sec += 1;
abstime.tv_nsec -= 1000000000;
}
return LWP_CondTimedWait ( cond->cond, mutex->id, &abstime ) ;
return LWP_CondTimedWait(cond->cond, mutex->id, &abstime);
}
/* Wait on the condition variable forever */
int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
{
return LWP_CondWait ( cond->cond, mutex->id ) ;
return LWP_CondWait(cond->cond, mutex->id);
}

View file

@ -28,20 +28,24 @@
#include <ogcsys.h>
struct SDL_mutex {
struct SDL_mutex
{
u32 id;
};
/* Create a mutex */
SDL_mutex *SDL_CreateMutex(void)
{
SDL_mutex *mutex;
SDL_mutex *mutex = NULL;
/* Allocate mutex memory */
mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex));
if ( mutex ) {
LWP_MutexInit (&mutex->id, 0);
} else {
mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
if (mutex)
{
LWP_MutexInit(&mutex->id, 0);
}
else
{
SDL_OutOfMemory();
}
return mutex;
@ -50,7 +54,8 @@ SDL_mutex *SDL_CreateMutex(void)
/* Free the mutex */
void SDL_DestroyMutex(SDL_mutex *mutex)
{
if ( mutex ) {
if (mutex)
{
LWP_MutexDestroy(mutex->id);
SDL_free(mutex);
}
@ -59,29 +64,24 @@ void SDL_DestroyMutex(SDL_mutex *mutex)
/* Lock the semaphore */
int SDL_mutexP(SDL_mutex *mutex)
{
if ( mutex == NULL ) {
if (mutex == NULL)
{
SDL_SetError("Passed a NULL mutex");
return -1;
}
return LWP_MutexLock(mutex->id);
}
/* Unlock the mutex */
int SDL_mutexV(SDL_mutex *mutex)
{
if ( mutex == NULL ) {
if (mutex == NULL)
{
SDL_SetError("Passed a NULL mutex");
return -1;
}
/* If we don't own the mutex, we can't unlock it */
/* if ( SDL_ThreadID() != mutex->owner ) {
SDL_SetError("mutex not owned by this thread");
return -1;
}*/
return LWP_MutexUnlock(mutex->id);
}

View file

@ -27,8 +27,6 @@
#include "SDL_thread.h"
#include "SDL_systhread_c.h"
struct SDL_semaphore
{
Uint32 count;
@ -41,8 +39,9 @@ SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
{
SDL_sem *sem;
sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
if ( ! sem ) {
sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
if (!sem)
{
SDL_OutOfMemory();
return NULL;
}
@ -51,7 +50,8 @@ SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
sem->count_lock = SDL_CreateMutex();
sem->count_nonzero = SDL_CreateCond();
if ( ! sem->count_lock || ! sem->count_nonzero ) {
if (!sem->count_lock || !sem->count_nonzero)
{
SDL_DestroySemaphore(sem);
return NULL;
}
@ -60,18 +60,21 @@ SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
}
/* WARNING:
You cannot call this function when another thread is using the semaphore.
*/
You cannot call this function when another thread is using the semaphore.
*/
void SDL_DestroySemaphore(SDL_sem *sem)
{
if ( sem ) {
if (sem)
{
sem->count = 0xFFFFFFFF;
while ( sem->waiters_count > 0) {
while (sem->waiters_count > 0)
{
SDL_CondSignal(sem->count_nonzero);
SDL_Delay(10);
}
SDL_DestroyCond(sem->count_nonzero);
if ( sem->count_lock ) {
if (sem->count_lock)
{
SDL_mutexP(sem->count_lock);
SDL_mutexV(sem->count_lock);
SDL_DestroyMutex(sem->count_lock);
@ -84,14 +87,16 @@ int SDL_SemTryWait(SDL_sem *sem)
{
int retval;
if ( ! sem ) {
if (!sem)
{
SDL_SetError("Passed a NULL semaphore");
return -1;
}
retval = SDL_MUTEX_TIMEDOUT;
SDL_LockMutex(sem->count_lock);
if ( sem->count > 0 ) {
if (sem->count > 0)
{
--sem->count;
retval = 0;
}
@ -104,22 +109,25 @@ int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
{
int retval;
if ( ! sem ) {
if (!sem)
{
SDL_SetError("Passed a NULL semaphore");
return -1;
}
/* A timeout of 0 is an easy case */
if ( timeout == 0 ) {
if (timeout == 0)
{
return SDL_SemTryWait(sem);
}
SDL_LockMutex(sem->count_lock);
++sem->waiters_count;
retval = 0;
while ( (sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT) ) {
retval = SDL_CondWaitTimeout(sem->count_nonzero,
sem->count_lock, timeout);
while ((sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT))
{
retval = SDL_CondWaitTimeout(sem->count_nonzero, sem->count_lock,
timeout);
}
--sem->waiters_count;
--sem->count;
@ -138,7 +146,8 @@ Uint32 SDL_SemValue(SDL_sem *sem)
Uint32 value;
value = 0;
if ( sem ) {
if (sem)
{
SDL_LockMutex(sem->count_lock);
value = sem->count;
SDL_UnlockMutex(sem->count_lock);
@ -148,13 +157,15 @@ Uint32 SDL_SemValue(SDL_sem *sem)
int SDL_SemPost(SDL_sem *sem)
{
if ( ! sem ) {
if (!sem)
{
SDL_SetError("Passed a NULL semaphore");
return -1;
}
SDL_LockMutex(sem->count_lock);
if ( sem->waiters_count > 0 ) {
if (sem->waiters_count > 0)
{
SDL_CondSignal(sem->count_nonzero);
}
++sem->count;
@ -162,5 +173,3 @@ int SDL_SemPost(SDL_sem *sem)
return 0;
}

View file

@ -43,14 +43,13 @@ struct SDL_Thread {
void *run_thread(void *data)
{
SDL_RunThread(data);
return((void *)0); /* Prevent compiler warning */
return ((void *) 0); /* Prevent compiler warning */
}
int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
{
//SDL_SetError("Threads are not supported on this platform");
LWP_CreateThread(&thread->handle, run_thread, args, 0, 0, 10);
return(0);
LWP_CreateThread(&thread->handle, run_thread, args, 0, 0, 10);
return (0);
}
void SDL_SYS_SetupThread(void)
@ -60,7 +59,7 @@ void SDL_SYS_SetupThread(void)
Uint32 SDL_ThreadID(void)
{
return (Uint32)LWP_GetSelf ();
return (Uint32) LWP_GetSelf();
}
void SDL_SYS_WaitThread(SDL_Thread *thread)
@ -74,4 +73,3 @@ void SDL_SYS_KillThread(SDL_Thread *thread)
{
return;
}