Skip to content

Commit 6306ad9

Browse files
improve idle detection
1 parent 546ad3d commit 6306ad9

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/rt/runtime.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ struct Scheduler {
3535
/// Set to `true` while a machine is polling the reactor.
3636
polling: bool,
3737

38+
progress: bool,
39+
3840
/// Available threads.
3941
threads: Vec<ThreadState>,
4042

@@ -83,6 +85,7 @@ impl Runtime {
8385
machines: Vec::with_capacity(*MAXPROCS),
8486
threads,
8587
polling: false,
88+
progress: false,
8689
}),
8790
}
8891
}
@@ -127,7 +130,7 @@ impl Runtime {
127130

128131
// println!("getting idle thread");
129132
let sched = self.sched.lock().unwrap();
130-
'inner: for (i, thread) in sched.threads.iter().enumerate() {
133+
'inner: for thread in sched.threads.iter() {
131134
// grab the first parked thread
132135
if thread
133136
.parked
@@ -166,7 +169,7 @@ impl Runtime {
166169
parker.park();
167170
// TODO: shutdown if idle for too long
168171
}
169-
// println!("{} thread unparked {}", k, i);
172+
// println!("thread unparked {}", i);
170173
// when this thread is unparked, retrieve machine
171174
let m: Arc<Machine> =
172175
machine_recv.recv().expect("failed to receive machine");
@@ -189,8 +192,6 @@ impl Runtime {
189192
.send(m)
190193
.expect("failed to send machine to thread");
191194

192-
// println!("started thread {}", i);
193-
194195
sched.threads.push(ThreadState {
195196
unparker,
196197
parked,
@@ -222,11 +223,14 @@ impl Runtime {
222223
// If no machine has been polling the reactor in a while, that means the runtime is
223224
// overloaded with work and we need to start another machine.
224225
if !sched.polling {
225-
if let Some(p) = sched.processors.pop() {
226-
let m = Arc::new(Machine::new(p));
227-
to_start.push(m.clone());
228-
sched.machines.push(m);
226+
if !sched.progress {
227+
if let Some(p) = sched.processors.pop() {
228+
let m = Arc::new(Machine::new(p));
229+
to_start.push(m.clone());
230+
sched.machines.push(m);
231+
}
229232
}
233+
sched.progress = false;
230234
}
231235

232236
to_start
@@ -420,13 +424,16 @@ impl Machine {
420424
sched.polling = false;
421425
//println!("polling stop");
422426
sched.machines.push(m);
427+
sched.progress = true;
423428

424429
runs = 0;
425430
fails = 0;
426431
}
432+
// println!("thread break");
427433

428434
// When shutting down the thread, take the processor out if still available.
429435
let opt_p = self.processor.lock().take();
436+
// println!("processor {:?}", opt_p.is_some());
430437

431438
// Return the processor to the scheduler and remove the machine.
432439
if let Some(p) = opt_p {
@@ -435,6 +442,7 @@ impl Machine {
435442
sched.processors.push(p);
436443
sched.machines.retain(|elem| !ptr::eq(&**elem, self));
437444
}
445+
// println!("thread run stopped");
438446
}
439447
}
440448

0 commit comments

Comments
 (0)