3 * - By John Hodge (thePowersGang)
6 * - Thread level event handling
11 #include <threads_int.h>
14 void Threads_PostEvent(tThread *Thread, Uint32 EventMask)
17 if( !Thread ) return ;
18 if( EventMask == 0 ) return ;
19 // TODO: Check that only one bit is set?
21 ENTER("pThread xEventMask", Thread, EventMask);
23 SHORTLOCK( &Thread->IsLocked );
25 Thread->EventState |= EventMask;
26 LOG("Thread->EventState = 0x%x", Thread->EventState);
28 // Currently sleeping on an event?
29 switch(Thread->Status)
31 // Waiting on this event?
32 case THREAD_STAT_EVENTSLEEP:
33 if( (Uint32)Thread->RetStatus & EventMask )
36 LOG("Waking thread %p(%i %s)", Thread, Thread->TID, Thread->ThreadName);
37 Threads_AddActive(Thread);
40 case THREAD_STAT_SEMAPHORESLEEP:
41 if( EventMask & THREAD_EVENT_TIMER )
43 LOG("Waking %p(%i %s) from semaphore on timer",
44 Thread, Thread->TID, Thread->ThreadName);
45 Semaphore_ForceWake(Thread);
52 SHORTREL( &Thread->IsLocked );
57 * \brief Clear an event without waiting
59 void Threads_ClearEvent(Uint32 EventMask)
61 Proc_GetCurThread()->EventState &= ~EventMask;
65 * \brief Wait for an event to occur
67 Uint32 Threads_WaitEvents(Uint32 EventMask)
70 tThread *us = Proc_GetCurThread();
72 ENTER("xEventMask", EventMask);
81 LOG("us = %p(%i %s)", us, us->TID, us->ThreadName);
83 // Check if a wait is needed
84 SHORTLOCK( &us->IsLocked );
85 LOG("Locked and preparing for wait");
86 if( (us->EventState & EventMask) == 0 )
88 Threads_int_Sleep(THREAD_STAT_EVENTSLEEP, NULL, EventMask,
89 &us, NULL, &us->IsLocked);
90 // Woken when lock is acquired
91 SHORTLOCK( &us->IsLocked );
94 // Get return value and clear changed event bits
95 rv = us->EventState & EventMask;
96 us->EventState &= ~EventMask;
98 SHORTREL( &us->IsLocked );