57ad6842a3aa9c4d11fad818a10ebf4e9c21f20e
[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, int EventID)
14 {
15         if( EventID < 0 || EventID > N_EVENTS )
16         {
17                 return ;
18         }
19         
20         SHORTLOCK( Thread->IsLocked );
21
22         Thread->EventState |= 1 << EventID;
23         
24         // Currently sleeping on an event?
25         if( Thread->State == THREAD_STAT_EVENTSLEEP )
26         {
27                 // Waiting on this event?
28                 if( (Uint32)Thread->RetStatus & (1 << EventID) )
29                 {
30                         // Wake up
31                         Threads_AddActive(Thread);
32                 }
33         }
34         
35         SHORTREL( Thread->IsLocked );
36 }
37
38 Uint32 Threads_WaitEvents(Uint32 EventMask)
39 {
40         Uint32  rv;
41         tThread *us = Proc_GetCurThread();
42
43         // Early return check
44         if( EventMask == 0 )
45         {
46                 return 0;
47         }
48         
49         // Check if a wait is needed
50         SHORTLOCK( &us->IsLocked );
51         if( !(us->EventState & EventMask) )
52         {
53                 // Wait
54                 us->RetStatus = EventMask;      // HACK: Store EventMask in RetStatus
55                 SHORTLOCK( &glThreadListLock );
56                 us = Threads_RemActive();
57                 us->Status = THREAD_STAT_EVENTSLEEP;
58                 SHORTREL( &glThreadListLock );
59                 SHORTREL( &us->IsLocked );
60                 while(us->Status == THREAD_STAT_MUTEXSLEEP)     Threads_Yield();
61                 // Woken when lock is acquired
62                 SHORTLOCK( &us->IsLocked );
63         }
64         
65         // Get return value and clear changed event bits
66         rv = us->EventState & EventMask;
67         us->EventState &= ~EventMask;
68         
69         SHORTREL( us->IsLocked );
70         
71         return rv;
72 }
73

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