1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use futures_lite::prelude::*;
use once_cell::sync::OnceCell;
use pin_project_lite::pin_project;
use std::{
pin::Pin,
sync::atomic::AtomicUsize,
sync::atomic::Ordering,
task::{Context, Poll},
time::Duration,
};
mod nursery;
pub use nursery::*;
const MONITOR_MS: u64 = 5;
static EXEC: async_executor::Executor<'static> = async_executor::Executor::new();
static FUTURES_BEING_POLLED: AtomicUsize = AtomicUsize::new(0);
static FBP_NONZERO: event_listener::Event = event_listener::Event::new();
static POLL_COUNT: AtomicUsize = AtomicUsize::new(0);
static THREAD_COUNT: AtomicUsize = AtomicUsize::new(0);
static MONITOR: OnceCell<std::thread::JoinHandle<()>> = OnceCell::new();
fn start_monitor() {
MONITOR.get_or_init(|| {
std::thread::Builder::new()
.name("sscale-mon".into())
.spawn(monitor_loop)
.unwrap()
});
}
fn monitor_loop() {
fn start_thread(exitable: bool) {
THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
std::thread::Builder::new()
.name("sscale-wkr".into())
.spawn(move || {
async_io::block_on(async {
scopeguard::defer!({
THREAD_COUNT.fetch_sub(1, Ordering::SeqCst);
});
loop {
let cont = async {
EXEC.tick().await;
true
}
.or(async {
async_io::Timer::after(Duration::from_millis(100)).await;
false
});
if !cont.await && exitable {
return;
}
}
})
})
.unwrap();
}
start_thread(false);
let mut consecutive_busy = 0;
loop {
std::thread::sleep(Duration::from_millis(MONITOR_MS));
let fbp = loop {
let fbp = FUTURES_BEING_POLLED.load(Ordering::SeqCst);
if fbp > 0 {
break fbp;
}
let listener = FBP_NONZERO.listen();
let fbp = FUTURES_BEING_POLLED.load(Ordering::SeqCst);
if fbp > 0 {
break fbp;
}
consecutive_busy = 0;
listener.wait();
};
let running_threads = THREAD_COUNT.load(Ordering::SeqCst);
if fbp >= running_threads {
consecutive_busy += 1;
if consecutive_busy > 10 {
start_thread(true);
consecutive_busy = 0;
}
} else {
consecutive_busy = 0;
}
}
}
pub fn block_on<T: Send + 'static>(future: impl Future<Output = T> + Send + 'static) -> T {
futures_lite::future::block_on(future)
}
pub fn spawn<T: Send + 'static>(
future: impl Future<Output = T> + Send + 'static,
) -> async_executor::Task<T> {
start_monitor();
EXEC.spawn(WrappedFuture::new(future))
}
pin_project! {
struct WrappedFuture<T, F: Future<Output = T>> {
#[pin]
fut: F,
}
}
impl<T, F: Future<Output = T>> Future for WrappedFuture<T, F> {
type Output = T;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
let pval = FUTURES_BEING_POLLED.fetch_add(1, Ordering::SeqCst);
if pval == 0 {
FBP_NONZERO.notify(1);
}
POLL_COUNT.fetch_add(1, Ordering::Relaxed);
scopeguard::defer!({
FUTURES_BEING_POLLED.fetch_sub(1, Ordering::Relaxed);
});
this.fut.poll(cx)
}
}
impl<T, F: Future<Output = T> + 'static> WrappedFuture<T, F> {
pub fn new(fut: F) -> Self {
WrappedFuture { fut }
}
}