Kernel/x86 - Clean up some of the task switching code (possibly a little broken)
[tpg/acess2.git] / KernelLand / Kernel / events.c
index da095e9..a572d43 100644 (file)
@@ -7,8 +7,8 @@
  */
 #define DEBUG  0
 #include <acess.h>
-#include <threads_int.h>
 #include <events.h>
+#include <threads_int.h>
 
 // === CODE ===
 void Threads_PostEvent(tThread *Thread, Uint32 EventMask)
@@ -26,15 +26,27 @@ void Threads_PostEvent(tThread *Thread, Uint32 EventMask)
        LOG("Thread->EventState = 0x%x", Thread->EventState);
        
        // Currently sleeping on an event?
-       if( Thread->Status == THREAD_STAT_EVENTSLEEP )
+       switch(Thread->Status)
        {
-               // Waiting on this event?
+       // Waiting on this event?
+       case THREAD_STAT_EVENTSLEEP:
                if( (Uint32)Thread->RetStatus & EventMask )
                {
                        // Wake up
                        LOG("Waking thread %p(%i %s)", Thread, Thread->TID, Thread->ThreadName);
                        Threads_AddActive(Thread);
                }
+               break;
+       case THREAD_STAT_SEMAPHORESLEEP:
+               if( EventMask & THREAD_EVENT_TIMER )
+               {
+                       LOG("Waking %p(%i %s) from semaphore on timer",
+                               Thread, Thread->TID, Thread->ThreadName);
+                       Semaphore_ForceWake(Thread);
+               }
+               break;
+       default:
+               break;
        }
        
        SHORTREL( &Thread->IsLocked );
@@ -70,19 +82,12 @@ Uint32 Threads_WaitEvents(Uint32 EventMask)
 
        // Check if a wait is needed
        SHORTLOCK( &us->IsLocked );
-       while( !(us->EventState & EventMask) )
+       LOG("Locked and preparing for wait");
+       if( (us->EventState & EventMask) == 0 )
        {
-               LOG("Locked and preparing for wait");
-               // Wait
-               us->RetStatus = EventMask;      // HACK: Store EventMask in RetStatus
-               SHORTLOCK( &glThreadListLock );
-               us = Threads_RemActive();
-               us->Status = THREAD_STAT_EVENTSLEEP;
-               // Note stored anywhere because we're woken using other means
-               SHORTREL( &glThreadListLock );
-               SHORTREL( &us->IsLocked );
-               while(us->Status == THREAD_STAT_EVENTSLEEP)     Threads_Yield();
-               // Woken when lock is acquired
+               Threads_int_Sleep(THREAD_STAT_EVENTSLEEP, NULL, EventMask,
+                       &us, NULL, &us->IsLocked);
+               // Woken when an event fires
                SHORTLOCK( &us->IsLocked );
        }
        

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