+ {
+ if(thread->CurCPU >= 0) continue;
+ if( caiTICKET_COUNTS[ thread->Priority ] > number) break;
+ number -= caiTICKET_COUNTS[ thread->Priority ];
+ }
+
+ // If we didn't find a thread, something went wrong
+ if(thread == NULL)
+ {
+ number = 0;
+ for(thread=gActiveThreads;thread;thread=thread->Next) {
+ if(thread->CurCPU >= 0) continue;
+ number += caiTICKET_COUNTS[ thread->Priority ];
+ }
+ Panic("Bookeeping Failed - giFreeTickets(%i) > true count (%i)",
+ giFreeTickets, number);
+ }
+
+ giFreeTickets -= caiTICKET_COUNTS[ thread->Priority ];
+ # if DEBUG_TRACE_TICKETS
+ LogF("Log: CPU%i allocated %p (%i %s), (%i [-%i] tickets in pool), \n",
+ CPU, thread, thread->TID, thread->ThreadName,
+ giFreeTickets, caiTICKET_COUNTS[ thread->Priority ]);
+ # endif
+ }
+
+ // ---
+ // Priority based round robin scheduler
+ // ---
+ #elif SCHEDULER_TYPE == SCHED_RR_PRI
+ {
+ int i;
+ for( i = 0; i < MIN_PRIORITY + 1; i ++ )
+ {
+ for(thread = gaActiveThreads[i]; thread; thread = thread->Next)
+ {
+ if( thread->CurCPU == -1 ) break;
+ }
+ // If we fall onto the same queue again, special handling is
+ // needed
+ if( Last && Last->Status == THREAD_STAT_ACTIVE && i == Last->Priority ) {
+ tThread *savedThread = thread;
+
+ // Find the next unscheduled thread in the list
+ for( thread = Last->Next; thread; thread = thread->Next )
+ {
+ if( thread->CurCPU == -1 ) break;
+ }
+ // If we don't find anything after, just use the one
+ // found above.
+ if( !thread ) thread = savedThread;
+ }
+ // Found a thread? Schedule it!
+ if( thread ) break;
+ }
+
+ // Anything to do?
+ if( !thread ) {
+ SHORTREL(&glThreadListLock);
+ return NULL;
+ }
+ if( thread->Status != THREAD_STAT_ACTIVE ) {
+ LogF("Oops, Thread %i (%s) is not active\n", thread->TID, thread->ThreadName);
+ }
+ }
+ #elif SCHEDULER_TYPE == SCHED_RR_SIM
+ {
+ // Find the next unscheduled thread in the list
+ for( thread = Last->Next; thread; thread = thread->Next )
+ {
+ if( thread->CurCPU == -1 ) break;
+ }
+ // If we don't find anything after, search from the beginning
+ if( !thread )
+ {
+ for(thread = gActiveThreads; thread; thread = thread->Next)
+ {
+ if( thread->CurCPU == -1 ) break;
+ }
+ }
+
+ // Anything to do?
+ if( !thread ) {
+ SHORTREL(&glThreadListLock);
+ return NULL;
+ }