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
98 __asm__ __volatile__ ("hlt"); // Just yeilds
107 * \fn void Threads_SetName(char *NewName)
108 * \brief Sets the current thread's name
110 void Threads_SetName(char *NewName)
112 tThread *cur = Proc_GetCurThread();
113 if( IsHeap(cur->ThreadName) )
114 free( cur->ThreadName );
115 cur->ThreadName = malloc(strlen(NewName)+1);
116 strcpy(cur->ThreadName, NewName);
120 * \fn void Threads_SetTickets(int Num)
121 * \brief Sets the 'priority' of a task
123 void Threads_SetTickets(int Num)
125 tThread *cur = Proc_GetCurThread();
127 if(Num > MAX_TICKETS) Num = MAX_TICKETS;
129 LOCK( &giThreadListLock );
130 giTotalTickets -= cur->NumTickets;
131 cur->NumTickets = Num;
132 giTotalTickets += Num;
133 //LOG("giTotalTickets = %i", giTotalTickets);
134 RELEASE( &giThreadListLock );
138 * \fn void Threads_WaitTID(int TID, int *status)
139 * \brief Wait for a task to change state
141 int Threads_WaitTID(int TID, int *status)
151 // Any peer/child thread
164 tThread *t = Threads_GetThread(TID);
165 int initStatus = t->Status;
167 while(t->Status == initStatus) Threads_Yield();
171 case THREAD_STAT_ZOMBIE:
172 t->Status = THREAD_STAT_DEAD;
173 if(status) *status = 0;
174 Threads_AddToDelete( t );
177 if(status) *status = -1;
187 * \fn tThread *Threads_GetThread(Uint TID)
188 * \brief Gets a thread given its TID
190 tThread *Threads_GetThread(Uint TID)
194 // Search Active List
195 for(thread = gActiveThreads;
197 thread = thread->Next)
199 if(thread->TID == TID)
203 // Search Sleeping List
204 for(thread = gSleepingThreads;
206 thread = thread->Next)
208 if(thread->TID == TID)
216 * \fn void Threads_AddToDelete(tThread *Thread)
217 * \brief Adds a thread to the delete queue
219 void Threads_AddToDelete(tThread *Thread)
221 // Add to delete queue
223 Thread->Next = gDeleteThreads;
224 gDeleteThreads = Thread;
227 gDeleteThreads = Thread;
232 * \fn tThread *Threads_int_GetPrev(tThread *List, tThread *Thread)
233 * \brief Gets the previous entry in a thead linked list
235 tThread *Threads_int_GetPrev(tThread **List, tThread *Thread)
239 if(*List == Thread) {
240 return (tThread*)List;
243 ret->Next && ret->Next != Thread;
246 // Error if the thread is not on the list
247 if(!ret->Next || ret->Next != Thread) {
255 * \fn void Threads_Exit(int TID, int Status)
256 * \brief Exit the current process
258 void Threads_Exit(int TID, int Status)
260 Threads_Kill( Proc_GetCurThread(), (Uint)Status & 0xFF );
264 * \fn void Threads_Kill(tThread *Thread, int Status)
265 * \brief Kill a thread
266 * \param TID Thread ID (0 for current)
268 void Threads_Kill(tThread *Thread, int Status)
277 for(child = gActiveThreads;
281 if(child->PTID == gCurrentThread->TID)
282 Threads_Kill(child, -1);
287 ///\note Double lock is needed due to overlap of locks
289 // Lock thread (stop us recieving messages)
290 LOCK( &Thread->IsLocked );
293 LOCK( &giThreadListLock );
295 // Get previous thread on list
296 prev = Threads_int_GetPrev( &gActiveThreads, Thread );
298 Warning("Proc_Exit - Current thread is not on the active queue");
302 // Clear Message Queue
303 while( Thread->Messages )
305 msg = Thread->Messages->Next;
306 free( Thread->Messages );
307 Thread->Messages = msg;
310 Thread->Remaining = 0; // Clear Remaining Quantum
311 Thread->Quantum = 0; // Clear Quantum to indicate dead thread
312 prev->Next = Thread->Next; // Remove from active
314 giNumActiveThreads --;
315 giTotalTickets -= Thread->NumTickets;
317 // Mark thread as a zombie
318 Thread->RetStatus = Status;
320 // Don't Zombie if we are being killed as part of a tree
323 Thread->Status = THREAD_STAT_DEAD;
324 Threads_AddToDelete( Thread );
326 Thread->Status = THREAD_STAT_ZOMBIE;
330 RELEASE( &Thread->IsLocked ); // Released first so that it IS released
331 RELEASE( &giThreadListLock );
332 if(Status != -1) HALT();
336 * \fn void Threads_Yield()
337 * \brief Yield remainder of timeslice
341 Proc_GetCurThread()->Remaining = 0;
346 * \fn void Threads_Sleep()
347 * \brief Take the current process off the run queue
351 tThread *cur = Proc_GetCurThread();
354 //Log("Proc_Sleep: %i going to sleep", gCurrentThread->TID);
357 LOCK( &giThreadListLock );
359 // Get thread before current thread
360 thread = Threads_int_GetPrev( &gActiveThreads, cur );
362 Warning("Proc_Sleep - Current thread is not on the active queue");
366 // Don't sleep if there is a message waiting
367 if( cur->Messages ) {
368 RELEASE( &giThreadListLock );
372 // Unset remaining timeslices (force a task switch on timer fire)
375 // Remove from active list
376 thread->Next = cur->Next;
378 // Add to Sleeping List (at the top)
379 cur->Next = gSleepingThreads;
380 gSleepingThreads = cur;
382 // Reduce the active count & ticket count
383 giNumActiveThreads --;
384 giTotalTickets -= cur->NumTickets;
386 // Mark thread as sleeping
387 cur->Status = THREAD_STAT_SLEEPING;
390 RELEASE( &giThreadListLock );
397 * \fn void Threads_Wake( tThread *Thread )
398 * \brief Wakes a sleeping/waiting thread up
400 void Threads_Wake(tThread *Thread)
403 switch(Thread->Status)
405 case THREAD_STAT_ACTIVE: break;
406 case THREAD_STAT_SLEEPING:
407 LOCK( &giThreadListLock );
408 prev = Threads_int_GetPrev(&gSleepingThreads, Thread);
409 prev->Next = Thread->Next; // Remove from sleeping queue
410 Thread->Next = gActiveThreads; // Add to active queue
411 gActiveThreads = Thread;
412 Thread->Status = THREAD_STAT_ACTIVE;
413 RELEASE( &giThreadListLock );
415 case THREAD_STAT_WAITING:
416 Warning("Thread_Wake - Waiting threads are not currently supported");
418 case THREAD_STAT_DEAD:
419 Warning("Thread_Wake - Attempt to wake dead thread (%i)", Thread->TID);
422 Warning("Thread_Wake - Unknown process status (%i)\n", Thread->Status);
429 * \fn void Threads_SetSignalHandler(int Num, void *Handler)
430 * \brief Sets the signal handler for a signal
432 void Threads_SetSignalHandler(int Num, void *Handler)
434 if(Num < 0 || Num >= NSIG) return;
436 gCurrentThread->SignalHandlers[Num] = Handler;
440 * \fn void Threads_SendSignal(int TID, int Num)
442 void Threads_SendSignal(int TID, int Num)
444 tThread *thread = Proc_GetThread(TID);
449 handler = thread->SignalHandlers[Num];
452 if(handler == SIG_ERR) {
462 if(handler == -2) return;
464 // Check the type and handle if the thread is already in a signal
465 if(thread->CurSignal != 0) {
466 if(Num < _SIGTYPE_FATAL)
469 while(thread->CurSignal != 0)
478 // --- Process Structure Access Functions ---
481 return Proc_GetCurThread()->TGID;
485 return Proc_GetCurThread()->TID;
489 return Proc_GetCurThread()->UID;
493 return Proc_GetCurThread()->GID;
497 * \fn void Threads_Dump()
498 * \brief Dums a list of currently running threads
504 Log("Active Threads:");
505 for(thread=gActiveThreads;thread;thread=thread->Next)
507 Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName);
508 Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum);
509 Log(" KStack 0x%x", thread->KernelStack);
511 Log("Sleeping Threads:");
512 for(thread=gSleepingThreads;thread;thread=thread->Next)
514 Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName);
515 Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum);
516 Log(" KStack 0x%x", thread->KernelStack);
521 * \fn tThread *Threads_GetNextToRun(int CPU)
522 * \brief Gets the next thread to run
524 tThread *Threads_GetNextToRun(int CPU)
530 // Special case: 1 thread
531 if(giNumActiveThreads == 1)
533 return gActiveThreads;
536 // Get the ticket number
537 ticket = number = rand() % giTotalTickets;
539 // Find the next thread
540 for(thread=gActiveThreads;thread;thread=thread->Next)
542 if(thread->NumTickets > number) break;
543 number -= thread->NumTickets;
550 for(thread=gActiveThreads;thread;thread=thread->Next)
551 number += thread->NumTickets;
552 Panic("Bookeeping Failed - giTotalTicketCount (%i) != true count (%i)",
553 giTotalTickets, number);