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)
259 Threads_Kill( Proc_GetCurThread(), (Uint)Status & 0xFF );
263 * \fn void Threads_Kill(tThread *Thread, int Status)
264 * \brief Kill a thread
265 * \param TID Thread ID (0 for current)
267 void Threads_Kill(tThread *Thread, int Status)
276 for(child = gActiveThreads;
280 if(child->PTID == gCurrentThread->TID)
281 Threads_Kill(child, -1);
286 ///\note Double lock is needed due to overlap of locks
288 // Lock thread (stop us recieving messages)
289 LOCK( &Thread->IsLocked );
292 LOCK( &giThreadListLock );
294 // Get previous thread on list
295 prev = Threads_int_GetPrev( &gActiveThreads, Thread );
297 Warning("Proc_Exit - Current thread is not on the active queue");
301 // Clear Message Queue
302 while( Thread->Messages )
304 msg = Thread->Messages->Next;
305 free( Thread->Messages );
306 Thread->Messages = msg;
309 Thread->Remaining = 0; // Clear Remaining Quantum
310 Thread->Quantum = 0; // Clear Quantum to indicate dead thread
311 prev->Next = Thread->Next; // Remove from active
313 giNumActiveThreads --;
314 giTotalTickets -= Thread->NumTickets;
316 // Mark thread as a zombie
317 Thread->RetStatus = Status;
319 // Don't Zombie if we are being killed as part of a tree
322 Thread->Status = THREAD_STAT_DEAD;
323 Threads_AddToDelete( Thread );
325 Thread->Status = THREAD_STAT_ZOMBIE;
329 RELEASE( &Thread->IsLocked ); // Released first so that it IS released
330 RELEASE( &giThreadListLock );
331 if(Status != -1) HALT();
335 * \fn void Threads_Yield()
336 * \brief Yield remainder of timeslice
340 Proc_GetCurThread()->Remaining = 0;
345 * \fn void Threads_Sleep()
346 * \brief Take the current process off the run queue
350 tThread *cur = Proc_GetCurThread();
353 Log("Proc_Sleep: %i going to sleep", cur->TID);
356 LOCK( &giThreadListLock );
358 // Get thread before current thread
359 thread = Threads_int_GetPrev( &gActiveThreads, cur );
361 Warning("Proc_Sleep - Current thread is not on the active queue");
365 // Don't sleep if there is a message waiting
366 if( cur->Messages ) {
367 RELEASE( &giThreadListLock );
371 // Unset remaining timeslices (force a task switch on timer fire)
374 // Remove from active list
375 thread->Next = cur->Next;
377 // Add to Sleeping List (at the top)
378 cur->Next = gSleepingThreads;
379 gSleepingThreads = cur;
381 // Reduce the active count & ticket count
382 giNumActiveThreads --;
383 giTotalTickets -= cur->NumTickets;
385 // Mark thread as sleeping
386 cur->Status = THREAD_STAT_SLEEPING;
389 RELEASE( &giThreadListLock );
396 * \fn void Threads_Wake( tThread *Thread )
397 * \brief Wakes a sleeping/waiting thread up
399 void Threads_Wake(tThread *Thread)
402 switch(Thread->Status)
404 case THREAD_STAT_ACTIVE: break;
405 case THREAD_STAT_SLEEPING:
406 LOCK( &giThreadListLock );
407 prev = Threads_int_GetPrev(&gSleepingThreads, Thread);
408 prev->Next = Thread->Next; // Remove from sleeping queue
409 Thread->Next = gActiveThreads; // Add to active queue
410 gActiveThreads = Thread;
411 Thread->Status = THREAD_STAT_ACTIVE;
412 RELEASE( &giThreadListLock );
414 case THREAD_STAT_WAITING:
415 Warning("Thread_Wake - Waiting threads are not currently supported");
417 case THREAD_STAT_DEAD:
418 Warning("Thread_Wake - Attempt to wake dead thread (%i)", Thread->TID);
421 Warning("Thread_Wake - Unknown process status (%i)\n", Thread->Status);
427 * \fn void Threads_AddActive(tThread *Thread)
428 * \brief Adds a thread to the active queue
430 void Threads_AddActive(tThread *Thread)
432 LOCK( &giThreadListLock );
433 Thread->Next = gActiveThreads;
434 gActiveThreads = Thread;
435 giNumActiveThreads ++;
436 giTotalTickets += Thread->NumTickets;
437 //Log("Threads_AddActive: giNumActiveThreads = %i, giTotalTickets = %i",
438 // giNumActiveThreads, giTotalTickets);
439 RELEASE( &giThreadListLock );
444 * \fn void Threads_SetSignalHandler(int Num, void *Handler)
445 * \brief Sets the signal handler for a signal
447 void Threads_SetSignalHandler(int Num, void *Handler)
449 if(Num < 0 || Num >= NSIG) return;
451 gCurrentThread->SignalHandlers[Num] = Handler;
455 * \fn void Threads_SendSignal(int TID, int Num)
457 void Threads_SendSignal(int TID, int Num)
459 tThread *thread = Proc_GetThread(TID);
464 handler = thread->SignalHandlers[Num];
467 if(handler == SIG_ERR) {
477 if(handler == -2) return;
479 // Check the type and handle if the thread is already in a signal
480 if(thread->CurSignal != 0) {
481 if(Num < _SIGTYPE_FATAL)
484 while(thread->CurSignal != 0)
493 // --- Process Structure Access Functions ---
496 return Proc_GetCurThread()->TGID;
500 return Proc_GetCurThread()->TID;
504 return Proc_GetCurThread()->UID;
508 return Proc_GetCurThread()->GID;
512 * \fn void Threads_Dump()
513 * \brief Dums a list of currently running threads
519 Log("Active Threads:");
520 for(thread=gActiveThreads;thread;thread=thread->Next)
522 Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName);
523 Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum);
524 Log(" KStack 0x%x", thread->KernelStack);
526 Log("Sleeping Threads:");
527 for(thread=gSleepingThreads;thread;thread=thread->Next)
529 Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName);
530 Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum);
531 Log(" KStack 0x%x", thread->KernelStack);
536 * \fn tThread *Threads_GetNextToRun(int CPU)
537 * \brief Gets the next thread to run
539 tThread *Threads_GetNextToRun(int CPU)
545 // Special case: 1 thread
546 if(giNumActiveThreads == 1)
548 return gActiveThreads;
551 //Log(" Threads_GetNextToRun: giNumActiveThreads=%i,giTotalTickets=%i",
552 // giNumActiveThreads, giTotalTickets);
553 // Get the ticket number
554 ticket = number = rand() % giTotalTickets;
556 //Log(" Threads_GetNextToRun: ticket = %i", ticket);
558 // Find the next thread
559 for(thread=gActiveThreads;thread;thread=thread->Next)
561 if(thread->NumTickets > number) break;
562 number -= thread->NumTickets;
569 for(thread=gActiveThreads;thread;thread=thread->Next)
570 number += thread->NumTickets;
571 Panic("Bookeeping Failed - giTotalTicketCount (%i) != true count (%i)",
572 giTotalTickets, number);