- #if 1
- number = 0;
- for(thread = gActiveThreads; thread; thread = thread->Next) {
- if(thread->CurCPU >= 0) continue;
- number += thread->NumTickets;
- }
- if(number != giFreeTickets) {
- Panic("Bookkeeping fail (giFreeTickets(%i) != number(%i)) - CPU%i",
- giFreeTickets, number, CPU);
- }
- #endif
-
- // No free tickets (all tasks delegated to cores)
- if( giFreeTickets == 0 ) {
- SHORTREL(&glThreadListLock);
- return NULL;
+ // ---
+ // Lottery Scheduler
+ // ---
+ #if SCHEDULER_TYPE == SCHED_LOTTERY
+ {
+ int ticket, number;
+ # if 1
+ number = 0;
+ for(thread = gActiveThreads; thread; thread = thread->Next) {
+ if(thread->CurCPU >= 0) continue;
+ if(thread->Status != THREAD_STAT_ACTIVE)
+ Panic("Bookkeeping fail - %p %i(%s) is on the active queue with a status of %i",
+ thread, thread->TID, thread->ThreadName, thread->Status);
+ if(thread->Next == thread) {
+ Panic("Bookkeeping fail - %p %i(%s) loops back on itself",
+ thread, thread->TID, thread->ThreadName, thread->Status);
+ }
+ number += caiTICKET_COUNTS[ thread->Priority ];
+ }
+ if(number != giFreeTickets) {
+ Panic("Bookkeeping fail (giFreeTickets(%i) != number(%i)) - CPU%i",
+ giFreeTickets, number, CPU);
+ }
+ # endif
+
+ // No free tickets (all tasks delegated to cores)
+ if( giFreeTickets == 0 ) {
+ SHORTREL(&glThreadListLock);
+ return NULL;
+ }
+
+ // Get the ticket number
+ ticket = number = rand() % giFreeTickets;
+
+ // Find the next thread
+ for(thread=gActiveThreads;thread;thread=thread->Next)
+ {
+ 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);
+ }
+ # if DEBUG_TRACE_TICKETS
+ LogF(" CPU%i giFreeTickets = %i, running %p (%i %s CPU=%i)\n",
+ CPU, giFreeTickets, thread, thread->TID, thread->ThreadName, thread->CurCPU);
+ # endif
+
+ giFreeTickets -= caiTICKET_COUNTS[ thread->Priority ];