80b2796680d39dba3c344d1b54aec7dc2f8b8140
[tpg/acess2.git] / KernelLand / Kernel / threads.c
1 /*
2  * Acess2 Kernel
3  * - By John Hodge (thePowersGang)
4  * threads.c
5  * - Common Thread Control
6  */
7 #include <acess.h>
8 #include <threads.h>
9 #include <threads_int.h>
10 #include <errno.h>
11 #include <hal_proc.h>
12 #include <semaphore.h>
13 #include <rwlock.h>
14 #include <vfs_threads.h>        // VFS Handle maintainence
15 #include <events.h>
16 #include <debug_hooks.h>
17
18 // Configuration
19 #define DEBUG_TRACE_ACTIVEQUEUE 0       // Trace adds/removals from the active queue
20 #define DEBUG_TRACE_TICKETS     0       // Trace ticket counts
21 #define DEBUG_TRACE_STATE       0       // Trace state changes (sleep/wake)
22 #define DEBUG_TRACE_SCHEDULE    0       // Trace scheduling
23
24 // --- Schedulers ---
25 #define SCHED_UNDEF     0
26 #define SCHED_LOTTERY   1       // Lottery scheduler
27 #define SCHED_RR_SIM    2       // Single Queue Round Robin
28 #define SCHED_RR_PRI    3       // Multi Queue Round Robin
29 // Set scheduler type
30 #define SCHEDULER_TYPE  SCHED_RR_PRI
31
32 // === CONSTANTS ===
33 #define DEFAULT_QUANTUM 5
34 #define DEFAULT_PRIORITY        5
35 #define MIN_PRIORITY            10
36
37 #define PRIthread_fmt   "%p(%i %s)"
38 #define PRIthread_args(t)       (t), ((t)?(t)->TID:-1), ((t)?(t)->ThreadName:"-")
39
40 // === IMPORTS ===
41 extern void     User_Signal_Kill(int SigNum);
42
43 // === TYPE ===
44 typedef struct
45 {
46         tThread *Head;
47         tThread *Tail;
48 } tThreadList;
49
50 // === PROTOTYPES ===
51 void    Threads_Init(void);
52 #if 0
53 void    Threads_Delete(tThread *Thread);
54  int    Threads_SetName(const char *NewName);
55 #endif
56 char    *Threads_GetName(tTID ID);
57 #if 0
58 void    Threads_SetPriority(tThread *Thread, int Pri);
59 tThread *Threads_CloneTCB(Uint Flags);
60  int    Threads_WaitTID(int TID, int *status);
61 tThread *Threads_GetThread(Uint TID);
62 #endif
63 tThread *Threads_int_DelFromQueue(tThreadList *List, tThread *Thread);
64 void    Threads_int_AddToList(tThreadList *List, tThread *Thread);
65 #if 0
66 void    Threads_Exit(int TID, int Status);
67 void    Threads_Kill(tThread *Thread, int Status);
68 void    Threads_Yield(void);
69  int    Threads_int_Sleep(enum eThreadStatus Status, void *Ptr, int Num, tThread **ListHead, tThread **ListTail, tShortSpinlock *Lock);
70 void    Threads_Sleep(void);
71  int    Threads_Wake(tThread *Thread);
72 void    Threads_AddActive(tThread *Thread);
73 tThread *Threads_RemActive(void);
74 void    Threads_ToggleTrace(int TID);
75 #endif
76 void    Threads_Fault(int Num);
77 void    Threads_SegFault(tVAddr Addr);
78 void    Threads_PostSignalTo(tThread *Thread, int SignalNum);
79 #if 0
80 void    Threads_PostSignal(int SignalNum);
81 void    Threads_SignalGroup(tPGID PGID, int SignalNum);
82 #endif
83  int    Threads_GetPendingSignal(void);
84 void    Threads_SetSignalHandler(int SignalNum, void *Handler);
85 void    *Threads_GetSignalHandler(int SignalNum);
86 #if 0
87  int    Threads_GetPID(void);
88  int    Threads_GetTID(void);
89 tUID    Threads_GetUID(void);
90 tGID    Threads_GetGID(void);
91  int    Threads_SetUID(Uint *Errno, tUID ID);
92  int    Threads_SetGID(Uint *Errno, tUID ID);
93 #endif
94 void    Threads_int_DumpThread(tThread *thread);
95 #if 0
96 void    Threads_Dump(void);
97 #endif
98 void    Threads_DumpActive(void);
99 tThread *Threads_int_GetRunnable(void);
100
101 // === GLOBALS ===
102 // -- Core Thread --
103 tProcess        gProcessZero = {
104         };
105 // Only used for the core kernel
106 tThread gThreadZero = {
107         .Status         = THREAD_STAT_ACTIVE,   // Status
108         .ThreadName     = (char*)"ThreadZero",  // Name
109         .Quantum        = DEFAULT_QUANTUM,      // Default Quantum
110         .Remaining      = DEFAULT_QUANTUM,      // Current Quantum
111         .Priority       = DEFAULT_PRIORITY      // Number of tickets
112         };
113 // -- Processes --
114 tProcess        *gAllProcesses = &gProcessZero;
115 // --- Locks ---
116 tShortSpinlock  glThreadListLock;       ///\note NEVER use a heap function while locked
117 // --- Current State ---
118 volatile int    giNumActiveThreads = 0; // Number of threads on the active queue
119 volatile Uint   giNextTID = 1;  // Next TID to allocate
120 // --- Thread Lists ---
121 tThread *gAllThreads = NULL;            // All allocated threads
122 tThreadList     gSleepingThreads;       // Sleeping Threads
123  int    giNumCPUs = 1;  // Number of CPUs
124 BOOL     gaThreads_NoTaskSwitch[MAX_CPUS];      // Disables task switches for each core (Pseudo-IF)
125 // --- Scheduler Types ---
126 #if SCHEDULER_TYPE == SCHED_LOTTERY
127 const int       caiTICKET_COUNTS[MIN_PRIORITY+1] = {100,81,64,49,36,25,16,9,4,1,0};
128 volatile int    giFreeTickets = 0;      // Number of tickets held by non-scheduled threads
129 tThreadList     gActiveThreads;         // Currently Running Threads
130 #elif SCHEDULER_TYPE == SCHED_RR_SIM
131 tThreadList     gActiveThreads;         // Currently Running Threads
132 #elif SCHEDULER_TYPE == SCHED_RR_PRI
133 tThreadList     gaActiveThreads[MIN_PRIORITY+1];        // Active threads for each priority level
134 #else
135 # error "Unkown scheduler type"
136 #endif
137
138 // === CODE ===
139 /**
140  * \fn void Threads_Init(void)
141  * \brief Initialse the thread list
142  */
143 void Threads_Init(void)
144 {
145         ArchThreads_Init();
146         
147         Log_Debug("Threads", "Offsets of tThread");
148         Log_Debug("Threads", ".Priority = %i", offsetof(tThread, Priority));
149         Log_Debug("Threads", ".KernelStack = %i", offsetof(tThread, KernelStack));
150         
151         // Create Initial Task
152         gAllThreads = &gThreadZero;
153         giNumActiveThreads = 1;
154         gThreadZero.Process = &gProcessZero;
155                 
156         Proc_Start();
157 }
158
159 void Threads_Delete(tThread *Thread)
160 {
161         // Set to dead
162         Thread->Status = THREAD_STAT_BURIED;
163
164         // Clear out process state
165         Proc_ClearThread(Thread);                       
166
167         Thread->Process->nThreads --;
168
169         if( Thread->Process->FirstThread == Thread )
170         {
171                 Thread->Process->FirstThread = Thread->ProcessNext;
172         }
173         else
174         {
175                 tThread *prev = Thread->Process->FirstThread;
176                 while(prev && prev->ProcessNext != Thread)
177                         prev = prev->ProcessNext;
178                 if( !prev )
179                         Log_Error("Threads", "Thread %p(%i %s) is not on the process's list",
180                                 Thread, Thread->TID, Thread->ThreadName
181                                 );
182                 else
183                         prev->ProcessNext = Thread->ProcessNext;
184         }
185
186         // If the final thread is being terminated, clean up the process
187         if( Thread->Process->nThreads == 0 )
188         {
189                 tProcess        *proc = Thread->Process;
190
191                 // Remove from global process list
192                 // TODO: RWLock
193                 if(proc->Prev)
194                         proc->Prev->Next = proc->Next;
195                 else
196                         gAllProcesses = proc->Next;
197                 if(proc->Next)
198                         proc->Next->Prev = proc->Prev;
199
200                 // VFS Cleanup
201                 VFS_CloseAllUserHandles( proc );
202                 // Architecture cleanup
203                 Proc_ClearProcess( proc );
204                 // VFS Configuration strings
205                 if( proc->CurrentWorkingDir)
206                         free( proc->CurrentWorkingDir );
207                 if( proc->RootDir )
208                         free( proc->RootDir );
209                 // Process descriptor
210                 free( proc );
211         }
212         
213         // Free name
214         if( IsHeap(Thread->ThreadName) )
215                 free(Thread->ThreadName);
216         
217         // Remove from global list
218         // TODO: Lock this too
219         if( Thread == gAllThreads )
220                 gAllThreads = Thread->GlobalNext;
221         else
222                 Thread->GlobalPrev->GlobalNext = Thread->GlobalNext;
223         
224         free(Thread);
225 }
226
227 /**
228  * \fn void Threads_SetName(const char *NewName)
229  * \brief Sets the current thread's name
230  * \param NewName       New name for the thread
231  * \return Boolean Failure
232  */
233 int Threads_SetName(const char *NewName)
234 {
235         tThread *cur = Proc_GetCurThread();
236         char    *oldname = cur->ThreadName;
237         
238         // NOTE: There is a possibility of non-thread safety here
239         // A thread could read the current name pointer before it is zeroed
240         
241         cur->ThreadName = NULL;
242         
243         if( IsHeap(oldname) )   free( oldname );        
244         cur->ThreadName = strdup(NewName);
245
246         Log_Debug("Threads", "Thread renamed to '%s'", NewName);        
247
248         return 0;
249 }
250
251 /**
252  * \fn char *Threads_GetName(int ID)
253  * \brief Gets a thread's name
254  * \param ID    Thread ID (-1 indicates current thread)
255  * \return Pointer to name
256  * \retval NULL Failure
257  */
258 char *Threads_GetName(tTID ID)
259 {
260         if(ID == -1) {
261                 return Proc_GetCurThread()->ThreadName;
262         }
263         return Threads_GetThread(ID)->ThreadName;
264 }
265
266 /**
267  * \fn void Threads_SetPriority(tThread *Thread, int Pri)
268  * \brief Sets the priority of a task
269  * \param Thread        Thread to update ticket count (NULL means current thread)
270  * \param Pri   New priority
271  */
272 void Threads_SetPriority(tThread *Thread, int Pri)
273 {
274         // Get current thread
275         if(Thread == NULL)      Thread = Proc_GetCurThread();
276         // Bounds checking
277         // - If < 0, set to lowest priority
278         // - Minumum priority is actualy a high number, 0 is highest
279         if(Pri < 0)     Pri = MIN_PRIORITY;
280         if(Pri > MIN_PRIORITY)  Pri = MIN_PRIORITY;
281         
282         // Do we actually have to do anything?
283         if( Pri == Thread->Priority )   return;
284         
285         #if SCHEDULER_TYPE == SCHED_RR_PRI
286         if( Thread != Proc_GetCurThread() )
287         {
288                 SHORTLOCK( &glThreadListLock );
289                 // Remove from old priority
290                 Threads_int_DelFromQueue( &gaActiveThreads[Thread->Priority], Thread );
291                 // And add to new
292                 Threads_int_AddToList( &gaActiveThreads[Pri], Thread );
293                 Thread->Priority = Pri;
294                 SHORTREL( &glThreadListLock );
295         }
296         else
297                 Thread->Priority = Pri;
298         #else
299         // If this isn't the current thread, we need to lock
300         if( Thread != Proc_GetCurThread() )
301         {
302                 SHORTLOCK( &glThreadListLock );
303                 
304                 #if SCHEDULER_TYPE == SCHED_LOTTERY
305                 giFreeTickets -= caiTICKET_COUNTS[Thread->Priority] - caiTICKET_COUNTS[Pri];
306                 # if DEBUG_TRACE_TICKETS
307                 Log("Threads_SetTickets: new giFreeTickets = %i [-%i+%i]",
308                         giFreeTickets,
309                         caiTICKET_COUNTS[Thread->Priority], caiTICKET_COUNTS[Pri]);
310                 # endif
311                 #endif
312                 Thread->Priority = Pri;
313                 SHORTREL( &glThreadListLock );
314         }
315         else
316                 Thread->Priority = Pri;
317         #endif
318         
319         #if DEBUG_TRACE_STATE
320         Log("Threads_SetPriority: %p(%i %s) pri set %i",
321                 Thread, Thread->TID, Thread->ThreadName,
322                 Pri);
323         #endif
324 }
325
326 /**
327  * \brief Clone the TCB of the current thread
328  * \param Flags Flags for something... (What is this for?)
329  */
330 tThread *Threads_CloneTCB(Uint Flags)
331 {
332         tThread *cur, *new;
333         cur = Proc_GetCurThread();
334         
335         // Allocate and duplicate
336         new = malloc(sizeof(tThread));
337         if(new == NULL) { errno = -ENOMEM; return NULL; }
338         memcpy(new, cur, sizeof(tThread));
339         
340         new->CurCPU = -1;
341         new->Next = NULL;
342         memset( &new->IsLocked, 0, sizeof(new->IsLocked));
343         new->Status = THREAD_STAT_PREINIT;
344         new->RetStatus = 0;
345         
346         // Get Thread ID
347         new->TID = giNextTID++;
348         new->Parent = cur;
349         new->bInstrTrace = 0;
350         
351         // Clone Name
352         new->ThreadName = strdup(cur->ThreadName);
353         
354         // Set Thread Group ID (PID)
355         if(Flags & CLONE_VM) {
356                 tProcess        *newproc, *oldproc;
357                 oldproc = cur->Process;
358                 new->Process = calloc( sizeof(struct sProcess), 1 );
359                 newproc = new->Process;
360                 newproc->PID = new->TID;
361                 if( Flags & CLONE_PGID )
362                         newproc->PGID = oldproc->PGID;
363                 else
364                         newproc->PGID = newproc->PID;
365                 newproc->UID = oldproc->UID;
366                 newproc->GID = oldproc->GID;
367                 newproc->MaxFD = oldproc->MaxFD;
368                 newproc->CurrentWorkingDir = oldproc->CurrentWorkingDir ? strdup( oldproc->CurrentWorkingDir ) : NULL;
369                 newproc->RootDir = oldproc->RootDir ? strdup( oldproc->RootDir ) : NULL;
370                 newproc->nThreads = 1;
371                 // Reference all handles in the VFS
372                 VFS_ReferenceUserHandles();
373                 
374                 // Add to global list
375                 newproc->Prev = NULL;
376                 // TODO: RWLock
377                 newproc->Next = gAllProcesses;
378                 gAllProcesses = newproc;
379
380                 newproc->FirstThread = new;
381                 new->ProcessNext = NULL;
382         }
383         else {
384                 new->Process->nThreads ++;
385                 new->Process = cur->Process;
386                 // TODO: Locking
387                 new->ProcessNext = new->Process->FirstThread;
388                 new->Process->FirstThread = new;
389         }
390         
391         // Messages are not inherited
392         new->Messages = NULL;
393         new->LastMessage = NULL;
394         
395         // Set State
396         new->Remaining = new->Quantum = cur->Quantum;
397         new->Priority = cur->Priority;
398         new->_errno = 0;
399         
400         // Set Signal Handlers
401         new->CurFaultNum = 0;
402         new->FaultHandler = cur->FaultHandler;
403         
404         // Maintain a global list of threads
405         SHORTLOCK( &glThreadListLock );
406         new->GlobalPrev = NULL; // Protect against bugs
407         new->GlobalNext = gAllThreads;
408         gAllThreads->GlobalPrev = new;
409         gAllThreads = new;
410         SHORTREL( &glThreadListLock );
411         
412         return new;
413 }
414
415 /**
416  * \brief Clone the TCB of the kernel thread
417  */
418 tThread *Threads_CloneThreadZero(void)
419 {
420         tThread *new;
421         
422         // Allocate and duplicate
423         new = malloc(sizeof(tThread));
424         if(new == NULL) {
425                 return NULL;
426         }
427         memcpy(new, &gThreadZero, sizeof(tThread));
428
429         new->Process->nThreads ++;
430         
431         new->CurCPU = -1;
432         new->Next = NULL;
433         memset( &new->IsLocked, 0, sizeof(new->IsLocked));
434         new->Status = THREAD_STAT_PREINIT;
435         new->RetStatus = 0;
436         
437         // Get Thread ID
438         new->TID = giNextTID++;
439         new->Parent = 0;
440         
441         // Clone Name
442         new->ThreadName = NULL;
443         
444         // Messages are not inherited
445         new->Messages = NULL;
446         new->LastMessage = NULL;
447         
448         // Set State
449         new->Remaining = new->Quantum = DEFAULT_QUANTUM;
450         new->Priority = DEFAULT_PRIORITY;
451         new->bInstrTrace = 0;
452         
453         // Set Signal Handlers
454         new->CurFaultNum = 0;
455         new->FaultHandler = 0;
456         
457         // Maintain a global list of threads
458         SHORTLOCK( &glThreadListLock );
459         new->GlobalPrev = NULL; // Protect against bugs
460         new->GlobalNext = gAllThreads;
461         gAllThreads->GlobalPrev = new;
462         gAllThreads = new;
463         SHORTREL( &glThreadListLock );
464         
465         return new;
466 }
467
468 /**
469  * \brief Wait for a task to change state
470  * \param TID   Thread ID to wait on (-1: Any child thread, 0: Any Child/Sibling, <-1: -PID)
471  * \param Status        Thread return status
472  * \return TID of child that changed state
473  */
474 tTID Threads_WaitTID(int TID, int *Status)
475 {       
476         // Any Child
477         if(TID == -1)
478         {
479                 Uint32 ev = Threads_WaitEvents(THREAD_EVENT_DEADCHILD);
480                 tTID    ret = -1;
481                 if( ev & THREAD_EVENT_DEADCHILD )
482                 {
483                         tThread * const us = Proc_GetCurThread();
484                         // A child died, get the TID
485                         ASSERT(us->LastDeadChild);
486                         tThread *dead_thread = us->LastDeadChild;
487                         us->LastDeadChild = dead_thread->Next;
488                         if( us->LastDeadChild )
489                                 Threads_PostEvent( us, THREAD_EVENT_DEADCHILD );
490                         else
491                                 Threads_ClearEvent( THREAD_EVENT_DEADCHILD );
492                         Mutex_Release(&us->DeadChildLock);
493                         
494                         ret = dead_thread->TID;
495                         // - Mark as dead (as opposed to undead)
496                         ASSERTC(dead_thread->Status, ==, THREAD_STAT_ZOMBIE);
497                         dead_thread->Status = THREAD_STAT_DEAD;
498                         // - Set return status
499                         if(Status)
500                                 *Status = dead_thread->RetStatus;
501                         
502                         Threads_Delete( dead_thread );
503                 }
504                 else
505                 {
506                         Log_Error("Threads", "TODO: Threads_WaitTID(TID=-1) - Any Child");
507                 }
508                 return ret;
509         }
510         
511         // Any peer/child thread
512         if(TID == 0) {
513                 Log_Error("Threads", "TODO: Threads_WaitTID(TID=0) - Any Child/Sibling");
514                 return -1;
515         }
516         
517         // TGID = abs(TID)
518         if(TID < -1) {
519                 Log_Error("Threads", "TODO: Threads_WaitTID(TID<0) - TGID");
520                 return -1;
521         }
522         
523         // Specific Thread
524         if(TID > 0)
525         {
526                 // TODO: Register on thread to be poked when it dies
527                 tTID    ret;
528                 // NOTE: Race condition - Other child dies, desired child dies, first death is 'lost'
529                 while( (ret = Threads_WaitTID(-1, Status)) != TID )
530                 {
531                         if( ret == -1 )
532                                 break;
533                 }
534                 return ret;
535         }
536         
537         return -1;
538 }
539
540 /**
541  * \brief Gets a thread given its TID
542  * \param TID   Thread ID
543  * \return Thread pointer
544  */
545 tThread *Threads_GetThread(Uint TID)
546 {
547         tThread *thread;
548         
549         // Search global list
550         for( thread = gAllThreads; thread; thread = thread->GlobalNext )
551         {
552                 if(thread->TID == TID)
553                         return thread;
554         }
555
556         Log_Notice("Threads", "Unable to find TID %i on main list\n", TID);
557         
558         return NULL;
559 }
560
561 /**
562  * \brief Deletes an entry from a list
563  * \param List  Pointer to the list head
564  * \param Thread        Thread to find
565  * \return \a Thread
566  */
567 tThread *Threads_int_DelFromQueue(tThreadList *List, tThread *Thread)
568 {
569         tThread *ret, *prev = NULL;
570         
571         for(ret = List->Head;
572                 ret && ret != Thread;
573                 prev = ret, ret = ret->Next
574                 );
575         
576         // Is the thread on the list
577         if(!ret) {
578                 //LogF("%p(%s) is not on list %p\n", Thread, Thread->ThreadName, List);
579                 return NULL;
580         }
581         
582         if( !prev ) {
583                 List->Head = Thread->Next;
584                 //LogF("%p(%s) removed from head of %p\n", Thread, Thread->ThreadName, List);
585         }
586         else {
587                 prev->Next = Thread->Next;
588                 //LogF("%p(%s) removed from %p (prev=%p)\n", Thread, Thread->ThreadName, List, prev);
589         }
590         if( Thread->Next == NULL )
591                 List->Tail = prev;
592         
593         return Thread;
594 }
595
596 void Threads_int_AddToList(tThreadList *List, tThread *Thread)
597 {
598         if( List->Head )
599                 List->Tail->Next = Thread;
600         else
601                 List->Head = Thread;
602         List->Tail = Thread;
603         Thread->Next = NULL;
604 }
605
606 /**
607  * \brief Exit the current process (or another?)
608  * \param TID   Thread ID to kill
609  * \param Status        Exit status
610  */
611 void Threads_Exit(int TID, int Status)
612 {
613         if( TID == 0 )
614                 Threads_Kill( Proc_GetCurThread(), (Uint)Status & 0xFF );
615         else
616                 Threads_Kill( Threads_GetThread(TID), (Uint)Status & 0xFF );
617         
618         // Halt forever, just in case
619         for(;;) HALT();
620 }
621
622 /**
623  * \fn void Threads_Kill(tThread *Thread, int Status)
624  * \brief Kill a thread
625  * \param Thread        Thread to kill
626  * \param Status        Status code to return to the parent
627  */
628 void Threads_Kill(tThread *Thread, int Status)
629 {
630         tMsg    *msg;
631          int    isCurThread = Thread == Proc_GetCurThread();
632         
633         // TODO: Disown all children?
634         #if 1
635         {
636                 tThread *child;
637                 // TODO: I should keep a .Children list
638                 for(child = gAllThreads;
639                         child;
640                         child = child->GlobalNext)
641                 {
642                         if(child->Parent == Thread)
643                                 child->Parent = &gThreadZero;
644                 }
645         }
646         #endif
647         
648         ///\note Double lock is needed due to overlap of lock areas
649         
650         // Lock thread (stop us recieving messages)
651         SHORTLOCK( &Thread->IsLocked );
652         
653         // Clear Message Queue
654         while( Thread->Messages )
655         {
656                 msg = Thread->Messages->Next;
657                 free( Thread->Messages );
658                 Thread->Messages = msg;
659         }
660         
661         // Lock thread list
662         SHORTLOCK( &glThreadListLock );
663         
664         switch(Thread->Status)
665         {
666         case THREAD_STAT_PREINIT:       // Only on main list
667                 break;
668         
669         // Currently active thread
670         case THREAD_STAT_ACTIVE:
671                 if( Thread != Proc_GetCurThread() )
672                 {
673                         #if SCHEDULER_TYPE == SCHED_RR_PRI
674                         tThreadList     *list = &gaActiveThreads[Thread->Priority];
675                         #else
676                         tThreadList     *list = &gActiveThreads;
677                         #endif
678                         if( Threads_int_DelFromQueue( list, Thread ) )
679                         {
680                         }
681                         else
682                         {
683                                 Log_Warning("Threads",
684                                         "Threads_Kill - Thread %p(%i,%s) marked as active, but not on list",
685                                         Thread, Thread->TID, Thread->ThreadName
686                                         );
687                         }
688                         #if SCHEDULER_TYPE == SCHED_LOTTERY
689                         giFreeTickets -= caiTICKET_COUNTS[ Thread->Priority ];
690                         #endif
691                 }
692                 // Ensure that we are not rescheduled
693                 Thread->Remaining = 0;  // Clear Remaining Quantum
694                 Thread->Quantum = 0;    // Clear Quantum to indicate dead thread
695                         
696                 // Update bookkeeping
697                 giNumActiveThreads --;
698                 break;
699         // Kill it while it sleeps!
700         case THREAD_STAT_SLEEPING:
701                 if( !Threads_int_DelFromQueue( &gSleepingThreads, Thread ) )
702                 {
703                         Log_Warning("Threads",
704                                 "Threads_Kill - Thread %p(%i,%s) marked as sleeping, but not on list",
705                                 Thread, Thread->TID, Thread->ThreadName
706                                 );
707                 }
708                 break;
709         
710         // Brains!... You cannot kill something that is already dead
711         case THREAD_STAT_ZOMBIE:
712                 Log_Warning("Threads", "Threads_Kill - Thread %p(%i,%s) is undead, you cannot kill it",
713                         Thread, Thread->TID, Thread->ThreadName);
714                 SHORTREL( &glThreadListLock );
715                 SHORTREL( &Thread->IsLocked );
716                 return ;
717         
718         default:
719                 Log_Warning("Threads", "Threads_Kill - BUG Un-checked status (%i)",
720                         Thread->Status);
721                 break;
722         }
723         
724         // Save exit status
725         Thread->RetStatus = Status;
726
727         SHORTREL( &Thread->IsLocked );
728
729         Thread->Status = THREAD_STAT_ZOMBIE;
730         SHORTREL( &glThreadListLock );
731         // TODO: It's possible that we could be timer-preempted here, should disable that... somehow
732         Mutex_Acquire( &Thread->Parent->DeadChildLock );        // released by parent
733         Thread->Next = Thread->Parent->LastDeadChild;
734         Thread->Parent->LastDeadChild = Thread;
735         Threads_PostEvent( Thread->Parent, THREAD_EVENT_DEADCHILD );
736         
737         // Process cleanup happens on reaping
738         Log("Thread %i went *hurk* (%i)", Thread->TID, Status);
739         
740         // And, reschedule
741         if(isCurThread)
742         {
743                 for( ;; )
744                         Proc_Reschedule();
745         }
746 }
747
748 /**
749  * \brief Yield remainder of the current thread's timeslice
750  */
751 void Threads_Yield(void)
752 {
753 //      Log("Threads_Yield: by %p", __builtin_return_address(0));
754         Proc_Reschedule();
755 }
756
757 /**
758  * \breif Wait for the thread status to not be a specified value
759  */
760 void Threads_int_WaitForStatusEnd(enum eThreadStatus Status)
761 {
762         tThread *us = Proc_GetCurThread();
763         ASSERT(Status != THREAD_STAT_ACTIVE);
764         ASSERT(Status != THREAD_STAT_DEAD);
765         while( us->Status == Status )
766         {
767                 Proc_Reschedule();
768                 if( us->Status == Status )
769                         Debug("Thread %p(%i %s) rescheduled while in %s state for %p",
770                                 us, us->TID, us->ThreadName,
771                                 casTHREAD_STAT[Status],
772                                 __builtin_return_address(0));
773         }
774 }
775
776 int Threads_int_Sleep(enum eThreadStatus Status, void *Ptr, int Num, tThread **ListHead, tThread **ListTail, tShortSpinlock *Lock)
777 {
778         SHORTLOCK( &glThreadListLock );
779         tThread *us = Threads_RemActive();
780         us->Next = NULL;
781         // - Mark as sleeping
782         us->Status = Status;
783         us->WaitPointer = Ptr;
784         us->RetStatus = Num;    // Use RetStatus as a temp variable
785                 
786         // - Add to waiting
787         if( ListTail ) {
788                 if(*ListTail) {
789                         (*ListTail)->Next = us;
790                 }
791                 else {
792                         *ListHead = us;
793                 }
794                 *ListTail = us;
795         }
796         else if( ListHead ) {
797                 us->Next = *ListHead;
798                 *ListHead = us;
799         }
800         else {
801                 // Nothing
802         }
803         
804         //if( Proc_ThreadSync(us) )
805         //      return ;
806         SHORTREL( &glThreadListLock );
807         if( Lock )
808                 SHORTREL( Lock );
809         Threads_int_WaitForStatusEnd(Status);
810         us->WaitPointer = NULL;
811         return us->RetStatus;
812 }
813
814 /**
815  * \fn void Threads_Sleep(void)
816  * \brief Take the current process off the run queue
817  */
818 void Threads_Sleep(void)
819 {
820         tThread *cur = Proc_GetCurThread();
821         
822         // Acquire Spinlock
823         SHORTLOCK( &glThreadListLock );
824         
825         // Don't sleep if there is a message waiting
826         if( cur->Messages ) {
827                 SHORTREL( &glThreadListLock );
828                 return;
829         }
830         
831         // Remove us from running queue
832         Threads_RemActive();
833         // Mark thread as sleeping
834         cur->Status = THREAD_STAT_SLEEPING;
835         
836         // Add to Sleeping List (at the top)
837         Threads_int_AddToList( &gSleepingThreads, cur );
838         
839         #if DEBUG_TRACE_STATE
840         Log("Threads_Sleep: %p (%i %s) sleeping", cur, cur->TID, cur->ThreadName);
841         #endif
842         
843         // Release Spinlock
844         SHORTREL( &glThreadListLock );
845         Threads_int_WaitForStatusEnd(THREAD_STAT_SLEEPING);
846 }
847
848
849 /**
850  * \brief Wakes a sleeping/waiting thread up
851  * \param Thread        Thread to wake
852  * \return Boolean Failure (Returns ERRNO)
853  * \warning This should ONLY be called with task switches disabled
854  */
855 int Threads_Wake(tThread *Thread)
856 {
857         if(!Thread)
858                 return -EINVAL;
859         
860         switch(Thread->Status)
861         {
862         case THREAD_STAT_ACTIVE:
863                 Log("Threads_Wake - Waking awake thread (%i)", Thread->TID);
864                 return -EALREADY;
865         
866         case THREAD_STAT_SLEEPING:
867                 // Remove from sleeping queue
868                 SHORTLOCK( &glThreadListLock );
869                 Threads_int_DelFromQueue(&gSleepingThreads, Thread);
870                 SHORTREL( &glThreadListLock );
871                 
872                 Threads_AddActive( Thread );
873                 
874                 #if DEBUG_TRACE_STATE
875                 Log("Threads_Sleep: %p (%i %s) woken", Thread, Thread->TID, Thread->ThreadName);
876                 #endif
877                 return -EOK;
878         
879         case THREAD_STAT_SEMAPHORESLEEP: {
880                 tSemaphore      *sem;
881                 tThread *th, *prev=NULL;
882                 
883                 sem = Thread->WaitPointer;
884                 
885                 SHORTLOCK( &sem->Protector );
886                 
887                 // Remove from sleeping queue
888                 for( th = sem->Waiting; th; prev = th, th = th->Next )
889                         if( th == Thread )      break;
890                 if( th )
891                 {
892                         if(prev)
893                                 prev->Next = Thread->Next;
894                         else
895                                 sem->Waiting = Thread->Next;
896                         if(sem->LastWaiting == Thread)
897                                 sem->LastWaiting = prev;
898                 }
899                 else
900                 {
901                         prev = NULL;
902                         for( th = sem->Signaling; th; prev = th, th = th->Next )
903                                 if( th == Thread )      break;
904                         if( !th ) {
905                                 Log_Warning("Threads", "Thread %p(%i %s) is not on semaphore %p(%s:%s)",
906                                         Thread, Thread->TID, Thread->ThreadName,
907                                         sem, sem->ModName, sem->Name);
908                                 return -EINTERNAL;
909                         }
910                         
911                         if(prev)
912                                 prev->Next = Thread->Next;
913                         else
914                                 sem->Signaling = Thread->Next;
915                         if(sem->LastSignaling == Thread)
916                                 sem->LastSignaling = prev;
917                 }
918                 
919                 Thread->RetStatus = 0;  // It didn't get anything
920                 Threads_AddActive( Thread );
921                 
922                 #if DEBUG_TRACE_STATE
923                 Log("Threads_Sleep: %p(%i %s) woken from semaphore", Thread, Thread->TID, Thread->ThreadName);
924                 #endif
925                 SHORTREL( &sem->Protector );
926                 } return -EOK;
927         
928         case THREAD_STAT_WAITING:
929                 Warning("Threads_Wake - Waiting threads are not currently supported");
930                 return -ENOTIMPL;
931         
932         case THREAD_STAT_DEAD:
933                 Warning("Threads_Wake - Attempt to wake dead thread (%i)", Thread->TID);
934                 return -ENOTIMPL;
935         
936         default:
937                 Log_Warning("Threads", "Threads_Wake - Unknown process status (%i)", Thread->Status);
938                 return -EINTERNAL;
939         }
940 }
941
942 /**
943  * \brief Wake a thread given the TID
944  * \param TID   Thread ID to wake
945  * \return Boolean Faulure (errno)
946  */
947 int Threads_WakeTID(tTID TID)
948 {
949         tThread *thread = Threads_GetThread(TID);
950          int    ret;
951         if(!thread)
952                 return -ENOENT;
953         ret = Threads_Wake( thread );
954         //Log_Debug("Threads", "TID %i woke %i (%p)", Threads_GetTID(), TID, thread);
955         return ret;
956 }
957
958 void Threads_ToggleTrace(int TID)
959 {
960         tThread *thread = Threads_GetThread(TID);
961         if(!thread)     return ;
962         thread->bInstrTrace = !thread->bInstrTrace;
963 }
964
965 /**
966  * \brief Adds a thread to the active queue
967  */
968 void Threads_AddActive(tThread *Thread)
969 {
970         #if DEBUG_TRACE_ACTIVEQUEUE
971         Debug("Threads_AddActive("PRIthread_fmt")", PRIthread_args(Thread));
972         #endif
973         SHORTLOCK( &glThreadListLock );
974         
975         if( Thread->Status == THREAD_STAT_ACTIVE )
976         {
977                 tThread *cur = Proc_GetCurThread();
978                 Log_KernelPanic("Threads",
979                         "ret=%p CPU%i "PRIthread_fmt" is adding "PRIthread_fmt" when it is active",
980                         __builtin_return_address(0),
981                         GetCPUNum(), PRIthread_args(cur), PRIthread_args(Thread));
982                 SHORTREL( &glThreadListLock );
983                 return ;
984         }
985         
986         // Set state
987         Thread->Status = THREAD_STAT_ACTIVE;
988         // Add to active list
989         // - Thread can be the current thread if we're interrupted just before
990         //   Proc_Reschedule in a sleep state.
991         if( Thread != Proc_GetCurThread() )
992         {
993                 #if SCHEDULER_TYPE == SCHED_RR_PRI
994                 tThreadList     *list = &gaActiveThreads[Thread->Priority];
995                 #else
996                 tThreadList     *list = &gActiveThreads;
997                 #endif
998                 #if DEBUG_TRACE_ACTIVEQUEUE
999                 Debug(" - Head="PRIthread_fmt",Tail="PRIthread_fmt"",
1000                         PRIthread_args(list->Head),
1001                         PRIthread_args(list->Tail)
1002                         );
1003                 #endif
1004                 Threads_int_AddToList( list, Thread );
1005         }
1006         
1007         // Update bookkeeping
1008         giNumActiveThreads ++;
1009         
1010         #if SCHEDULER_TYPE == SCHED_LOTTERY
1011         {
1012                  int    delta;
1013                 // Only change the ticket count if the thread is un-scheduled
1014                 if(Thread->CurCPU != -1)
1015                         delta = 0;
1016                 else
1017                         delta = caiTICKET_COUNTS[ Thread->Priority ];
1018                 
1019                 giFreeTickets += delta;
1020                 # if DEBUG_TRACE_TICKETS
1021                 Log("CPU%i %p (%i %s) added, new giFreeTickets = %i [+%i]",
1022                         GetCPUNum(), Thread, Thread->TID, Thread->ThreadName,
1023                         giFreeTickets, delta
1024                         );
1025                 # endif
1026         }
1027         #endif
1028         
1029         SHORTREL( &glThreadListLock );
1030 }
1031
1032 /**
1033  * \brief Removes the current thread from the active queue
1034  * \warning This should ONLY be called with the lock held
1035  * \return Current thread pointer
1036  */
1037 tThread *Threads_RemActive(void)
1038 {
1039         tThread *us = Proc_GetCurThread();
1040         #if DEBUG_TRACE_ACTIVEQUEUE
1041         Debug("Threads_RemActive(%p(%i %s))", us, us->TID, us->ThreadName);
1042         #endif
1043         giNumActiveThreads --;
1044         return us;
1045 }
1046
1047 /**
1048  * \fn void Threads_SetFaultHandler(Uint Handler)
1049  * \brief Sets the signal handler for a signal
1050  */
1051 void Threads_SetFaultHandler(Uint Handler)
1052 {       
1053         //Log_Debug("Threads", "Threads_SetFaultHandler: Handler = %p", Handler);
1054         Proc_GetCurThread()->FaultHandler = Handler;
1055 }
1056
1057 /**
1058  * \fn void Threads_Fault(int Num)
1059  * \brief Calls a fault handler
1060  */
1061 void Threads_Fault(int Num)
1062 {
1063         tThread *thread = Proc_GetCurThread();
1064         
1065         if(!thread)     return ;
1066         
1067         Log_Log("Threads", "Threads_Fault: thread->FaultHandler = %p", thread->FaultHandler);
1068         
1069         switch(thread->FaultHandler)
1070         {
1071         case 0: // Panic?
1072                 Threads_Kill(thread, -1);
1073                 HALT();
1074                 return ;
1075         case 1: // Dump Core?
1076                 Threads_Kill(thread, -1);
1077                 HALT();
1078                 return ;
1079         }
1080         
1081         // Double Fault? Oh, F**k
1082         if(thread->CurFaultNum != 0) {
1083                 Log_Warning("Threads", "Threads_Fault: Double fault on %i", thread->TID);
1084                 Threads_Kill(thread, -1);       // For now, just kill
1085                 HALT();
1086         }
1087         
1088         thread->CurFaultNum = Num;
1089         
1090         Proc_CallFaultHandler(thread);
1091 }
1092
1093 /**
1094  * \fn void Threads_SegFault(tVAddr Addr)
1095  * \brief Called when a Segment Fault occurs
1096  */
1097 void Threads_SegFault(tVAddr Addr)
1098 {
1099         tThread *cur = Proc_GetCurThread();
1100         cur->bInstrTrace = 0;
1101         Log_Warning("Threads", "Thread #%i committed a segfault at address %p", cur->TID, Addr);
1102         MM_DumpTables(0, USER_MAX);
1103         Threads_Fault( 1 );
1104         //Threads_Exit( 0, -1 );
1105 }
1106
1107
1108 void Threads_PostSignalTo(tThread *Thread, int SignalNum)
1109 {
1110         ASSERT(Thread);
1111         Log_Debug("Threads", "Signalling %i(%s) with %i", Thread->TID, Thread->ThreadName, SignalNum);
1112         Thread->PendingSignal = SignalNum;
1113         Threads_PostEvent(Thread, THREAD_EVENT_SIGNAL);
1114 }
1115 void Threads_PostSignal(int SignalNum)
1116 {
1117         Threads_PostSignalTo( Proc_GetCurThread(), SignalNum );
1118 }
1119
1120 void Threads_SignalGroup(tPGID PGID, int Signal)
1121 {
1122         for( tProcess *proc = gAllProcesses; proc; proc = proc->Next )
1123         {
1124                 if(proc->PGID == PGID)
1125                 {
1126                         Threads_PostSignalTo(proc->FirstThread, Signal);
1127                 }
1128         }
1129 }
1130
1131 /**
1132  */
1133 int Threads_GetPendingSignal(void)
1134 {
1135         tThread *cur = Proc_GetCurThread();
1136         
1137         // Atomic AND with 0 fetches and clears in one operation
1138         int ret = __sync_fetch_and_and( &cur->PendingSignal, 0 );
1139         if( ret )
1140         {
1141                 Log_Debug("Threads", "Thread %i(%s) has signal %i pending",
1142                         cur->TID, cur->ThreadName, ret);
1143         }
1144         return ret;
1145 }
1146
1147 /*
1148  * \brief Update the current thread's signal handler
1149  */
1150 void Threads_SetSignalHandler(int SignalNum, void *Handler)
1151 {
1152         if( SignalNum <= 0 || SignalNum >= NSIGNALS )
1153                 return ;
1154         if( !MM_IsUser(Handler) )
1155                 return ;
1156         Proc_GetCurThread()->Process->SignalHandlers[SignalNum] = Handler;
1157 }
1158
1159 /**
1160  * \brief Gets the registered (or default, if none set) handler for a signal.
1161  * \return Handler function pointer, OR NULL if no signal to be ignored
1162  */
1163 void *Threads_GetSignalHandler(int SignalNum)
1164 {
1165         // TODO: Core dump
1166         void *User_Signal_Core = User_Signal_Kill;
1167         
1168         if( SignalNum <= 0 || SignalNum >= NSIGNALS )
1169                 return NULL;
1170         void *ret = Proc_GetCurThread()->Process->SignalHandlers[SignalNum];
1171         if( !ret || (SignalNum == SIGKILL || SignalNum == SIGSTOP) )
1172         {
1173                 // Defaults
1174                 switch(SignalNum)
1175                 {
1176                 case SIGHUP:
1177                 case SIGINT:
1178                         ret = User_Signal_Kill;
1179                         break;
1180                 case SIGQUIT:
1181                 case SIGILL:
1182                 case SIGABRT:
1183                 case SIGFPE:
1184                         ret = User_Signal_Core;
1185                         break;
1186                 case SIGKILL:
1187                         ret = User_Signal_Kill;
1188                         break;
1189                 case SIGSEGV:
1190                         ret = User_Signal_Core;
1191                         break;
1192                 case SIGPIPE:
1193                 case SIGALRM:
1194                 case SIGTERM:
1195                         ret = User_Signal_Kill;
1196                         break;
1197                 default:
1198                         ret = NULL;
1199                         break;
1200                 }
1201         }
1202         Log_Debug("Threads", "Handler %p for signal %i", ret, SignalNum);
1203         return ret;
1204 }
1205
1206 // --- Process Structure Access Functions ---
1207 tPGID Threads_GetPGID(void)
1208 {
1209         return Proc_GetCurThread()->Process->PGID;
1210 }
1211 tPID Threads_GetPID(void)
1212 {
1213         return Proc_GetCurThread()->Process->PID;
1214 }
1215 tTID Threads_GetTID(void)
1216 {
1217         return Proc_GetCurThread()->TID;
1218 }
1219 tUID Threads_GetUID(void)
1220 {
1221         return Proc_GetCurThread()->Process->UID;
1222 }
1223 tGID Threads_GetGID(void)
1224 {
1225         return Proc_GetCurThread()->Process->GID;
1226 }
1227
1228 int Threads_SetUID(tUID ID)
1229 {
1230         tThread *t = Proc_GetCurThread();
1231         if( t->Process->UID != 0 ) {
1232                 errno = -EACCES;
1233                 return -1;
1234         }
1235         Log_Debug("Threads", "PID %i's UID set to %i", t->Process->PID, ID);
1236         t->Process->UID = ID;
1237         return 0;
1238 }
1239
1240 int Threads_SetGID(tGID ID)
1241 {
1242         tThread *t = Proc_GetCurThread();
1243         if( t->Process->UID != 0 ) {
1244                 errno = -EACCES;
1245                 return -1;
1246         }
1247         Log_Debug("Threads", "PID %i's GID set to %i", t->Process->PID, ID);
1248         t->Process->GID = ID;
1249         return 0;
1250 }
1251
1252 // --- Per-thread storage ---
1253 int *Threads_GetErrno(void)
1254 {
1255         return &Proc_GetCurThread()->_errno;
1256 }
1257
1258 // --- Configuration ---
1259 int *Threads_GetMaxFD(tProcess *Process)
1260 {
1261         if(!Process)    Process = Proc_GetCurThread()->Process;
1262         return &Process->MaxFD;
1263 }
1264 char **Threads_GetChroot(tProcess *Process)
1265 {
1266         if(!Process)    Process = Proc_GetCurThread()->Process;
1267         return &Process->RootDir;
1268 }
1269 char **Threads_GetCWD(tProcess *Process)
1270 {
1271         if(!Process)    Process = Proc_GetCurThread()->Process;
1272         return &Process->CurrentWorkingDir;
1273 }
1274 // ---
1275
1276 void Threads_int_DumpThread(tThread *thread)
1277 {
1278         if( !thread ) {
1279                 Log(" %p NULL", thread);
1280                 return ;
1281         }
1282         if( !CheckMem(thread, sizeof(tThread)) ) {
1283                 Log(" %p INVAL", thread);
1284                 return ;
1285         }
1286         tPID    pid = (thread->Process ? thread->Process->PID : -1);
1287         const char      *statstr = (thread->Status < sizeof(casTHREAD_STAT)/sizeof(casTHREAD_STAT[0])
1288                 ? casTHREAD_STAT[thread->Status] : "");
1289         Log(" %p %i (%i) - %s (CPU %i) - %i (%s)",
1290                 thread, thread->TID, pid, thread->ThreadName, thread->CurCPU,
1291                 thread->Status, statstr
1292                 );
1293         switch(thread->Status)
1294         {
1295         case THREAD_STAT_MUTEXSLEEP:
1296                 Log("  Mutex Pointer: %p", thread->WaitPointer);
1297                 break;
1298         case THREAD_STAT_RWLOCKSLEEP:
1299                 Log("  Lock Pointer: %p", thread->WaitPointer);
1300                 Log("  Lock Name: %s",
1301                         ((tRWLock*)thread->WaitPointer)->Name);
1302                 break;
1303         case THREAD_STAT_SEMAPHORESLEEP:
1304                 Log("  Semaphore Pointer: %p", thread->WaitPointer);
1305                 Log("  Semaphore Name: %s:%s", 
1306                         ((tSemaphore*)thread->WaitPointer)->ModName,
1307                         ((tSemaphore*)thread->WaitPointer)->Name
1308                         );
1309                 break;
1310         case THREAD_STAT_EVENTSLEEP:
1311                 Log("  Event Mask: %x", thread->RetStatus);
1312                 break;
1313         case THREAD_STAT_ZOMBIE:
1314                 Log("  Return Status: %i", thread->RetStatus);
1315                 break;
1316         default:        break;
1317         }
1318         Log("  Priority %i, Quantum %i", thread->Priority, thread->Quantum);
1319         Log("  KStack %p", thread->KernelStack);
1320         if( thread->bInstrTrace )
1321                 Log("  Tracing Enabled");
1322         Proc_DumpThreadCPUState(thread);
1323 }
1324
1325 /**
1326  * \fn void Threads_Dump(void)
1327  */
1328 void Threads_DumpActive(void)
1329 {
1330         Log("Active Threads: (%i reported)", giNumActiveThreads);
1331         
1332         #if SCHEDULER_TYPE == SCHED_RR_PRI
1333         for( int i = 0; i < MIN_PRIORITY+1; i++ )
1334         {
1335                 tThreadList *list = &gaActiveThreads[i];
1336         #else
1337                 tThreadList *list = &gActiveThreads;
1338         #endif
1339                 for(tThread *thread = list->Head; thread; thread = thread->Next)
1340                 {
1341                         Threads_int_DumpThread(thread);
1342                         if(thread->Status != THREAD_STAT_ACTIVE)
1343                                 Log("  ERROR State (%i) != THREAD_STAT_ACTIVE (%i)",
1344                                         thread->Status, THREAD_STAT_ACTIVE);
1345                 }
1346         
1347         #if SCHEDULER_TYPE == SCHED_RR_PRI
1348         }
1349         #endif
1350 }
1351
1352 /**
1353  * \fn void Threads_Dump(void)
1354  * \brief Dumps a list of currently running threads
1355  */
1356 void Threads_Dump(void)
1357 {
1358         Log("--- Thread Dump ---");
1359         Threads_DumpActive();
1360         
1361         Log("All Threads:");
1362         for(tThread *thread = gAllThreads; thread; thread = thread->GlobalNext)
1363         {
1364                 Threads_int_DumpThread(thread);
1365         }
1366 }
1367
1368 tThread *Threads_int_GetRunnable(void)
1369 {
1370         #if SCHEDULER_TYPE == SCHED_LOTTERY
1371         // -----------------------------------
1372         // Lottery Scheduler
1373         // -----------------------------------
1374         #if DEBUG_VALIDATE_TICKET_COUNTS
1375         {
1376                 int total_tickets = 0;
1377                 for(const tThread *thread = gActiveThreads.Head; thread; thread = thread->Next)
1378                 {
1379                         if(thread->Status != THREAD_STAT_ACTIVE)
1380                                 Panic("Bookkeeping fail - %p %i(%s) is on the active queue with a status of %i",
1381                                         thread, thread->TID, thread->ThreadName, thread->Status);
1382                         if(thread->Next == thread) {
1383                                 Panic("Bookkeeping fail - %p %i(%s) loops back on itself",
1384                                         thread, thread->TID, thread->ThreadName, thread->Status);
1385                         }
1386                         total_tickets += caiTICKET_COUNTS[ thread->Priority ];
1387                 }
1388                 if(total_tickets != giFreeTickets)
1389                 {
1390                         Panic("Bookkeeping fail (giFreeTickets(%i) != number(%i)) - CPU%i",
1391                                 giFreeTickets, total_tickets, CPU);
1392                 }
1393         }
1394         # endif
1395         
1396         // No free tickets (all tasks delegated to cores)
1397         if( giFreeTickets == 0 )
1398         {
1399                 return NULL;
1400         }
1401         
1402         // Get the ticket number
1403         int ticket = rand() % giFreeTickets;
1404         int number = ticket;
1405         
1406         // Find the next thread
1407         tThread **pnp = &gActiveThreads.Head;
1408         tThread *thread;
1409         for(thread = gActiveThreads.Head; thread; pnp = &thread->Next, thread = thread->Next )
1410         {
1411                 if( caiTICKET_COUNTS[ thread->Priority ] > number)
1412                         break;
1413                 number -= caiTICKET_COUNTS[ thread->Priority ];
1414         }
1415         
1416         // If we didn't find a thread, something went wrong
1417         if(thread == NULL)
1418         {
1419                 int total_tickets = 0;
1420                 for(thread=gActiveThreads;thread;thread=thread->Next) {
1421                         if(thread->CurCPU >= 0) continue;
1422                         total_tickets += caiTICKET_COUNTS[ thread->Priority ];
1423                 }
1424                 Panic("Bookeeping Failed - giFreeTickets(%i) > true count (%i)",
1425                         giFreeTickets, total_tickets);
1426         }
1427
1428         // Remove
1429         *pnp = thread->Next;
1430         if( !thread->Next )
1431                 gActiveThreads.Tail = prev;             
1432
1433         giFreeTickets -= caiTICKET_COUNTS[ thread->Priority ];
1434         # if DEBUG_TRACE_TICKETS
1435         LogF("Log: CPU%i allocated %p (%i %s), (%i [-%i] tickets in pool), \n",
1436                 CPU, thread, thread->TID, thread->ThreadName,
1437                 giFreeTickets, caiTICKET_COUNTS[ thread->Priority ]);
1438         # endif
1439         return thread;
1440         
1441         #elif SCHEDULER_TYPE == SCHED_RR_PRI
1442         
1443         // -----------------------------------
1444         // Priority based round robin scheduler
1445         // -----------------------------------
1446         for( int i = 0; i < MIN_PRIORITY + 1; i ++ )
1447         {
1448                 if( gaActiveThreads[i].Head == NULL )
1449                         continue ;
1450         
1451                 tThread *thread = gaActiveThreads[i].Head;
1452
1453                 if( thread->Status != THREAD_STAT_ACTIVE ) {
1454                         for( const tThread *t = gaActiveThreads[i].Head; t; t = t->Next )
1455                                 LogF("- %p(%i %s)\n", t, t->TID, t->ThreadName);
1456                         Panic("Thread %p(%i %s) from pqueue %i is active!",
1457                                 thread, thread->TID, thread->ThreadName, i);
1458                 }
1459
1460                 // Remove from head
1461                 gaActiveThreads[i].Head = thread->Next;
1462                 if(!thread->Next)
1463                         gaActiveThreads[i].Tail = NULL;
1464                 thread->Next = NULL;
1465                 return thread;
1466         }
1467         return NULL;
1468         
1469         #elif SCHEDULER_TYPE == SCHED_RR_SIM
1470         
1471         // -----------------------------------
1472         // Single-list round-robin
1473         // -----------------------------------
1474         tThread *thread = gActiveThreads.Head;
1475         if( thread )
1476         {
1477                 gActiveThreads.Head = thread->Next;
1478                 if(!thread->Next)
1479                         gaActiveThreads.Tail = NULL;
1480                 thread->Next = NULL;
1481         }
1482         return thread;
1483         #else
1484         # error "Unimplemented scheduling algorithm"
1485         return NULL;
1486         #endif
1487 }
1488
1489 /**
1490  * \brief Gets the next thread to run
1491  * \param CPU   Current CPU
1492  * \param Last  The thread the CPU was running
1493  */
1494 tThread *Threads_GetNextToRun(int CPU, tThread *Last)
1495 {
1496         // If this CPU has the lock, we must let it complete
1497         if( CPU_HAS_LOCK( &glThreadListLock ) )
1498                 return Last;
1499         
1500         // Don't change threads if the current CPU has switches disabled
1501         if( gaThreads_NoTaskSwitch[CPU] )
1502                 return Last;
1503
1504         // Lock thread list
1505         SHORTLOCK( &glThreadListLock );
1506         
1507         // Make sure the current (well, old) thread is marked as de-scheduled   
1508         if(Last)        Last->CurCPU = -1;
1509
1510         // No active threads, just take a nap
1511         if(giNumActiveThreads == 0) {
1512                 SHORTREL( &glThreadListLock );
1513                 #if DEBUG_TRACE_TICKETS
1514                 Log("No active threads");
1515                 #endif
1516                 return NULL;
1517         }
1518
1519         // Allow the old thread to be scheduled again
1520         if( Last )
1521         {
1522                 if( Last->Status == THREAD_STAT_ACTIVE ) {
1523                         tThreadList     *list;
1524                         #if SCHEDULER_TYPE == SCHED_LOTTERY
1525                         giFreeTickets += caiTICKET_COUNTS[ Last->Priority ];
1526                         # if DEBUG_TRACE_TICKETS
1527                         LogF("Log: CPU%i released %p (%i %s) into the pool (%i [+%i] tickets in pool)\n",
1528                                 CPU, Last, Last->TID, Last->ThreadName, giFreeTickets,
1529                                 caiTICKET_COUNTS[ Last->Priority ]);
1530                         # endif
1531                         #endif
1532                         
1533                         #if SCHEDULER_TYPE == SCHED_RR_PRI
1534                         list = &gaActiveThreads[ Last->Priority ];
1535                         #else
1536                         list = &gActiveThreads;
1537                         #endif
1538                         // Add to end of list
1539                         Threads_int_AddToList( list, Last );
1540                         #if DEBUG_TRACE_ACTIVEQUEUE > 1
1541                         Debug("Threads_GetNextToRun: Append thread %p(%i %s)",
1542                                 Last, Last->TID, Last->ThreadName);
1543                         #endif
1544                 }
1545                 #if SCHEDULER_TYPE == SCHED_LOTTERY && DEBUG_TRACE_TICKETS
1546                 else
1547                         LogF("Log: CPU%i released %p (%i %s)->Status = %i (Released,not in pool)\n",
1548                                 CPU, Last, Last->TID, Last->ThreadName, Last->Status);
1549                 #endif
1550                 Last->CurCPU = -1;
1551         }
1552
1553         // Call actual scheduler        
1554         tThread *thread = Threads_int_GetRunnable();
1555                 
1556         // Anything to do?
1557         if( thread )
1558         {
1559                 if( thread->Status != THREAD_STAT_ACTIVE )
1560                 {
1561                         LogF("Thread %p(%i %s) scheduled while not active\n",
1562                                 thread, thread->TID, thread->ThreadName);
1563                 }
1564
1565                 // Make the new thread non-schedulable
1566                 thread->CurCPU = CPU;
1567                 thread->Remaining = thread->Quantum;
1568                 
1569                 #if DEBUG_TRACE_SCHEDULE
1570                 Debug("Scheduled "PRIthread_fmt", next = %p",
1571                         PRIthread_args(thread),
1572                         thread->Next);
1573                 #endif
1574         }
1575         else
1576         {
1577                 // No thread possible, warning condition (idle thread should be runnable)
1578                 Warning("No runnable thread for CPU%i", CPU);
1579         }
1580         
1581         SHORTREL( &glThreadListLock );
1582         
1583         return thread;
1584 }
1585
1586 // === EXPORTS ===
1587 EXPORT(Threads_GetUID);
1588 EXPORT(Threads_GetGID);

UCC git Repository :: git.ucc.asn.au