11 /* A simple spinlock. Yield the thread while the given integer is set by
12 * another. Could probably be improved... */
13 static void Lock(volatile int *l
)
15 while(ExchangeInt(l
, true) == true)
19 static void Unlock(volatile int *l
)
21 ExchangeInt(l
, false);
25 void RWLockInit(RWLock
*lock
)
27 InitRef(&lock
->read_count
, 0);
28 InitRef(&lock
->write_count
, 0);
29 lock
->read_lock
= false;
30 lock
->read_entry_lock
= false;
31 lock
->write_lock
= false;
34 void ReadLock(RWLock
*lock
)
36 Lock(&lock
->read_entry_lock
);
37 Lock(&lock
->read_lock
);
38 if(IncrementRef(&lock
->read_count
) == 1)
39 Lock(&lock
->write_lock
);
40 Unlock(&lock
->read_lock
);
41 Unlock(&lock
->read_entry_lock
);
44 void ReadUnlock(RWLock
*lock
)
46 if(DecrementRef(&lock
->read_count
) == 0)
47 Unlock(&lock
->write_lock
);
50 void WriteLock(RWLock
*lock
)
52 if(IncrementRef(&lock
->write_count
) == 1)
53 Lock(&lock
->read_lock
);
54 Lock(&lock
->write_lock
);
57 void WriteUnlock(RWLock
*lock
)
59 Unlock(&lock
->write_lock
);
60 if(DecrementRef(&lock
->write_count
) == 0)
61 Unlock(&lock
->read_lock
);