4 * - Common Thread Control
10 #define DEBUG_TRACE_TICKETS 0 // Trace ticket counts
11 #define DEBUG_TRACE_STATE 0 // Trace state changes (sleep/wake)
14 #define DEFAULT_QUANTUM 10
15 #define DEFAULT_TICKETS 5
16 #define MAX_TICKETS 10
17 const enum eConfigTypes cCONFIG_TYPES[] = {
18 CFGT_HEAPSTR, // e.g. CFG_VFS_CWD
19 CFGT_INT, // e.g. CFG_VFS_MAXFILES
24 extern void ArchThreads_Init(void);
25 extern void Proc_Start(void);
26 extern tThread *Proc_GetCurThread(void);
27 extern int Proc_Clone(Uint *Err, Uint Flags);
28 extern void Proc_CallFaultHandler(tThread *Thread);
31 void Threads_Init(void);
32 int Threads_SetName(char *NewName);
33 char *Threads_GetName(int ID);
34 void Threads_SetTickets(tThread *Thread, int Num);
35 tThread *Threads_CloneTCB(Uint *Err, Uint Flags);
36 int Threads_WaitTID(int TID, int *status);
37 tThread *Threads_GetThread(Uint TID);
38 void Threads_AddToDelete(tThread *Thread);
39 tThread *Threads_int_GetPrev(tThread **List, tThread *Thread);
40 void Threads_Exit(int TID, int Status);
41 void Threads_Kill(tThread *Thread, int Status);
42 void Threads_Yield(void);
43 void Threads_Sleep(void);
44 int Threads_Wake(tThread *Thread);
45 void Threads_AddActive(tThread *Thread);
46 void Threads_int_AddActive(tThread *Thread);
47 tThread *Threads_RemActive(void);
48 int Threads_GetPID(void);
49 int Threads_GetTID(void);
50 tUID Threads_GetUID(void);
51 int Threads_SetUID(Uint *Errno, tUID ID);
52 tGID Threads_GetGID(void);
53 int Threads_SetGID(Uint *Errno, tUID ID);
54 void Threads_Dump(void);
55 void Mutex_Acquire(tMutex *Mutex);
56 void Mutex_Release(tMutex *Mutex);
57 int Mutex_IsLocked(tMutex *Mutex);
61 // Only used for the core kernel
62 tThread gThreadZero = {
63 Status: THREAD_STAT_ACTIVE, // Status
64 ThreadName: "ThreadZero", // Name
65 Quantum: DEFAULT_QUANTUM, // Default Quantum
66 Remaining: DEFAULT_QUANTUM, // Current Quantum
67 NumTickets: DEFAULT_TICKETS // Number of tickets
71 tShortSpinlock glThreadListLock; ///\note NEVER use a heap function while locked
72 // --- Current State ---
73 volatile int giNumActiveThreads = 0; // Number of threads on the active queue
74 volatile int giFreeTickets = 0; // Number of tickets held by non-scheduled threads
75 volatile Uint giNextTID = 1; // Next TID to allocate
76 // --- Thread Lists ---
77 tThread *gAllThreads = NULL; // All allocated threads
78 tThread *gActiveThreads = NULL; // Currently Running Threads
79 tThread *gSleepingThreads = NULL; // Sleeping Threads
80 tThread *gDeleteThreads = NULL; // Threads to delete
81 int giNumCPUs = 1; // Number of CPUs
85 * \fn void Threads_Init(void)
86 * \brief Initialse the thread list
88 void Threads_Init(void)
92 // Create Initial Task
93 gActiveThreads = &gThreadZero;
94 gAllThreads = &gThreadZero;
95 //giFreeTickets = gThreadZero.NumTickets; // Not needed, as ThreadZero is scheduled
96 giNumActiveThreads = 1;
102 * \fn void Threads_SetName(char *NewName)
103 * \brief Sets the current thread's name
104 * \param NewName New name for the thread
105 * \return Boolean Failure
107 int Threads_SetName(char *NewName)
109 tThread *cur = Proc_GetCurThread();
110 char *oldname = cur->ThreadName;
112 // NOTE: There is a possibility of non-thread safety here
113 // A thread could read the current name pointer before it is zeroed
115 cur->ThreadName = NULL;
117 if( IsHeap(oldname) ) free( oldname );
119 cur->ThreadName = strdup(NewName);
124 * \fn char *Threads_GetName(int ID)
125 * \brief Gets a thread's name
126 * \param ID Thread ID (-1 indicates current thread)
127 * \return Pointer to name
128 * \retval NULL Failure
130 char *Threads_GetName(tTID ID)
133 return Proc_GetCurThread()->ThreadName;
135 return Threads_GetThread(ID)->ThreadName;
139 * \fn void Threads_SetTickets(tThread *Thread, int Num)
140 * \brief Sets the 'priority' of a task
141 * \param Thread Thread to update ticket count (NULL means current thread)
142 * \param Num New ticket count (must be >= 0, clipped to \a MAX_TICKETS)
144 void Threads_SetTickets(tThread *Thread, int Num)
146 // Get current thread
147 if(Thread == NULL) Thread = Proc_GetCurThread();
150 if(Num > MAX_TICKETS) Num = MAX_TICKETS;
152 // If this isn't the current thread, we need to lock
153 if( Thread != Proc_GetCurThread() ) {
154 SHORTLOCK( &glThreadListLock );
155 giFreeTickets -= Thread->NumTickets - Num;
156 Thread->NumTickets = Num;
157 #if DEBUG_TRACE_TICKETS
158 Log("Threads_SetTickets: new giFreeTickets = %i", giFreeTickets);
160 SHORTREL( &glThreadListLock );
163 Thread->NumTickets = Num;
167 * \fn tThread *Threads_CloneTCB(Uint *Err, Uint Flags)
168 * \brief Clone the TCB of the current thread
169 * \param Err Error pointer
170 * \param Flags Flags for something... (What is this for?)
172 tThread *Threads_CloneTCB(Uint *Err, Uint Flags)
176 cur = Proc_GetCurThread();
178 // Allocate and duplicate
179 new = malloc(sizeof(tThread));
184 memcpy(new, cur, sizeof(tThread));
188 memset( &new->IsLocked, 0, sizeof(new->IsLocked));
189 new->Status = THREAD_STAT_PREINIT;
193 new->TID = giNextTID++;
197 new->ThreadName = strdup(cur->ThreadName);
199 // Set Thread Group ID (PID)
201 new->TGID = new->TID;
203 new->TGID = cur->TGID;
205 // Messages are not inherited
206 new->Messages = NULL;
207 new->LastMessage = NULL;
210 new->Remaining = new->Quantum = cur->Quantum;
211 new->NumTickets = cur->NumTickets;
213 // Set Signal Handlers
214 new->CurFaultNum = 0;
215 new->FaultHandler = cur->FaultHandler;
217 for( i = 0; i < NUM_CFG_ENTRIES; i ++ )
219 switch(cCONFIG_TYPES[i])
222 new->Config[i] = cur->Config[i];
226 new->Config[i] = (Uint) strdup( (void*)cur->Config[i] );
233 // Maintain a global list of threads
234 SHORTLOCK( &glThreadListLock );
235 new->GlobalPrev = NULL; // Protect against bugs
236 new->GlobalNext = gAllThreads;
238 SHORTREL( &glThreadListLock );
244 * \brief Get a configuration pointer from the Per-Thread data area
245 * \param ID Config slot ID
246 * \return Pointer at ID
248 Uint *Threads_GetCfgPtr(int ID)
250 if(ID < 0 || ID >= NUM_CFG_ENTRIES) {
251 Warning("Threads_GetCfgPtr: Index %i is out of bounds", ID);
255 return &Proc_GetCurThread()->Config[ID];
259 * \brief Wait for a task to change state
260 * \param TID Thread ID to wait on (-1: Any child thread, 0: Any Child/Sibling, <-1: -PID)
261 * \param Status Thread return status
262 * \return TID of child that changed state
264 tTID Threads_WaitTID(int TID, int *Status)
268 Log_Error("Threads", "TODO: Threads_WaitTID(TID=-1) - Any Child");
272 // Any peer/child thread
274 Log_Error("Threads", "TODO: Threads_WaitTID(TID=0) - Any Child/Sibling");
280 Log_Error("Threads", "TODO: Threads_WaitTID(TID<0) - TGID");
286 tThread *t = Threads_GetThread(TID);
287 int initStatus = t->Status;
290 // Wait for the thread to die!
291 if(initStatus != THREAD_STAT_ZOMBIE) {
292 // TODO: Handle child also being suspended if wanted
293 while(t->Status != THREAD_STAT_ZOMBIE) {
295 Log_Debug("Threads", "%i waiting for %i, t->Status = %i",
296 Threads_GetTID(), t->TID, t->Status);
301 Log_Debug("Threads", "%i waiting for %i, t->Status = %i",
302 Threads_GetTID(), t->TID, t->Status);
306 case THREAD_STAT_ZOMBIE:
308 t->Status = THREAD_STAT_DEAD;
309 // TODO: Child return value?
310 if(Status) *Status = t->RetStatus;
311 // add to delete queue
312 Threads_AddToDelete( t );
315 if(Status) *Status = -1;
325 * \brief Gets a thread given its TID
326 * \param TID Thread ID
327 * \return Thread pointer
329 tThread *Threads_GetThread(Uint TID)
333 // Search global list
334 for(thread = gAllThreads;
336 thread = thread->GlobalNext)
338 if(thread->TID == TID)
342 Log("Unable to find TID %i on main list\n", TID);
348 * \brief Adds a thread to the delete queue
349 * \param Thread Thread to delete
351 void Threads_AddToDelete(tThread *Thread)
353 // Add to delete queue
354 // TODO: Is locking needed?
356 Thread->Next = gDeleteThreads;
357 gDeleteThreads = Thread;
360 gDeleteThreads = Thread;
365 * \brief Gets the previous entry in a thead linked list
366 * \param List Pointer to the list head
367 * \param Thread Thread to find
368 * \return Thread before \a Thread on \a List
369 * \note This uses a massive hack of assuming that the first field in the
370 * structure is the .Next pointer. By doing this, we can return \a List
371 * as a (tThread*) and simplify other code.
373 tThread *Threads_int_GetPrev(tThread **List, tThread *Thread)
377 if(*List == Thread) {
378 return (tThread*)List;
383 ret->Next && ret->Next != Thread;
386 // Error if the thread is not on the list
387 if(!ret->Next || ret->Next != Thread) {
395 * \brief Exit the current process (or another?)
396 * \param TID Thread ID to kill
397 * \param Status Exit status
399 void Threads_Exit(int TID, int Status)
402 Threads_Kill( Proc_GetCurThread(), (Uint)Status & 0xFF );
404 Threads_Kill( Threads_GetThread(TID), (Uint)Status & 0xFF );
406 // Halt forever, just in case
411 * \fn void Threads_Kill(tThread *Thread, int Status)
412 * \brief Kill a thread
413 * \param Thread Thread to kill
414 * \param Status Status code to return to the parent
416 void Threads_Kill(tThread *Thread, int Status)
421 // TODO: Kill all children
425 // TODO: I should keep a .Parent pointer, and a .Children list
426 for(child = gActiveThreads;
430 if(child->PTID == Thread->TID)
431 Threads_Kill(child, -1);
436 ///\note Double lock is needed due to overlap of lock areas
438 // Lock thread (stop us recieving messages)
439 SHORTLOCK( &Thread->IsLocked );
442 SHORTLOCK( &glThreadListLock );
444 // Get previous thread on list
445 prev = Threads_int_GetPrev( &gActiveThreads, Thread );
447 Warning("Proc_Exit - Current thread is not on the active queue");
448 SHORTREL( &glThreadListLock );
449 SHORTREL( &Thread->IsLocked );
453 // Clear Message Queue
454 while( Thread->Messages )
456 msg = Thread->Messages->Next;
457 free( Thread->Messages ); // BIG NO-NO
458 Thread->Messages = msg;
461 // Ensure that we are not rescheduled
462 Thread->Remaining = 0; // Clear Remaining Quantum
463 Thread->Quantum = 0; // Clear Quantum to indicate dead thread
464 prev->Next = Thread->Next; // Remove from active
466 // Update bookkeeping
467 giNumActiveThreads --;
468 if( Thread != Proc_GetCurThread() )
469 giFreeTickets -= Thread->NumTickets;
472 Thread->RetStatus = Status;
474 // Don't Zombie if we are being killed because our parent is
477 Thread->Status = THREAD_STAT_DEAD;
478 Threads_AddToDelete( Thread );
480 Thread->Status = THREAD_STAT_ZOMBIE;
482 Threads_Wake( Thread->Parent );
485 Log("Thread %i went *hurk* (%i)", Thread->TID, Thread->Status);
488 SHORTREL( &glThreadListLock );
489 SHORTREL( &Thread->IsLocked ); // TODO: We may not actually be released...
499 * \brief Yield remainder of the current thread's timeslice
501 void Threads_Yield(void)
503 tThread *thread = Proc_GetCurThread();
504 thread->Remaining = 0;
505 //while(thread->Remaining == 0)
510 * \fn void Threads_Sleep(void)
511 * \brief Take the current process off the run queue
513 void Threads_Sleep(void)
515 tThread *cur = Proc_GetCurThread();
518 SHORTLOCK( &glThreadListLock );
520 // Don't sleep if there is a message waiting
521 if( cur->Messages ) {
522 SHORTREL( &glThreadListLock );
526 // Remove us from running queue
528 // Mark thread as sleeping
529 cur->Status = THREAD_STAT_SLEEPING;
531 // Add to Sleeping List (at the top)
532 cur->Next = gSleepingThreads;
533 gSleepingThreads = cur;
536 #if DEBUG_TRACE_STATE
537 Log("Threads_Sleep: %p (%i %s) sleeping", cur, cur->TID, cur->ThreadName);
541 SHORTREL( &glThreadListLock );
543 while(cur->Status != THREAD_STAT_ACTIVE) HALT();
548 * \fn int Threads_Wake( tThread *Thread )
549 * \brief Wakes a sleeping/waiting thread up
550 * \param Thread Thread to wake
551 * \return Boolean Failure (Returns ERRNO)
552 * \warning This should ONLY be called with task switches disabled
554 int Threads_Wake(tThread *Thread)
561 switch(Thread->Status)
563 case THREAD_STAT_ACTIVE:
564 Log("Thread_Wake: Waking awake thread (%i)", Thread->TID);
567 case THREAD_STAT_SLEEPING:
568 // Remove from sleeping queue
569 prev = Threads_int_GetPrev(&gSleepingThreads, Thread);
570 prev->Next = Thread->Next;
572 Threads_int_AddActive( Thread );
574 #if DEBUG_TRACE_STATE
575 Log("Threads_Sleep: %p (%i %s) woken", Thread, Thread->TID, Thread->ThreadName);
579 case THREAD_STAT_WAITING:
580 Warning("Thread_Wake - Waiting threads are not currently supported");
583 case THREAD_STAT_DEAD:
584 Warning("Thread_Wake - Attempt to wake dead thread (%i)", Thread->TID);
588 Warning("Thread_Wake - Unknown process status (%i)\n", Thread->Status);
594 * \brief Wake a thread given the TID
595 * \param TID Thread ID to wake
596 * \return Boolean Faulure (errno)
598 int Threads_WakeTID(tTID TID)
600 tThread *thread = Threads_GetThread(TID);
604 SHORTLOCK( &glThreadListLock );
605 ret = Threads_Wake( thread );
606 SHORTREL( &glThreadListLock );
607 //Log_Debug("Threads", "TID %i woke %i (%p)", Threads_GetTID(), TID, thread);
612 * \brief Adds a thread to the active queue
614 void Threads_AddActive(tThread *Thread)
616 SHORTLOCK( &glThreadListLock );
617 Threads_int_AddActive(Thread);
618 SHORTREL( &glThreadListLock );
622 * \brief Adds a thread to the active queue
623 * \note This version MUST have the thread list lock held
625 void Threads_int_AddActive(tThread *Thread)
627 // Add to active list
628 Thread->Next = gActiveThreads;
629 gActiveThreads = Thread;
631 Thread->Status = THREAD_STAT_ACTIVE;
634 // Update bookkeeping
635 giNumActiveThreads ++;
636 giFreeTickets += Thread->NumTickets;
638 #if DEBUG_TRACE_TICKETS
639 Log("Threads_int_AddActive: new giFreeTickets = %i", giFreeTickets);
644 * \brief Removes the current thread from the active queue
645 * \warning This should ONLY be called with task switches disabled
646 * \return Current thread pointer
648 tThread *Threads_RemActive(void)
650 tThread *ret = Proc_GetCurThread();
651 tThread *prev = Threads_int_GetPrev(&gActiveThreads, ret);
652 if(!prev) return NULL;
655 prev->Next = ret->Next;
656 giNumActiveThreads --;
661 * \fn void Threads_SetFaultHandler(Uint Handler)
662 * \brief Sets the signal handler for a signal
664 void Threads_SetFaultHandler(Uint Handler)
666 //Log_Debug("Threads", "Threads_SetFaultHandler: Handler = %p", Handler);
667 Proc_GetCurThread()->FaultHandler = Handler;
671 * \fn void Threads_Fault(int Num)
672 * \brief Calls a fault handler
674 void Threads_Fault(int Num)
676 tThread *thread = Proc_GetCurThread();
678 Log_Log("Threads", "Threads_Fault: thread = %p", thread);
682 Log_Log("Threads", "Threads_Fault: thread->FaultHandler = %p", thread->FaultHandler);
684 switch(thread->FaultHandler)
687 Threads_Kill(thread, -1);
690 case 1: // Dump Core?
691 Threads_Kill(thread, -1);
696 // Double Fault? Oh, F**k
697 if(thread->CurFaultNum != 0) {
698 Threads_Kill(thread, -1); // For now, just kill
702 thread->CurFaultNum = Num;
704 Proc_CallFaultHandler(thread);
707 // --- Process Structure Access Functions ---
708 tPID Threads_GetPID(void)
710 return Proc_GetCurThread()->TGID;
712 tTID Threads_GetTID(void)
714 return Proc_GetCurThread()->TID;
716 tUID Threads_GetUID(void)
718 return Proc_GetCurThread()->UID;
720 tGID Threads_GetGID(void)
722 return Proc_GetCurThread()->GID;
725 int Threads_SetUID(Uint *Errno, tUID ID)
727 tThread *t = Proc_GetCurThread();
732 Log_Debug("Threads", "TID %i's UID set to %i", t->TID, ID);
737 int Threads_SetGID(Uint *Errno, tGID ID)
739 tThread *t = Proc_GetCurThread();
744 Log_Debug("Threads", "TID %i's GID set to %i", t->TID, ID);
750 * \fn void Threads_Dump(void)
751 * \brief Dums a list of currently running threads
753 void Threads_Dump(void)
757 Log("Active Threads:");
758 for(thread=gActiveThreads;thread;thread=thread->Next)
760 Log(" %i (%i) - %s (CPU %i)",
761 thread->TID, thread->TGID, thread->ThreadName, thread->CurCPU);
762 if(thread->Status != THREAD_STAT_ACTIVE)
763 Log(" ERROR State (%i) != THREAD_STAT_ACTIVE (%i)", thread->Status, THREAD_STAT_ACTIVE);
764 Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum);
765 Log(" KStack 0x%x", thread->KernelStack);
769 for(thread=gAllThreads;thread;thread=thread->GlobalNext)
771 Log(" %i (%i) - %s (CPU %i)",
772 thread->TID, thread->TGID, thread->ThreadName, thread->CurCPU);
773 Log(" State %i", thread->Status);
774 Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum);
775 Log(" KStack 0x%x", thread->KernelStack);
780 * \brief Gets the next thread to run
781 * \param CPU Current CPU
782 * \param Last The thread the CPU was running
784 tThread *Threads_GetNextToRun(int CPU, tThread *Last)
791 SHORTLOCK( &glThreadListLock );
793 // Clear Delete Queue
794 // - I should probably put this in a worker thread to avoid calling free() in the scheduler
795 while(gDeleteThreads)
797 thread = gDeleteThreads->Next;
798 if( IS_LOCKED(&gDeleteThreads->IsLocked) ) { // Only free if structure is unused
800 gDeleteThreads->Status = THREAD_STAT_DEAD;
802 if( IsHeap(gDeleteThreads->ThreadName) )
803 free(gDeleteThreads->ThreadName);
804 // Remove from global list
805 if( gDeleteThreads == gAllThreads )
806 gAllThreads = gDeleteThreads->GlobalNext;
808 gDeleteThreads->GlobalPrev->GlobalNext = gDeleteThreads->GlobalNext;
809 free( gDeleteThreads );
811 gDeleteThreads = thread;
814 // No active threads, just take a nap
815 if(giNumActiveThreads == 0) {
816 SHORTREL( &glThreadListLock );
817 #if DEBUG_TRACE_TICKETS
818 Log("No active threads");
823 // Special case: 1 thread
824 if(giNumActiveThreads == 1) {
825 if( gActiveThreads->CurCPU == -1 )
826 gActiveThreads->CurCPU = CPU;
827 SHORTREL( &glThreadListLock );
828 if( gActiveThreads->CurCPU == CPU )
829 return gActiveThreads;
830 return NULL; // CPU has nothing to do
833 // Allow the old thread to be scheduled again
835 if( Last->Status == THREAD_STAT_ACTIVE ) {
836 giFreeTickets += Last->NumTickets;
837 #if DEBUG_TRACE_TICKETS
838 LogF(" CPU %i released %p (%i %s) into the pool (%i tickets in pool)\n",
839 CPU, Last, Last->TID, Last->ThreadName, giFreeTickets);
842 #if DEBUG_TRACE_TICKETS
844 LogF(" %p (%s)->Status = %i (Released)\n", Last, Last->ThreadName, Last->Status);
851 for(thread = gActiveThreads; thread; thread = thread->Next) {
852 if(thread->CurCPU >= 0) continue;
853 number += thread->NumTickets;
855 if(number != giFreeTickets) {
856 Panic("Bookkeeping fail (giFreeTickets(%i) != number(%i)) - CPU%i",
857 giFreeTickets, number, CPU);
861 // No free tickets (all tasks delegated to cores)
862 if( giFreeTickets == 0 ) {
863 SHORTREL(&glThreadListLock);
867 // Get the ticket number
868 ticket = number = rand() % giFreeTickets;
870 // Find the next thread
871 for(thread=gActiveThreads;thread;thread=thread->Next)
873 if(thread->CurCPU >= 0) continue;
874 if(thread->NumTickets > number) break;
875 number -= thread->NumTickets;
881 for(thread=gActiveThreads;thread;thread=thread->Next) {
882 if(thread->CurCPU >= 0) continue;
883 number += thread->NumTickets;
885 Panic("Bookeeping Failed - giFreeTickets(%i) > true count (%i)",
886 giFreeTickets, number);
888 #if DEBUG_TRACE_TICKETS
889 LogF(" CPU%i giFreeTickets = %i\n", CPU, giFreeTickets);
892 // Make the new thread non-schedulable
893 giFreeTickets -= thread->NumTickets;
894 thread->CurCPU = CPU;
897 #if DEBUG_TRACE_TICKETS
898 LogF(" CPU%i giFreeTickets = %i, giving %p (%i %s CPU=%i)\n",
899 CPU, giFreeTickets, thread, thread->TID, thread->ThreadName, thread->CurCPU);
902 SHORTREL( &glThreadListLock );
908 * \fn void Threads_SegFault(tVAddr Addr)
909 * \brief Called when a Segment Fault occurs
911 void Threads_SegFault(tVAddr Addr)
913 Warning("Thread #%i committed a segfault at address %p", Proc_GetCurThread()->TID, Addr);
915 //Threads_Exit( 0, -1 );
919 * \brief Acquire a heavy mutex
920 * \param Mutex Mutex to acquire
922 * This type of mutex checks if the mutex is avaliable, and acquires it
923 * if it is. Otherwise, the current thread is added to the mutex's wait
924 * queue and the thread suspends. When the holder of the mutex completes,
925 * the oldest thread (top thread) on the queue is given the lock and
928 void Mutex_Acquire(tMutex *Mutex)
930 tThread *us = Proc_GetCurThread();
933 SHORTLOCK( &Mutex->Protector );
935 //Log("Mutex_Acquire: (%p)", Mutex);
937 // Check if the lock is already held
939 SHORTLOCK( &glThreadListLock );
940 // - Remove from active list
942 // - Mark as sleeping
943 us->Status = THREAD_STAT_OFFSLEEP;
946 if(Mutex->LastWaiting) {
947 Mutex->LastWaiting->Next = us;
948 Mutex->LastWaiting = us;
952 Mutex->LastWaiting = us;
954 SHORTREL( &glThreadListLock );
955 SHORTREL( &Mutex->Protector );
956 while(us->Status == THREAD_STAT_OFFSLEEP) Threads_Yield();
957 // We're only woken when we get the lock
959 // Ooh, let's take it!
962 SHORTREL( &Mutex->Protector );
967 * \brief Release a held mutex
968 * \param Mutex Mutex to release
970 void Mutex_Release(tMutex *Mutex)
972 SHORTLOCK( &Mutex->Protector );
973 //Log("Mutex_Release: (%p)", Mutex);
974 if( Mutex->Waiting ) {
975 Mutex->Owner = Mutex->Waiting; // Set owner
976 Mutex->Waiting = Mutex->Waiting->Next; // Next!
978 Threads_AddActive(Mutex->Owner);
979 //Log("Mutex %p Woke %p", Mutex, Mutex->Owner);
984 SHORTREL( &Mutex->Protector );
988 * \brief Is this mutex locked?
989 * \param Mutex Mutex pointer
991 int Mutex_IsLocked(tMutex *Mutex)
993 return Mutex->Owner != NULL;
997 EXPORT(Threads_GetUID);
998 EXPORT(Mutex_Acquire);
999 EXPORT(Mutex_Release);
1000 EXPORT(Mutex_IsLocked);