Question: A reader-writer (RW) spinlock is a multiprocessor spinlock which allows multiple concurrent readers but only one writer. Of course, a writer cannot enter critical section
A reader-writer (RW) spinlock is a multiprocessor spinlock which allows multiple concurrent readers but only one writer. Of course, a writer cannot enter critical section if there is any reader or writer. A reader cannot enter critical section if there is an /* compare old and *ptr. update *ptr with new only if they are the same. return most recent *ptr value */ int compare_and_swap(int *ptr, int old, int new) { asm( cas new, old, [ptr] ); return old; } void atomic_add(int *ptr, int val) { int res, old = *ptr; while (1) { res = compare_and_swap(ptr, old, old+val); if (res == old) break; old = res; } } active writer. The difference from blocking RW lock is that this spinning RW lock should spin-wait instead of calling scheduler.
Implement spinning RW lock (SRWLock) using compare_and_swap() atomic instruction. SRWLock should use a signed 32-bit value to store lock status using following convention: value == 0 : lock is free (no active reader, no active writer) value >= 1 : lock is held by value number of active reader(s) value == -1 : lock is held by single active writer


The class prototype is given below. Your task is to implement the four functions class SRWLock private int value; public: RWLock value(0) f; RWLock void start Read //Reader acquire void doneRead(); Reader release void start Write Writer acquire void doneWrite(); //Writer release You should use compare and swap function as your atomic primitive. The class prototype is given below. Your task is to implement the four functions class SRWLock private int value; public: RWLock value(0) f; RWLock void start Read //Reader acquire void doneRead(); Reader release void start Write Writer acquire void doneWrite(); //Writer release You should use compare and swap function as your atomic primitive
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
