4 * - Common Thread Control
10 #define DEFAULT_QUANTUM 10
11 #define DEFAULT_TICKETS 5
12 #define MAX_TICKETS 10
15 extern void ArchThreads_Init();
16 extern void Proc_Start();
17 extern tThread *Proc_GetCurThread();
18 extern int Proc_Clone(Uint *Err, Uint Flags);
22 void Threads_SetName(char *NewName);
23 void Threads_SetTickets(int Num);
24 int Threads_WaitTID(int TID, int *status);
25 tThread *Threads_GetThread(Uint TID);
26 void Threads_AddToDelete(tThread *Thread);
27 tThread *Threads_int_GetPrev(tThread **List, tThread *Thread);
28 void Threads_Exit(int TID, int Status);
29 void Threads_Kill(tThread *Thread, int Status);
32 void Threads_Wake(tThread *Thread);
33 void Threads_AddActive(tThread *Thread);
42 tThread gThreadZero = {
43 NULL, 0, // Next, Lock
44 THREAD_STAT_ACTIVE, // Status
48 0, // Parent Thread ID
55 0, {0}, {0}, // Signal State
57 NULL, NULL, // Messages, Last Message
58 DEFAULT_QUANTUM, DEFAULT_QUANTUM, // Quantum, Remaining
60 {0} // Default config to zero
64 volatile int giThreadListLock = 0; ///\note NEVER use a heap function while locked
65 // --- Current State ---
66 volatile int giNumActiveThreads = 0;
67 volatile int giTotalTickets = 0;
68 volatile Uint giNextTID = 1;
69 // --- Thread Lists ---
70 tThread *gActiveThreads = NULL; // Currently Running Threads
71 tThread *gSleepingThreads = NULL; // Sleeping Threads
72 tThread *gDeleteThreads = NULL; // Threads to delete
77 * \fn void Threads_Init()
78 * \brief Initialse the thread list
84 // Create Initial Task
85 gActiveThreads = &gThreadZero;
86 giTotalTickets = gThreadZero.NumTickets;
87 giNumActiveThreads = 1;
91 if(Proc_Clone(0, 0) == 0)
93 tThread *cur = Proc_GetCurThread();
94 cur->ThreadName = "Idle Thread";
95 Threads_SetTickets(0); // Never called randomly
96 cur->Quantum = 1; // 1 slice quantum
99 HALT(); // Just yeilds
108 * \fn void Threads_SetName(char *NewName)
109 * \brief Sets the current thread's name
111 void Threads_SetName(char *NewName)
113 tThread *cur = Proc_GetCurThread();
114 if( IsHeap(cur->ThreadName) )
115 free( cur->ThreadName );
116 cur->ThreadName = malloc(strlen(NewName)+1);
117 strcpy(cur->ThreadName, NewName);
121 * \fn void Threads_SetTickets(int Num)
122 * \brief Sets the 'priority' of a task
124 void Threads_SetTickets(int Num)
126 tThread *cur = Proc_GetCurThread();
128 if(Num > MAX_TICKETS) Num = MAX_TICKETS;
130 LOCK( &giThreadListLock );
131 giTotalTickets -= cur->NumTickets;
132 cur->NumTickets = Num;
133 giTotalTickets += Num;
134 //LOG("giTotalTickets = %i", giTotalTickets);
135 RELEASE( &giThreadListLock );
139 * \fn void Threads_WaitTID(int TID, int *status)
140 * \brief Wait for a task to change state
142 int Threads_WaitTID(int TID, int *status)
150 // Any peer/child thread
163 tThread *t = Threads_GetThread(TID);
164 int initStatus = t->Status;
166 while(t->Status == initStatus) Threads_Yield();
170 case THREAD_STAT_ZOMBIE:
171 t->Status = THREAD_STAT_DEAD;
172 if(status) *status = 0;
173 Threads_AddToDelete( t );
176 if(status) *status = -1;
186 * \fn tThread *Threads_GetThread(Uint TID)
187 * \brief Gets a thread given its TID
189 tThread *Threads_GetThread(Uint TID)
193 // Search Active List
194 for(thread = gActiveThreads;
196 thread = thread->Next)
198 if(thread->TID == TID)
202 // Search Sleeping List
203 for(thread = gSleepingThreads;
205 thread = thread->Next)
207 if(thread->TID == TID)
215 * \fn void Threads_AddToDelete(tThread *Thread)
216 * \brief Adds a thread to the delete queue
218 void Threads_AddToDelete(tThread *Thread)
220 // Add to delete queue
222 Thread->Next = gDeleteThreads;
223 gDeleteThreads = Thread;
226 gDeleteThreads = Thread;
231 * \fn tThread *Threads_int_GetPrev(tThread *List, tThread *Thread)
232 * \brief Gets the previous entry in a thead linked list
234 tThread *Threads_int_GetPrev(tThread **List, tThread *Thread)
238 if(*List == Thread) {
239 return (tThread*)List;
242 ret->Next && ret->Next != Thread;
245 // Error if the thread is not on the list
246 if(!ret->Next || ret->Next != Thread) {
254 * \fn void Threads_Exit(int TID, int Status)
255 * \brief Exit the current process
257 void Threads_Exit(int TID, int Status)
260 Threads_Kill( Proc_GetCurThread(), (Uint)Status & 0xFF );
262 Threads_Kill( Threads_GetThread(TID), (Uint)Status & 0xFF );
266 * \fn void Threads_Kill(tThread *Thread, int Status)
267 * \brief Kill a thread
268 * \param TID Thread ID (0 for current)
270 void Threads_Kill(tThread *Thread, int Status)
279 for(child = gActiveThreads;
283 if(child->PTID == Thread->TID)
284 Threads_Kill(child, -1);
289 ///\note Double lock is needed due to overlap of locks
291 // Lock thread (stop us recieving messages)
292 LOCK( &Thread->IsLocked );
295 LOCK( &giThreadListLock );
297 // Get previous thread on list
298 prev = Threads_int_GetPrev( &gActiveThreads, Thread );
300 Warning("Proc_Exit - Current thread is not on the active queue");
304 // Clear Message Queue
305 while( Thread->Messages )
307 msg = Thread->Messages->Next;
308 free( Thread->Messages );
309 Thread->Messages = msg;
312 Thread->Remaining = 0; // Clear Remaining Quantum
313 Thread->Quantum = 0; // Clear Quantum to indicate dead thread
314 prev->Next = Thread->Next; // Remove from active
316 giNumActiveThreads --;
317 giTotalTickets -= Thread->NumTickets;
319 // Mark thread as a zombie
320 Thread->RetStatus = Status;
322 // Don't Zombie if we are being killed as part of a tree
325 Thread->Status = THREAD_STAT_DEAD;
326 Threads_AddToDelete( Thread );
328 Thread->Status = THREAD_STAT_ZOMBIE;
332 RELEASE( &Thread->IsLocked ); // Released first so that it IS released
333 RELEASE( &giThreadListLock );
334 if(Status != -1) HALT();
338 * \fn void Threads_Yield()
339 * \brief Yield remainder of timeslice
343 Proc_GetCurThread()->Remaining = 0;
348 * \fn void Threads_Sleep()
349 * \brief Take the current process off the run queue
353 tThread *cur = Proc_GetCurThread();
356 Log("Proc_Sleep: %i going to sleep", cur->TID);
359 LOCK( &giThreadListLock );
361 // Get thread before current thread
362 thread = Threads_int_GetPrev( &gActiveThreads, cur );
364 Warning("Proc_Sleep - Current thread is not on the active queue");
368 // Don't sleep if there is a message waiting
369 if( cur->Messages ) {
370 RELEASE( &giThreadListLock );
374 // Unset remaining timeslices (force a task switch on timer fire)
377 // Remove from active list
378 thread->Next = cur->Next;
380 // Add to Sleeping List (at the top)
381 cur->Next = gSleepingThreads;
382 gSleepingThreads = cur;
384 // Reduce the active count & ticket count
385 giNumActiveThreads --;
386 giTotalTickets -= cur->NumTickets;
388 // Mark thread as sleeping
389 cur->Status = THREAD_STAT_SLEEPING;
392 RELEASE( &giThreadListLock );
399 * \fn void Threads_Wake( tThread *Thread )
400 * \brief Wakes a sleeping/waiting thread up
402 void Threads_Wake(tThread *Thread)
405 switch(Thread->Status)
407 case THREAD_STAT_ACTIVE: break;
408 case THREAD_STAT_SLEEPING:
409 LOCK( &giThreadListLock );
410 prev = Threads_int_GetPrev(&gSleepingThreads, Thread);
411 prev->Next = Thread->Next; // Remove from sleeping queue
412 Thread->Next = gActiveThreads; // Add to active queue
413 gActiveThreads = Thread;
414 Thread->Status = THREAD_STAT_ACTIVE;
415 RELEASE( &giThreadListLock );
417 case THREAD_STAT_WAITING:
418 Warning("Thread_Wake - Waiting threads are not currently supported");
420 case THREAD_STAT_DEAD:
421 Warning("Thread_Wake - Attempt to wake dead thread (%i)", Thread->TID);
424 Warning("Thread_Wake - Unknown process status (%i)\n", Thread->Status);
430 * \fn void Threads_AddActive(tThread *Thread)
431 * \brief Adds a thread to the active queue
433 void Threads_AddActive(tThread *Thread)
435 LOCK( &giThreadListLock );
436 Thread->Next = gActiveThreads;
437 gActiveThreads = Thread;
438 giNumActiveThreads ++;
439 giTotalTickets += Thread->NumTickets;
440 //Log("Threads_AddActive: giNumActiveThreads = %i, giTotalTickets = %i",
441 // giNumActiveThreads, giTotalTickets);
442 RELEASE( &giThreadListLock );
447 * \fn void Threads_SetSignalHandler(int Num, void *Handler)
448 * \brief Sets the signal handler for a signal
450 void Threads_SetSignalHandler(int Num, void *Handler)
452 if(Num < 0 || Num >= NSIG) return;
454 gCurrentThread->SignalHandlers[Num] = Handler;
458 * \fn void Threads_SendSignal(int TID, int Num)
459 * \brief Send a signal to a thread
461 void Threads_SendSignal(int TID, int Num)
463 tThread *thread = Proc_GetThread(TID);
468 handler = thread->SignalHandlers[Num];
471 if(handler == SIG_ERR) {
481 if(handler == -2) return;
483 // Check the type and handle if the thread is already in a signal
484 if(thread->CurSignal != 0) {
485 if(Num < _SIGTYPE_FATAL)
488 while(thread->CurSignal != 0)
497 // --- Process Structure Access Functions ---
500 return Proc_GetCurThread()->TGID;
504 return Proc_GetCurThread()->TID;
508 return Proc_GetCurThread()->UID;
512 return Proc_GetCurThread()->GID;
516 * \fn void Threads_Dump()
517 * \brief Dums a list of currently running threads
523 Log("Active Threads:");
524 for(thread=gActiveThreads;thread;thread=thread->Next)
526 Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName);
527 Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum);
528 Log(" KStack 0x%x", thread->KernelStack);
530 Log("Sleeping Threads:");
531 for(thread=gSleepingThreads;thread;thread=thread->Next)
533 Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName);
534 Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum);
535 Log(" KStack 0x%x", thread->KernelStack);
540 * \fn tThread *Threads_GetNextToRun(int CPU)
541 * \brief Gets the next thread to run
543 tThread *Threads_GetNextToRun(int CPU)
549 // Special case: 1 thread
550 if(giNumActiveThreads == 1)
552 return gActiveThreads;
555 //Log(" Threads_GetNextToRun: giNumActiveThreads=%i,giTotalTickets=%i",
556 // giNumActiveThreads, giTotalTickets);
557 // Get the ticket number
558 ticket = number = rand() % giTotalTickets;
560 //Log(" Threads_GetNextToRun: ticket = %i", ticket);
562 // Find the next thread
563 for(thread=gActiveThreads;thread;thread=thread->Next)
565 if(thread->NumTickets > number) break;
566 number -= thread->NumTickets;
573 for(thread=gActiveThreads;thread;thread=thread->Next)
574 number += thread->NumTickets;
575 Panic("Bookeeping Failed - giTotalTicketCount (%i) != true count (%i)",
576 giTotalTickets, number);
583 * \fn void Threads_SegFault(tVAddr Addr)
584 * \brief Called when a Segment Fault occurs
586 void Threads_SegFault(tVAddr Addr)
588 //Threads_SendSignal( Proc_GetCurThread()->TID, SIGSEGV );
589 Threads_Kill( Proc_GetCurThread(), 0 );