Skip to content

Commit 8be66e2

Browse files
committed
std: Implement yields on receives for channels
This will prevent a deadlock when a task spins in a try_recv when using channel communication routines is a clear location for a M:N scheduling to happen.
1 parent 14caf00 commit 8be66e2

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/libstd/comm/mod.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ use rt::local::Local;
238238
use rt::task::{Task, BlockedTask};
239239
use rt::thread::Thread;
240240
use sync::atomics::{AtomicInt, AtomicBool, SeqCst, Relaxed};
241-
use task;
242241
use vec::{ImmutableVector, OwnedVector};
243242

244243
use spsc = sync::spsc_queue;
@@ -346,6 +345,7 @@ struct Packet {
346345
selection_id: uint,
347346
select_next: *mut Packet,
348347
select_prev: *mut Packet,
348+
recv_cnt: int,
349349
}
350350

351351
///////////////////////////////////////////////////////////////////////////////
@@ -367,6 +367,7 @@ impl Packet {
367367
selection_id: 0,
368368
select_next: 0 as *mut Packet,
369369
select_prev: 0 as *mut Packet,
370+
recv_cnt: 0,
370371
}
371372
}
372373

@@ -611,8 +612,9 @@ impl<T: Send> Chan<T> {
611612
// the TLS overhead can be a bit much.
612613
n => {
613614
assert!(n >= 0);
614-
if can_resched && n > 0 && n % RESCHED_FREQ == 0 {
615-
task::deschedule();
615+
if n > 0 && n % RESCHED_FREQ == 0 {
616+
let task: ~Task = Local::take();
617+
task.maybe_yield();
616618
}
617619
true
618620
}
@@ -704,8 +706,9 @@ impl<T: Send> SharedChan<T> {
704706
DISCONNECTED => {} // oh well, we tried
705707
-1 => { (*packet).wakeup(can_resched); }
706708
n => {
707-
if can_resched && n > 0 && n % RESCHED_FREQ == 0 {
708-
task::deschedule();
709+
if n > 0 && n % RESCHED_FREQ == 0 {
710+
let task: ~Task = Local::take();
711+
task.maybe_yield();
709712
}
710713
}
711714
}
@@ -773,6 +776,18 @@ impl<T: Send> Port<T> {
773776
// This is a "best effort" situation, so if a queue is inconsistent just
774777
// don't worry about it.
775778
let this = unsafe { cast::transmute_mut(self) };
779+
780+
// See the comment about yielding on sends, but the same applies here.
781+
// If a thread is spinning in try_recv we should try
782+
unsafe {
783+
let packet = this.queue.packet();
784+
(*packet).recv_cnt += 1;
785+
if (*packet).recv_cnt % RESCHED_FREQ == 0 {
786+
let task: ~Task = Local::take();
787+
task.maybe_yield();
788+
}
789+
}
790+
776791
let ret = match this.queue {
777792
SPSC(ref mut queue) => queue.pop(),
778793
MPSC(ref mut queue) => match queue.pop() {

0 commit comments

Comments
 (0)