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

View file

@ -28,20 +28,24 @@
#include <ogcsys.h> #include <ogcsys.h>
struct SDL_mutex { struct SDL_mutex
{
u32 id; u32 id;
}; };
/* Create a mutex */ /* Create a mutex */
SDL_mutex *SDL_CreateMutex(void) SDL_mutex *SDL_CreateMutex(void)
{ {
SDL_mutex *mutex; SDL_mutex *mutex = NULL;
/* Allocate mutex memory */ /* Allocate mutex memory */
mutex = (SDL_mutex *)SDL_malloc(sizeof(*mutex)); mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
if ( mutex ) { if (mutex)
LWP_MutexInit (&mutex->id, 0); {
} else { LWP_MutexInit(&mutex->id, 0);
}
else
{
SDL_OutOfMemory(); SDL_OutOfMemory();
} }
return mutex; return mutex;
@ -50,7 +54,8 @@ SDL_mutex *SDL_CreateMutex(void)
/* Free the mutex */ /* Free the mutex */
void SDL_DestroyMutex(SDL_mutex *mutex) void SDL_DestroyMutex(SDL_mutex *mutex)
{ {
if ( mutex ) { if (mutex)
{
LWP_MutexDestroy(mutex->id); LWP_MutexDestroy(mutex->id);
SDL_free(mutex); SDL_free(mutex);
} }
@ -59,29 +64,24 @@ void SDL_DestroyMutex(SDL_mutex *mutex)
/* Lock the semaphore */ /* Lock the semaphore */
int SDL_mutexP(SDL_mutex *mutex) int SDL_mutexP(SDL_mutex *mutex)
{ {
if ( mutex == NULL ) { if (mutex == NULL)
{
SDL_SetError("Passed a NULL mutex"); SDL_SetError("Passed a NULL mutex");
return -1; return -1;
} }
return LWP_MutexLock(mutex->id); return LWP_MutexLock(mutex->id);
} }
/* Unlock the mutex */ /* Unlock the mutex */
int SDL_mutexV(SDL_mutex *mutex) int SDL_mutexV(SDL_mutex *mutex)
{ {
if ( mutex == NULL ) { if (mutex == NULL)
{
SDL_SetError("Passed a NULL mutex"); SDL_SetError("Passed a NULL mutex");
return -1; 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); return LWP_MutexUnlock(mutex->id);
} }

View file

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

View file

@ -43,14 +43,13 @@ struct SDL_Thread {
void *run_thread(void *data) void *run_thread(void *data)
{ {
SDL_RunThread(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) 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); LWP_CreateThread(&thread->handle, run_thread, args, 0, 0, 10);
return(0); return (0);
} }
void SDL_SYS_SetupThread(void) void SDL_SYS_SetupThread(void)
@ -60,7 +59,7 @@ void SDL_SYS_SetupThread(void)
Uint32 SDL_ThreadID(void) Uint32 SDL_ThreadID(void)
{ {
return (Uint32)LWP_GetSelf (); return (Uint32) LWP_GetSelf();
} }
void SDL_SYS_WaitThread(SDL_Thread *thread) void SDL_SYS_WaitThread(SDL_Thread *thread)
@ -74,4 +73,3 @@ void SDL_SYS_KillThread(SDL_Thread *thread)
{ {
return; return;
} }