I am trying to learn some operating system engineering so I came about MIT's operating system engineering course which has been wonderful so far.
The problem I'm having is with the second problem read-write lock.
I believe I know the concept of readers-writer lock as described in the lab, readers gets a shared access and writers need exclusive access. I've been trying to implement it with writer preference as asked but it just doesn't seem to work. So there are some questions I want to ask:
Is it true that I am not suppose to use sleep(channel, lock), awake (channel) functions in the implementation because it's a spinlock?
I've tried doing readers, writers, writer_active flag with writer exclusive lock but readers were jumping ahead of writers occasionally.
I think I've tried to the best of my knowledge and what I could find. I would love some guidance.
This is the link to the lock lab.
https://pdos.csail.mit.edu/6.1810/2025/labs/lock.html
If you really want to see my previous code Xd
static int check_waiters(struct rwspinlock *rwlk) {
return __atomic_load_n(&rwlk->write_waiters, __ATOMIC_SEQ_CST);
}
static void read_acquire_inner(struct rwspinlock *rwlk) {
while (1) {
acquire(&rwlk->wl);
if (check_waiters(rwlk) == 0) {
__atomic_fetch_add(&rwlk->readers, 1, __ATOMIC_SEQ_CST);
release(&rwlk->wl);
break; // Successfully acquired read lock
}
release(&rwlk->wl);
}
}
static void read_release_inner(struct rwspinlock *rwlk) {
__atomic_fetch_sub(&rwlk->readers, 1, __ATOMIC_SEQ_CST);
}
static void write_acquire_inner(struct rwspinlock *rwlk) {
acquire(&rwlk->wl);
__atomic_fetch_add(&rwlk->write_waiters, 1, __ATOMIC_SEQ_CST);
while (1) {
if (__atomic_load_n(&rwlk->readers, __ATOMIC_SEQ_CST) == 0) {
break;
}
release(&rwlk->wl);
acquire(&rwlk->wl);
}
}
static void write_release_inner(struct rwspinlock *rwlk) {
__atomic_fetch_sub(&rwlk->write_waiters, 1, __ATOMIC_SEQ_CST);
release(&rwlk->wl);
}