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
use futures::{IntoFuture, Future};
use tokio::runtime;
use super::AutoRuntime;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::io;
const UNINITIALIZED: usize = 0;
const INITIALIZING: usize = 1;
const INITIALIZED: usize = 2;
static GLOBAL_GUARD: AtomicUsize = AtomicUsize::new(UNINITIALIZED);
static mut TOKIO: Option<runtime::Runtime> = None;
const RUNTIME_NOT_AVAIL: &str = "Runtime is not set";
pub struct Runtime {
}
impl Runtime {
pub fn new() -> io::Result<Self> {
Self::from_build(runtime::Builder::new().name_prefix("yukikaze"))
}
pub fn from_build(builder: &mut runtime::Builder) -> io::Result<Self> {
let runtime = builder.build()?;
match GLOBAL_GUARD.compare_and_swap(UNINITIALIZED, INITIALIZING, Ordering::Release) {
UNINITIALIZED => unsafe {
TOKIO = Some(runtime);
GLOBAL_GUARD.store(INITIALIZED, Ordering::SeqCst);
},
_ => panic!("Setting tokio runtime twice")
}
Ok(Self {
})
}
}
impl Drop for Runtime {
fn drop(&mut self) {
match GLOBAL_GUARD.compare_and_swap(INITIALIZED, INITIALIZING, Ordering::Release) {
INITIALIZED => unsafe {
match TOKIO.take() {
Some(runtime) => {
runtime.shutdown_now();
},
None => unreach!(),
}
GLOBAL_GUARD.store(UNINITIALIZED, Ordering::SeqCst);
},
_ => panic!("Runtime is not set, but dropping global guard!"),
}
}
}
pub fn init() -> Runtime {
Runtime::new().expect("To create runtime")
}
pub fn run<F, R, IF, I, E>(runner: F) -> Result<R::Item, R::Error>
where F: FnOnce() -> R,
R: IntoFuture<Future=IF, Item=I, Error=E>,
IF: Future<Item=I, Error=E> + Send + 'static,
I: Send + 'static,
E: Send + 'static,
{
block_on(runner().into_future())
}
pub fn block_on<F: Send + 'static + Future<Item=I, Error=E>, I: Send + 'static, E: Send + 'static>(future: F) -> Result<F::Item, F::Error> {
match GLOBAL_GUARD.load(Ordering::Acquire) {
INITIALIZED => unsafe { match TOKIO.as_mut() {
Some(runtime) => runtime.block_on(future),
None => unreach!(),
}},
_ => panic!(RUNTIME_NOT_AVAIL)
}
}
pub fn spawn<F: Future<Item=(), Error=()> + 'static + Send>(fut: F) {
match GLOBAL_GUARD.load(Ordering::Acquire) {
INITIALIZED => unsafe { match TOKIO.as_mut() {
Some(runtime) => {
runtime.spawn(fut);
},
None => unreach!(),
}},
_ => panic!(RUNTIME_NOT_AVAIL)
}
}
impl<F: Send + 'static + Future> AutoRuntime for F where F::Item: Send + 'static, F::Error: Send + 'static {
#[inline]
fn finish(self) -> Result<Self::Item, Self::Error> {
block_on(self)
}
#[inline]
fn spawn(self) where Self: Future<Item=(), Error=()> {
spawn(self)
}
}