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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//! executor.rs
use crate::*;
use std::{
sync::Arc,
time::Instant,
};
use portable_atomic::{
AtomicBool,
Ordering::Relaxed,
};
use once_cell::sync::OnceCell;
/// common state between this executor and outside.
#[derive(Debug)]
pub(crate) struct ExecutorState {
/// per-process unique ID for each executor.
/// this ID will not reuse even a executor exits.
pub id: ExecutorId,
/// checks whether a executor working (for run the Runnable).
pub working: AtomicBool,
/// whether this executor can be exit if idle.
pub exitable: bool,
/// start timestamp of this executor.
pub started: Instant,
/// how many time used for working?
pub working_time: AtomicDuration,
/// the join_handle initial by external caller that creates this executor.
pub join_handle: OnceCell<Arc<std::thread::JoinHandle<()>>>,
/// requests a exitable executor thread to exit in next checkpoint.
/// this is no effect for persist executors.
pub please_exit: AtomicBool,
}
impl ExecutorState {
/// create new ExecutorState.
#[inline(always)]
pub fn new(id: ExecutorId) -> Self {
Self {
id,
working: AtomicBool::new(false),
started: Instant::now(),
working_time: AtomicDuration::zero(),
exitable: false,
join_handle: OnceCell::new(),
please_exit: AtomicBool::new(false),
}
}
/// check if this executor is working (by running Runnable)
#[inline(always)]
pub fn is_working(&self) -> bool {
self.working.load(Relaxed)
}
/// returns range within 0.0 ~ 1.0 (f64).
/// * 1.0 means all of time is working.
/// * 0.0 means all of time is idle.
#[inline(always)]
pub fn working_ratio(&self) -> f64 {
let running_time = self.started.elapsed();
let working_time = self.working_time.get();
working_time.as_secs_f64() / running_time.as_secs_f64()
}
/// request the temporary executor to exit.
///
/// do nothing if this executor is persist.
#[inline(always)]
pub fn exit(&self) -> Result<(), &'static str> {
if self.exitable {
self.please_exit.store(true, Relaxed);
Ok(())
} else {
Err("executor is not exitable!")
}
}
}
/// the Executor of asyncute.
#[derive(Debug)]
pub struct Executor {
/// variable for avoid double drop.
dropped: bool,
/// common state between this executor and outside.
state: Arc<ExecutorState>,
/// channel receiver for receive runnable.
runinfo_rx: Receiver<RunInfo>,
_not_send_sync_unpin: PhantomNonSendSyncUnpin,
}
impl Executor {
/// create new executor.
#[inline(always)]
pub(crate) const fn new(
state: Arc<ExecutorState>,
runinfo_rx: Receiver<RunInfo>,
) -> Self {
Self {
dropped: false,
runinfo_rx,
state,
_not_send_sync_unpin: PhantomData,
}
}
/// run a runnable, catch unwind if it panic (if possible)
#[inline(always)]
fn run_one(&self, runinfo: RunInfo, bulk: bool) {
let idc = self.state.id.count();
log::debug!("Runnable ScheduleInfo: {:?}", runinfo.info);
let taskinfo = runinfo.runnable.metadata().clone();
if ! bulk {
self.state.working.store(true, Relaxed);
}
let t = Instant::now();
match std::panic::catch_unwind(move || { runinfo.runnable.run() }) {
Ok(_) => {},
Err(boxed_any) => {
let ref_any = &*boxed_any;
// core::any::type_name_of_val() is useless due to it does not resolve trait objects to real type name...
let type_id = ref_any.type_id();
macro_rules! log_any {
($($t:ty,)*) => {
#[allow(deprecated)]
if false {
}
$(
else if let Some(val) = ref_any.downcast_ref::<$t>() {
log::error!(concat!("Executor {} Runnable panic! TypeId={:?} ", stringify!($t), " = {:?}"), idc, type_id, val);
}
)*
else {
log::error!("Executor {idc} Runnable panic! TypeId={type_id:?} dyn Any = {ref_any:?}");
}
}
}
log_any!(
String, &'_ String,
&'_ str,
&'_ dyn std::error::Error,
&'_ dyn std::fmt::Debug,
std::io::Error,
std::io::ErrorKind,
std::panic::PanicInfo,
std::panic::PanicHookInfo,
core::panic::PanicInfo,
);
}
}
let t = t.elapsed();
if ! bulk {
self.state.working.store(false, Relaxed);
}
taskinfo.0.run_count.checked_add(1);
taskinfo.0.run_took.add(t);
self.state.working_time.add(t);
if ProfileConfig::global().is_enabled() {
let p = Profile::global();
p.started();
p.runnable.run_count.checked_add(1);
}
}
/// the execute loop of executor.
#[inline(always)]
pub fn run(self) -> std::io::Result<()> {
#[cfg(test)]
let idc = self.state.id.count();
let rx = &self.runinfo_rx;
let _defer = Defer::new(|| {
self.state.working.store(false, Relaxed);
});
let exitable = self.state.exitable;
let execonfig = ExecutorConfig::global();
let mut interval = execonfig.interval.get();
let max_idle = interval * 2;
let mut worked;
let mut t = Instant::now();
#[cfg(not(feature="crossbeam-deque"))]
let error_closed = Err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "runinfo channel closed!"));
loop {
worked = false;
loop {
#[cfg(feature="crossbeam-deque")]
match rx.try_recv() {
Some(runinfo) => {
if ! worked {
worked = true;
self.state.working.store(true, Relaxed);
}
self.run_one(runinfo, true);
}
_ => {
// crossbeam-deque is always open and no "closed" state.
break;
}
}
#[cfg(not(feature="crossbeam-deque"))]
match rx.try_recv() {
Ok(runinfo) => {
#[cfg(feature="kanal")]
let runinfo =
match runinfo {
Some(v) => v,
_ => {
break;
}
};
if ! worked {
worked = true;
self.state.working.store(true, Relaxed);
}
self.run_one(runinfo, true);
},
Err(err) => {
#[cfg(feature="flume")]
match err {
flume::TryRecvError::Empty => {
break;
},
_ => {
return error_closed;
}
}
#[cfg(feature="crossbeam-channel")]
if err.is_empty() {
break;
} else {
return error_closed;
}
#[cfg(feature="kanal")]
return error_closed;
// std::sync::mpmc is ported from crossbeam-channel's implementation.
// but it uses std::sync::mpsc's Error type, and without such as "is_empty" methods.
#[cfg(feature="std-mpmc")]
match err {
std::sync::mpmc::TryRecvError::Empty => {
break;
},
_ => {
return error_closed;
}
}
}
}
}
if worked {
#[cfg(test)]
log::trace!("{idc} worked");
self.state.working.store(false, Relaxed);
if exitable {
t = Instant::now();
}
} else {
#[cfg(test)]
log::trace!("{idc} not worked");
}
if execonfig.interval.changed() {
interval = execonfig.interval.get();
}
#[cfg(feature="crossbeam-deque")]
match rx.recv_timeout(interval) {
Some(runinfo) => {
if exitable {
t = Instant::now();
}
self.run_one(runinfo, false);
},
_ => {
// crossbeam-deque is always open and no "closed" state.
}
}
#[cfg(not(feature="crossbeam-deque"))]
match rx.recv_timeout(interval) {
Ok(runinfo) => {
if exitable {
t = Instant::now();
}
self.run_one(runinfo, false);
},
Err(err) => {
#[cfg(feature="flume")]
match err {
flume::RecvTimeoutError::Timeout => {},
_ => {
return error_closed;
}
}
#[cfg(feature="crossbeam-channel")]
if ! err.is_timeout() {
return error_closed;
}
#[cfg(feature="kanal")]
match err {
kanal::ReceiveErrorTimeout::Timeout => {},
_ => {
return error_closed;
}
}
// std::sync::mpmc is ported from crossbeam-channel's implementation.
// but it uses std::sync::mpsc's Error type, and without such as "is_timeout" methods.
#[cfg(feature="std-mpmc")]
match err {
std::sync::mpmc::RecvTimeoutError::Timeout => {},
_ => {
return error_closed;
}
}
}
}
if exitable {
if t.elapsed() > max_idle {
break;
}
if self.state.working_ratio() < execonfig.standby_threshold() {
break;
}
if self.state.please_exit.load(Relaxed) {
break;
}
}
}
Ok(())
}
}
impl Drop for Executor {
#[inline(always)]
fn drop(&mut self) {
if self.dropped {
return;
}
self.dropped = true;
// remove this executor from global index.
EXECUTOR_INDEX.remove(&self.state.id);
// set working state to idle.
self.state.working.store(false, Relaxed);
}
}