2 * Acess2 libnative (Kernel Simulation Library)
3 * - By John Hodge (thePowersGang)
11 #include <threads_int.h>
14 int RWLock_AcquireRead(tRWLock *Lock)
16 SHORTLOCK(&Lock->Protector);
17 while( Lock->Level < 0 || Lock->WriterWaiting )
20 tThread *me = Proc_GetCurThread();
22 if( Lock->ReaderWaiting )
23 Lock->ReaderWaitingLast->Next = me;
25 Lock->ReaderWaiting = me;
26 Lock->ReaderWaitingLast = me;
27 SHORTREL(&Lock->Protector);
28 Threads_WaitEvents(THREAD_EVENT_RWLOCK);
29 SHORTLOCK(&Lock->Protector);
32 SHORTREL(&Lock->Protector);
36 int RWLock_AcquireWrite(tRWLock *Lock)
38 SHORTLOCK(&Lock->Protector);
40 while( Lock->Level != 0 )
42 tThread *me = Proc_GetCurThread();
44 if( Lock->WriterWaiting )
45 Lock->WriterWaitingLast->Next = me;
47 Lock->WriterWaiting = me;
49 SHORTREL(&Lock->Protector);
50 Threads_WaitEvents(THREAD_EVENT_RWLOCK);
51 SHORTLOCK(&Lock->Protector);
54 SHORTREL(&Lock->Protector);
58 void RWLock_Release(tRWLock *Lock)
60 SHORTLOCK(&Lock->Protector);
61 if( Lock->Level == -1 || Lock->Level == 1 )
63 // Last reader or a writer yeilding
64 // - Yields to writers first, then readers
65 if( Lock->WriterWaiting )
67 Threads_PostEvent(Lock->WriterWaiting, THREAD_EVENT_RWLOCK);
68 Lock->WriterWaiting = Lock->WriterWaiting->ListNext;
72 while(Lock->ReaderWaiting)
74 Threads_PostEvent(Lock->ReaderWaiting, THREAD_EVENT_RWLOCK);
75 Lock->ReaderWaiting = Lock->ReaderWaiting->ListNext;
78 // Nobody to wake, oh well
81 else if( Lock->Level == 0 )
89 SHORTREL(&Lock->Protector);