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);
41 tThread gThreadZero = {
42 NULL, 0, // Next, Lock
43 THREAD_STAT_ACTIVE, // Status
47 0, // Parent Thread ID
54 0, {0}, {0}, // Signal State
56 NULL, NULL, // Messages, Last Message
57 DEFAULT_QUANTUM, DEFAULT_QUANTUM, // Quantum, Remaining
59 {0} // Default config to zero
63 volatile int giThreadListLock = 0; ///\note NEVER use a heap function while locked
64 // --- Current State ---
65 volatile int giNumActiveThreads = 0;
66 volatile int giTotalTickets = 0;
67 volatile Uint giNextTID = 1;
68 // --- Thread Lists ---
69 tThread *gActiveThreads = NULL; // Currently Running Threads
70 tThread *gSleepingThreads = NULL; // Sleeping Threads
71 tThread *gDeleteThreads = NULL; // Threads to delete
76 * \fn void Threads_Init()
77 * \brief Initialse the thread list
83 // Create Initial Task
84 gActiveThreads = &gThreadZero;
85 giTotalTickets = gThreadZero.NumTickets;
86 giNumActiveThreads = 1;
90 if(Proc_Clone(0, 0) == 0)
92 tThread *cur = Proc_GetCurThread();
93 cur->ThreadName = "Idle Thread";
94 Threads_SetTickets(0); // Never called randomly
95 cur->Quantum = 1; // 1 slice quantum
100 HALT(); // Just yeilds
109 * \fn void Threads_SetName(char *NewName)
110 * \brief Sets the current thread's name
112 void Threads_SetName(char *NewName)
114 tThread *cur = Proc_GetCurThread();
115 if( IsHeap(cur->ThreadName) )
116 free( cur->ThreadName );
117 cur->ThreadName = malloc(strlen(NewName)+1);
118 strcpy(cur->ThreadName, NewName);
122 * \fn void Threads_SetTickets(int Num)
123 * \brief Sets the 'priority' of a task
125 void Threads_SetTickets(int Num)
127 tThread *cur = Proc_GetCurThread();
129 if(Num > MAX_TICKETS) Num = MAX_TICKETS;
131 LOCK( &giThreadListLock );
132 giTotalTickets -= cur->NumTickets;
133 cur->NumTickets = Num;
134 giTotalTickets += Num;
135 //LOG("giTotalTickets = %i", giTotalTickets);
136 RELEASE( &giThreadListLock );
140 * \fn void Threads_WaitTID(int TID, int *status)
141 * \brief Wait for a task to change state
143 int Threads_WaitTID(int TID, int *status)
153 // Any peer/child thread
166 tThread *t = Threads_GetThread(TID);
167 int initStatus = t->Status;
169 while(t->Status == initStatus) Threads_Yield();
173 case THREAD_STAT_ZOMBIE:
174 t->Status = THREAD_STAT_DEAD;
175 if(status) *status = 0;
176 Threads_AddToDelete( t );
179 if(status) *status = -1;
189 * \fn tThread *Threads_GetThread(Uint TID)
190 * \brief Gets a thread given its TID
192 tThread *Threads_GetThread(Uint TID)
196 // Search Active List
197 for(thread = gActiveThreads;
199 thread = thread->Next)
201 if(thread->TID == TID)
205 // Search Sleeping List
206 for(thread = gSleepingThreads;
208 thread = thread->Next)
210 if(thread->TID == TID)
218 * \fn void Threads_AddToDelete(tThread *Thread)
219 * \brief Adds a thread to the delete queue
221 void Threads_AddToDelete(tThread *Thread)
223 // Add to delete queue
225 Thread->Next = gDeleteThreads;
226 gDeleteThreads = Thread;
229 gDeleteThreads = Thread;
234 * \fn tThread *Threads_int_GetPrev(tThread *List, tThread *Thread)
235 * \brief Gets the previous entry in a thead linked list
237 tThread *Threads_int_GetPrev(tThread **List, tThread *Thread)
241 if(*List == Thread) {
242 return (tThread*)List;
245 ret->Next && ret->Next != Thread;
248 // Error if the thread is not on the list
249 if(!ret->Next || ret->Next != Thread) {
257 * \fn void Threads_Exit(int TID, int Status)
258 * \brief Exit the current process
260 void Threads_Exit(int TID, int Status)
262 Threads_Kill( Proc_GetCurThread(), (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 == gCurrentThread->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);
431 * \fn void Threads_SetSignalHandler(int Num, void *Handler)
432 * \brief Sets the signal handler for a signal
434 void Threads_SetSignalHandler(int Num, void *Handler)
436 if(Num < 0 || Num >= NSIG) return;
438 gCurrentThread->SignalHandlers[Num] = Handler;
442 * \fn void Threads_SendSignal(int TID, int Num)
444 void Threads_SendSignal(int TID, int Num)
446 tThread *thread = Proc_GetThread(TID);
451 handler = thread->SignalHandlers[Num];
454 if(handler == SIG_ERR) {
464 if(handler == -2) return;
466 // Check the type and handle if the thread is already in a signal
467 if(thread->CurSignal != 0) {
468 if(Num < _SIGTYPE_FATAL)
471 while(thread->CurSignal != 0)
480 // --- Process Structure Access Functions ---
483 return Proc_GetCurThread()->TGID;
487 return Proc_GetCurThread()->TID;
491 return Proc_GetCurThread()->UID;
495 return Proc_GetCurThread()->GID;
499 * \fn void Threads_Dump()
500 * \brief Dums a list of currently running threads
506 Log("Active Threads:");
507 for(thread=gActiveThreads;thread;thread=thread->Next)
509 Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName);
510 Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum);
511 Log(" KStack 0x%x", thread->KernelStack);
513 Log("Sleeping Threads:");
514 for(thread=gSleepingThreads;thread;thread=thread->Next)
516 Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName);
517 Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum);
518 Log(" KStack 0x%x", thread->KernelStack);
523 * \fn tThread *Threads_GetNextToRun(int CPU)
524 * \brief Gets the next thread to run
526 tThread *Threads_GetNextToRun(int CPU)
532 // Special case: 1 thread
533 if(giNumActiveThreads == 1)
535 return gActiveThreads;
538 // Get the ticket number
539 ticket = number = rand() % giTotalTickets;
541 // Find the next thread
542 for(thread=gActiveThreads;thread;thread=thread->Next)
544 if(thread->NumTickets > number) break;
545 number -= thread->NumTickets;
552 for(thread=gActiveThreads;thread;thread=thread->Next)
553 number += thread->NumTickets;
554 Panic("Bookeeping Failed - giTotalTicketCount (%i) != true count (%i)",
555 giTotalTickets, number);