3 * - By John Hodge (thePowersGang)
6 * - Reader-Writer Lockes
10 #include <threads_int.h>
15 // Acquire as a reader (see rwlock.h for documentation)
17 int RWLock_AcquireRead(tRWLock *Lock)
19 LOG("Acquire RWLock Read %p", Lock);
21 SHORTLOCK( &Lock->Protector );
23 // Check if the lock is already held by a writer
27 Threads_int_Sleep(THREAD_STAT_RWLOCKSLEEP, Lock, 0,
28 &Lock->ReaderWaiting, &Lock->ReaderWaitingLast,
31 // Ooh, no problems then!
35 SHORTREL( & Lock->Protector );
42 int RWLock_AcquireWrite(tRWLock *Lock)
44 LOG("Acquire RWLock Write %p", Lock);
46 SHORTLOCK(&Lock->Protector);
47 if( Lock->Owner || Lock->Level != 0 )
50 Threads_int_Sleep(THREAD_STAT_RWLOCKSLEEP, Lock, 0,
51 &Lock->WriterWaiting, &Lock->WriterWaitingLast,
56 // Nothing else is using the lock, nice :)
57 Lock->Owner = Proc_GetCurThread();
58 SHORTREL(&Lock->Protector);
65 void RWLock_Release(tRWLock *Lock)
67 LOG("Release RWLock %p", Lock);
68 SHORTLOCK( &Lock->Protector );
70 if( Lock->Owner != Proc_GetCurThread() )
74 if( Lock->WriterWaiting )
76 Lock->Owner = Lock->WriterWaiting; // Set owner
77 Lock->WriterWaiting = Lock->WriterWaiting->Next; // Next!
80 if( Lock->Owner->Status != THREAD_STAT_ACTIVE )
81 Threads_AddActive(Lock->Owner);
87 while( Lock->ReaderWaiting ) {
89 Threads_AddActive(Lock->ReaderWaiting);
90 Lock->ReaderWaiting = Lock->ReaderWaiting->Next;
93 SHORTREL( &Lock->Protector );
97 EXPORT(RWLock_AcquireRead);
98 EXPORT(RWLock_AcquireWrite);
99 EXPORT(RWLock_Release);