+tThread *Threads_int_GetRunnable(void)
+{
+ #if SCHEDULER_TYPE == SCHED_LOTTERY
+ // -----------------------------------
+ // Lottery Scheduler
+ // -----------------------------------
+ #if DEBUG_VALIDATE_TICKET_COUNTS
+ {
+ int total_tickets = 0;
+ for(const tThread *thread = gActiveThreads.Head; thread; thread = thread->Next)
+ {
+ 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);
+ }
+ total_tickets += caiTICKET_COUNTS[ thread->Priority ];
+ }
+ if(total_tickets != giFreeTickets)
+ {
+ Panic("Bookkeeping fail (giFreeTickets(%i) != number(%i)) - CPU%i",
+ giFreeTickets, total_tickets, CPU);
+ }
+ }
+ # endif
+
+ // No free tickets (all tasks delegated to cores)
+ if( giFreeTickets == 0 )
+ {
+ return NULL;
+ }
+
+ // Get the ticket number
+ int ticket = rand() % giFreeTickets;
+ int number = ticket;
+
+ // Find the next thread
+ tThread **pnp = &gActiveThreads.Head;
+ tThread *thread;
+ for(thread = gActiveThreads.Head; thread; pnp = &thread->Next, thread = thread->Next )
+ {
+ 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)
+ {
+ int total_tickets = 0;
+ for(thread=gActiveThreads;thread;thread=thread->Next) {
+ if(thread->CurCPU >= 0) continue;
+ total_tickets += caiTICKET_COUNTS[ thread->Priority ];
+ }
+ Panic("Bookeeping Failed - giFreeTickets(%i) > true count (%i)",
+ giFreeTickets, total_tickets);
+ }
+
+ // Remove
+ *pnp = thread->Next;
+ if( !thread->Next )
+ gActiveThreads.Tail = prev;
+
+ 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
+ return thread;
+
+ #elif SCHEDULER_TYPE == SCHED_RR_PRI
+
+ // -----------------------------------
+ // Priority based round robin scheduler
+ // -----------------------------------
+ for( int i = 0; i < MIN_PRIORITY + 1; i ++ )
+ {
+ if( gaActiveThreads[i].Head == NULL )
+ continue ;
+
+ tThread *thread = gaActiveThreads[i].Head;
+
+ if( thread->Status != THREAD_STAT_ACTIVE ) {
+ for( const tThread *t = gaActiveThreads[i].Head; t; t = t->Next )
+ LogF("- %p(%i %s)\n", t, t->TID, t->ThreadName);
+ Panic("Thread %p(%i %s) from pqueue %i is active!",
+ thread, thread->TID, thread->ThreadName, i);
+ }
+
+ // Remove from head
+ gaActiveThreads[i].Head = thread->Next;
+ if(!thread->Next)
+ gaActiveThreads[i].Tail = NULL;
+ thread->Next = NULL;
+ return thread;
+ }
+ return NULL;
+
+ #elif SCHEDULER_TYPE == SCHED_RR_SIM
+
+ // -----------------------------------
+ // Single-list round-robin
+ // -----------------------------------
+ tThread *thread = gActiveThreads.Head;
+ LOG("thread = %p", thread);
+ if( thread )
+ {
+ gActiveThreads.Head = thread->Next;
+ if(!thread->Next)
+ gaActiveThreads.Tail = NULL;
+ thread->Next = NULL;
+ }
+ return thread;
+ #else
+ # error "Unimplemented scheduling algorithm"
+ return NULL;
+ #endif
+}
+