Skip to content

Commit 68e8fe9

Browse files
committed
Add a test case for #7065.
1 parent bd019c4 commit 68e8fe9

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/libextra/arc.rs

+62
Original file line numberDiff line numberDiff line change
@@ -813,4 +813,66 @@ mod tests {
813813

814814
wp2.recv(); // complete handshake with writer
815815
}
816+
#[cfg(test)]
817+
fn test_rw_write_cond_downgrade_read_race_helper() {
818+
// Tests that when a downgrader hands off the "reader cloud" lock
819+
// because of a contending reader, a writer can't race to get it
820+
// instead, which would result in readers_and_writers. This tests
821+
// the sync module rather than this one, but it's here because an
822+
// rwarc gives us extra shared state to help check for the race.
823+
// If you want to see this test fail, go to sync.rs and replace the
824+
// line in RWlock::write_cond() that looks like:
825+
// "blk(&Condvar { order: opt_lock, ..*cond })"
826+
// with just "blk(cond)".
827+
let x = ~RWARC(true);
828+
let (wp, wc) = comm::stream();
829+
830+
// writer task
831+
let xw = (*x).clone();
832+
do task::spawn {
833+
do xw.write_cond |state, c| {
834+
wc.send(()); // tell downgrader it's ok to go
835+
c.wait();
836+
// The core of the test is here: the condvar reacquire path
837+
// must involve order_lock, so that it cannot race with a reader
838+
// trying to receive the "reader cloud lock hand-off".
839+
*state = false;
840+
}
841+
}
842+
843+
wp.recv(); // wait for writer to get in
844+
845+
do x.write_downgrade |mut write_mode| {
846+
do write_mode.write_cond |state, c| {
847+
assert!(*state);
848+
// make writer contend in the cond-reacquire path
849+
c.signal();
850+
}
851+
// make a reader task to trigger the "reader cloud lock" handoff
852+
let xr = (*x).clone();
853+
let (rp, rc) = comm::stream();
854+
do task::spawn {
855+
rc.send(());
856+
do xr.read |_state| { }
857+
}
858+
rp.recv(); // wait for reader task to exist
859+
860+
let read_mode = x.downgrade(write_mode);
861+
do read_mode.read |state| {
862+
// if writer mistakenly got in, make sure it mutates state
863+
// before we assert on it
864+
for 5.times { task::yield(); }
865+
// make sure writer didn't get in.
866+
assert!(*state);
867+
}
868+
}
869+
}
870+
#[test]
871+
fn test_rw_write_cond_downgrade_read_race() {
872+
// Ideally the above test case would have yield statements in it that
873+
// helped to expose the race nearly 100% of the time... but adding
874+
// yields in the intuitively-right locations made it even less likely,
875+
// and I wasn't sure why :( . This is a mediocre "next best" option.
876+
for 8.times { test_rw_write_cond_downgrade_read_race_helper() }
877+
}
816878
}

0 commit comments

Comments
 (0)