3 * - By John Hodge (thePowersGang)
6 * - Thread level event handling
10 #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);
50 SHORTREL( &Thread->IsLocked );
55 * \brief Clear an event without waiting
57 void Threads_ClearEvent(Uint32 EventMask)
59 Proc_GetCurThread()->EventState &= ~EventMask;
63 * \brief Wait for an event to occur
65 Uint32 Threads_WaitEvents(Uint32 EventMask)
68 tThread *us = Proc_GetCurThread();
70 ENTER("xEventMask", EventMask);
79 LOG("us = %p(%i %s)", us, us->TID, us->ThreadName);
81 // Check if a wait is needed
82 SHORTLOCK( &us->IsLocked );
83 while( !(us->EventState & EventMask) )
85 LOG("Locked and preparing for wait");
87 us->RetStatus = EventMask; // HACK: Store EventMask in RetStatus
88 SHORTLOCK( &glThreadListLock );
89 us = Threads_RemActive();
90 us->Status = THREAD_STAT_EVENTSLEEP;
91 // Note stored anywhere because we're woken using other means
92 SHORTREL( &glThreadListLock );
93 SHORTREL( &us->IsLocked );
94 Threads_int_WaitForStatusEnd(THREAD_STAT_EVENTSLEEP);
95 // Woken when lock is acquired
96 SHORTLOCK( &us->IsLocked );
99 // Get return value and clear changed event bits
100 rv = us->EventState & EventMask;
101 us->EventState &= ~EventMask;
103 SHORTREL( &us->IsLocked );