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
use std::sync::atomic::{AtomicIsize, Ordering};
use futures::channel::mpsc;
use once_cell::sync::OnceCell;
pub use exec::Executor;
pub use spawner::Spawner;
mod exec;
mod spawner;
impl<T: ?Sized> SpawnExt for T where T: futures::Future {}
pub trait SpawnExt: futures::Future {
fn spawn(self, exec: &Executor) -> Spawner<Self>
where
Self: Sized + Send + 'static,
Self::Output: Send + 'static,
{
let f = Spawner::new(exec, self);
assert_future::<_, _>(f)
}
}
impl<T: ?Sized> SpawnDefaultExt for T where T: futures::Future {}
pub trait SpawnDefaultExt: futures::Future {
fn spawn(self) -> Spawner<'static, Self>
where
Self: Sized + Send + 'static,
Self::Output: Send + 'static,
{
let f = Spawner::new(default(), self);
assert_future::<_, _>(f)
}
}
pub struct Builder {
workers: usize,
queue_max: usize,
}
impl Default for Builder {
fn default() -> Self {
Self {
workers: 100,
queue_max: 100_000,
}
}
}
impl Builder {
#[inline]
pub fn workers(mut self, workers: usize) -> Self {
self.workers = workers;
self
}
#[inline]
pub fn queue_max(mut self, queue_max: usize) -> Self {
self.queue_max = queue_max;
self
}
pub fn build(self) -> (Executor, impl futures::Future<Output=()>) {
Executor::new(self.workers, self.queue_max)
}
}
#[derive(Clone, Debug)]
struct Counter(std::sync::Arc<AtomicIsize>);
impl Counter {
fn new() -> Self {
Counter(std::sync::Arc::new(AtomicIsize::new(0)))
}
fn inc(&self) {
self.0.fetch_add(1, Ordering::SeqCst);
}
fn dec(&self) {
self.0.fetch_sub(1, Ordering::SeqCst);
}
fn value(&self) -> isize {
self.0.load(Ordering::SeqCst)
}
}
#[derive(thiserror::Error, Debug)]
pub enum Error<T> {
#[error("send error")]
SendError(ErrorType<T>),
#[error("try send error")]
TrySendError(ErrorType<T>),
#[error("send timeout error")]
SendTimeoutError(ErrorType<T>),
#[error("recv result error")]
RecvResultError,
}
#[derive(Debug, Eq, PartialEq)]
pub enum ErrorType<T> {
Full(Option<T>),
Closed(Option<T>),
Timeout(Option<T>),
}
impl<T> From<mpsc::TrySendError<T>> for Error<T> {
fn from(e: mpsc::TrySendError<T>) -> Self {
if e.is_full() {
Error::TrySendError(ErrorType::Full(Some(e.into_inner())))
} else {
Error::TrySendError(ErrorType::Closed(Some(e.into_inner())))
}
}
}
impl<T> From<mpsc::SendError> for Error<T> {
fn from(e: mpsc::SendError) -> Self {
if e.is_full() {
Error::SendError(ErrorType::Full(None))
} else {
Error::SendError(ErrorType::Closed(None))
}
}
}
pub(crate) fn assert_future<T, F>(future: F) -> F
where
F: futures::Future<Output=T>,
{
future
}
static DEFAULT_EXECUTOR: OnceCell<Executor> = OnceCell::new();
pub fn set_default(exec: Executor) -> Result<(), Executor> {
DEFAULT_EXECUTOR.set(exec)
}
pub fn init_default() -> impl futures::Future<Output=()> {
let (exec, runner) = Builder::default().workers(100).queue_max(100_000).build();
DEFAULT_EXECUTOR.set(exec).ok().unwrap();
runner
}
pub fn default() -> &'static Executor {
DEFAULT_EXECUTOR
.get()
.expect("default executor must be set first")
}