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
24 // - OR, there's a writer waiting to write
25 if( Lock->Owner || Lock->WriterWaiting )
28 Threads_int_Sleep(THREAD_STAT_RWLOCKSLEEP, Lock, 0,
29 &Lock->ReaderWaiting, &Lock->ReaderWaitingLast,
32 // Ooh, no problems then!
36 SHORTREL( & Lock->Protector );
43 int RWLock_AcquireWrite(tRWLock *Lock)
45 LOG("Acquire RWLock Write %p", Lock);
47 SHORTLOCK(&Lock->Protector);
48 if( Lock->Owner || Lock->Level != 0 )
51 Threads_int_Sleep(THREAD_STAT_RWLOCKSLEEP, Lock, 0,
52 &Lock->WriterWaiting, &Lock->WriterWaitingLast,
57 // Nothing else is using the lock, nice :)
58 Lock->Owner = Proc_GetCurThread();
59 SHORTREL(&Lock->Protector);
66 void RWLock_Release(tRWLock *Lock)
68 LOG("Release RWLock %p", Lock);
69 SHORTLOCK( &Lock->Protector );
71 if( Lock->Owner != Proc_GetCurThread() )
75 if( Lock->WriterWaiting )
77 Lock->Owner = Lock->WriterWaiting; // Set owner
78 Lock->WriterWaiting = Lock->WriterWaiting->Next; // Next!
81 if( Lock->Owner->Status != THREAD_STAT_ACTIVE )
82 Threads_AddActive(Lock->Owner);
88 while( Lock->ReaderWaiting ) {
90 Threads_AddActive(Lock->ReaderWaiting);
91 Lock->ReaderWaiting = Lock->ReaderWaiting->Next;
94 SHORTREL( &Lock->Protector );
98 EXPORT(RWLock_AcquireRead);
99 EXPORT(RWLock_AcquireWrite);
100 EXPORT(RWLock_Release);