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
96 for(;;) __asm__ __volatile__ ("hlt"); // Just yeilds
104 * \fn void Threads_SetName(char *NewName)
105 * \brief Sets the current thread's name
107 void Threads_SetName(char *NewName)
109 tThread *cur = Proc_GetCurThread();
110 if( IsHeap(cur->ThreadName) )
111 free( cur->ThreadName );
112 cur->ThreadName = malloc(strlen(NewName)+1);
113 strcpy(cur->ThreadName, NewName);
117 * \fn void Threads_SetTickets(int Num)
118 * \brief Sets the 'priority' of a task
120 void Threads_SetTickets(int Num)
122 tThread *cur = Proc_GetCurThread();
124 if(Num > MAX_TICKETS) Num = MAX_TICKETS;
126 LOCK( &giThreadListLock );
127 giTotalTickets -= cur->NumTickets;
128 cur->NumTickets = Num;
129 giTotalTickets += Num;
130 //LOG("giTotalTickets = %i", giTotalTickets);
131 RELEASE( &giThreadListLock );
135 * \fn void Threads_WaitTID(int TID, int *status)
136 * \brief Wait for a task to change state
138 int Threads_WaitTID(int TID, int *status)
148 // Any peer/child thread
161 tThread *t = Threads_GetThread(TID);
162 int initStatus = t->Status;
164 while(t->Status == initStatus) Threads_Yield();
168 case THREAD_STAT_ZOMBIE:
169 t->Status = THREAD_STAT_DEAD;
170 if(status) *status = 0;
171 Threads_AddToDelete( t );
174 if(status) *status = -1;
184 * \fn tThread *Threads_GetThread(Uint TID)
185 * \brief Gets a thread given its TID
187 tThread *Threads_GetThread(Uint TID)
191 // Search Active List
192 for(thread = gActiveThreads;
194 thread = thread->Next)
196 if(thread->TID == TID)
200 // Search Sleeping List
201 for(thread = gSleepingThreads;
203 thread = thread->Next)
205 if(thread->TID == TID)
213 * \fn void Threads_AddToDelete(tThread *Thread)
214 * \brief Adds a thread to the delete queue
216 void Threads_AddToDelete(tThread *Thread)
218 // Add to delete queue
220 Thread->Next = gDeleteThreads;
221 gDeleteThreads = Thread;
224 gDeleteThreads = Thread;
229 * \fn tThread *Threads_int_GetPrev(tThread *List, tThread *Thread)
230 * \brief Gets the previous entry in a thead linked list
232 tThread *Threads_int_GetPrev(tThread **List, tThread *Thread)
236 if(*List == Thread) {
237 return (tThread*)List;
240 ret->Next && ret->Next != Thread;
243 // Error if the thread is not on the list
244 if(!ret->Next || ret->Next != Thread) {
252 * \fn void Threads_Exit(int TID, int Status)
253 * \brief Exit the current process
255 void Threads_Exit(int TID, int Status)
257 Threads_Kill( Proc_GetCurThread(), (Uint)Status & 0xFF );
261 * \fn void Threads_Kill(tThread *Thread, int Status)
262 * \brief Kill a thread
263 * \param TID Thread ID (0 for current)
265 void Threads_Kill(tThread *Thread, int Status)
274 for(child = gActiveThreads;
278 if(child->PTID == gCurrentThread->TID)
279 Threads_Kill(child, -1);
284 ///\note Double lock is needed due to overlap of locks
286 // Lock thread (stop us recieving messages)
287 LOCK( &Thread->IsLocked );
290 LOCK( &giThreadListLock );
292 // Get previous thread on list
293 prev = Threads_int_GetPrev( &gActiveThreads, Thread );
295 Warning("Proc_Exit - Current thread is not on the active queue");
299 // Clear Message Queue
300 while( Thread->Messages )
302 msg = Thread->Messages->Next;
303 free( Thread->Messages );
304 Thread->Messages = msg;
307 Thread->Remaining = 0; // Clear Remaining Quantum
308 Thread->Quantum = 0; // Clear Quantum to indicate dead thread
309 prev->Next = Thread->Next; // Remove from active
311 giNumActiveThreads --;
312 giTotalTickets -= Thread->NumTickets;
314 // Mark thread as a zombie
315 Thread->RetStatus = Status;
317 // Don't Zombie if we are being killed as part of a tree
320 Thread->Status = THREAD_STAT_DEAD;
321 Threads_AddToDelete( Thread );
323 Thread->Status = THREAD_STAT_ZOMBIE;
327 RELEASE( &Thread->IsLocked ); // Released first so that it IS released
328 RELEASE( &giThreadListLock );
329 if(Status != -1) HALT();
333 * \fn void Threads_Yield()
334 * \brief Yield remainder of timeslice
338 Proc_GetCurThread()->Remaining = 0;
343 * \fn void Threads_Sleep()
344 * \brief Take the current process off the run queue
348 tThread *cur = Proc_GetCurThread();
351 //Log("Proc_Sleep: %i going to sleep", gCurrentThread->TID);
354 LOCK( &giThreadListLock );
356 // Get thread before current thread
357 thread = Threads_int_GetPrev( &gActiveThreads, cur );
359 Warning("Proc_Sleep - Current thread is not on the active queue");
363 // Don't sleep if there is a message waiting
364 if( cur->Messages ) {
365 RELEASE( &giThreadListLock );
369 // Unset remaining timeslices (force a task switch on timer fire)
372 // Remove from active list
373 thread->Next = cur->Next;
375 // Add to Sleeping List (at the top)
376 cur->Next = gSleepingThreads;
377 gSleepingThreads = cur;
379 // Reduce the active count & ticket count
380 giNumActiveThreads --;
381 giTotalTickets -= cur->NumTickets;
383 // Mark thread as sleeping
384 cur->Status = THREAD_STAT_SLEEPING;
387 RELEASE( &giThreadListLock );
394 * \fn void Threads_Wake( tThread *Thread )
395 * \brief Wakes a sleeping/waiting thread up
397 void Threads_Wake(tThread *Thread)
400 switch(Thread->Status)
402 case THREAD_STAT_ACTIVE: break;
403 case THREAD_STAT_SLEEPING:
404 LOCK( &giThreadListLock );
405 prev = Threads_int_GetPrev(&gSleepingThreads, Thread);
406 prev->Next = Thread->Next; // Remove from sleeping queue
407 Thread->Next = gActiveThreads; // Add to active queue
408 gActiveThreads = Thread;
409 Thread->Status = THREAD_STAT_ACTIVE;
410 RELEASE( &giThreadListLock );
412 case THREAD_STAT_WAITING:
413 Warning("Thread_Wake - Waiting threads are not currently supported");
415 case THREAD_STAT_DEAD:
416 Warning("Thread_Wake - Attempt to wake dead thread (%i)", Thread->TID);
419 Warning("Thread_Wake - Unknown process status (%i)\n", Thread->Status);
426 * \fn void Threads_SetSignalHandler(int Num, void *Handler)
427 * \brief Sets the signal handler for a signal
429 void Threads_SetSignalHandler(int Num, void *Handler)
431 if(Num < 0 || Num >= NSIG) return;
433 gCurrentThread->SignalHandlers[Num] = Handler;
437 * \fn void Threads_SendSignal(int TID, int Num)
439 void Threads_SendSignal(int TID, int Num)
441 tThread *thread = Proc_GetThread(TID);
446 handler = thread->SignalHandlers[Num];
449 if(handler == SIG_ERR) {
459 if(handler == -2) return;
461 // Check the type and handle if the thread is already in a signal
462 if(thread->CurSignal != 0) {
463 if(Num < _SIGTYPE_FATAL)
466 while(thread->CurSignal != 0)
475 // --- Process Structure Access Functions ---
478 return Proc_GetCurThread()->TGID;
482 return Proc_GetCurThread()->TID;
486 return Proc_GetCurThread()->UID;
490 return Proc_GetCurThread()->GID;
494 * \fn void Threads_Dump()
495 * \brief Dums a list of currently running threads
501 Log("Active Threads:");
502 for(thread=gActiveThreads;thread;thread=thread->Next)
504 Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName);
505 Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum);
506 Log(" KStack 0x%x", thread->KernelStack);
508 Log("Sleeping Threads:");
509 for(thread=gSleepingThreads;thread;thread=thread->Next)
511 Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName);
512 Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum);
513 Log(" KStack 0x%x", thread->KernelStack);
518 * \fn tThread *Threads_GetNextToRun(int CPU)
519 * \brief Gets the next thread to run
521 tThread *Threads_GetNextToRun(int CPU)
527 // Special case: 1 thread
528 if(giNumActiveThreads == 1)
530 return gActiveThreads;
533 // Get the ticket number
534 ticket = number = rand() % giTotalTickets;
536 // Find the next thread
537 for(thread=gActiveThreads;thread;thread=thread->Next)
539 if(thread->NumTickets > number) break;
540 number -= thread->NumTickets;
547 for(thread=gActiveThreads;thread;thread=thread->Next)
548 number += thread->NumTickets;
549 Panic("Bookeeping Failed - giTotalTicketCount (%i) != true count (%i)",
550 giTotalTickets, number);