Skip to content

Commit

Permalink
Isolate common wait_end logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mstewartgallus committed Aug 9, 2013
1 parent 9db698a commit 1e576a3
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/libextra/sync.rs
Expand Up @@ -71,6 +71,12 @@ impl WaitQueue {
}
count
}

fn wait_end(&self) -> WaitEnd {
let (wait_end, signal_end) = comm::oneshot();
self.tail.send_deferred(signal_end);
wait_end
}
}

// The building-block used to make semaphores, mutexes, and rwlocks.
Expand Down Expand Up @@ -99,12 +105,9 @@ impl<Q:Send> Sem<Q> {
do (**self).with |state| {
state.count -= 1;
if state.count < 0 {
// Create waiter nobe.
let (WaitEnd, SignalEnd) = comm::oneshot();
// Tell outer scope we need to block.
waiter_nobe = Some(WaitEnd);
// Enqueue ourself.
state.waiters.tail.send_deferred(SignalEnd);
// Create waiter nobe, enqueue ourself, and tell
// outer scope we need to block.
waiter_nobe = Some(state.waiters.wait_end());
}
}
// Uncomment if you wish to test for sem races. Not valgrind-friendly.
Expand Down Expand Up @@ -200,10 +203,7 @@ impl<'self> Condvar<'self> {
* wait() is equivalent to wait_on(0).
*/
pub fn wait_on(&self, condvar_id: uint) {
// Create waiter nobe.
let (WaitEnd, SignalEnd) = comm::oneshot();
let mut WaitEnd = Some(WaitEnd);
let mut SignalEnd = Some(SignalEnd);
let mut WaitEnd = None;
let mut out_of_bounds = None;
do task::unkillable {
// Release lock, 'atomically' enqueuing ourselves in so doing.
Expand All @@ -215,9 +215,9 @@ impl<'self> Condvar<'self> {
if state.count <= 0 {
state.waiters.signal();
}
// Enqueue ourself to be woken up by a signaller.
let SignalEnd = SignalEnd.take_unwrap();
state.blocked[condvar_id].tail.send_deferred(SignalEnd);
// Create waiter nobe, and enqueue ourself to
// be woken up by a signaller.
WaitEnd = Some(state.blocked[condvar_id].wait_end());
} else {
out_of_bounds = Some(state.blocked.len());
}
Expand Down

5 comments on commit 1e576a3

@bors
Copy link
Contributor

@bors bors commented on 1e576a3 Aug 10, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from bblum
at mstewartgallus@1e576a3

@bors
Copy link
Contributor

@bors bors commented on 1e576a3 Aug 10, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging sstewartgallus/rust/factor_out_waitqueue = 1e576a3 into auto

@bors
Copy link
Contributor

@bors bors commented on 1e576a3 Aug 10, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sstewartgallus/rust/factor_out_waitqueue = 1e576a3 merged ok, testing candidate = 2ba36ec

@bors
Copy link
Contributor

@bors bors commented on 1e576a3 Aug 10, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 2ba36ec

Please sign in to comment.