Skip to content

Commit 7ec8db1

Browse files
committed
no need to consider multipe runtime, there will be only exactly one
1 parent 5b65919 commit 7ec8db1

File tree

3 files changed

+22
-34
lines changed

3 files changed

+22
-34
lines changed

src/rt/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,15 @@ pub static RUNTIME: Lazy<Runtime> = Lazy::new(|| {
2424

2525
Runtime::new()
2626
});
27+
28+
/// Spawn new worker thread
29+
pub fn scale_up() {
30+
RUNTIME.scale_up();
31+
}
32+
33+
/// Terminate one worker thread.
34+
/// The number of worker thread cannot go below the number of availabe cpus,
35+
/// so this function when will do nothing if that happen.
36+
pub fn scale_down() {
37+
RUNTIME.scale_down();
38+
}

src/rt/monitor.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1-
use std::cell::Cell;
2-
use std::future::Future;
31
use std::sync::{Arc, Mutex};
42
use std::thread;
53
use std::time::Duration;
64

7-
use crate::rt::RUNTIME;
5+
use crate::rt;
86
use crate::task;
97
use crate::utils::abort_on_panic;
108

11-
fn spawn_monitor_task<F>(future: F)
12-
where
13-
F: Future<Output = ()> + Send + 'static,
14-
{
15-
task::Builder::new()
16-
.spawn_in_runtime(future, &RUNTIME)
17-
.expect("cannot spawn monitor task");
18-
}
19-
209
pub fn spawn_thread() {
2110
thread::Builder::new()
2211
.name("async-std/monitor".to_string())
@@ -25,35 +14,35 @@ pub fn spawn_thread() {
2514
const SCALING_DOWN_SEC: u64 = 1 * 60; // 1 minute
2615

2716
abort_on_panic(|| {
28-
let running = &Arc::new(Mutex::new(Cell::new(false)));
17+
let running = &Arc::new(Mutex::new(false));
2918

3019
{
3120
let running = Arc::clone(running);
32-
spawn_monitor_task(async move {
21+
task::spawn(async move {
3322
loop {
34-
running.lock().unwrap().set(true);
23+
*running.lock().unwrap() = true;
3524
task::sleep(Duration::from_millis(PROBING_DURATION_MS)).await;
3625
}
3726
});
3827
}
3928

4029
{
41-
spawn_monitor_task(async {
30+
task::spawn(async {
4231
loop {
4332
task::sleep(Duration::from_secs(SCALING_DOWN_SEC)).await;
44-
RUNTIME.scale_down();
33+
rt::scale_down();
4534
}
4635
});
4736
}
4837

4938
loop {
50-
running.lock().unwrap().set(false);
39+
*running.lock().unwrap() = false;
5140
thread::sleep(Duration::from_millis(PROBING_DURATION_MS * 2));
52-
if !running.lock().unwrap().get() {
41+
if !*running.lock().unwrap() {
5342
eprintln!(
5443
"WARNING: You are blocking the runtime, please use spawn_blocking"
5544
);
56-
RUNTIME.scale_up();
45+
rt::scale_up();
5746
}
5847
}
5948
})

src/task/builder.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::future::Future;
33
use kv_log_macro::trace;
44

55
use crate::io;
6-
use crate::rt::Runtime;
76
use crate::rt::RUNTIME;
87
use crate::task::{JoinHandle, Task};
98
use crate::utils::abort_on_panic;
@@ -30,18 +29,6 @@ impl Builder {
3029

3130
/// Spawns a task with the configured settings.
3231
pub fn spawn<F, T>(self, future: F) -> io::Result<JoinHandle<T>>
33-
where
34-
F: Future<Output = T> + Send + 'static,
35-
T: Send + 'static,
36-
{
37-
self.spawn_in_runtime(future, &RUNTIME)
38-
}
39-
40-
pub(crate) fn spawn_in_runtime<F, T>(
41-
self,
42-
future: F,
43-
rt: &'static Runtime,
44-
) -> io::Result<JoinHandle<T>>
4532
where
4633
F: Future<Output = T> + Send + 'static,
4734
T: Send + 'static,
@@ -71,7 +58,7 @@ impl Builder {
7158
future.await
7259
};
7360

74-
let schedule = move |t| rt.schedule(Runnable(t));
61+
let schedule = move |t| RUNTIME.schedule(Runnable(t));
7562
let (task, handle) = async_task::spawn(future, schedule, task);
7663
task.schedule();
7764
Ok(JoinHandle::new(handle))

0 commit comments

Comments
 (0)