// === IMPORTS ===
extern void ArchThreads_Init(void);
extern void Proc_CallFaultHandler(tThread *Thread);
+extern void Proc_DumpThreadCPUState(tThread *Thread);
extern int GetCPUNum(void);
// === PROTOTYPES ===
// -- Core Thread --
// Only used for the core kernel
tThread gThreadZero = {
- Status: THREAD_STAT_ACTIVE, // Status
- ThreadName: (char*)"ThreadZero", // Name
- Quantum: DEFAULT_QUANTUM, // Default Quantum
- Remaining: DEFAULT_QUANTUM, // Current Quantum
- Priority: DEFAULT_PRIORITY // Number of tickets
+ .Status = THREAD_STAT_ACTIVE, // Status
+ .ThreadName = (char*)"ThreadZero", // Name
+ .Quantum = DEFAULT_QUANTUM, // Default Quantum
+ .Remaining = DEFAULT_QUANTUM, // Current Quantum
+ .Priority = DEFAULT_PRIORITY // Number of tickets
};
// -- Processes --
// --- Locks ---
tThread *cur = Proc_GetCurThread();
Warning("WTF, CPU%i %p (%i %s) is adding %p (%i %s) when it is active",
GetCPUNum(), cur, cur->TID, cur->ThreadName, Thread, Thread->TID, Thread->ThreadName);
+ SHORTREL( &glThreadListLock );
+ return ;
}
// Set state
Thread->Status = THREAD_STAT_ACTIVE;
- Thread->CurCPU = -1;
// Add to active list
#if SCHEDULER_TYPE == SCHED_RR_PRI
Thread->Next = gaActiveThreads[Thread->Priority];
giNumActiveThreads ++;
#if SCHEDULER_TYPE == SCHED_LOTTERY
- giFreeTickets += caiTICKET_COUNTS[ Thread->Priority ];
- # if DEBUG_TRACE_TICKETS
- Log("Threads_AddActive: CPU%i %p %i (%s) added, new giFreeTickets = %i",
- GetCPUNum(), Thread, Thread->TID, Thread->ThreadName, giFreeTickets);
- # endif
+ {
+ int delta;
+ // Only change the ticket count if the thread is un-scheduled
+ if(Thread->CurCPU != -1)
+ delta = 0;
+ else
+ delta = caiTICKET_COUNTS[ Thread->Priority ];
+
+ giFreeTickets += delta;
+ # if DEBUG_TRACE_TICKETS
+ Log("CPU%i %p (%i %s) added, new giFreeTickets = %i [+%i]",
+ GetCPUNum(), Thread, Thread->TID, Thread->ThreadName,
+ giFreeTickets, delta
+ );
+ # endif
+ }
#endif
SHORTREL( &glThreadListLock );
ret->Next = NULL;
ret->Remaining = 0;
- ret->CurCPU = -1;
giNumActiveThreads --;
// no need to decrement tickets, scheduler did it for us
#if SCHEDULER_TYPE == SCHED_LOTTERY && DEBUG_TRACE_TICKETS
- Log("Threads_RemActive: CPU%i %p %i (%s) removed, giFreeTickets = %i",
+ Log("CPU%i %p (%i %s) removed, giFreeTickets = %i [nc]",
GetCPUNum(), ret, ret->TID, ret->ThreadName, giFreeTickets);
#endif
Log(" ERROR State (%i) != THREAD_STAT_ACTIVE (%i)", thread->Status, THREAD_STAT_ACTIVE);
Log(" Priority %i, Quantum %i", thread->Priority, thread->Quantum);
Log(" KStack 0x%x", thread->KernelStack);
+ Proc_DumpThreadCPUState(thread);
}
#if SCHEDULER_TYPE == SCHED_RR_PRI
#if SCHEDULER_TYPE == SCHED_LOTTERY
giFreeTickets += caiTICKET_COUNTS[ Last->Priority ];
# if DEBUG_TRACE_TICKETS
- LogF(" CPU %i released %p (%i %s) into the pool (%i tickets in pool)\n",
- CPU, Last, Last->TID, Last->ThreadName, giFreeTickets);
+ 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(" CPU %i released %p (%i %s)->Status = %i (Released)\n",
+ LogF("Log: CPU%i released %p (%i %s)->Status = %i (Released)\n",
CPU, Last, Last->TID, Last->ThreadName, Last->Status);
#endif
Last->CurCPU = -1;
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 ];
+ # 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
}
// ---
/**
* \brief Release a held mutex
* \param Mutex Mutex to release
+ * \note Releasing a non-held mutex has no effect
*/
void Mutex_Release(tMutex *Mutex)
{
//
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
if( Sem->MaxValue && Sem->Value == Sem->MaxValue )
{
tThread *us;
- SHORTLOCK( &glThreadListLock );
+ #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;