4c872bc0c5c25118bb0de50fa20f256f6858a9a8
[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         // Sanity checking
16         if( !Thread )   return ;
17         if( EventMask == 0 )    return ;
18         // TODO: Check that only one bit is set?
19         
20         SHORTLOCK( &Thread->IsLocked );
21
22         Thread->EventState |= EventMask;
23         
24         // Currently sleeping on an event?
25         if( Thread->Status == THREAD_STAT_EVENTSLEEP )
26         {
27                 // Waiting on this event?
28                 if( (Uint32)Thread->RetStatus & EventMask )
29                 {
30                         // Wake up
31                         // TODO: Does it matter if the thread is locked here?
32                         Threads_AddActive(Thread);
33                 }
34         }
35         
36         SHORTREL( &Thread->IsLocked );
37 }
38
39 /**
40  * \brief Wait for an event to occur
41  */
42 Uint32 Threads_WaitEvents(Uint32 EventMask)
43 {
44         Uint32  rv;
45         tThread *us = Proc_GetCurThread();
46
47         // Early return check
48         if( EventMask == 0 )
49         {
50                 return 0;
51         }
52         
53         // Check if a wait is needed
54         SHORTLOCK( &us->IsLocked );
55         while( !(us->EventState & EventMask) )
56         {
57                 // Wait
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 );
67         }
68         
69         // Get return value and clear changed event bits
70         rv = us->EventState & EventMask;
71         us->EventState &= ~EventMask;
72         
73         SHORTREL( &us->IsLocked );
74         
75         return rv;
76 }
77

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