3 * - By John Hodge (thePowersGang)
6 * - Thread level event handling
9 #include <threads_int.h>
13 void Threads_PostEvent(tThread *Thread, Uint32 EventMask)
16 if( !Thread ) return ;
17 if( EventMask == 0 ) return ;
18 // TODO: Check that only one bit is set?
20 SHORTLOCK( &Thread->IsLocked );
22 Thread->EventState |= EventMask;
24 // Currently sleeping on an event?
25 if( Thread->Status == THREAD_STAT_EVENTSLEEP )
27 // Waiting on this event?
28 if( (Uint32)Thread->RetStatus & EventMask )
31 // TODO: Does it matter if the thread is locked here?
32 Threads_AddActive(Thread);
36 SHORTREL( &Thread->IsLocked );
40 * \brief Wait for an event to occur
42 Uint32 Threads_WaitEvents(Uint32 EventMask)
45 tThread *us = Proc_GetCurThread();
53 // Check if a wait is needed
54 SHORTLOCK( &us->IsLocked );
55 while( !(us->EventState & EventMask) )
58 us->RetStatus = EventMask; // HACK: Store EventMask in RetStatus
59 SHORTLOCK( &glThreadListLock );
60 us = Threads_RemActive();
61 us->Status = THREAD_STAT_EVENTSLEEP;
62 SHORTREL( &glThreadListLock );
63 SHORTREL( &us->IsLocked );
64 while(us->Status == THREAD_STAT_MUTEXSLEEP) Threads_Yield();
65 // Woken when lock is acquired
66 SHORTLOCK( &us->IsLocked );
69 // Get return value and clear changed event bits
70 rv = us->EventState & EventMask;
71 us->EventState &= ~EventMask;
73 SHORTREL( &us->IsLocked );