Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a27f339

Browse files
committed
std::rt: Don't allow schedulers to exit before handling all messages
Every time run_sched_once performs a 'scheduling action' it needs to guarantee that it runs at least one more time, so enqueue another run_sched_once callback. The primary reason it needs to do this is because not all async callbacks are guaranteed to run, it's only guaranteed that *a* callback will run after enqueing one - some may get dropped. At the moment this means we wastefully create lots of callbacks to ensure that there will *definitely* be a callback queued up to continue running the scheduler. The logic really needs to be tightened up here.
1 parent f0f7e1b commit a27f339

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/libstd/rt/sched.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ impl Scheduler {
172172

173173
rtdebug!("stopping scheduler %u", stask.sched.get_ref().sched_id());
174174

175+
// Should not have any messages
176+
let message = stask.sched.get_mut_ref().message_queue.pop();
177+
assert!(message.is_none());
178+
175179
stask.destroyed = true;
176180
}
177181

@@ -336,11 +340,14 @@ impl Scheduler {
336340
match this.message_queue.pop() {
337341
Some(PinnedTask(task)) => {
338342
let mut task = task;
343+
this.event_loop.callback(Scheduler::run_sched_once);
339344
task.give_home(Sched(this.make_handle()));
340345
this.resume_task_immediately(task);
341346
return None;
342347
}
343348
Some(TaskFromFriend(task)) => {
349+
this.event_loop.callback(Scheduler::run_sched_once);
350+
rtdebug!("got a task from a friend. lovely!");
344351
return this.sched_schedule_task(task);
345352
}
346353
Some(Wake) => {
@@ -395,6 +402,7 @@ impl Scheduler {
395402
/// Take a non-homed task we aren't allowed to run here and send
396403
/// it to the designated friend scheduler to execute.
397404
fn send_to_friend(&mut self, task: ~Task) {
405+
rtdebug!("sending a task to friend");
398406
match self.friend_handle {
399407
Some(ref mut handle) => {
400408
handle.send(TaskFromFriend(task));
@@ -426,12 +434,14 @@ impl Scheduler {
426434
Scheduler::send_task_home(task);
427435
return Some(this);
428436
} else {
437+
this.event_loop.callback(Scheduler::run_sched_once);
429438
task.give_home(Sched(home_handle));
430439
this.resume_task_immediately(task);
431440
return None;
432441
}
433442
}
434443
AnySched if this.run_anything => {
444+
this.event_loop.callback(Scheduler::run_sched_once);
435445
task.give_home(AnySched);
436446
this.resume_task_immediately(task);
437447
return None;

0 commit comments

Comments
 (0)