3 * - By John Hodge (thePowersGang)
9 #include <threads_int.h>
14 int Mutex_Acquire(tMutex *Mutex);
15 void Mutex_Release(tMutex *Mutex);
16 int Mutex_IsLocked(tMutex *Mutex);
21 // Acquire mutex (see mutex.h for documentation)
23 int Mutex_Acquire(tMutex *Mutex)
25 tThread *us = Proc_GetCurThread();
28 SHORTLOCK( &Mutex->Protector );
30 // Log("Mutex_Acquire: (%p)", Mutex);
32 // Check if the lock is already held
35 Threads_int_Sleep(THREAD_STAT_MUTEXSLEEP,
37 &Mutex->Waiting, &Mutex->LastWaiting, &Mutex->Protector);
38 // - We're only woken when we get the lock
41 // If not, just obtain it
43 SHORTREL( &Mutex->Protector );
47 extern tMutex glPhysAlloc;
48 if( Mutex != &glPhysAlloc )
49 LogF("Mutex %p taken by %i %p\n", Mutex, us->TID, __builtin_return_address(0));
56 void Mutex_Release(tMutex *Mutex)
58 SHORTLOCK( &Mutex->Protector );
59 //Log("Mutex_Release: (%p)", Mutex);
60 if( Mutex->Waiting ) {
61 Mutex->Owner = Mutex->Waiting; // Set owner
62 Mutex->Waiting = Mutex->Waiting->Next; // Next!
63 // Reset ->LastWaiting to NULL if we have just removed the last waiting thread
64 if( Mutex->LastWaiting == Mutex->Owner )
65 Mutex->LastWaiting = NULL;
68 if( Mutex->Owner->Status != THREAD_STAT_ACTIVE )
69 Threads_AddActive(Mutex->Owner);
74 SHORTREL( &Mutex->Protector );
77 extern tMutex glPhysAlloc;
78 if( Mutex != &glPhysAlloc )
79 LogF("Mutex %p released by %i %p\n", Mutex, Threads_GetTID(), __builtin_return_address(0));
83 // Check if a mutex is locked
84 int Mutex_IsLocked(tMutex *Mutex)
86 return Mutex->Owner != NULL;
90 EXPORT(Mutex_Acquire);
91 EXPORT(Mutex_Release);
92 EXPORT(Mutex_IsLocked);