4 * - Common Thread Control
8 #include <threads_int.h>
11 #include <semaphore.h>
14 #define DEBUG_TRACE_TICKETS 0 // Trace ticket counts
15 #define DEBUG_TRACE_STATE 0 // Trace state changes (sleep/wake)
19 #define SCHED_LOTTERY 1 // Lottery scheduler
20 #define SCHED_RR_SIM 2 // Single Queue Round Robin
21 #define SCHED_RR_PRI 3 // Multi Queue Round Robin
23 #define SCHEDULER_TYPE SCHED_RR_PRI
26 #define DEFAULT_QUANTUM 5
27 #define DEFAULT_PRIORITY 5
28 #define MIN_PRIORITY 10
40 void Threads_Init(void);
42 void Threads_Delete(tThread *Thread);
43 int Threads_SetName(const char *NewName);
45 char *Threads_GetName(tTID ID);
47 void Threads_SetPriority(tThread *Thread, int Pri);
48 tThread *Threads_CloneTCB(Uint *Err, Uint Flags);
49 int Threads_WaitTID(int TID, int *status);
50 tThread *Threads_GetThread(Uint TID);
52 tThread *Threads_int_DelFromQueue(tThreadList *List, tThread *Thread);
53 void Threads_int_AddToList(tThreadList *List, tThread *Thread);
55 void Threads_Exit(int TID, int Status);
56 void Threads_Kill(tThread *Thread, int Status);
57 void Threads_Yield(void);
58 void Threads_Sleep(void);
59 int Threads_Wake(tThread *Thread);
60 void Threads_AddActive(tThread *Thread);
61 tThread *Threads_RemActive(void);
63 void Threads_ToggleTrace(int TID);
64 void Threads_Fault(int Num);
65 void Threads_SegFault(tVAddr Addr);
67 int Threads_GetPID(void);
68 int Threads_GetTID(void);
69 tUID Threads_GetUID(void);
70 tGID Threads_GetGID(void);
71 int Threads_SetUID(Uint *Errno, tUID ID);
72 int Threads_SetGID(Uint *Errno, tUID ID);
74 void Threads_Dump(void);
75 void Threads_DumpActive(void);
79 struct sProcess gProcessZero = {
81 // Only used for the core kernel
82 tThread gThreadZero = {
83 .Status = THREAD_STAT_ACTIVE, // Status
84 .ThreadName = (char*)"ThreadZero", // Name
85 .Quantum = DEFAULT_QUANTUM, // Default Quantum
86 .Remaining = DEFAULT_QUANTUM, // Current Quantum
87 .Priority = DEFAULT_PRIORITY // Number of tickets
91 tShortSpinlock glThreadListLock; ///\note NEVER use a heap function while locked
92 // --- Current State ---
93 volatile int giNumActiveThreads = 0; // Number of threads on the active queue
94 volatile Uint giNextTID = 1; // Next TID to allocate
95 // --- Thread Lists ---
96 tThread *gAllThreads = NULL; // All allocated threads
97 tThreadList gSleepingThreads; // Sleeping Threads
98 int giNumCPUs = 1; // Number of CPUs
99 BOOL gaThreads_NoTaskSwitch[MAX_CPUS]; // Disables task switches for each core (Pseudo-IF)
100 // --- Scheduler Types ---
101 #if SCHEDULER_TYPE == SCHED_LOTTERY
102 const int caiTICKET_COUNTS[MIN_PRIORITY+1] = {100,81,64,49,36,25,16,9,4,1,0};
103 volatile int giFreeTickets = 0; // Number of tickets held by non-scheduled threads
104 tThreadList gActiveThreads; // Currently Running Threads
105 #elif SCHEDULER_TYPE == SCHED_RR_SIM
106 tThreadList gActiveThreads; // Currently Running Threads
107 #elif SCHEDULER_TYPE == SCHED_RR_PRI
108 tThreadList gaActiveThreads[MIN_PRIORITY+1]; // Active threads for each priority level
110 # error "Unkown scheduler type"
115 * \fn void Threads_Init(void)
116 * \brief Initialse the thread list
118 void Threads_Init(void)
122 Log_Debug("Threads", "Offsets of tThread");
123 Log_Debug("Threads", ".Priority = %i", offsetof(tThread, Priority));
124 Log_Debug("Threads", ".KernelStack = %i", offsetof(tThread, KernelStack));
126 // Create Initial Task
127 // #if SCHEDULER_TYPE == SCHED_RR_PRI
128 // gaActiveThreads[gThreadZero.Priority].Head = &gThreadZero;
129 // gaActiveThreads[gThreadZero.Priority].Tail = &gThreadZero;
131 // gActiveThreads.Head = &gThreadZero;
132 // gActiveThreads.Tail = &gThreadZero;
135 gAllThreads = &gThreadZero;
136 giNumActiveThreads = 1;
137 gThreadZero.Process = &gProcessZero;
142 void Threads_Delete(tThread *Thread)
145 Thread->Status = THREAD_STAT_BURIED;
147 // Clear out process state
148 Proc_ClearThread(Thread);
150 Thread->Process->nThreads --;
151 if( Thread->Process->nThreads == 0 )
153 tProcess *proc = Thread->Process;
154 Proc_ClearProcess( proc );
155 if( proc->CurrentWorkingDir)
156 free( proc->CurrentWorkingDir );
158 free( proc->RootDir );
163 if( IsHeap(Thread->ThreadName) )
164 free(Thread->ThreadName);
166 // Remove from global list
167 // TODO: Lock this too
168 if( Thread == gAllThreads )
169 gAllThreads = Thread->GlobalNext;
171 Thread->GlobalPrev->GlobalNext = Thread->GlobalNext;
177 * \fn void Threads_SetName(const char *NewName)
178 * \brief Sets the current thread's name
179 * \param NewName New name for the thread
180 * \return Boolean Failure
182 int Threads_SetName(const char *NewName)
184 tThread *cur = Proc_GetCurThread();
185 char *oldname = cur->ThreadName;
187 // NOTE: There is a possibility of non-thread safety here
188 // A thread could read the current name pointer before it is zeroed
190 cur->ThreadName = NULL;
192 if( IsHeap(oldname) ) free( oldname );
193 cur->ThreadName = strdup(NewName);
195 Log_Debug("Threads", "Thread renamed to '%s'", NewName);
201 * \fn char *Threads_GetName(int ID)
202 * \brief Gets a thread's name
203 * \param ID Thread ID (-1 indicates current thread)
204 * \return Pointer to name
205 * \retval NULL Failure
207 char *Threads_GetName(tTID ID)
210 return Proc_GetCurThread()->ThreadName;
212 return Threads_GetThread(ID)->ThreadName;
216 * \fn void Threads_SetPriority(tThread *Thread, int Pri)
217 * \brief Sets the priority of a task
218 * \param Thread Thread to update ticket count (NULL means current thread)
219 * \param Pri New priority
221 void Threads_SetPriority(tThread *Thread, int Pri)
223 // Get current thread
224 if(Thread == NULL) Thread = Proc_GetCurThread();
226 // - If < 0, set to lowest priority
227 // - Minumum priority is actualy a high number, 0 is highest
228 if(Pri < 0) Pri = MIN_PRIORITY;
229 if(Pri > MIN_PRIORITY) Pri = MIN_PRIORITY;
231 // Do we actually have to do anything?
232 if( Pri == Thread->Priority ) return;
234 #if SCHEDULER_TYPE == SCHED_RR_PRI
235 if( Thread != Proc_GetCurThread() )
237 SHORTLOCK( &glThreadListLock );
238 // Remove from old priority
239 Threads_int_DelFromQueue( &gaActiveThreads[Thread->Priority], Thread );
241 Threads_int_AddToList( &gaActiveThreads[Pri], Thread );
242 Thread->Priority = Pri;
243 SHORTREL( &glThreadListLock );
246 Thread->Priority = Pri;
248 // If this isn't the current thread, we need to lock
249 if( Thread != Proc_GetCurThread() )
251 SHORTLOCK( &glThreadListLock );
253 #if SCHEDULER_TYPE == SCHED_LOTTERY
254 giFreeTickets -= caiTICKET_COUNTS[Thread->Priority] - caiTICKET_COUNTS[Pri];
255 # if DEBUG_TRACE_TICKETS
256 Log("Threads_SetTickets: new giFreeTickets = %i [-%i+%i]",
258 caiTICKET_COUNTS[Thread->Priority], caiTICKET_COUNTS[Pri]);
261 Thread->Priority = Pri;
262 SHORTREL( &glThreadListLock );
265 Thread->Priority = Pri;
268 #if DEBUG_TRACE_STATE
269 Log("Threads_SetPriority: %p(%i %s) pri set %i",
270 Thread, Thread->TID, Thread->ThreadName,
276 * \brief Clone the TCB of the current thread
277 * \param Flags Flags for something... (What is this for?)
279 tThread *Threads_CloneTCB(Uint Flags)
282 cur = Proc_GetCurThread();
284 // Allocate and duplicate
285 new = malloc(sizeof(tThread));
286 if(new == NULL) { errno = -ENOMEM; return NULL; }
287 memcpy(new, cur, sizeof(tThread));
291 memset( &new->IsLocked, 0, sizeof(new->IsLocked));
292 new->Status = THREAD_STAT_PREINIT;
296 new->TID = giNextTID++;
298 new->bInstrTrace = 0;
301 new->ThreadName = strdup(cur->ThreadName);
303 // Set Thread Group ID (PID)
304 if(Flags & CLONE_VM) {
305 tProcess *newproc, *oldproc;
306 oldproc = cur->Process;
307 new->Process = malloc( sizeof(struct sProcess) );
308 newproc = new->Process;
309 newproc->PID = new->TID;
310 newproc->UID = oldproc->UID;
311 newproc->GID = oldproc->GID;
312 newproc->MaxFD = oldproc->MaxFD;
313 if( oldproc->CurrentWorkingDir )
314 newproc->CurrentWorkingDir = strdup( oldproc->CurrentWorkingDir );
316 newproc->CurrentWorkingDir = NULL;
317 if( oldproc->RootDir )
318 newproc->RootDir = strdup( oldproc->RootDir );
320 newproc->RootDir = NULL;
321 newproc->nThreads = 1;
324 new->Process->nThreads ++;
327 // Messages are not inherited
328 new->Messages = NULL;
329 new->LastMessage = NULL;
332 new->Remaining = new->Quantum = cur->Quantum;
333 new->Priority = cur->Priority;
336 // Set Signal Handlers
337 new->CurFaultNum = 0;
338 new->FaultHandler = cur->FaultHandler;
340 // Maintain a global list of threads
341 SHORTLOCK( &glThreadListLock );
342 new->GlobalPrev = NULL; // Protect against bugs
343 new->GlobalNext = gAllThreads;
344 gAllThreads->GlobalPrev = new;
346 SHORTREL( &glThreadListLock );
352 * \brief Clone the TCB of the kernel thread
354 tThread *Threads_CloneThreadZero(void)
358 // Allocate and duplicate
359 new = malloc(sizeof(tThread));
363 memcpy(new, &gThreadZero, sizeof(tThread));
365 new->Process->nThreads ++;
369 memset( &new->IsLocked, 0, sizeof(new->IsLocked));
370 new->Status = THREAD_STAT_PREINIT;
374 new->TID = giNextTID++;
378 new->ThreadName = NULL;
380 // Messages are not inherited
381 new->Messages = NULL;
382 new->LastMessage = NULL;
385 new->Remaining = new->Quantum = DEFAULT_QUANTUM;
386 new->Priority = DEFAULT_PRIORITY;
387 new->bInstrTrace = 0;
389 // Set Signal Handlers
390 new->CurFaultNum = 0;
391 new->FaultHandler = 0;
393 // Maintain a global list of threads
394 SHORTLOCK( &glThreadListLock );
395 new->GlobalPrev = NULL; // Protect against bugs
396 new->GlobalNext = gAllThreads;
397 gAllThreads->GlobalPrev = new;
399 SHORTREL( &glThreadListLock );
405 * \brief Wait for a task to change state
406 * \param TID Thread ID to wait on (-1: Any child thread, 0: Any Child/Sibling, <-1: -PID)
407 * \param Status Thread return status
408 * \return TID of child that changed state
410 tTID Threads_WaitTID(int TID, int *Status)
414 Log_Error("Threads", "TODO: Threads_WaitTID(TID=-1) - Any Child");
418 // Any peer/child thread
420 Log_Error("Threads", "TODO: Threads_WaitTID(TID=0) - Any Child/Sibling");
426 Log_Error("Threads", "TODO: Threads_WaitTID(TID<0) - TGID");
432 tThread *t = Threads_GetThread(TID);
435 // Wait for the thread to die!
436 // TODO: Handle child also being suspended if wanted
437 while(t->Status != THREAD_STAT_ZOMBIE) {
439 Log_Debug("Threads", "%i waiting for %i, t->Status = %i",
440 Threads_GetTID(), t->TID, t->Status);
447 case THREAD_STAT_ZOMBIE:
449 t->Status = THREAD_STAT_DEAD;
450 // TODO: Child return value?
451 if(Status) *Status = t->RetStatus;
452 // add to delete queue
456 if(Status) *Status = -1;
466 * \brief Gets a thread given its TID
467 * \param TID Thread ID
468 * \return Thread pointer
470 tThread *Threads_GetThread(Uint TID)
474 // Search global list
475 for(thread = gAllThreads;
477 thread = thread->GlobalNext)
479 if(thread->TID == TID)
483 Log("Unable to find TID %i on main list\n", TID);
489 * \brief Deletes an entry from a list
490 * \param List Pointer to the list head
491 * \param Thread Thread to find
494 tThread *Threads_int_DelFromQueue(tThreadList *List, tThread *Thread)
496 tThread *ret, *prev = NULL;
498 for(ret = List->Head;
499 ret && ret != Thread;
500 prev = ret, ret = ret->Next
503 // Is the thread on the list
505 //LogF("%p(%s) is not on list %p\n", Thread, Thread->ThreadName, List);
510 List->Head = Thread->Next;
511 //LogF("%p(%s) removed from head of %p\n", Thread, Thread->ThreadName, List);
514 prev->Next = Thread->Next;
515 //LogF("%p(%s) removed from %p (prev=%p)\n", Thread, Thread->ThreadName, List, prev);
517 if( Thread->Next == NULL )
523 void Threads_int_AddToList(tThreadList *List, tThread *Thread)
526 List->Tail->Next = Thread;
534 * \brief Exit the current process (or another?)
535 * \param TID Thread ID to kill
536 * \param Status Exit status
538 void Threads_Exit(int TID, int Status)
541 Threads_Kill( Proc_GetCurThread(), (Uint)Status & 0xFF );
543 Threads_Kill( Threads_GetThread(TID), (Uint)Status & 0xFF );
545 // Halt forever, just in case
550 * \fn void Threads_Kill(tThread *Thread, int Status)
551 * \brief Kill a thread
552 * \param Thread Thread to kill
553 * \param Status Status code to return to the parent
555 void Threads_Kill(tThread *Thread, int Status)
558 int isCurThread = Thread == Proc_GetCurThread();
560 // TODO: Disown all children?
564 // TODO: I should keep a .Children list
565 for(child = gAllThreads;
567 child = child->GlobalNext)
569 if(child->Parent == Thread)
570 child->Parent = &gThreadZero;
575 ///\note Double lock is needed due to overlap of lock areas
577 // Lock thread (stop us recieving messages)
578 SHORTLOCK( &Thread->IsLocked );
580 // Clear Message Queue
581 while( Thread->Messages )
583 msg = Thread->Messages->Next;
584 free( Thread->Messages );
585 Thread->Messages = msg;
589 SHORTLOCK( &glThreadListLock );
591 switch(Thread->Status)
593 case THREAD_STAT_PREINIT: // Only on main list
596 // Currently active thread
597 case THREAD_STAT_ACTIVE:
598 if( Thread != Proc_GetCurThread() )
600 #if SCHEDULER_TYPE == SCHED_RR_PRI
601 tThreadList *list = &gaActiveThreads[Thread->Priority];
603 tThreadList *list = &gActiveThreads;
605 if( Threads_int_DelFromQueue( list, Thread ) )
610 Log_Warning("Threads",
611 "Threads_Kill - Thread %p(%i,%s) marked as active, but not on list",
612 Thread, Thread->TID, Thread->ThreadName
615 #if SCHEDULER_TYPE == SCHED_LOTTERY
616 giFreeTickets -= caiTICKET_COUNTS[ Thread->Priority ];
619 // Ensure that we are not rescheduled
620 Thread->Remaining = 0; // Clear Remaining Quantum
621 Thread->Quantum = 0; // Clear Quantum to indicate dead thread
623 // Update bookkeeping
624 giNumActiveThreads --;
626 // Kill it while it sleeps!
627 case THREAD_STAT_SLEEPING:
628 if( !Threads_int_DelFromQueue( &gSleepingThreads, Thread ) )
630 Log_Warning("Threads",
631 "Threads_Kill - Thread %p(%i,%s) marked as sleeping, but not on list",
632 Thread, Thread->TID, Thread->ThreadName
637 // Brains!... You cannot kill something that is already dead
638 case THREAD_STAT_ZOMBIE:
639 Log_Warning("Threads", "Threads_Kill - Thread %p(%i,%s) is undead, you cannot kill it",
640 Thread, Thread->TID, Thread->ThreadName);
641 SHORTREL( &glThreadListLock );
642 SHORTREL( &Thread->IsLocked );
646 Log_Warning("Threads", "Threads_Kill - BUG Un-checked status (%i)",
652 Thread->RetStatus = Status;
654 SHORTREL( &Thread->IsLocked );
656 Thread->Status = THREAD_STAT_ZOMBIE;
657 SHORTREL( &glThreadListLock );
658 // TODO: Send something like SIGCHLD
659 Threads_Wake( Thread->Parent );
661 Log("Thread %i went *hurk* (%i)", Thread->TID, Status);
672 * \brief Yield remainder of the current thread's timeslice
674 void Threads_Yield(void)
676 // Log("Threads_Yield: by %p", __builtin_return_address(0));
681 * \fn void Threads_Sleep(void)
682 * \brief Take the current process off the run queue
684 void Threads_Sleep(void)
686 tThread *cur = Proc_GetCurThread();
689 SHORTLOCK( &glThreadListLock );
691 // Don't sleep if there is a message waiting
692 if( cur->Messages ) {
693 SHORTREL( &glThreadListLock );
697 // Remove us from running queue
699 // Mark thread as sleeping
700 cur->Status = THREAD_STAT_SLEEPING;
702 // Add to Sleeping List (at the top)
703 Threads_int_AddToList( &gSleepingThreads, cur );
705 #if DEBUG_TRACE_STATE
706 Log("Threads_Sleep: %p (%i %s) sleeping", cur, cur->TID, cur->ThreadName);
710 SHORTREL( &glThreadListLock );
712 while(cur->Status != THREAD_STAT_ACTIVE) {
714 if( cur->Status != THREAD_STAT_ACTIVE )
715 Log("%i - Huh? why am I up? zzzz...", cur->TID);
721 * \brief Wakes a sleeping/waiting thread up
722 * \param Thread Thread to wake
723 * \return Boolean Failure (Returns ERRNO)
724 * \warning This should ONLY be called with task switches disabled
726 int Threads_Wake(tThread *Thread)
731 switch(Thread->Status)
733 case THREAD_STAT_ACTIVE:
734 Log("Threads_Wake - Waking awake thread (%i)", Thread->TID);
737 case THREAD_STAT_SLEEPING:
738 SHORTLOCK( &glThreadListLock );
739 // Remove from sleeping queue
740 Threads_int_DelFromQueue(&gSleepingThreads, Thread);
742 SHORTREL( &glThreadListLock );
743 Threads_AddActive( Thread );
745 #if DEBUG_TRACE_STATE
746 Log("Threads_Sleep: %p (%i %s) woken", Thread, Thread->TID, Thread->ThreadName);
750 case THREAD_STAT_SEMAPHORESLEEP: {
752 tThread *th, *prev=NULL;
754 sem = Thread->WaitPointer;
756 SHORTLOCK( &sem->Protector );
758 // Remove from sleeping queue
759 for( th = sem->Waiting; th; prev = th, th = th->Next )
760 if( th == Thread ) break;
764 prev->Next = Thread->Next;
766 sem->Waiting = Thread->Next;
767 if(sem->LastWaiting == Thread)
768 sem->LastWaiting = prev;
773 for( th = sem->Signaling; th; prev = th, th = th->Next )
774 if( th == Thread ) break;
776 Log_Warning("Threads", "Thread %p(%i %s) is not on semaphore %p(%s:%s)",
777 Thread, Thread->TID, Thread->ThreadName,
778 sem, sem->ModName, sem->Name);
783 prev->Next = Thread->Next;
785 sem->Signaling = Thread->Next;
786 if(sem->LastSignaling == Thread)
787 sem->LastSignaling = prev;
790 Thread->RetStatus = 0; // It didn't get anything
791 Threads_AddActive( Thread );
793 #if DEBUG_TRACE_STATE
794 Log("Threads_Sleep: %p(%i %s) woken from semaphore", Thread, Thread->TID, Thread->ThreadName);
796 SHORTREL( &sem->Protector );
799 case THREAD_STAT_WAITING:
800 Warning("Threads_Wake - Waiting threads are not currently supported");
803 case THREAD_STAT_DEAD:
804 Warning("Threads_Wake - Attempt to wake dead thread (%i)", Thread->TID);
808 Warning("Threads_Wake - Unknown process status (%i)\n", Thread->Status);
814 * \brief Wake a thread given the TID
815 * \param TID Thread ID to wake
816 * \return Boolean Faulure (errno)
818 int Threads_WakeTID(tTID TID)
820 tThread *thread = Threads_GetThread(TID);
824 ret = Threads_Wake( thread );
825 //Log_Debug("Threads", "TID %i woke %i (%p)", Threads_GetTID(), TID, thread);
829 void Threads_ToggleTrace(int TID)
831 tThread *thread = Threads_GetThread(TID);
833 thread->bInstrTrace = !thread->bInstrTrace;
837 * \brief Adds a thread to the active queue
839 void Threads_AddActive(tThread *Thread)
841 SHORTLOCK( &glThreadListLock );
843 if( Thread->Status == THREAD_STAT_ACTIVE ) {
844 tThread *cur = Proc_GetCurThread();
845 Log_Warning("Threads", "WTF, %p CPU%i %p (%i %s) is adding %p (%i %s) when it is active",
846 __builtin_return_address(0),
847 GetCPUNum(), cur, cur->TID, cur->ThreadName, Thread, Thread->TID, Thread->ThreadName);
848 SHORTREL( &glThreadListLock );
853 Thread->Status = THREAD_STAT_ACTIVE;
854 // Thread->CurCPU = -1;
855 // Add to active list
857 #if SCHEDULER_TYPE == SCHED_RR_PRI
858 tThreadList *list = &gaActiveThreads[Thread->Priority];
860 tThreadList *list = &gActiveThreads;
862 Threads_int_AddToList( list, Thread );
865 // Update bookkeeping
866 giNumActiveThreads ++;
868 #if SCHEDULER_TYPE == SCHED_LOTTERY
871 // Only change the ticket count if the thread is un-scheduled
872 if(Thread->CurCPU != -1)
875 delta = caiTICKET_COUNTS[ Thread->Priority ];
877 giFreeTickets += delta;
878 # if DEBUG_TRACE_TICKETS
879 Log("CPU%i %p (%i %s) added, new giFreeTickets = %i [+%i]",
880 GetCPUNum(), Thread, Thread->TID, Thread->ThreadName,
887 SHORTREL( &glThreadListLock );
891 * \brief Removes the current thread from the active queue
892 * \warning This should ONLY be called with the lock held
893 * \return Current thread pointer
895 tThread *Threads_RemActive(void)
898 tThread *ret = Proc_GetCurThread();
900 if( !IS_LOCKED(&glThreadListLock) ) {
901 Log_KernelPanic("Threads", "Threads_RemActive called without lock held");
905 // Delete from active queue
906 #if SCHEDULER_TYPE == SCHED_RR_PRI
907 if( !Threads_int_DelFromQueue(&gaActiveThreads[ret->Priority], ret) )
909 if( !Threads_int_DelFromQueue(&gActiveThreads, ret) )
912 Log_Warning("Threads", "Current thread %p(%i %s) is not on active queue",
913 ret, ret->TID, ret->ThreadName
921 giNumActiveThreads --;
922 // no need to decrement tickets, scheduler did it for us
924 #if SCHEDULER_TYPE == SCHED_LOTTERY && DEBUG_TRACE_TICKETS
925 Log("CPU%i %p (%i %s) removed, giFreeTickets = %i [nc]",
926 GetCPUNum(), ret, ret->TID, ret->ThreadName, giFreeTickets);
931 return Proc_GetCurThread();
936 * \fn void Threads_SetFaultHandler(Uint Handler)
937 * \brief Sets the signal handler for a signal
939 void Threads_SetFaultHandler(Uint Handler)
941 //Log_Debug("Threads", "Threads_SetFaultHandler: Handler = %p", Handler);
942 Proc_GetCurThread()->FaultHandler = Handler;
946 * \fn void Threads_Fault(int Num)
947 * \brief Calls a fault handler
949 void Threads_Fault(int Num)
951 tThread *thread = Proc_GetCurThread();
955 Log_Log("Threads", "Threads_Fault: thread->FaultHandler = %p", thread->FaultHandler);
957 switch(thread->FaultHandler)
960 Threads_Kill(thread, -1);
963 case 1: // Dump Core?
964 Threads_Kill(thread, -1);
969 // Double Fault? Oh, F**k
970 if(thread->CurFaultNum != 0) {
971 Log_Warning("Threads", "Threads_Fault: Double fault on %i", thread->TID);
972 Threads_Kill(thread, -1); // For now, just kill
976 thread->CurFaultNum = Num;
978 Proc_CallFaultHandler(thread);
982 * \fn void Threads_SegFault(tVAddr Addr)
983 * \brief Called when a Segment Fault occurs
985 void Threads_SegFault(tVAddr Addr)
987 tThread *cur = Proc_GetCurThread();
988 cur->bInstrTrace = 0;
989 Log_Warning("Threads", "Thread #%i committed a segfault at address %p", cur->TID, Addr);
990 MM_DumpTables(0, USER_MAX);
992 //Threads_Exit( 0, -1 );
995 // --- Process Structure Access Functions ---
996 tPID Threads_GetPID(void)
998 return Proc_GetCurThread()->Process->PID;
1000 tTID Threads_GetTID(void)
1002 return Proc_GetCurThread()->TID;
1004 tUID Threads_GetUID(void)
1006 return Proc_GetCurThread()->Process->UID;
1008 tGID Threads_GetGID(void)
1010 return Proc_GetCurThread()->Process->GID;
1013 int Threads_SetUID(tUID ID)
1015 tThread *t = Proc_GetCurThread();
1016 if( t->Process->UID != 0 ) {
1020 Log_Debug("Threads", "PID %i's UID set to %i", t->Process->PID, ID);
1021 t->Process->UID = ID;
1025 int Threads_SetGID(tGID ID)
1027 tThread *t = Proc_GetCurThread();
1028 if( t->Process->UID != 0 ) {
1032 Log_Debug("Threads", "PID %i's GID set to %i", t->Process->PID, ID);
1033 t->Process->GID = ID;
1037 // --- Per-thread storage ---
1038 int *Threads_GetErrno(void)
1040 return &Proc_GetCurThread()->_errno;
1043 // --- Configuration ---
1044 int *Threads_GetMaxFD(void)
1046 return &Proc_GetCurThread()->Process->MaxFD;
1048 char **Threads_GetChroot(void)
1050 return &Proc_GetCurThread()->Process->RootDir;
1052 char **Threads_GetCWD(void)
1054 return &Proc_GetCurThread()->Process->CurrentWorkingDir;
1059 * \fn void Threads_Dump(void)
1061 void Threads_DumpActive(void)
1065 #if SCHEDULER_TYPE == SCHED_RR_PRI
1069 Log("Active Threads: (%i reported)", giNumActiveThreads);
1071 #if SCHEDULER_TYPE == SCHED_RR_PRI
1072 for( i = 0; i < MIN_PRIORITY+1; i++ )
1074 list = &gaActiveThreads[i];
1076 list = &gActiveThreads;
1078 for(thread=list->Head;thread;thread=thread->Next)
1080 Log(" %p %i (%i) - %s (CPU %i)",
1081 thread, thread->TID, thread->Process->PID, thread->ThreadName, thread->CurCPU);
1082 if(thread->Status != THREAD_STAT_ACTIVE)
1083 Log(" ERROR State (%i) != THREAD_STAT_ACTIVE (%i)",
1084 thread->Status, THREAD_STAT_ACTIVE);
1085 Log(" Priority %i, Quantum %i", thread->Priority, thread->Quantum);
1086 Log(" KStack 0x%x", thread->KernelStack);
1087 if( thread->bInstrTrace )
1088 Log(" Tracing Enabled");
1089 Proc_DumpThreadCPUState(thread);
1092 #if SCHEDULER_TYPE == SCHED_RR_PRI
1098 * \fn void Threads_Dump(void)
1099 * \brief Dumps a list of currently running threads
1101 void Threads_Dump(void)
1105 Log("--- Thread Dump ---");
1106 Threads_DumpActive();
1108 Log("All Threads:");
1109 for(thread=gAllThreads;thread;thread=thread->GlobalNext)
1111 Log(" %p %i (%i) - %s (CPU %i)",
1112 thread, thread->TID, thread->Process->PID, thread->ThreadName, thread->CurCPU);
1113 Log(" State %i (%s)", thread->Status, casTHREAD_STAT[thread->Status]);
1114 switch(thread->Status)
1116 case THREAD_STAT_MUTEXSLEEP:
1117 Log(" Mutex Pointer: %p", thread->WaitPointer);
1119 case THREAD_STAT_SEMAPHORESLEEP:
1120 Log(" Semaphore Pointer: %p", thread->WaitPointer);
1121 Log(" Semaphore Name: %s:%s",
1122 ((tSemaphore*)thread->WaitPointer)->ModName,
1123 ((tSemaphore*)thread->WaitPointer)->Name
1126 case THREAD_STAT_ZOMBIE:
1127 Log(" Return Status: %i", thread->RetStatus);
1131 Log(" Priority %i, Quantum %i", thread->Priority, thread->Quantum);
1132 Log(" KStack 0x%x", thread->KernelStack);
1133 if( thread->bInstrTrace )
1134 Log(" Tracing Enabled");
1135 Proc_DumpThreadCPUState(thread);
1140 * \brief Gets the next thread to run
1141 * \param CPU Current CPU
1142 * \param Last The thread the CPU was running
1144 tThread *Threads_GetNextToRun(int CPU, tThread *Last)
1148 // If this CPU has the lock, we must let it complete
1149 if( CPU_HAS_LOCK( &glThreadListLock ) )
1152 // Don't change threads if the current CPU has switches disabled
1153 if( gaThreads_NoTaskSwitch[CPU] )
1157 SHORTLOCK( &glThreadListLock );
1159 // Make sure the current (well, old) thread is marked as de-scheduled
1160 if(Last) Last->CurCPU = -1;
1162 // No active threads, just take a nap
1163 if(giNumActiveThreads == 0) {
1164 SHORTREL( &glThreadListLock );
1165 #if DEBUG_TRACE_TICKETS
1166 Log("No active threads");
1172 #if SCHEDULER_TYPE != SCHED_RR_PRI
1173 // Special case: 1 thread
1174 if(giNumActiveThreads == 1) {
1175 if( gActiveThreads.Head->CurCPU == -1 )
1176 gActiveThreads.Head->CurCPU = CPU;
1178 SHORTREL( &glThreadListLock );
1180 if( gActiveThreads.Head->CurCPU == CPU )
1181 return gActiveThreads.Head;
1183 return NULL; // CPU has nothing to do
1188 // Allow the old thread to be scheduled again
1190 if( Last->Status == THREAD_STAT_ACTIVE ) {
1192 #if SCHEDULER_TYPE == SCHED_LOTTERY
1193 giFreeTickets += caiTICKET_COUNTS[ Last->Priority ];
1194 # if DEBUG_TRACE_TICKETS
1195 LogF("Log: CPU%i released %p (%i %s) into the pool (%i [+%i] tickets in pool)\n",
1196 CPU, Last, Last->TID, Last->ThreadName, giFreeTickets,
1197 caiTICKET_COUNTS[ Last->Priority ]);
1201 #if SCHEDULER_TYPE == SCHED_RR_PRI
1202 list = &gaActiveThreads[ Last->Priority ];
1204 list = &gActiveThreads;
1206 // Add to end of list
1207 Threads_int_AddToList( list, Last );
1209 #if SCHEDULER_TYPE == SCHED_LOTTERY && DEBUG_TRACE_TICKETS
1211 LogF("Log: CPU%i released %p (%i %s)->Status = %i (Released,not in pool)\n",
1212 CPU, Last, Last->TID, Last->ThreadName, Last->Status);
1218 // Lottery Scheduler
1220 #if SCHEDULER_TYPE == SCHED_LOTTERY
1225 for(thread = gActiveThreads.Head; thread; thread = thread->Next)
1227 if(thread->Status != THREAD_STAT_ACTIVE)
1228 Panic("Bookkeeping fail - %p %i(%s) is on the active queue with a status of %i",
1229 thread, thread->TID, thread->ThreadName, thread->Status);
1230 if(thread->Next == thread) {
1231 Panic("Bookkeeping fail - %p %i(%s) loops back on itself",
1232 thread, thread->TID, thread->ThreadName, thread->Status);
1234 number += caiTICKET_COUNTS[ thread->Priority ];
1236 if(number != giFreeTickets) {
1237 Panic("Bookkeeping fail (giFreeTickets(%i) != number(%i)) - CPU%i",
1238 giFreeTickets, number, CPU);
1242 // No free tickets (all tasks delegated to cores)
1243 if( giFreeTickets == 0 ) {
1244 SHORTREL(&glThreadListLock);
1248 // Get the ticket number
1249 ticket = number = rand() % giFreeTickets;
1251 // Find the next thread
1252 for(thread = gActiveThreads.Head; thread; prev = thread, thread = thread->Next )
1254 if( caiTICKET_COUNTS[ thread->Priority ] > number) break;
1255 number -= caiTICKET_COUNTS[ thread->Priority ];
1258 // If we didn't find a thread, something went wrong
1262 for(thread=gActiveThreads;thread;thread=thread->Next) {
1263 if(thread->CurCPU >= 0) continue;
1264 number += caiTICKET_COUNTS[ thread->Priority ];
1266 Panic("Bookeeping Failed - giFreeTickets(%i) > true count (%i)",
1267 giFreeTickets, number);
1272 prev->Next = thread->Next;
1274 gActiveThreads.Head = thread->Next;
1276 gActiveThreads.Tail = prev;
1278 giFreeTickets -= caiTICKET_COUNTS[ thread->Priority ];
1279 # if DEBUG_TRACE_TICKETS
1280 LogF("Log: CPU%i allocated %p (%i %s), (%i [-%i] tickets in pool), \n",
1281 CPU, thread, thread->TID, thread->ThreadName,
1282 giFreeTickets, caiTICKET_COUNTS[ thread->Priority ]);
1287 // Priority based round robin scheduler
1289 #elif SCHEDULER_TYPE == SCHED_RR_PRI
1293 for( i = 0; i < MIN_PRIORITY + 1; i ++ )
1295 if( !gaActiveThreads[i].Head )
1298 thread = gaActiveThreads[i].Head;
1301 gaActiveThreads[i].Head = thread->Next;
1303 gaActiveThreads[i].Tail = NULL;
1304 thread->Next = NULL;
1310 SHORTREL(&glThreadListLock);
1313 if( thread->Status != THREAD_STAT_ACTIVE ) {
1314 LogF("Oops, Thread %i (%s) is not active\n", thread->TID, thread->ThreadName);
1317 #elif SCHEDULER_TYPE == SCHED_RR_SIM
1319 // Get the next thread off the list
1320 thread = gActiveThreads.Head;
1321 gActiveThreads.Head = thread->Next;
1323 gaActiveThreads.Tail = NULL;
1324 thread->Next = NULL;
1328 SHORTREL(&glThreadListLock);
1333 # error "Unimplemented scheduling algorithm"
1336 // Make the new thread non-schedulable
1337 thread->CurCPU = CPU;
1338 thread->Remaining = thread->Quantum;
1340 SHORTREL( &glThreadListLock );
1346 EXPORT(Threads_GetUID);
1347 EXPORT(Threads_GetGID);