+
+/**
+ * \brief Gets the next thread to run
+ * \param CPU Current CPU
+ * \param Last The thread the CPU was running
+ */
+tThread *Threads_GetNextToRun(int CPU, tThread *Last)
+{
+ tThread *thread;
+
+ // If this CPU has the lock, we must let it complete
+ if( CPU_HAS_LOCK( &glThreadListLock ) )
+ return Last;
+
+ // Don't change threads if the current CPU has switches disabled
+ if( gaThreads_NoTaskSwitch[CPU] )
+ return Last;
+
+
+ // Lock thread list
+ SHORTLOCK( &glThreadListLock );
+
+ // Clear Delete Queue
+ // - I should probably put this in a worker thread to avoid calling free() in the scheduler
+ // DEFINITELY - free() can deadlock in this case
+ while(gDeleteThreads)
+ {
+ thread = gDeleteThreads->Next;
+ // Only free if structure is unused
+ if( !IS_LOCKED(&gDeleteThreads->IsLocked) )
+ {
+ // Set to dead
+ gDeleteThreads->Status = THREAD_STAT_BURIED;
+ // Free name
+ if( IsHeap(gDeleteThreads->ThreadName) )
+ free(gDeleteThreads->ThreadName);
+ // Remove from global list
+ if( gDeleteThreads == gAllThreads )
+ gAllThreads = gDeleteThreads->GlobalNext;
+ else
+ gDeleteThreads->GlobalPrev->GlobalNext = gDeleteThreads->GlobalNext;
+ free( gDeleteThreads );
+ }
+ gDeleteThreads = thread;
+ }
+
+ // No active threads, just take a nap
+ if(giNumActiveThreads == 0) {
+ SHORTREL( &glThreadListLock );
+ #if DEBUG_TRACE_TICKETS
+ Log("No active threads");
+ #endif
+ return NULL;
+ }
+
+ #if SCHEDULER_TYPE != SCHED_RR_PRI
+ // Special case: 1 thread
+ if(giNumActiveThreads == 1) {
+ if( gActiveThreads->CurCPU == -1 )
+ gActiveThreads->CurCPU = CPU;
+
+ SHORTREL( &glThreadListLock );
+
+ if( gActiveThreads->CurCPU == CPU )
+ return gActiveThreads;
+
+ return NULL; // CPU has nothing to do
+ }
+ #endif
+
+ // Allow the old thread to be scheduled again
+ if( Last ) {
+ if( Last->Status == THREAD_STAT_ACTIVE ) {
+ #if SCHEDULER_TYPE == SCHED_LOTTERY
+ giFreeTickets += caiTICKET_COUNTS[ Last->Priority ];
+ # if DEBUG_TRACE_TICKETS
+ LogF("Log: CPU%i released %p (%i %s) into the pool (%i [+%i] tickets in pool)\n",
+ CPU, Last, Last->TID, Last->ThreadName, giFreeTickets,
+ caiTICKET_COUNTS[ Last->Priority ]);
+ # endif
+ #endif
+ }
+ #if SCHEDULER_TYPE == SCHED_LOTTERY && DEBUG_TRACE_TICKETS
+ else
+ LogF("Log: CPU%i released %p (%i %s)->Status = %i (Released)\n",
+ CPU, Last, Last->TID, Last->ThreadName, Last->Status);
+ #endif
+ Last->CurCPU = -1;
+ }
+
+ // ---
+ // 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);
+ }
+
+ 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( 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;
+ }
+ }
+ #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;
+ }
+ }
+ #else
+ # error "Unimplemented scheduling algorithm"
+ #endif
+
+ // Make the new thread non-schedulable
+ thread->CurCPU = CPU;
+
+ SHORTREL( &glThreadListLock );
+
+ return thread;
+}
+
+/**
+ * \brief Acquire a heavy mutex
+ * \param Mutex Mutex to acquire
+ *
+ * This type of mutex checks if the mutex is avaliable, and acquires it
+ * if it is. Otherwise, the current thread is added to the mutex's wait
+ * queue and the thread suspends. When the holder of the mutex completes,
+ * the oldest thread (top thread) on the queue is given the lock and
+ * restarted.
+ */
+int Mutex_Acquire(tMutex *Mutex)
+{
+ tThread *us = Proc_GetCurThread();
+
+ // Get protector
+ SHORTLOCK( &Mutex->Protector );
+
+ //Log("Mutex_Acquire: (%p)", Mutex);
+
+ // Check if the lock is already held
+ if( Mutex->Owner ) {
+ SHORTLOCK( &glThreadListLock );
+ // - Remove from active list
+ us = Threads_RemActive();
+ us->Next = NULL;
+ // - Mark as sleeping
+ us->Status = THREAD_STAT_MUTEXSLEEP;
+ us->WaitPointer = Mutex;
+
+ // - Add to waiting
+ if(Mutex->LastWaiting) {
+ Mutex->LastWaiting->Next = us;
+ Mutex->LastWaiting = us;
+ }
+ else {
+ Mutex->Waiting = us;
+ Mutex->LastWaiting = us;
+ }
+ #if 0
+ {
+ int i = 0;
+ tThread *t;
+ for( t = Mutex->Waiting; t; t = t->Next, i++ )
+ Log("[%i] (tMutex)%p->Waiting[%i] = %p (%i %s)", us->TID, Mutex, i,
+ t, t->TID, t->ThreadName);
+ }
+ #endif
+
+ SHORTREL( &glThreadListLock );
+ SHORTREL( &Mutex->Protector );
+ while(us->Status == THREAD_STAT_MUTEXSLEEP) Threads_Yield();
+ // We're only woken when we get the lock
+ us->WaitPointer = NULL;
+ }
+ // Ooh, let's take it!
+ else {
+ Mutex->Owner = us;
+ SHORTREL( &Mutex->Protector );
+ }
+
+ #if 0
+ extern tMutex glPhysAlloc;
+ if( Mutex != &glPhysAlloc )
+ LogF("Mutex %p taken by %i %p\n", Mutex, us->TID, __builtin_return_address(0));
+ #endif
+
+ return 0;
+}
+
+/**
+ * \brief Release a held mutex
+ * \param Mutex Mutex to release
+ * \note Releasing a non-held mutex has no effect
+ */
+void Mutex_Release(tMutex *Mutex)
+{
+ SHORTLOCK( &Mutex->Protector );
+ //Log("Mutex_Release: (%p)", Mutex);
+ if( Mutex->Waiting ) {
+ Mutex->Owner = Mutex->Waiting; // Set owner
+ Mutex->Waiting = Mutex->Waiting->Next; // Next!
+ // Reset ->LastWaiting to NULL if we have just removed the last waiting thread
+ // 2010-10-02 21:50 - Comemerating the death of the longest single
+ // blocker in the Acess2 history. REMEMBER TO
+ // FUCKING MAINTAIN YOUR FUCKING LISTS DIPWIT
+ if( Mutex->LastWaiting == Mutex->Owner )
+ Mutex->LastWaiting = NULL;
+
+ // Wake new owner
+ SHORTLOCK( &glThreadListLock );
+ if( Mutex->Owner->Status != THREAD_STAT_ACTIVE )
+ Threads_AddActive(Mutex->Owner);
+ SHORTREL( &glThreadListLock );
+ }
+ else {
+ Mutex->Owner = NULL;
+ }
+ SHORTREL( &Mutex->Protector );
+
+ #if 0
+ extern tMutex glPhysAlloc;
+ if( Mutex != &glPhysAlloc )
+ LogF("Mutex %p released by %i %p\n", Mutex, Threads_GetTID(), __builtin_return_address(0));
+ #endif
+}
+
+/**
+ * \brief Is this mutex locked?
+ * \param Mutex Mutex pointer
+ */
+int Mutex_IsLocked(tMutex *Mutex)
+{
+ return Mutex->Owner != NULL;
+}
+
+//
+// Initialise a semaphore
+//
+void Semaphore_Init(tSemaphore *Sem, int Value, int MaxValue, const char *Module, const char *Name)
+{
+ memset(Sem, 0, sizeof(tSemaphore));
+ Sem->Value = Value;
+ Sem->ModName = Module;
+ Sem->Name = Name;
+ Sem->MaxValue = MaxValue;
+}
+//
+// Wait for items to be avaliable
+//
+int Semaphore_Wait(tSemaphore *Sem, int MaxToTake)
+{
+ tThread *us;
+ int taken;
+ if( MaxToTake < 0 ) {
+ Log_Warning("Threads", "Semaphore_Wait: User bug - MaxToTake(%i) < 0, Sem=%p(%s)",
+ MaxToTake, Sem, Sem->Name);
+ }
+
+ SHORTLOCK( &Sem->Protector );
+
+ // Check if there's already items avaliable
+ if( Sem->Value > 0 ) {
+ // Take what we need
+ if( MaxToTake && Sem->Value > MaxToTake )
+ taken = MaxToTake;
+ else
+ taken = Sem->Value;
+ Sem->Value -= taken;
+ SHORTREL( &Sem->Protector );
+ }
+ else
+ {
+ SHORTLOCK( &glThreadListLock );
+
+ // - Remove from active list
+ us = Threads_RemActive();
+ us->Next = NULL;
+ // - Mark as sleeping
+ us->Status = THREAD_STAT_SEMAPHORESLEEP;
+ us->WaitPointer = Sem;
+ us->RetStatus = MaxToTake; // Use RetStatus as a temp variable
+
+ // - Add to waiting
+ if(Sem->LastWaiting) {
+ Sem->LastWaiting->Next = us;
+ Sem->LastWaiting = us;
+ }
+ else {
+ Sem->Waiting = us;
+ Sem->LastWaiting = us;
+ }
+
+ SHORTREL( &glThreadListLock );
+ SHORTREL( &Sem->Protector );
+ while(us->Status == THREAD_STAT_SEMAPHORESLEEP) Threads_Yield();
+ // We're only woken when there's something avaliable
+ us->WaitPointer = NULL;
+
+ taken = us->RetStatus;
+
+ // Get the lock again
+ SHORTLOCK( &Sem->Protector );
+ }
+
+ // While there is space, and there are thread waiting
+ // wake the first thread and give it what it wants (or what's left)
+ while( (Sem->MaxValue == 0 || Sem->Value < Sem->MaxValue) && Sem->Signaling )
+ {
+ int given;
+ tThread *toWake = Sem->Signaling;
+
+ Sem->Signaling = Sem->Signaling->Next;
+ // Reset ->LastWaiting to NULL if we have just removed the last waiting thread
+ if( Sem->Signaling == NULL )
+ Sem->LastSignaling = NULL;
+
+ // Figure out how much to give
+ if( toWake->RetStatus && Sem->Value + toWake->RetStatus < Sem->MaxValue )
+ given = toWake->RetStatus;
+ else
+ given = Sem->MaxValue - Sem->Value;
+ Sem->Value -= given;
+
+ // Save the number we gave to the thread's status
+ toWake->RetStatus = given;
+
+ // Wake the sleeper
+ SHORTLOCK( &glThreadListLock );
+ if( toWake->Status != THREAD_STAT_ACTIVE )
+ Threads_AddActive(toWake);
+ SHORTREL( &glThreadListLock );
+ }
+ SHORTREL( &Sem->Protector );
+
+ return taken;
+}
+
+//
+// Add items to a semaphore
+//
+int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd)
+{
+ int given;
+ int added;
+
+ if( AmmountToAdd < 0 ) {
+ Log_Warning("Threads", "Semaphore_Signal: User bug - AmmountToAdd(%i) < 0, Sem=%p(%s)",
+ AmmountToAdd, Sem, Sem->Name);
+ }
+ SHORTLOCK( &Sem->Protector );
+
+ // Check if we have to block
+ if( Sem->MaxValue && Sem->Value == Sem->MaxValue )
+ {
+ tThread *us;
+ #if 0
+ Log_Debug("Threads", "Semaphore_Signal: IDLE Sem = %s:%s", Sem->ModName, Sem->Name);
+ Log_Debug("Threads", "Semaphore_Signal: Sem->Value(%i) == Sem->MaxValue(%i)", Sem->Value, Sem->MaxValue);
+ #endif
+
+ SHORTLOCK( &glThreadListLock );
+ // - Remove from active list
+ us = Threads_RemActive();
+ us->Next = NULL;
+ // - Mark as sleeping
+ us->Status = THREAD_STAT_SEMAPHORESLEEP;
+ us->WaitPointer = Sem;
+ us->RetStatus = AmmountToAdd; // Use RetStatus as a temp variable
+
+ // - Add to waiting
+ if(Sem->LastSignaling) {
+ Sem->LastSignaling->Next = us;
+ Sem->LastSignaling = us;
+ }
+ else {
+ Sem->Signaling = us;
+ Sem->LastSignaling = us;
+ }
+
+ SHORTREL( &glThreadListLock );
+ SHORTREL( &Sem->Protector );
+ while(us->Status == THREAD_STAT_SEMAPHORESLEEP) Threads_Yield();
+ // We're only woken when there's something avaliable
+ us->WaitPointer = NULL;
+
+ added = us->RetStatus;
+
+ // Get the lock again
+ SHORTLOCK( &Sem->Protector );
+ }
+ // Non blocking
+ else
+ {
+ // Figure out how much we need to take off
+ if( Sem->MaxValue && Sem->Value + AmmountToAdd > Sem->MaxValue)
+ added = Sem->MaxValue - Sem->Value;
+ else
+ added = AmmountToAdd;
+ Sem->Value += added;
+ }
+
+ // While there are items avaliable, and there are thread waiting
+ // wake the first thread and give it what it wants (or what's left)
+ while( Sem->Value && Sem->Waiting )
+ {
+ tThread *toWake = Sem->Waiting;
+
+ Sem->Waiting = Sem->Waiting->Next;
+ // Reset ->LastWaiting to NULL if we have just removed the last waiting thread
+ if( Sem->Waiting == NULL )
+ Sem->LastWaiting = NULL;
+
+ // Figure out how much to give
+ if( toWake->RetStatus && Sem->Value > toWake->RetStatus )
+ given = toWake->RetStatus;
+ else
+ given = Sem->Value;
+ Sem->Value -= given;
+
+ // Save the number we gave to the thread's status
+ toWake->RetStatus = given;
+
+ // Wake the sleeper
+ SHORTLOCK( &glThreadListLock );
+ if( toWake->Status != THREAD_STAT_ACTIVE )
+ Threads_AddActive(toWake);
+ SHORTREL( &glThreadListLock );
+ }
+ SHORTREL( &Sem->Protector );
+
+ return added;
+}
+
+//
+// Get the current value of a semaphore
+//
+int Semaphore_GetValue(tSemaphore *Sem)
+{
+ return Sem->Value;
+}
+
+// === EXPORTS ===
+EXPORT(Threads_GetUID);
+EXPORT(Threads_GetGID);
+EXPORT(Mutex_Acquire);
+EXPORT(Mutex_Release);
+EXPORT(Mutex_IsLocked);
+EXPORT(Semaphore_Init);
+EXPORT(Semaphore_Wait);
+EXPORT(Semaphore_Signal);