+ case THREAD_STAT_SEMAPHORESLEEP: {
+ tSemaphore *sem;
+ tThread *th, *prev=NULL;
+
+ sem = Thread->WaitPointer;
+
+ SHORTLOCK( &sem->Protector );
+
+ // Remove from sleeping queue
+ for( th = sem->Waiting; th; prev = th, th = th->Next )
+ if( th == Thread ) break;
+ if( th )
+ {
+ if(prev)
+ prev->Next = Thread->Next;
+ else
+ sem->Waiting = Thread->Next;
+ if(sem->LastWaiting == Thread)
+ sem->LastWaiting = prev;
+ }
+ else
+ {
+ prev = NULL;
+ for( th = sem->Signaling; th; prev = th, th = th->Next )
+ if( th == Thread ) break;
+ if( !th ) {
+ Log_Warning("Threads", "Thread %p(%i %s) is not on semaphore %p(%s:%s)",
+ Thread, Thread->TID, Thread->ThreadName,
+ sem, sem->ModName, sem->Name);
+ return -EINTERNAL;
+ }
+
+ if(prev)
+ prev->Next = Thread->Next;
+ else
+ sem->Signaling = Thread->Next;
+ if(sem->LastSignaling == Thread)
+ sem->LastSignaling = prev;
+ }
+
+ SHORTLOCK( &glThreadListLock );
+ Threads_AddActive( Thread );
+ SHORTREL( &glThreadListLock );
+
+ #if DEBUG_TRACE_STATE
+ Log("Threads_Sleep: %p(%i %s) woken from semaphore", Thread, Thread->TID, Thread->ThreadName);
+ #endif
+ SHORTREL( &sem->Protector );
+ } return -EOK;
+