X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=KernelLand%2FKernel%2Fsemaphore.c;h=0b70df5e71069cb4144db1702c8e083915d32eca;hb=986a763a1f3ee23b2fe3e809e2b5f023d9df6898;hp=ce7d4ba05f4ef40f37583a47ec5c310658c39fe1;hpb=7ba570fe3cc5418f42decf5b72ac2295cce9e60f;p=tpg%2Facess2.git diff --git a/KernelLand/Kernel/semaphore.c b/KernelLand/Kernel/semaphore.c index ce7d4ba0..0b70df5e 100644 --- a/KernelLand/Kernel/semaphore.c +++ b/KernelLand/Kernel/semaphore.c @@ -28,7 +28,6 @@ void Semaphore_Init(tSemaphore *Sem, int Value, int MaxValue, const char *Module // int Semaphore_Wait(tSemaphore *Sem, int MaxToTake) { - tThread *us; int taken; if( MaxToTake < 0 ) { Log_Warning("Threads", "Semaphore_Wait: User bug - MaxToTake(%i) < 0, Sem=%p(%s)", @@ -50,49 +49,9 @@ int Semaphore_Wait(tSemaphore *Sem, int MaxToTake) } else { - SHORTLOCK( &glThreadListLock ); - - // - Remove from active list - us = Threads_RemActive(); - us->Next = NULL; - // - Mark as sleeping - us->Status = THREAD_STAT_SEMAPHORESLEEP; - us->WaitPointer = Sem; - us->RetStatus = MaxToTake; // Use RetStatus as a temp variable - - // - Add to waiting - if(Sem->LastWaiting) { - Sem->LastWaiting->Next = us; - Sem->LastWaiting = us; - } - else { - Sem->Waiting = us; - Sem->LastWaiting = us; - } - - #if DEBUG_TRACE_STATE || SEMAPHORE_DEBUG - Log("%p (%i %s) waiting on semaphore %p %s:%s", - us, us->TID, us->ThreadName, - Sem, Sem->ModName, Sem->Name); - #endif - - SHORTREL( &Sem->Protector ); // Release first to make sure it is released - SHORTREL( &glThreadListLock ); - // Sleep until woken (either by getting what we need, or a timer event) - while( us->Status == THREAD_STAT_SEMAPHORESLEEP ) - { - Threads_Yield(); - if(us->Status == THREAD_STAT_SEMAPHORESLEEP) - Log_Warning("Threads", "Semaphore %p %s:%s re-schedulued while asleep", - Sem, Sem->ModName, Sem->Name); - } - #if DEBUG_TRACE_STATE || SEMAPHORE_DEBUG - Log("Semaphore %p %s:%s woken", Sem, Sem->ModName, Sem->Name); - #endif - // We're only woken when there's something avaliable (or a signal arrives) - us->WaitPointer = NULL; - - taken = us->RetStatus; + taken = Threads_int_Sleep(THREAD_STAT_SEMAPHORESLEEP, + Sem, MaxToTake, + &Sem->Waiting, &Sem->LastWaiting, &Sem->Protector); // Get the lock again SHORTLOCK( &Sem->Protector ); @@ -158,44 +117,13 @@ int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd) // Check if we have to block if( Sem->MaxValue && Sem->Value == Sem->MaxValue ) { - tThread *us; #if 0 Log_Debug("Threads", "Semaphore_Signal: IDLE Sem = %s:%s", Sem->ModName, Sem->Name); Log_Debug("Threads", "Semaphore_Signal: Sem->Value(%i) == Sem->MaxValue(%i)", Sem->Value, Sem->MaxValue); #endif - - SHORTLOCK( &glThreadListLock ); - // - Remove from active list - us = Threads_RemActive(); - us->Next = NULL; - // - Mark as sleeping - us->Status = THREAD_STAT_SEMAPHORESLEEP; - us->WaitPointer = Sem; - us->RetStatus = AmmountToAdd; // Use RetStatus as a temp variable - - // - Add to waiting - if(Sem->LastSignaling) { - Sem->LastSignaling->Next = us; - Sem->LastSignaling = us; - } - else { - Sem->Signaling = us; - Sem->LastSignaling = us; - } - - #if DEBUG_TRACE_STATE || SEMAPHORE_DEBUG - Log("%p (%i %s) signaling semaphore %p %s:%s", - us, us->TID, us->ThreadName, - Sem, Sem->ModName, Sem->Name); - #endif - - SHORTREL( &glThreadListLock ); - SHORTREL( &Sem->Protector ); - while(us->Status == THREAD_STAT_SEMAPHORESLEEP) Threads_Yield(); - // We're only woken when there's something avaliable - us->WaitPointer = NULL; - - added = us->RetStatus; + added = Threads_int_Sleep(THREAD_STAT_SEMAPHORESLEEP, + Sem, AmmountToAdd, + &Sem->Signaling, &Sem->LastSignaling, &Sem->Protector); // Get the lock again SHORTLOCK( &Sem->Protector );