From: John Hodge Date: Sun, 22 Jan 2012 13:49:53 +0000 (+0800) Subject: Kernel/threads - Working on centralised event handling X-Git-Tag: rel0.15~792^2~22 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=a42596b1f729fb8b4c2bbf7a44bb6f8af511f5ae;p=tpg%2Facess2.git Kernel/threads - Working on centralised event handling --- diff --git a/Kernel/events.c b/Kernel/events.c new file mode 100644 index 00000000..57ad6842 --- /dev/null +++ b/Kernel/events.c @@ -0,0 +1,73 @@ +/* + * Acess2 Kernel + * - By John Hodge (thePowersGang) + * + * events.c + * - Thread level event handling + */ +#include +#include +#include + +// === CODE === +void Threads_PostEvent(tThread *Thread, int EventID) +{ + if( EventID < 0 || EventID > N_EVENTS ) + { + return ; + } + + SHORTLOCK( Thread->IsLocked ); + + Thread->EventState |= 1 << EventID; + + // Currently sleeping on an event? + if( Thread->State == THREAD_STAT_EVENTSLEEP ) + { + // Waiting on this event? + if( (Uint32)Thread->RetStatus & (1 << EventID) ) + { + // Wake up + Threads_AddActive(Thread); + } + } + + SHORTREL( Thread->IsLocked ); +} + +Uint32 Threads_WaitEvents(Uint32 EventMask) +{ + Uint32 rv; + tThread *us = Proc_GetCurThread(); + + // Early return check + if( EventMask == 0 ) + { + return 0; + } + + // Check if a wait is needed + SHORTLOCK( &us->IsLocked ); + if( !(us->EventState & EventMask) ) + { + // Wait + us->RetStatus = EventMask; // HACK: Store EventMask in RetStatus + SHORTLOCK( &glThreadListLock ); + us = Threads_RemActive(); + us->Status = THREAD_STAT_EVENTSLEEP; + SHORTREL( &glThreadListLock ); + SHORTREL( &us->IsLocked ); + while(us->Status == THREAD_STAT_MUTEXSLEEP) Threads_Yield(); + // Woken when lock is acquired + SHORTLOCK( &us->IsLocked ); + } + + // Get return value and clear changed event bits + rv = us->EventState & EventMask; + us->EventState &= ~EventMask; + + SHORTREL( us->IsLocked ); + + return rv; +} + diff --git a/Kernel/include/threads_int.h b/Kernel/include/threads_int.h index 6516b35a..0bc941df 100644 --- a/Kernel/include/threads_int.h +++ b/Kernel/include/threads_int.h @@ -69,6 +69,9 @@ typedef struct sThread volatile int CurCPU; int bInstrTrace; + + // --- event.c + Uint32 EventState; } tThread; @@ -79,6 +82,7 @@ enum { THREAD_STAT_MUTEXSLEEP, // Mutex Sleep THREAD_STAT_SEMAPHORESLEEP, // Semaphore Sleep THREAD_STAT_QUEUESLEEP, // Queue + THREAD_STAT_EVENTSLEEP, // Event sleep THREAD_STAT_WAITING, // ??? (Waiting for a thread) THREAD_STAT_PREINIT, // Being created THREAD_STAT_ZOMBIE, // Died/Killed, but parent not informed @@ -92,6 +96,7 @@ static const char * const casTHREAD_STAT[] = { "THREAD_STAT_MUTEXSLEEP", "THREAD_STAT_SEMAPHORESLEEP", "THREAD_STAT_QUEUESLEEP", + "THREAD_STAT_EVENTSLEEP", "THREAD_STAT_WAITING", "THREAD_STAT_PREINIT", "THREAD_STAT_ZOMBIE",