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
29 const enum eConfigTypes cCONFIG_TYPES[] = {
30 CFGT_HEAPSTR, // e.g. CFG_VFS_CWD
31 CFGT_INT, // e.g. CFG_VFS_MAXFILES
38 void Threads_Init(void);
40 int Threads_SetName(const char *NewName);
42 char *Threads_GetName(int ID);
44 void Threads_SetPriority(tThread *Thread, int Pri);
45 tThread *Threads_CloneTCB(Uint *Err, Uint Flags);
46 int Threads_WaitTID(int TID, int *status);
47 tThread *Threads_GetThread(Uint TID);
49 void Threads_AddToDelete(tThread *Thread);
50 tThread *Threads_int_DelFromQueue(tThread **List, tThread *Thread);
52 void Threads_Exit(int TID, int Status);
53 void Threads_Kill(tThread *Thread, int Status);
54 void Threads_Yield(void);
55 void Threads_Sleep(void);
56 int Threads_Wake(tThread *Thread);
57 void Threads_AddActive(tThread *Thread);
58 tThread *Threads_RemActive(void);
60 void Threads_ToggleTrace(int TID);
61 void Threads_Fault(int Num);
62 void Threads_SegFault(tVAddr Addr);
64 int Threads_GetPID(void);
65 int Threads_GetTID(void);
66 tUID Threads_GetUID(void);
67 tGID Threads_GetGID(void);
68 int Threads_SetUID(Uint *Errno, tUID ID);
69 int Threads_SetGID(Uint *Errno, tUID ID);
71 void Threads_Dump(void);
72 void Threads_DumpActive(void);
76 // Only used for the core kernel
77 tThread gThreadZero = {
78 .Status = THREAD_STAT_ACTIVE, // Status
79 .ThreadName = (char*)"ThreadZero", // Name
80 .Quantum = DEFAULT_QUANTUM, // Default Quantum
81 .Remaining = DEFAULT_QUANTUM, // Current Quantum
82 .Priority = DEFAULT_PRIORITY // Number of tickets
86 tShortSpinlock glThreadListLock; ///\note NEVER use a heap function while locked
87 // --- Current State ---
88 volatile int giNumActiveThreads = 0; // Number of threads on the active queue
89 volatile Uint giNextTID = 1; // Next TID to allocate
90 // --- Thread Lists ---
91 tThread *gAllThreads = NULL; // All allocated threads
92 tThread *gSleepingThreads = NULL; // Sleeping Threads
93 tThread *gDeleteThreads = NULL; // Threads to delete
94 int giNumCPUs = 1; // Number of CPUs
95 BOOL gaThreads_NoTaskSwitch[MAX_CPUS]; // Disables task switches for each core (Pseudo-IF)
96 // --- Scheduler Types ---
97 #if SCHEDULER_TYPE == SCHED_LOTTERY
98 const int caiTICKET_COUNTS[MIN_PRIORITY+1] = {100,81,64,49,36,25,16,9,4,1,0};
99 volatile int giFreeTickets = 0; // Number of tickets held by non-scheduled threads
100 tThread *gActiveThreads = NULL; // Currently Running Threads
101 #elif SCHEDULER_TYPE == SCHED_RR_SIM
102 tThread *gActiveThreads = NULL; // Currently Running Threads
103 #elif SCHEDULER_TYPE == SCHED_RR_PRI
104 tThread *gaActiveThreads[MIN_PRIORITY+1]; // Active threads for each priority level
106 # error "Unkown scheduler type"
111 * \fn void Threads_Init(void)
112 * \brief Initialse the thread list
114 void Threads_Init(void)
118 Log_Debug("Threads", "Offsets of tThread");
119 Log_Debug("Threads", ".Priority = %i", offsetof(tThread, Priority));
120 Log_Debug("Threads", ".KernelStack = %i", offsetof(tThread, KernelStack));
122 // Create Initial Task
123 #if SCHEDULER_TYPE == SCHED_RR_PRI
124 gaActiveThreads[gThreadZero.Priority] = &gThreadZero;
126 gActiveThreads = &gThreadZero;
129 gAllThreads = &gThreadZero;
130 giNumActiveThreads = 1;
136 * \fn void Threads_SetName(const char *NewName)
137 * \brief Sets the current thread's name
138 * \param NewName New name for the thread
139 * \return Boolean Failure
141 int Threads_SetName(const char *NewName)
143 tThread *cur = Proc_GetCurThread();
144 char *oldname = cur->ThreadName;
146 // NOTE: There is a possibility of non-thread safety here
147 // A thread could read the current name pointer before it is zeroed
149 cur->ThreadName = NULL;
151 if( IsHeap(oldname) ) free( oldname );
153 cur->ThreadName = strdup(NewName);
158 * \fn char *Threads_GetName(int ID)
159 * \brief Gets a thread's name
160 * \param ID Thread ID (-1 indicates current thread)
161 * \return Pointer to name
162 * \retval NULL Failure
164 char *Threads_GetName(tTID ID)
167 return Proc_GetCurThread()->ThreadName;
169 return Threads_GetThread(ID)->ThreadName;
173 * \fn void Threads_SetPriority(tThread *Thread, int Pri)
174 * \brief Sets the priority of a task
175 * \param Thread Thread to update ticket count (NULL means current thread)
176 * \param Pri New priority
178 void Threads_SetPriority(tThread *Thread, int Pri)
180 // Get current thread
181 if(Thread == NULL) Thread = Proc_GetCurThread();
183 // - If < 0, set to lowest priority
184 // - Minumum priority is actualy a high number, 0 is highest
185 if(Pri < 0) Pri = MIN_PRIORITY;
186 if(Pri > MIN_PRIORITY) Pri = MIN_PRIORITY;
188 // Do we actually have to do anything?
189 if( Pri == Thread->Priority ) return;
191 #if SCHEDULER_TYPE == SCHED_RR_PRI
192 SHORTLOCK( &glThreadListLock );
193 // Remove from old priority
194 Threads_int_DelFromQueue( &gaActiveThreads[Thread->Priority], Thread );
196 Thread->Next = gaActiveThreads[Pri];
197 gaActiveThreads[Pri] = Thread;
198 Thread->Priority = Pri;
199 SHORTREL( &glThreadListLock );
201 // If this isn't the current thread, we need to lock
202 if( Thread != Proc_GetCurThread() )
204 SHORTLOCK( &glThreadListLock );
206 #if SCHEDULER_TYPE == SCHED_LOTTERY
207 giFreeTickets -= caiTICKET_COUNTS[Thread->Priority] - caiTICKET_COUNTS[Pri];
208 # if DEBUG_TRACE_TICKETS
209 Log("Threads_SetTickets: new giFreeTickets = %i [-%i+%i]",
211 caiTICKET_COUNTS[Thread->Priority], caiTICKET_COUNTS[Pri]);
214 Thread->Priority = Pri;
215 SHORTREL( &glThreadListLock );
218 Thread->Priority = Pri;
221 #if DEBUG_TRACE_STATE
222 Log("Threads_SetPriority: %p(%i %s) pri set %i",
223 Thread, Thread->TID, Thread->ThreadName,
229 * \brief Clone the TCB of the current thread
230 * \param Flags Flags for something... (What is this for?)
232 tThread *Threads_CloneTCB(Uint Flags)
236 cur = Proc_GetCurThread();
238 // Allocate and duplicate
239 new = malloc(sizeof(tThread));
240 if(new == NULL) { errno = -ENOMEM; return NULL; }
241 memcpy(new, cur, sizeof(tThread));
245 memset( &new->IsLocked, 0, sizeof(new->IsLocked));
246 new->Status = THREAD_STAT_PREINIT;
250 new->TID = giNextTID++;
252 new->bInstrTrace = 0;
255 new->ThreadName = strdup(cur->ThreadName);
257 // Set Thread Group ID (PID)
259 new->TGID = new->TID;
261 new->TGID = cur->TGID;
263 // Messages are not inherited
264 new->Messages = NULL;
265 new->LastMessage = NULL;
268 new->Remaining = new->Quantum = cur->Quantum;
269 new->Priority = cur->Priority;
271 // Set Signal Handlers
272 new->CurFaultNum = 0;
273 new->FaultHandler = cur->FaultHandler;
275 for( i = 0; i < NUM_CFG_ENTRIES; i ++ )
277 switch(cCONFIG_TYPES[i])
280 new->Config[i] = cur->Config[i];
284 new->Config[i] = (Uint) strdup( (void*)cur->Config[i] );
291 // Maintain a global list of threads
292 SHORTLOCK( &glThreadListLock );
293 new->GlobalPrev = NULL; // Protect against bugs
294 new->GlobalNext = gAllThreads;
295 gAllThreads->GlobalPrev = new;
297 SHORTREL( &glThreadListLock );
303 * \brief Clone the TCB of the kernel thread
305 tThread *Threads_CloneThreadZero(void)
310 // Allocate and duplicate
311 new = malloc(sizeof(tThread));
315 memcpy(new, &gThreadZero, sizeof(tThread));
319 memset( &new->IsLocked, 0, sizeof(new->IsLocked));
320 new->Status = THREAD_STAT_PREINIT;
324 new->TID = giNextTID++;
328 new->ThreadName = NULL;
330 // Messages are not inherited
331 new->Messages = NULL;
332 new->LastMessage = NULL;
335 new->Remaining = new->Quantum = DEFAULT_QUANTUM;
336 new->Priority = DEFAULT_PRIORITY;
337 new->bInstrTrace = 0;
339 // Set Signal Handlers
340 new->CurFaultNum = 0;
341 new->FaultHandler = 0;
343 for( i = 0; i < NUM_CFG_ENTRIES; i ++ )
348 // Maintain a global list of threads
349 SHORTLOCK( &glThreadListLock );
350 new->GlobalPrev = NULL; // Protect against bugs
351 new->GlobalNext = gAllThreads;
352 gAllThreads->GlobalPrev = new;
354 SHORTREL( &glThreadListLock );
360 * \brief Get a configuration pointer from the Per-Thread data area
361 * \param ID Config slot ID
362 * \return Pointer at ID
364 Uint *Threads_GetCfgPtr(int ID)
366 if(ID < 0 || ID >= NUM_CFG_ENTRIES) {
367 Warning("Threads_GetCfgPtr: Index %i is out of bounds", ID);
371 return &Proc_GetCurThread()->Config[ID];
375 * \brief Wait for a task to change state
376 * \param TID Thread ID to wait on (-1: Any child thread, 0: Any Child/Sibling, <-1: -PID)
377 * \param Status Thread return status
378 * \return TID of child that changed state
380 tTID Threads_WaitTID(int TID, int *Status)
384 Log_Error("Threads", "TODO: Threads_WaitTID(TID=-1) - Any Child");
388 // Any peer/child thread
390 Log_Error("Threads", "TODO: Threads_WaitTID(TID=0) - Any Child/Sibling");
396 Log_Error("Threads", "TODO: Threads_WaitTID(TID<0) - TGID");
402 tThread *t = Threads_GetThread(TID);
403 int initStatus = t->Status;
406 // Wait for the thread to die!
407 if(initStatus != THREAD_STAT_ZOMBIE) {
408 // TODO: Handle child also being suspended if wanted
409 while(t->Status != THREAD_STAT_ZOMBIE) {
411 Log_Debug("Threads", "%i waiting for %i, t->Status = %i",
412 Threads_GetTID(), t->TID, t->Status);
417 Log_Debug("Threads", "%i waiting for %i, t->Status = %i",
418 Threads_GetTID(), t->TID, t->Status);
422 case THREAD_STAT_ZOMBIE:
424 t->Status = THREAD_STAT_DEAD;
425 // TODO: Child return value?
426 if(Status) *Status = t->RetStatus;
427 // add to delete queue
428 Threads_AddToDelete( t );
431 if(Status) *Status = -1;
441 * \brief Gets a thread given its TID
442 * \param TID Thread ID
443 * \return Thread pointer
445 tThread *Threads_GetThread(Uint TID)
449 // Search global list
450 for(thread = gAllThreads;
452 thread = thread->GlobalNext)
454 if(thread->TID == TID)
458 Log("Unable to find TID %i on main list\n", TID);
464 * \brief Adds a thread to the delete queue
465 * \param Thread Thread to delete
467 void Threads_AddToDelete(tThread *Thread)
469 // Add to delete queue
470 // TODO: Is locking needed?
472 Thread->Next = gDeleteThreads;
473 gDeleteThreads = Thread;
476 gDeleteThreads = Thread;
481 * \brief Deletes an entry from a list
482 * \param List Pointer to the list head
483 * \param Thread Thread to find
486 tThread *Threads_int_DelFromQueue(tThread **List, tThread *Thread)
488 tThread *ret, *prev = NULL;
491 ret && ret != Thread;
492 prev = ret, ret = ret->Next
495 // Is the thread on the list
497 //LogF("%p(%s) is not on list %p\n", Thread, Thread->ThreadName, List);
502 *List = Thread->Next;
503 //LogF("%p(%s) removed from head of %p\n", Thread, Thread->ThreadName, List);
506 prev->Next = Thread->Next;
507 //LogF("%p(%s) removed from %p (prev=%p)\n", Thread, Thread->ThreadName, List, prev);
514 * \brief Exit the current process (or another?)
515 * \param TID Thread ID to kill
516 * \param Status Exit status
518 void Threads_Exit(int TID, int Status)
521 Threads_Kill( Proc_GetCurThread(), (Uint)Status & 0xFF );
523 Threads_Kill( Threads_GetThread(TID), (Uint)Status & 0xFF );
525 // Halt forever, just in case
530 * \fn void Threads_Kill(tThread *Thread, int Status)
531 * \brief Kill a thread
532 * \param Thread Thread to kill
533 * \param Status Status code to return to the parent
535 void Threads_Kill(tThread *Thread, int Status)
538 int isCurThread = Thread == Proc_GetCurThread();
540 // TODO: Kill all children
544 // TODO: I should keep a .Parent pointer, and a .Children list
545 for(child = gAllThreads;
547 child = child->GlobalNext)
549 if(child->Parent == Thread)
550 Threads_Kill(child, -1);
555 ///\note Double lock is needed due to overlap of lock areas
557 // Lock thread (stop us recieving messages)
558 SHORTLOCK( &Thread->IsLocked );
560 // Clear Message Queue
561 while( Thread->Messages )
563 msg = Thread->Messages->Next;
564 free( Thread->Messages );
565 Thread->Messages = msg;
569 SHORTLOCK( &glThreadListLock );
571 switch(Thread->Status)
573 case THREAD_STAT_PREINIT: // Only on main list
576 // Currently active thread
577 case THREAD_STAT_ACTIVE:
578 #if SCHEDULER_TYPE == SCHED_RR_PRI
579 if( Threads_int_DelFromQueue( &gaActiveThreads[Thread->Priority], Thread ) )
581 if( Threads_int_DelFromQueue( &gActiveThreads, Thread ) )
584 // Ensure that we are not rescheduled
585 Thread->Remaining = 0; // Clear Remaining Quantum
586 Thread->Quantum = 0; // Clear Quantum to indicate dead thread
588 // Update bookkeeping
589 giNumActiveThreads --;
590 #if SCHEDULER_TYPE == SCHED_LOTTERY
591 if( Thread != Proc_GetCurThread() )
592 giFreeTickets -= caiTICKET_COUNTS[ Thread->Priority ];
597 Log_Warning("Threads",
598 "Threads_Kill - Thread %p(%i,%s) marked as active, but not on list",
599 Thread, Thread->TID, Thread->ThreadName
603 // Kill it while it sleeps!
604 case THREAD_STAT_SLEEPING:
605 if( !Threads_int_DelFromQueue( &gSleepingThreads, Thread ) )
607 Log_Warning("Threads",
608 "Threads_Kill - Thread %p(%i,%s) marked as sleeping, but not on list",
609 Thread, Thread->TID, Thread->ThreadName
614 // Brains!... You cannot kill something that is already dead
615 case THREAD_STAT_ZOMBIE:
616 Log_Warning("Threads", "Threads_Kill - Thread %p(%i,%s) is undead, you cannot kill it",
617 Thread, Thread->TID, Thread->ThreadName);
618 SHORTREL( &glThreadListLock );
619 SHORTREL( &Thread->IsLocked );
623 Log_Warning("Threads", "Threads_Kill - BUG Un-checked status (%i)",
629 Thread->RetStatus = Status;
631 SHORTREL( &Thread->IsLocked );
633 // Don't Zombie if we are being killed because our parent is
636 Thread->Status = THREAD_STAT_DEAD;
637 Threads_AddToDelete( Thread );
638 SHORTREL( &glThreadListLock );
640 Thread->Status = THREAD_STAT_ZOMBIE;
641 SHORTREL( &glThreadListLock );
643 Threads_Wake( Thread->Parent );
646 Log("Thread %i went *hurk* (%i)", Thread->TID, Status);
657 * \brief Yield remainder of the current thread's timeslice
659 void Threads_Yield(void)
661 // Log("Threads_Yield: by %p", __builtin_return_address(0));
666 * \fn void Threads_Sleep(void)
667 * \brief Take the current process off the run queue
669 void Threads_Sleep(void)
671 tThread *cur = Proc_GetCurThread();
674 SHORTLOCK( &glThreadListLock );
676 // Don't sleep if there is a message waiting
677 if( cur->Messages ) {
678 SHORTREL( &glThreadListLock );
682 // Remove us from running queue
684 // Mark thread as sleeping
685 cur->Status = THREAD_STAT_SLEEPING;
687 // Add to Sleeping List (at the top)
688 cur->Next = gSleepingThreads;
689 gSleepingThreads = cur;
692 #if DEBUG_TRACE_STATE
693 Log("Threads_Sleep: %p (%i %s) sleeping", cur, cur->TID, cur->ThreadName);
697 SHORTREL( &glThreadListLock );
699 while(cur->Status != THREAD_STAT_ACTIVE) {
701 if( cur->Status != THREAD_STAT_ACTIVE )
702 Log("%i - Huh? why am I up? zzzz...", cur->TID);
708 * \brief Wakes a sleeping/waiting thread up
709 * \param Thread Thread to wake
710 * \return Boolean Failure (Returns ERRNO)
711 * \warning This should ONLY be called with task switches disabled
713 int Threads_Wake(tThread *Thread)
718 switch(Thread->Status)
720 case THREAD_STAT_ACTIVE:
721 Log("Threads_Wake - Waking awake thread (%i)", Thread->TID);
724 case THREAD_STAT_SLEEPING:
725 SHORTLOCK( &glThreadListLock );
726 // Remove from sleeping queue
727 Threads_int_DelFromQueue(&gSleepingThreads, Thread);
729 SHORTREL( &glThreadListLock );
730 Threads_AddActive( Thread );
732 #if DEBUG_TRACE_STATE
733 Log("Threads_Sleep: %p (%i %s) woken", Thread, Thread->TID, Thread->ThreadName);
737 case THREAD_STAT_SEMAPHORESLEEP: {
739 tThread *th, *prev=NULL;
741 sem = Thread->WaitPointer;
743 SHORTLOCK( &sem->Protector );
745 // Remove from sleeping queue
746 for( th = sem->Waiting; th; prev = th, th = th->Next )
747 if( th == Thread ) break;
751 prev->Next = Thread->Next;
753 sem->Waiting = Thread->Next;
754 if(sem->LastWaiting == Thread)
755 sem->LastWaiting = prev;
760 for( th = sem->Signaling; th; prev = th, th = th->Next )
761 if( th == Thread ) break;
763 Log_Warning("Threads", "Thread %p(%i %s) is not on semaphore %p(%s:%s)",
764 Thread, Thread->TID, Thread->ThreadName,
765 sem, sem->ModName, sem->Name);
770 prev->Next = Thread->Next;
772 sem->Signaling = Thread->Next;
773 if(sem->LastSignaling == Thread)
774 sem->LastSignaling = prev;
777 Thread->RetStatus = 0; // It didn't get anything
778 Threads_AddActive( Thread );
780 #if DEBUG_TRACE_STATE
781 Log("Threads_Sleep: %p(%i %s) woken from semaphore", Thread, Thread->TID, Thread->ThreadName);
783 SHORTREL( &sem->Protector );
786 case THREAD_STAT_WAITING:
787 Warning("Threads_Wake - Waiting threads are not currently supported");
790 case THREAD_STAT_DEAD:
791 Warning("Threads_Wake - Attempt to wake dead thread (%i)", Thread->TID);
795 Warning("Threads_Wake - Unknown process status (%i)\n", Thread->Status);
801 * \brief Wake a thread given the TID
802 * \param TID Thread ID to wake
803 * \return Boolean Faulure (errno)
805 int Threads_WakeTID(tTID TID)
807 tThread *thread = Threads_GetThread(TID);
811 ret = Threads_Wake( thread );
812 //Log_Debug("Threads", "TID %i woke %i (%p)", Threads_GetTID(), TID, thread);
816 void Threads_ToggleTrace(int TID)
818 tThread *thread = Threads_GetThread(TID);
820 thread->bInstrTrace = !thread->bInstrTrace;
824 * \brief Adds a thread to the active queue
826 void Threads_AddActive(tThread *Thread)
828 SHORTLOCK( &glThreadListLock );
830 if( Thread->Status == THREAD_STAT_ACTIVE ) {
831 tThread *cur = Proc_GetCurThread();
832 Log_Warning("Threads", "WTF, %p CPU%i %p (%i %s) is adding %p (%i %s) when it is active",
833 __builtin_return_address(0),
834 GetCPUNum(), cur, cur->TID, cur->ThreadName, Thread, Thread->TID, Thread->ThreadName);
835 SHORTREL( &glThreadListLock );
840 Thread->Status = THREAD_STAT_ACTIVE;
841 // Thread->CurCPU = -1;
842 // Add to active list
844 tThread *tmp, *prev = NULL;
845 #if SCHEDULER_TYPE == SCHED_RR_PRI
846 for( tmp = gaActiveThreads[Thread->Priority]; tmp; prev = tmp, tmp = tmp->Next );
850 gaActiveThreads[Thread->Priority] = Thread;
852 for( tmp = gActiveThreads; tmp; prev = tmp, tmp = tmp->Next );
856 gActiveThreads = Thread;
861 // Update bookkeeping
862 giNumActiveThreads ++;
864 #if SCHEDULER_TYPE == SCHED_LOTTERY
867 // Only change the ticket count if the thread is un-scheduled
868 if(Thread->CurCPU != -1)
871 delta = caiTICKET_COUNTS[ Thread->Priority ];
873 giFreeTickets += delta;
874 # if DEBUG_TRACE_TICKETS
875 Log("CPU%i %p (%i %s) added, new giFreeTickets = %i [+%i]",
876 GetCPUNum(), Thread, Thread->TID, Thread->ThreadName,
883 SHORTREL( &glThreadListLock );
887 * \brief Removes the current thread from the active queue
888 * \warning This should ONLY be called with the lock held
889 * \return Current thread pointer
891 tThread *Threads_RemActive(void)
893 tThread *ret = Proc_GetCurThread();
895 if( !IS_LOCKED(&glThreadListLock) ) {
896 Log_KernelPanic("Threads", "Threads_RemActive called without lock held");
900 // Delete from active queue
901 #if SCHEDULER_TYPE == SCHED_RR_PRI
902 if( !Threads_int_DelFromQueue(&gaActiveThreads[ret->Priority], ret) )
904 if( !Threads_int_DelFromQueue(&gActiveThreads, ret) )
907 SHORTREL( &glThreadListLock );
908 Log_Warning("Threads", "Current thread %p(%i %s) is not on active queue",
909 ret, ret->TID, ret->ThreadName
917 giNumActiveThreads --;
918 // no need to decrement tickets, scheduler did it for us
920 #if SCHEDULER_TYPE == SCHED_LOTTERY && DEBUG_TRACE_TICKETS
921 Log("CPU%i %p (%i %s) removed, giFreeTickets = %i [nc]",
922 GetCPUNum(), ret, ret->TID, ret->ThreadName, giFreeTickets);
929 * \fn void Threads_SetFaultHandler(Uint Handler)
930 * \brief Sets the signal handler for a signal
932 void Threads_SetFaultHandler(Uint Handler)
934 //Log_Debug("Threads", "Threads_SetFaultHandler: Handler = %p", Handler);
935 Proc_GetCurThread()->FaultHandler = Handler;
939 * \fn void Threads_Fault(int Num)
940 * \brief Calls a fault handler
942 void Threads_Fault(int Num)
944 tThread *thread = Proc_GetCurThread();
948 Log_Log("Threads", "Threads_Fault: thread->FaultHandler = %p", thread->FaultHandler);
950 switch(thread->FaultHandler)
953 Threads_Kill(thread, -1);
956 case 1: // Dump Core?
957 Threads_Kill(thread, -1);
962 // Double Fault? Oh, F**k
963 if(thread->CurFaultNum != 0) {
964 Log_Warning("Threads", "Threads_Fault: Double fault on %i", thread->TID);
965 Threads_Kill(thread, -1); // For now, just kill
969 thread->CurFaultNum = Num;
971 Proc_CallFaultHandler(thread);
975 * \fn void Threads_SegFault(tVAddr Addr)
976 * \brief Called when a Segment Fault occurs
978 void Threads_SegFault(tVAddr Addr)
980 tThread *cur = Proc_GetCurThread();
981 cur->bInstrTrace = 0;
982 Log_Warning("Threads", "Thread #%i committed a segfault at address %p", cur->TID, Addr);
983 MM_DumpTables(0, USER_MAX);
985 //Threads_Exit( 0, -1 );
988 // --- Process Structure Access Functions ---
989 tPID Threads_GetPID(void)
991 return Proc_GetCurThread()->TGID;
993 tTID Threads_GetTID(void)
995 return Proc_GetCurThread()->TID;
997 tUID Threads_GetUID(void)
999 return Proc_GetCurThread()->UID;
1001 tGID Threads_GetGID(void)
1003 return Proc_GetCurThread()->GID;
1006 int Threads_SetUID(Uint *Errno, tUID ID)
1008 tThread *t = Proc_GetCurThread();
1013 Log_Debug("Threads", "TID %i's UID set to %i", t->TID, ID);
1018 int Threads_SetGID(Uint *Errno, tGID ID)
1020 tThread *t = Proc_GetCurThread();
1025 Log_Debug("Threads", "TID %i's GID set to %i", t->TID, ID);
1031 * \fn void Threads_Dump(void)
1033 void Threads_DumpActive(void)
1036 #if SCHEDULER_TYPE == SCHED_RR_PRI
1040 Log("Active Threads: (%i reported)", giNumActiveThreads);
1042 #if SCHEDULER_TYPE == SCHED_RR_PRI
1043 for( i = 0; i < MIN_PRIORITY+1; i++ )
1045 for(thread=gaActiveThreads[i];thread;thread=thread->Next)
1047 for(thread=gActiveThreads;thread;thread=thread->Next)
1050 Log(" %p %i (%i) - %s (CPU %i)",
1051 thread, thread->TID, thread->TGID, thread->ThreadName, thread->CurCPU);
1052 if(thread->Status != THREAD_STAT_ACTIVE)
1053 Log(" ERROR State (%i) != THREAD_STAT_ACTIVE (%i)", thread->Status, THREAD_STAT_ACTIVE);
1054 Log(" Priority %i, Quantum %i", thread->Priority, thread->Quantum);
1055 Log(" KStack 0x%x", thread->KernelStack);
1056 if( thread->bInstrTrace )
1057 Log(" Tracing Enabled");
1058 Proc_DumpThreadCPUState(thread);
1061 #if SCHEDULER_TYPE == SCHED_RR_PRI
1067 * \fn void Threads_Dump(void)
1068 * \brief Dumps a list of currently running threads
1070 void Threads_Dump(void)
1074 Log("--- Thread Dump ---");
1075 Threads_DumpActive();
1077 Log("All Threads:");
1078 for(thread=gAllThreads;thread;thread=thread->GlobalNext)
1080 Log(" %p %i (%i) - %s (CPU %i)",
1081 thread, thread->TID, thread->TGID, thread->ThreadName, thread->CurCPU);
1082 Log(" State %i (%s)", thread->Status, casTHREAD_STAT[thread->Status]);
1083 switch(thread->Status)
1085 case THREAD_STAT_MUTEXSLEEP:
1086 Log(" Mutex Pointer: %p", thread->WaitPointer);
1088 case THREAD_STAT_SEMAPHORESLEEP:
1089 Log(" Semaphore Pointer: %p", thread->WaitPointer);
1090 Log(" Semaphore Name: %s:%s",
1091 ((tSemaphore*)thread->WaitPointer)->ModName,
1092 ((tSemaphore*)thread->WaitPointer)->Name
1095 case THREAD_STAT_ZOMBIE:
1096 Log(" Return Status: %i", thread->RetStatus);
1100 Log(" Priority %i, Quantum %i", thread->Priority, thread->Quantum);
1101 Log(" KStack 0x%x", thread->KernelStack);
1102 if( thread->bInstrTrace )
1103 Log(" Tracing Enabled");
1104 Proc_DumpThreadCPUState(thread);
1109 * \brief Gets the next thread to run
1110 * \param CPU Current CPU
1111 * \param Last The thread the CPU was running
1113 tThread *Threads_GetNextToRun(int CPU, tThread *Last)
1117 // If this CPU has the lock, we must let it complete
1118 if( CPU_HAS_LOCK( &glThreadListLock ) )
1121 // Don't change threads if the current CPU has switches disabled
1122 if( gaThreads_NoTaskSwitch[CPU] )
1126 SHORTLOCK( &glThreadListLock );
1128 // Clear Delete Queue
1129 // - I should probably put this in a worker thread to avoid calling free() in the scheduler
1130 // DEFINITELY - free() can deadlock in this case
1131 // I'll do it when it becomes an issue
1132 while(gDeleteThreads)
1134 thread = gDeleteThreads->Next;
1135 // Only free if structure is unused
1136 if( !IS_LOCKED(&gDeleteThreads->IsLocked) )
1139 gDeleteThreads->Status = THREAD_STAT_BURIED;
1141 if( IsHeap(gDeleteThreads->ThreadName) )
1142 free(gDeleteThreads->ThreadName);
1143 // Remove from global list
1144 if( gDeleteThreads == gAllThreads )
1145 gAllThreads = gDeleteThreads->GlobalNext;
1147 gDeleteThreads->GlobalPrev->GlobalNext = gDeleteThreads->GlobalNext;
1148 free( gDeleteThreads );
1150 gDeleteThreads = thread;
1153 // Make sure the current (well, old) thread is marked as de-scheduled
1154 if(Last) Last->CurCPU = -1;
1156 // No active threads, just take a nap
1157 if(giNumActiveThreads == 0) {
1158 SHORTREL( &glThreadListLock );
1159 #if DEBUG_TRACE_TICKETS
1160 Log("No active threads");
1165 #if SCHEDULER_TYPE != SCHED_RR_PRI
1166 // Special case: 1 thread
1167 if(giNumActiveThreads == 1) {
1168 if( gActiveThreads->CurCPU == -1 )
1169 gActiveThreads->CurCPU = CPU;
1171 SHORTREL( &glThreadListLock );
1173 if( gActiveThreads->CurCPU == CPU )
1174 return gActiveThreads;
1176 return NULL; // CPU has nothing to do
1180 // Allow the old thread to be scheduled again
1182 if( Last->Status == THREAD_STAT_ACTIVE ) {
1183 #if SCHEDULER_TYPE == SCHED_LOTTERY
1184 giFreeTickets += caiTICKET_COUNTS[ Last->Priority ];
1185 # if DEBUG_TRACE_TICKETS
1186 LogF("Log: CPU%i released %p (%i %s) into the pool (%i [+%i] tickets in pool)\n",
1187 CPU, Last, Last->TID, Last->ThreadName, giFreeTickets,
1188 caiTICKET_COUNTS[ Last->Priority ]);
1192 #if SCHEDULER_TYPE == SCHED_LOTTERY && DEBUG_TRACE_TICKETS
1194 LogF("Log: CPU%i released %p (%i %s)->Status = %i (Released,not in pool)\n",
1195 CPU, Last, Last->TID, Last->ThreadName, Last->Status);
1201 // Lottery Scheduler
1203 #if SCHEDULER_TYPE == SCHED_LOTTERY
1208 for(thread = gActiveThreads; thread; thread = thread->Next) {
1209 if(thread->CurCPU >= 0) continue;
1210 if(thread->Status != THREAD_STAT_ACTIVE)
1211 Panic("Bookkeeping fail - %p %i(%s) is on the active queue with a status of %i",
1212 thread, thread->TID, thread->ThreadName, thread->Status);
1213 if(thread->Next == thread) {
1214 Panic("Bookkeeping fail - %p %i(%s) loops back on itself",
1215 thread, thread->TID, thread->ThreadName, thread->Status);
1217 number += caiTICKET_COUNTS[ thread->Priority ];
1219 if(number != giFreeTickets) {
1220 Panic("Bookkeeping fail (giFreeTickets(%i) != number(%i)) - CPU%i",
1221 giFreeTickets, number, CPU);
1225 // No free tickets (all tasks delegated to cores)
1226 if( giFreeTickets == 0 ) {
1227 SHORTREL(&glThreadListLock);
1231 // Get the ticket number
1232 ticket = number = rand() % giFreeTickets;
1234 // Find the next thread
1235 for(thread=gActiveThreads;thread;thread=thread->Next)
1237 if(thread->CurCPU >= 0) continue;
1238 if( caiTICKET_COUNTS[ thread->Priority ] > number) break;
1239 number -= caiTICKET_COUNTS[ thread->Priority ];
1242 // If we didn't find a thread, something went wrong
1246 for(thread=gActiveThreads;thread;thread=thread->Next) {
1247 if(thread->CurCPU >= 0) continue;
1248 number += caiTICKET_COUNTS[ thread->Priority ];
1250 Panic("Bookeeping Failed - giFreeTickets(%i) > true count (%i)",
1251 giFreeTickets, number);
1254 giFreeTickets -= caiTICKET_COUNTS[ thread->Priority ];
1255 # if DEBUG_TRACE_TICKETS
1256 LogF("Log: CPU%i allocated %p (%i %s), (%i [-%i] tickets in pool), \n",
1257 CPU, thread, thread->TID, thread->ThreadName,
1258 giFreeTickets, caiTICKET_COUNTS[ thread->Priority ]);
1263 // Priority based round robin scheduler
1265 #elif SCHEDULER_TYPE == SCHED_RR_PRI
1268 for( i = 0; i < MIN_PRIORITY + 1; i ++ )
1270 for(thread = gaActiveThreads[i]; thread; thread = thread->Next)
1272 if( thread->CurCPU == -1 ) break;
1274 // If we fall onto the same queue again, special handling is
1276 if( Last && Last->Status == THREAD_STAT_ACTIVE && i == Last->Priority ) {
1277 tThread *savedThread = thread;
1279 // Find the next unscheduled thread in the list
1280 for( thread = Last->Next; thread; thread = thread->Next )
1282 if( thread->CurCPU == -1 ) break;
1284 // If we don't find anything after, just use the one
1286 if( !thread ) thread = savedThread;
1288 // Found a thread? Schedule it!
1294 SHORTREL(&glThreadListLock);
1297 if( thread->Status != THREAD_STAT_ACTIVE ) {
1298 LogF("Oops, Thread %i (%s) is not active\n", thread->TID, thread->ThreadName);
1301 #elif SCHEDULER_TYPE == SCHED_RR_SIM
1303 // Find the next unscheduled thread in the list
1304 for( thread = Last->Next; thread; thread = thread->Next )
1306 if( thread->CurCPU == -1 ) break;
1308 // If we don't find anything after, search from the beginning
1311 for(thread = gActiveThreads; thread; thread = thread->Next)
1313 if( thread->CurCPU == -1 ) break;
1319 SHORTREL(&glThreadListLock);
1324 # error "Unimplemented scheduling algorithm"
1327 // Make the new thread non-schedulable
1328 thread->CurCPU = CPU;
1329 thread->Remaining = thread->Quantum;
1331 SHORTREL( &glThreadListLock );
1337 EXPORT(Threads_GetUID);
1338 EXPORT(Threads_GetGID);