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
99 __asm__ __volatile__ ("hlt"); // 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)
152 // Any peer/child thread
165 tThread *t = Threads_GetThread(TID);
166 int initStatus = t->Status;
168 while(t->Status == initStatus) Threads_Yield();
172 case THREAD_STAT_ZOMBIE:
173 t->Status = THREAD_STAT_DEAD;
174 if(status) *status = 0;
175 Threads_AddToDelete( t );
178 if(status) *status = -1;
188 * \fn tThread *Threads_GetThread(Uint TID)
189 * \brief Gets a thread given its TID
191 tThread *Threads_GetThread(Uint TID)
195 // Search Active List
196 for(thread = gActiveThreads;
198 thread = thread->Next)
200 if(thread->TID == TID)
204 // Search Sleeping List
205 for(thread = gSleepingThreads;
207 thread = thread->Next)
209 if(thread->TID == TID)
217 * \fn void Threads_AddToDelete(tThread *Thread)
218 * \brief Adds a thread to the delete queue
220 void Threads_AddToDelete(tThread *Thread)
222 // Add to delete queue
224 Thread->Next = gDeleteThreads;
225 gDeleteThreads = Thread;
228 gDeleteThreads = Thread;
233 * \fn tThread *Threads_int_GetPrev(tThread *List, tThread *Thread)
234 * \brief Gets the previous entry in a thead linked list
236 tThread *Threads_int_GetPrev(tThread **List, tThread *Thread)
240 if(*List == Thread) {
241 return (tThread*)List;
244 ret->Next && ret->Next != Thread;
247 // Error if the thread is not on the list
248 if(!ret->Next || ret->Next != Thread) {
256 * \fn void Threads_Exit(int TID, int Status)
257 * \brief Exit the current process
259 void Threads_Exit(int TID, int Status)
261 Threads_Kill( Proc_GetCurThread(), (Uint)Status & 0xFF );
265 * \fn void Threads_Kill(tThread *Thread, int Status)
266 * \brief Kill a thread
267 * \param TID Thread ID (0 for current)
269 void Threads_Kill(tThread *Thread, int Status)
278 for(child = gActiveThreads;
282 if(child->PTID == gCurrentThread->TID)
283 Threads_Kill(child, -1);
288 ///\note Double lock is needed due to overlap of locks
290 // Lock thread (stop us recieving messages)
291 LOCK( &Thread->IsLocked );
294 LOCK( &giThreadListLock );
296 // Get previous thread on list
297 prev = Threads_int_GetPrev( &gActiveThreads, Thread );
299 Warning("Proc_Exit - Current thread is not on the active queue");
303 // Clear Message Queue
304 while( Thread->Messages )
306 msg = Thread->Messages->Next;
307 free( Thread->Messages );
308 Thread->Messages = msg;
311 Thread->Remaining = 0; // Clear Remaining Quantum
312 Thread->Quantum = 0; // Clear Quantum to indicate dead thread
313 prev->Next = Thread->Next; // Remove from active
315 giNumActiveThreads --;
316 giTotalTickets -= Thread->NumTickets;
318 // Mark thread as a zombie
319 Thread->RetStatus = Status;
321 // Don't Zombie if we are being killed as part of a tree
324 Thread->Status = THREAD_STAT_DEAD;
325 Threads_AddToDelete( Thread );
327 Thread->Status = THREAD_STAT_ZOMBIE;
331 RELEASE( &Thread->IsLocked ); // Released first so that it IS released
332 RELEASE( &giThreadListLock );
333 if(Status != -1) HALT();
337 * \fn void Threads_Yield()
338 * \brief Yield remainder of timeslice
342 Proc_GetCurThread()->Remaining = 0;
347 * \fn void Threads_Sleep()
348 * \brief Take the current process off the run queue
352 tThread *cur = Proc_GetCurThread();
355 Log("Proc_Sleep: %i going to sleep", cur->TID);
358 LOCK( &giThreadListLock );
360 // Get thread before current thread
361 thread = Threads_int_GetPrev( &gActiveThreads, cur );
363 Warning("Proc_Sleep - Current thread is not on the active queue");
367 // Don't sleep if there is a message waiting
368 if( cur->Messages ) {
369 RELEASE( &giThreadListLock );
373 // Unset remaining timeslices (force a task switch on timer fire)
376 // Remove from active list
377 thread->Next = cur->Next;
379 // Add to Sleeping List (at the top)
380 cur->Next = gSleepingThreads;
381 gSleepingThreads = cur;
383 // Reduce the active count & ticket count
384 giNumActiveThreads --;
385 giTotalTickets -= cur->NumTickets;
387 // Mark thread as sleeping
388 cur->Status = THREAD_STAT_SLEEPING;
391 RELEASE( &giThreadListLock );
398 * \fn void Threads_Wake( tThread *Thread )
399 * \brief Wakes a sleeping/waiting thread up
401 void Threads_Wake(tThread *Thread)
404 switch(Thread->Status)
406 case THREAD_STAT_ACTIVE: break;
407 case THREAD_STAT_SLEEPING:
408 LOCK( &giThreadListLock );
409 prev = Threads_int_GetPrev(&gSleepingThreads, Thread);
410 prev->Next = Thread->Next; // Remove from sleeping queue
411 Thread->Next = gActiveThreads; // Add to active queue
412 gActiveThreads = Thread;
413 Thread->Status = THREAD_STAT_ACTIVE;
414 RELEASE( &giThreadListLock );
416 case THREAD_STAT_WAITING:
417 Warning("Thread_Wake - Waiting threads are not currently supported");
419 case THREAD_STAT_DEAD:
420 Warning("Thread_Wake - Attempt to wake dead thread (%i)", Thread->TID);
423 Warning("Thread_Wake - Unknown process status (%i)\n", Thread->Status);
430 * \fn void Threads_SetSignalHandler(int Num, void *Handler)
431 * \brief Sets the signal handler for a signal
433 void Threads_SetSignalHandler(int Num, void *Handler)
435 if(Num < 0 || Num >= NSIG) return;
437 gCurrentThread->SignalHandlers[Num] = Handler;
441 * \fn void Threads_SendSignal(int TID, int Num)
443 void Threads_SendSignal(int TID, int Num)
445 tThread *thread = Proc_GetThread(TID);
450 handler = thread->SignalHandlers[Num];
453 if(handler == SIG_ERR) {
463 if(handler == -2) return;
465 // Check the type and handle if the thread is already in a signal
466 if(thread->CurSignal != 0) {
467 if(Num < _SIGTYPE_FATAL)
470 while(thread->CurSignal != 0)
479 // --- Process Structure Access Functions ---
482 return Proc_GetCurThread()->TGID;
486 return Proc_GetCurThread()->TID;
490 return Proc_GetCurThread()->UID;
494 return Proc_GetCurThread()->GID;
498 * \fn void Threads_Dump()
499 * \brief Dums a list of currently running threads
505 Log("Active Threads:");
506 for(thread=gActiveThreads;thread;thread=thread->Next)
508 Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName);
509 Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum);
510 Log(" KStack 0x%x", thread->KernelStack);
512 Log("Sleeping Threads:");
513 for(thread=gSleepingThreads;thread;thread=thread->Next)
515 Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName);
516 Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum);
517 Log(" KStack 0x%x", thread->KernelStack);
522 * \fn tThread *Threads_GetNextToRun(int CPU)
523 * \brief Gets the next thread to run
525 tThread *Threads_GetNextToRun(int CPU)
531 // Special case: 1 thread
532 if(giNumActiveThreads == 1)
534 return gActiveThreads;
537 // Get the ticket number
538 ticket = number = rand() % giTotalTickets;
540 // Find the next thread
541 for(thread=gActiveThreads;thread;thread=thread->Next)
543 if(thread->NumTickets > number) break;
544 number -= thread->NumTickets;
551 for(thread=gActiveThreads;thread;thread=thread->Next)
552 number += thread->NumTickets;
553 Panic("Bookeeping Failed - giTotalTicketCount (%i) != true count (%i)",
554 giTotalTickets, number);