* rwlock.c
* - Reader-Writer Lockes
*/
+#define DEBUG 0
#include <acess.h>
#include <threads_int.h>
#include <rwlock.h>
int RWLock_AcquireRead(tRWLock *Lock)
{
tThread *us;
+ LOG("Acquire RWLock Read %p", Lock);
// Get protector
SHORTLOCK( &Lock->Protector );
// Check if the lock is already held by a writer
if( Lock->Owner )
{
+ LOG("Waiting");
SHORTLOCK( &glThreadListLock );
// - Remove from active list
Lock->Level++;
SHORTREL( & Lock->Protector );
}
+ LOG("Obtained");
return 0;
}
{
tThread *us;
+ LOG("Acquire RWLock Write %p", Lock);
+
SHORTLOCK(&Lock->Protector);
if( Lock->Owner || Lock->Level != 0 )
{
+ LOG("Waiting");
SHORTLOCK(&glThreadListLock);
us = Threads_RemActive();
Lock->Owner = Proc_GetCurThread();
SHORTREL(&Lock->Protector);
}
+ LOG("Obtained");
return 0;
}
// Release a mutex
void RWLock_Release(tRWLock *Lock)
{
+ LOG("Release RWLock %p", Lock);
SHORTLOCK( &Lock->Protector );
- if( Lock->Owner == Proc_GetCurThread() )
+ if( Lock->Owner != Proc_GetCurThread() )
Lock->Level --;
// Writers first