int Threads_WaitTID(int TID, int *status);
tThread *Threads_GetThread(Uint TID);
void Threads_AddToDelete(tThread *Thread);
-tThread *Threads_int_GetPrev(tThread **List, tThread *Thread);
+tThread *Threads_int_DelFromQueue(tThread **List, tThread *Thread);
void Threads_Exit(int TID, int Status);
void Threads_Kill(tThread *Thread, int Status);
void Threads_Yield(void);
void Threads_Sleep(void);
int Threads_Wake(tThread *Thread);
void Threads_AddActive(tThread *Thread);
-void Threads_int_AddActive(tThread *Thread);
tThread *Threads_RemActive(void);
int Threads_GetPID(void);
int Threads_GetTID(void);
tGID Threads_GetGID(void);
int Threads_SetGID(Uint *Errno, tUID ID);
void Threads_Dump(void);
+void Threads_DumpActive(void);
void Mutex_Acquire(tMutex *Mutex);
void Mutex_Release(tMutex *Mutex);
int Mutex_IsLocked(tMutex *Mutex);
tThread *gSleepingThreads = NULL; // Sleeping Threads
tThread *gDeleteThreads = NULL; // Threads to delete
int giNumCPUs = 1; // Number of CPUs
+BOOL gaThreads_NoTaskSwitch[MAX_CPUS]; // Disables task switches for each core (Pseudo-IF)
// === CODE ===
/**
}
/**
- * \brief Gets the previous entry in a thead linked list
+ * \brief Deletes an entry from a list
* \param List Pointer to the list head
* \param Thread Thread to find
- * \return Thread before \a Thread on \a List
- * \note This uses a massive hack of assuming that the first field in the
- * structure is the .Next pointer. By doing this, we can return \a List
- * as a (tThread*) and simplify other code.
+ * \return \a Thread
*/
-tThread *Threads_int_GetPrev(tThread **List, tThread *Thread)
+tThread *Threads_int_DelFromQueue(tThread **List, tThread *Thread)
{
- tThread *ret;
- // First Entry
- if(*List == Thread) {
- return (tThread*)List;
+ tThread *ret, *prev = NULL;
+
+ for(ret = *List;
+ ret && ret != Thread;
+ prev = ret, ret = ret->Next
+ );
+
+ // Is the thread on the list
+ if(!ret) {
+ //LogF("%p(%s) is not on list %p\n", Thread, Thread->ThreadName, List);
+ return NULL;
+ }
+
+ if( !prev ) {
+ *List = Thread->Next;
+ //LogF("%p(%s) removed from head of %p\n", Thread, Thread->ThreadName, List);
}
- // Or not
else {
- for(ret = *List;
- ret->Next && ret->Next != Thread;
- ret = ret->Next
- );
- // Error if the thread is not on the list
- if(!ret->Next || ret->Next != Thread) {
- return NULL;
- }
+ prev->Next = Thread->Next;
+ //LogF("%p(%s) removed from %p (prev=%p)\n", Thread, Thread->ThreadName, List, prev);
}
- return ret;
+
+ return Thread;
}
/**
*/
void Threads_Kill(tThread *Thread, int Status)
{
- tThread *prev;
tMsg *msg;
// TODO: Kill all children
// Lock thread (stop us recieving messages)
SHORTLOCK( &Thread->IsLocked );
+ // Clear Message Queue
+ while( Thread->Messages )
+ {
+ msg = Thread->Messages->Next;
+ free( Thread->Messages );
+ Thread->Messages = msg;
+ }
+
// Lock thread list
SHORTLOCK( &glThreadListLock );
- // Get previous thread on list
- prev = Threads_int_GetPrev( &gActiveThreads, Thread );
- if(!prev) {
+ // Delete from active list
+ if( !Threads_int_DelFromQueue( &gActiveThreads, Thread ) )
+ {
Warning("Proc_Exit - Current thread is not on the active queue");
SHORTREL( &glThreadListLock );
SHORTREL( &Thread->IsLocked );
return;
}
- // Clear Message Queue
- while( Thread->Messages )
- {
- msg = Thread->Messages->Next;
- free( Thread->Messages ); // BIG NO-NO
- Thread->Messages = msg;
- }
-
// Ensure that we are not rescheduled
Thread->Remaining = 0; // Clear Remaining Quantum
Thread->Quantum = 0; // Clear Quantum to indicate dead thread
- prev->Next = Thread->Next; // Remove from active
// Update bookkeeping
giNumActiveThreads --;
*/
int Threads_Wake(tThread *Thread)
{
- tThread *prev;
-
if(!Thread)
return -EINVAL;
return -EALREADY;
case THREAD_STAT_SLEEPING:
+ SHORTLOCK( &glThreadListLock );
// Remove from sleeping queue
- prev = Threads_int_GetPrev(&gSleepingThreads, Thread);
- prev->Next = Thread->Next;
+ Threads_int_DelFromQueue(&gSleepingThreads, Thread);
- Threads_int_AddActive( Thread );
+ Threads_AddActive( Thread );
#if DEBUG_TRACE_STATE
Log("Threads_Sleep: %p (%i %s) woken", Thread, Thread->TID, Thread->ThreadName);
#endif
+ SHORTREL( &glThreadListLock );
return -EOK;
case THREAD_STAT_WAITING:
int ret;
if(!thread)
return -ENOENT;
- SHORTLOCK( &glThreadListLock );
ret = Threads_Wake( thread );
- SHORTREL( &glThreadListLock );
//Log_Debug("Threads", "TID %i woke %i (%p)", Threads_GetTID(), TID, thread);
return ret;
}
void Threads_AddActive(tThread *Thread)
{
SHORTLOCK( &glThreadListLock );
- Threads_int_AddActive(Thread);
- SHORTREL( &glThreadListLock );
-}
-
-/**
- * \brief Adds a thread to the active queue
- * \note This version MUST have the thread list lock held
- */
-void Threads_int_AddActive(tThread *Thread)
-{
- // Add to active list
- Thread->Next = gActiveThreads;
- gActiveThreads = Thread;
+
+ #if 1
+ {
+ tThread *t;
+ for( t = gActiveThreads; t; t = t->Next )
+ {
+ if( t == Thread ) {
+ Panic("Threads_AddActive: Attempting a double add of TID %i (0x%x)",
+ Thread->TID, __builtin_return_address(0));
+ }
+
+ if(t->Status != THREAD_STAT_ACTIVE) {
+ Panic("Threads_AddActive: TID %i status != THREAD_STAT_ACTIVE",
+ Thread->TID);
+ }
+ }
+ }
+ #endif
+
// Set state
Thread->Status = THREAD_STAT_ACTIVE;
Thread->CurCPU = -1;
+ // Add to active list
+ Thread->Next = gActiveThreads;
+ gActiveThreads = Thread;
// Update bookkeeping
giNumActiveThreads ++;
giFreeTickets += Thread->NumTickets;
#if DEBUG_TRACE_TICKETS
- Log("Threads_int_AddActive: new giFreeTickets = %i", giFreeTickets);
+ Log("Threads_AddActive: %p %i (%s) added, new giFreeTickets = %i",
+ Thread, Thread->TID, Thread->ThreadName, giFreeTickets);
#endif
+ SHORTREL( &glThreadListLock );
}
/**
tThread *Threads_RemActive(void)
{
tThread *ret = Proc_GetCurThread();
- tThread *prev = Threads_int_GetPrev(&gActiveThreads, ret);
- if(!prev) return NULL;
+
+ SHORTLOCK( &glThreadListLock );
+
+ // Delete from active queue
+ if( !Threads_int_DelFromQueue(&gActiveThreads, ret) ) {
+ SHORTREL( &glThreadListLock );
+ return NULL;
+ }
+
ret->Remaining = 0;
ret->CurCPU = -1;
- prev->Next = ret->Next;
+
giNumActiveThreads --;
+ // no need to decrement tickets, scheduler did it for us
+
+ #if DEBUG_TRACE_TICKETS
+ Log("Threads_RemActive: %p %i (%s) removed, giFreeTickets = %i",
+ ret, ret->TID, ret->ThreadName, giFreeTickets);
+ #endif
+
+ SHORTREL( &glThreadListLock );
+
return ret;
}
/**
* \fn void Threads_Dump(void)
- * \brief Dums a list of currently running threads
+ * \brief Dumps a list of currently running threads
*/
void Threads_Dump(void)
{
tThread *thread;
- Log("Active Threads:");
+ Log("--- Thread Dump ---");
+ Log("Active Threads: (%i reported)", giNumActiveThreads);
for(thread=gActiveThreads;thread;thread=thread->Next)
{
Log(" %i (%i) - %s (CPU %i)",
Log(" KStack 0x%x", thread->KernelStack);
}
}
+/**
+ * \fn void Threads_Dump(void)
+ */
+void Threads_DumpActive(void)
+{
+ tThread *thread;
+
+ Log("Active Threads:");
+ for(thread=gActiveThreads;thread;thread=thread->Next)
+ {
+ Log(" %i (%i) - %s (CPU %i)",
+ thread->TID, thread->TGID, thread->ThreadName, thread->CurCPU);
+ if(thread->Status != THREAD_STAT_ACTIVE)
+ Log(" ERROR State (%i) != THREAD_STAT_ACTIVE (%i)", thread->Status, THREAD_STAT_ACTIVE);
+ Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum);
+ Log(" KStack 0x%x", thread->KernelStack);
+ }
+}
/**
* \brief Gets the next thread to run
{
tThread *thread;
int ticket;
- int number;
+ int number;
+
+ // 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 );
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
}
}
#if DEBUG_TRACE_TICKETS
else
- LogF(" %p (%s)->Status = %i (Released)\n", Last, Last->ThreadName, Last->Status);
+ LogF(" CPU %i released %p (%i %s)->Status = %i (Released)\n",
+ CPU, Last, Last->TID, Last->ThreadName, Last->Status);
#endif
Last->CurCPU = -1;
}
+ #if DEBUG_TRACE_TICKETS
+ //Threads_DumpActive();
+ #endif
+
#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 += thread->NumTickets;
}
if(number != giFreeTickets) {
if( Mutex->Owner ) {
SHORTLOCK( &glThreadListLock );
// - Remove from active list
- Threads_RemActive();
+ us = Threads_RemActive();
// - Mark as sleeping
us->Status = THREAD_STAT_OFFSLEEP;
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
- Threads_AddActive(Mutex->Owner);
- //Log("Mutex %p Woke %p", Mutex, Mutex->Owner);
+ SHORTLOCK( &glThreadListLock );
+ if( Mutex->Owner->Status != THREAD_STAT_ACTIVE )
+ Threads_AddActive(Mutex->Owner);
+ SHORTREL( &glThreadListLock );
}
else {
Mutex->Owner = NULL;
// === EXPORTS ===
EXPORT(Threads_GetUID);
+EXPORT(Threads_GetGID);
EXPORT(Mutex_Acquire);
EXPORT(Mutex_Release);
EXPORT(Mutex_IsLocked);