X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=sidebyside;f=Kernel%2Fevents.c;h=a9eab7865e86244ded7a8cf9dd08bbcf529e7678;hb=61cfad415a64c52ca253460231046f47fcb7fb15;hp=57ad6842a3aa9c4d11fad818a10ebf4e9c21f20e;hpb=a42596b1f729fb8b4c2bbf7a44bb6f8af511f5ae;p=tpg%2Facess2.git diff --git a/Kernel/events.c b/Kernel/events.c index 57ad6842..a9eab786 100644 --- a/Kernel/events.c +++ b/Kernel/events.c @@ -5,59 +5,75 @@ * events.c * - Thread level event handling */ +#define DEBUG 0 #include #include #include // === CODE === -void Threads_PostEvent(tThread *Thread, int EventID) +void Threads_PostEvent(tThread *Thread, Uint32 EventMask) { - if( EventID < 0 || EventID > N_EVENTS ) - { - return ; - } + // Sanity checking + if( !Thread ) return ; + if( EventMask == 0 ) return ; + // TODO: Check that only one bit is set? - SHORTLOCK( Thread->IsLocked ); + ENTER("pThread xEventMask", Thread, EventMask); + + SHORTLOCK( &Thread->IsLocked ); - Thread->EventState |= 1 << EventID; + Thread->EventState |= EventMask; + LOG("Thread->EventState = 0x%x", Thread->EventState); // Currently sleeping on an event? - if( Thread->State == THREAD_STAT_EVENTSLEEP ) + if( Thread->Status == THREAD_STAT_EVENTSLEEP ) { // Waiting on this event? - if( (Uint32)Thread->RetStatus & (1 << EventID) ) + if( (Uint32)Thread->RetStatus & EventMask ) { // Wake up + LOG("Waking thread %p(%i %s)", Thread, Thread->TID, Thread->ThreadName); Threads_AddActive(Thread); } } - SHORTREL( Thread->IsLocked ); + SHORTREL( &Thread->IsLocked ); + LEAVE('-'); } +/** + * \brief Wait for an event to occur + */ Uint32 Threads_WaitEvents(Uint32 EventMask) { Uint32 rv; tThread *us = Proc_GetCurThread(); + ENTER("xEventMask", EventMask); + // Early return check if( EventMask == 0 ) { + LEAVE('i', 0); return 0; } + LOG("us = %p(%i %s)", us, us->TID, us->ThreadName); + // Check if a wait is needed SHORTLOCK( &us->IsLocked ); - if( !(us->EventState & EventMask) ) + while( !(us->EventState & EventMask) ) { + LOG("Locked and preparing for wait"); // Wait us->RetStatus = EventMask; // HACK: Store EventMask in RetStatus SHORTLOCK( &glThreadListLock ); us = Threads_RemActive(); us->Status = THREAD_STAT_EVENTSLEEP; + // Note stored anywhere because we're woken using other means SHORTREL( &glThreadListLock ); SHORTREL( &us->IsLocked ); - while(us->Status == THREAD_STAT_MUTEXSLEEP) Threads_Yield(); + while(us->Status == THREAD_STAT_EVENTSLEEP) Threads_Yield(); // Woken when lock is acquired SHORTLOCK( &us->IsLocked ); } @@ -66,8 +82,9 @@ Uint32 Threads_WaitEvents(Uint32 EventMask) rv = us->EventState & EventMask; us->EventState &= ~EventMask; - SHORTREL( us->IsLocked ); + SHORTREL( &us->IsLocked ); + LEAVE('x', rv); return rv; }