Skip to content

Commit 1e576a3

Browse files
Isolate common wait_end logic
1 parent 9db698a commit 1e576a3

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/libextra/sync.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ impl WaitQueue {
7171
}
7272
count
7373
}
74+
75+
fn wait_end(&self) -> WaitEnd {
76+
let (wait_end, signal_end) = comm::oneshot();
77+
self.tail.send_deferred(signal_end);
78+
wait_end
79+
}
7480
}
7581

7682
// The building-block used to make semaphores, mutexes, and rwlocks.
@@ -99,12 +105,9 @@ impl<Q:Send> Sem<Q> {
99105
do (**self).with |state| {
100106
state.count -= 1;
101107
if state.count < 0 {
102-
// Create waiter nobe.
103-
let (WaitEnd, SignalEnd) = comm::oneshot();
104-
// Tell outer scope we need to block.
105-
waiter_nobe = Some(WaitEnd);
106-
// Enqueue ourself.
107-
state.waiters.tail.send_deferred(SignalEnd);
108+
// Create waiter nobe, enqueue ourself, and tell
109+
// outer scope we need to block.
110+
waiter_nobe = Some(state.waiters.wait_end());
108111
}
109112
}
110113
// Uncomment if you wish to test for sem races. Not valgrind-friendly.
@@ -200,10 +203,7 @@ impl<'self> Condvar<'self> {
200203
* wait() is equivalent to wait_on(0).
201204
*/
202205
pub fn wait_on(&self, condvar_id: uint) {
203-
// Create waiter nobe.
204-
let (WaitEnd, SignalEnd) = comm::oneshot();
205-
let mut WaitEnd = Some(WaitEnd);
206-
let mut SignalEnd = Some(SignalEnd);
206+
let mut WaitEnd = None;
207207
let mut out_of_bounds = None;
208208
do task::unkillable {
209209
// Release lock, 'atomically' enqueuing ourselves in so doing.
@@ -215,9 +215,9 @@ impl<'self> Condvar<'self> {
215215
if state.count <= 0 {
216216
state.waiters.signal();
217217
}
218-
// Enqueue ourself to be woken up by a signaller.
219-
let SignalEnd = SignalEnd.take_unwrap();
220-
state.blocked[condvar_id].tail.send_deferred(SignalEnd);
218+
// Create waiter nobe, and enqueue ourself to
219+
// be woken up by a signaller.
220+
WaitEnd = Some(state.blocked[condvar_id].wait_end());
221221
} else {
222222
out_of_bounds = Some(state.blocked.len());
223223
}

0 commit comments

Comments
 (0)