Kernel - Added events to build
[tpg/acess2.git] / Kernel / events.c
1 /*
2  * Acess2 Kernel
3  * - By John Hodge (thePowersGang)
4  *
5  * events.c
6  * - Thread level event handling
7  */
8 #include <acess.h>
9 #include <threads_int.h>
10 #include <events.h>
11
12 // === CODE ===
13 void Threads_PostEvent(tThread *Thread, Uint32 EventMask)
14 {
15         // TODO: Check that only one bit is set?
16         if( EventMask == 0 )    return ;
17         
18         SHORTLOCK( &Thread->IsLocked );
19
20         Thread->EventState |= EventMask;
21         
22         // Currently sleeping on an event?
23         if( Thread->Status == THREAD_STAT_EVENTSLEEP )
24         {
25                 // Waiting on this event?
26                 if( (Uint32)Thread->RetStatus & EventMask )
27                 {
28                         // Wake up
29                         // TODO: Does it matter if the thread is locked here?
30                         Threads_AddActive(Thread);
31                 }
32         }
33         
34         SHORTREL( &Thread->IsLocked );
35 }
36
37 Uint32 Threads_WaitEvents(Uint32 EventMask)
38 {
39         Uint32  rv;
40         tThread *us = Proc_GetCurThread();
41
42         // Early return check
43         if( EventMask == 0 )
44         {
45                 return 0;
46         }
47         
48         // Check if a wait is needed
49         SHORTLOCK( &us->IsLocked );
50         if( !(us->EventState & EventMask) )
51         {
52                 // Wait
53                 us->RetStatus = EventMask;      // HACK: Store EventMask in RetStatus
54                 SHORTLOCK( &glThreadListLock );
55                 us = Threads_RemActive();
56                 us->Status = THREAD_STAT_EVENTSLEEP;
57                 SHORTREL( &glThreadListLock );
58                 SHORTREL( &us->IsLocked );
59                 while(us->Status == THREAD_STAT_MUTEXSLEEP)     Threads_Yield();
60                 // Woken when lock is acquired
61                 SHORTLOCK( &us->IsLocked );
62         }
63         
64         // Get return value and clear changed event bits
65         rv = us->EventState & EventMask;
66         us->EventState &= ~EventMask;
67         
68         SHORTREL( &us->IsLocked );
69         
70         return rv;
71 }
72

UCC git Repository :: git.ucc.asn.au