Hi all, I am implementing the read-write lock from this paper in Rust. Listing 1 on page 5 of the PDF has a clear pseudocode implementation I've tried to follow. I also have a reference implementation in C from a related paper.
My implementation: https://github.com/cmnord/pflock/blob/master/src/lib.rs
The C reference implementation: https://github.com/cmnord/pflock/blob/master/pflock_c/pft.h
Each implementation has fewer than 100 lines of code, but I've introduced a bug in translation.
The bug is reproduced in
tests/simple.rs (run with
cargo test simple). When readers and writers access the same shared object from different threads, sometimes the writer acquires the write lock before a reader has released a read lock. I use a
RefCell as the lock-guarded object in my test to catch this bug at runtime.
Would anyone more familiar with C and Rust (and the translation between) be willing to take a look at these two implementations and see where I've messed up? They should basically correspond line-by-line.