extern void Proc_Start();
extern tThread *Proc_GetCurThread();
extern int Proc_Clone(Uint *Err, Uint Flags);
+extern void Proc_CallFaultHandler(tThread *Thread);
// === PROTOTYPES ===
void Threads_Init();
-void Threads_SetName(char *NewName);
+ int Threads_SetName(char *NewName);
char *Threads_GetName(int ID);
void Threads_SetTickets(int Num);
tThread *Threads_CloneTCB(Uint *Err, Uint Flags);
{0}, // Saved State
{0}, // VM State
- 0, {0}, {0}, // Signal State
+ 0, 0, // Current Fault, Fault Handler
NULL, NULL, // Messages, Last Message
DEFAULT_QUANTUM, DEFAULT_QUANTUM, // Quantum, Remaining
* \fn void Threads_SetName(char *NewName)
* \brief Sets the current thread's name
*/
-void Threads_SetName(char *NewName)
+int Threads_SetName(char *NewName)
{
tThread *cur = Proc_GetCurThread();
if( IsHeap(cur->ThreadName) )
free( cur->ThreadName );
cur->ThreadName = malloc(strlen(NewName)+1);
strcpy(cur->ThreadName, NewName);
+ return 0;
}
/**
new->NumTickets = cur->NumTickets;
// Set Signal Handlers
- new->CurSignal = 0;
- if(Flags & CLONE_VM)
- memset(new->SignalHandlers, 0, sizeof(new->SignalHandlers));
- else
- memcpy(new->SignalHandlers, cur->SignalHandlers, sizeof(new->SignalHandlers));
- memset(&new->SignalState, 0, sizeof(tTaskState));
+ new->CurFaultNum = 0;
+ new->FaultHandler = cur->FaultHandler;
for( i = 0; i < NUM_CFG_ENTRIES; i ++ )
{
int initStatus = t->Status;
int ret;
- if(initStatus != THREAD_STAT_ZOMBIE)
+ if(initStatus != THREAD_STAT_ZOMBIE) {
while(t->Status == initStatus) {
Threads_Yield();
}
+ }
ret = t->RetStatus;
switch(t->Status)
tThread *cur = Proc_GetCurThread();
tThread *thread;
- Log("Proc_Sleep: %i going to sleep", cur->TID);
+ //Log_Log("Threads", "%i going to sleep", cur->TID);
// Acquire Spinlock
LOCK( &giThreadListLock );
{
case THREAD_STAT_ACTIVE: break;
case THREAD_STAT_SLEEPING:
+ //Log_Log("Threads", "Waking %i (%p) from sleeping", Thread->TID, Thread);
LOCK( &giThreadListLock );
prev = Threads_int_GetPrev(&gSleepingThreads, Thread);
prev->Next = Thread->Next; // Remove from sleeping queue
Thread->Next = gActiveThreads; // Add to active queue
gActiveThreads = Thread;
+ giNumActiveThreads ++;
+ giTotalTickets += Thread->NumTickets;
Thread->Status = THREAD_STAT_ACTIVE;
RELEASE( &giThreadListLock );
break;
}
}
+void Threads_WakeTID(tTID Thread)
+{
+ Threads_Wake( Threads_GetThread(Thread) );
+}
+
/**
* \fn void Threads_AddActive(tThread *Thread)
* \brief Adds a thread to the active queue
RELEASE( &giThreadListLock );
}
-#if 0
/**
- * \fn void Threads_SetSignalHandler(int Num, void *Handler)
+ * \fn void Threads_SetFaultHandler(Uint Handler)
* \brief Sets the signal handler for a signal
*/
-void Threads_SetSignalHandler(int Num, void *Handler)
-{
- if(Num < 0 || Num >= NSIG) return;
-
- gCurrentThread->SignalHandlers[Num] = Handler;
+void Threads_SetFaultHandler(Uint Handler)
+{
+ Log_Log("Threads", "Threads_SetFaultHandler: Handler = %p", Handler);
+ Proc_GetCurThread()->FaultHandler = Handler;
}
/**
- * \fn void Threads_SendSignal(int TID, int Num)
- * \brief Send a signal to a thread
+ * \fn void Threads_Fault(int Num)
+ * \brief Calls a fault handler
*/
-void Threads_SendSignal(int TID, int Num)
+void Threads_Fault(int Num)
{
- tThread *thread = Proc_GetThread(TID);
- void *handler;
+ tThread *thread = Proc_GetCurThread();
+
+ Log_Log("Threads", "Threads_Fault: thread = %p", thread);
if(!thread) return ;
- handler = thread->SignalHandlers[Num];
+ Log_Log("Threads", "Threads_Fault: thread->FaultHandler = %p", thread->FaultHandler);
- // Panic?
- if(handler == SIG_ERR) {
- Proc_Kill(TID);
+ switch(thread->FaultHandler)
+ {
+ case 0: // Panic?
+ Threads_Kill(thread, -1);
+ HALT();
return ;
- }
- // Dump Core?
- if(handler == -2) {
- Proc_Kill(TID);
+ case 1: // Dump Core?
+ Threads_Kill(thread, -1);
+ HALT();
return ;
}
- // Ignore?
- if(handler == -2) return;
-
- // Check the type and handle if the thread is already in a signal
- if(thread->CurSignal != 0) {
- if(Num < _SIGTYPE_FATAL)
- Proc_Kill(TID);
- } else {
- while(thread->CurSignal != 0)
- Proc_Yield();
- }
+
+ // Double Fault? Oh, F**k
+ if(thread->CurFaultNum != 0) {
+ Threads_Kill(thread, -1); // For now, just kill
+ HALT();
}
- //TODO:
+ thread->CurFaultNum = Num;
+
+ Proc_CallFaultHandler(thread);
}
-#endif
// --- Process Structure Access Functions ---
tPID Threads_GetPID()
int ticket;
int number;
- if(giNumActiveThreads == 0) return NULL;
+ if(giNumActiveThreads == 0) {
+ return NULL;
+ }
// Special case: 1 thread
if(giNumActiveThreads == 1) {
return gActiveThreads;
}
- //Log(" Threads_GetNextToRun: giNumActiveThreads=%i,giTotalTickets=%i",
- // giNumActiveThreads, giTotalTickets);
// Get the ticket number
ticket = number = rand() % giTotalTickets;
- //Log(" Threads_GetNextToRun: ticket = %i", ticket);
-
// Find the next thread
for(thread=gActiveThreads;thread;thread=thread->Next)
{
*/
void Threads_SegFault(tVAddr Addr)
{
- //Threads_SendSignal( Proc_GetCurThread()->TID, SIGSEGV );
Warning("Thread #%i committed a segfault at address %p", Proc_GetCurThread()->TID, Addr);
- Threads_Exit( 0, -1 );
+ Threads_Fault( 1 );
+ //Threads_Exit( 0, -1 );
}
// === EXPORTS ===