+ // TODO: Enable the code to tell if the current CPU has the lock or
+ // another does.
+
+ // Check if the thread list is locked by other code
+ // - If so, don't switch (give it a chance to complete)
+ if( IS_LOCKED(&glThreadListLock) )
+ return Last;
+
+ // Clear Delete Queue
+ while(gDeleteThreads)
+ {
+ thread = gDeleteThreads->Next;
+ if(gDeleteThreads->IsLocked) { // Only free if structure is unused
+ gDeleteThreads->Status = THREAD_STAT_NULL;
+ free( gDeleteThreads );
+ }
+ gDeleteThreads = thread;
+ }
+
+ // No active threads, just take a nap
+ if(giNumActiveThreads == 0) {
+ #if DEBUG_TRACE_TICKETS
+ Log("No active threads");
+ #endif
+ return NULL;
+ }
+
+ // Lock thread list
+ // - HLT lock (Used because only another CPU can obtain the lock,
+ // but it has a potentially long lock period)
+ // - Well, this CPU can obtain the lock, but that is aliveviated by
+ // the above.
+ TIGHTLOCK( &glThreadListLock );