open_coroutine/
lib.rs

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
#![deny(
    // The following are allowed by default lints according to
    // https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html
    anonymous_parameters,
    bare_trait_objects,
    // elided_lifetimes_in_paths, // allow anonymous lifetime
    missing_copy_implementations,
    missing_debug_implementations,
    missing_docs, // TODO: add documents
    single_use_lifetimes, // TODO: fix lifetime names only used once
    trivial_casts, // TODO: remove trivial casts in code
    trivial_numeric_casts,
    // unreachable_pub, allow clippy::redundant_pub_crate lint instead
    // unsafe_code,
    unstable_features,
    unused_extern_crates,
    unused_import_braces,
    unused_qualifications,
    unused_results,
    variant_size_differences,

    warnings, // treat all wanings as errors

    clippy::all,
    // clippy::restriction,
    clippy::pedantic,
    // clippy::nursery, // It's still under development
    clippy::cargo,
    unreachable_pub,
)]
#![allow(
    // Some explicitly allowed Clippy lints, must have clear reason to allow
    clippy::blanket_clippy_restriction_lints, // allow clippy::restriction
    clippy::implicit_return, // actually omitting the return keyword is idiomatic Rust code
    clippy::module_name_repetitions, // repeation of module name in a struct name is not big deal
    clippy::multiple_crate_versions, // multi-version dependency crates is not able to fix
    clippy::missing_errors_doc, // TODO: add error docs
    clippy::missing_panics_doc, // TODO: add panic docs
    clippy::panic_in_result_fn,
    clippy::shadow_same, // Not too much bad
    clippy::shadow_reuse, // Not too much bad
    clippy::exhaustive_enums,
    clippy::exhaustive_structs,
    clippy::indexing_slicing,
    clippy::separated_literal_suffix, // conflicts with clippy::unseparated_literal_suffix
    clippy::single_char_lifetime_names, // TODO: change lifetime names
)]
//! see `https://github.com/acl-dev/open-coroutine`

use open_coroutine_core::co_pool::task::UserTaskFunc;
use open_coroutine_core::common::constants::SLICE;
pub use open_coroutine_core::net::config::Config;
use open_coroutine_core::net::UserFunc;
pub use open_coroutine_macros::*;
use std::cmp::Ordering;
use std::ffi::{c_int, c_longlong, c_uint, c_void};
use std::io::{Error, ErrorKind};
use std::marker::PhantomData;
use std::net::{TcpStream, ToSocketAddrs};
use std::ops::Deref;
use std::time::Duration;

extern "C" {
    fn open_coroutine_init(config: Config) -> c_int;

    fn open_coroutine_stop(secs: c_uint) -> c_int;

    fn maybe_grow_stack(
        red_zone: usize,
        stack_size: usize,
        f: UserFunc,
        param: usize,
    ) -> c_longlong;
}

#[allow(improper_ctypes)]
extern "C" {
    fn task_crate(f: UserTaskFunc, param: usize) -> open_coroutine_core::net::join::JoinHandle;

    fn task_join(handle: &open_coroutine_core::net::join::JoinHandle) -> c_longlong;

    fn task_timeout_join(
        handle: &open_coroutine_core::net::join::JoinHandle,
        ns_time: u64,
    ) -> c_longlong;
}

/// Init the open-coroutine.
pub fn init(config: Config) {
    assert_eq!(
        0,
        unsafe { open_coroutine_init(config) },
        "open-coroutine init failed !"
    );
}

/// Shutdown the open-coroutine.
pub fn shutdown() {
    unsafe { _ = open_coroutine_stop(30) };
}

/// Create a task.
#[macro_export]
macro_rules! task {
    ( $f: expr , $param:expr $(,)? ) => {
        $crate::task($f, $param)
    };
}

/// Create a task.
pub fn task<P: 'static, R: 'static, F: FnOnce(P) -> R>(f: F, param: P) -> JoinHandle<R> {
    extern "C" fn task_main<P: 'static, R: 'static, F: FnOnce(P) -> R>(input: usize) -> usize {
        unsafe {
            let ptr = &mut *((input as *mut c_void).cast::<(F, P)>());
            let data = std::ptr::read_unaligned(ptr);
            let result: &'static mut R = Box::leak(Box::new((data.0)(data.1)));
            std::ptr::from_mut::<R>(result).cast::<c_void>() as usize
        }
    }
    let inner = Box::leak(Box::new((f, param)));
    unsafe {
        task_crate(
            task_main::<P, R, F>,
            std::ptr::from_mut::<(F, P)>(inner).cast::<c_void>() as usize,
        )
        .into()
    }
}

#[allow(missing_docs)]
#[repr(C)]
#[derive(Debug)]
pub struct JoinHandle<R>(open_coroutine_core::net::join::JoinHandle, PhantomData<R>);

#[allow(missing_docs)]
impl<R> JoinHandle<R> {
    #[allow(clippy::cast_possible_truncation)]
    pub fn timeout_join(&self, dur: Duration) -> std::io::Result<Option<R>> {
        unsafe {
            let ptr = task_timeout_join(self, dur.as_nanos() as u64);
            match ptr.cmp(&0) {
                Ordering::Less => Err(Error::new(ErrorKind::Other, "timeout join failed")),
                Ordering::Equal => Ok(None),
                Ordering::Greater => Ok(Some(std::ptr::read_unaligned(ptr as *mut R))),
            }
        }
    }

    pub fn join(self) -> std::io::Result<Option<R>> {
        unsafe {
            let ptr = task_join(&self);
            match ptr.cmp(&0) {
                Ordering::Less => Err(Error::new(ErrorKind::Other, "join failed")),
                Ordering::Equal => Ok(None),
                Ordering::Greater => Ok(Some(std::ptr::read_unaligned(ptr as *mut R))),
            }
        }
    }
}

impl<R> From<open_coroutine_core::net::join::JoinHandle> for JoinHandle<R> {
    fn from(val: open_coroutine_core::net::join::JoinHandle) -> Self {
        Self(val, PhantomData)
    }
}

impl<R> From<JoinHandle<R>> for open_coroutine_core::net::join::JoinHandle {
    fn from(val: JoinHandle<R>) -> Self {
        val.0
    }
}

impl<R> Deref for JoinHandle<R> {
    type Target = open_coroutine_core::net::join::JoinHandle;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// Grows the call stack if necessary.
#[macro_export]
macro_rules! maybe_grow {
    ($red_zone:expr, $stack_size:expr, $f:expr $(,)?) => {
        $crate::maybe_grow($red_zone, $stack_size, $f)
    };
    ($stack_size:literal, $f:expr $(,)?) => {
        $crate::maybe_grow(
            open_coroutine_core::common::default_red_zone(),
            $stack_size,
            $f,
        )
    };
    ($f:expr $(,)?) => {
        $crate::maybe_grow(
            open_coroutine_core::common::default_red_zone(),
            open_coroutine_core::common::constants::DEFAULT_STACK_SIZE,
            $f,
        )
    };
}

/// Create a coroutine.
pub fn maybe_grow<R: 'static, F: FnOnce() -> R>(
    red_zone: usize,
    stack_size: usize,
    f: F,
) -> std::io::Result<R> {
    extern "C" fn execute_on_stack<R: 'static, F: FnOnce() -> R>(input: usize) -> usize {
        unsafe {
            let ptr = &mut *((input as *mut c_void).cast::<F>());
            let data = std::ptr::read_unaligned(ptr);
            let result: &'static mut R = Box::leak(Box::new(data()));
            std::ptr::from_mut::<R>(result).cast::<c_void>() as usize
        }
    }
    let inner = Box::leak(Box::new(f));
    unsafe {
        let ptr = maybe_grow_stack(
            red_zone,
            stack_size,
            execute_on_stack::<R, F>,
            std::ptr::from_mut::<F>(inner).cast::<c_void>() as usize,
        );
        if ptr < 0 {
            return Err(Error::new(ErrorKind::InvalidInput, "grow stack failed"));
        }
        Ok(*Box::from_raw(
            usize::try_from(ptr).expect("overflow") as *mut R
        ))
    }
}

/// Opens a TCP connection to a remote host.
///
/// `addr` is an address of the remote host. Anything which implements
/// [`ToSocketAddrs`] trait can be supplied for the address; see this trait
/// documentation for concrete examples.
///
/// If `addr` yields multiple addresses, `connect` will be attempted with
/// each of the addresses until a connection is successful. If none of
/// the addresses result in a successful connection, the error returned from
/// the last connection attempt (the last address) is returned.
///
/// # Examples
///
/// Open a TCP connection to `127.0.0.1:8080`:
///
/// ```no_run
/// if let Ok(stream) = open_coroutine::connect_timeout("127.0.0.1:8080", std::time::Duration::from_secs(3)) {
///     println!("Connected to the server!");
/// } else {
///     println!("Couldn't connect to server...");
/// }
/// ```
///
/// Open a TCP connection to `127.0.0.1:8080`. If the connection fails, open
/// a TCP connection to `127.0.0.1:8081`:
///
/// ```no_run
/// use std::net::SocketAddr;
///
/// let addrs = [
///     SocketAddr::from(([127, 0, 0, 1], 8080)),
///     SocketAddr::from(([127, 0, 0, 1], 8081)),
/// ];
/// if let Ok(stream) = open_coroutine::connect_timeout(&addrs[..], std::time::Duration::from_secs(3)) {
///     println!("Connected to the server!");
/// } else {
///     println!("Couldn't connect to server...");
/// }
/// ```
pub fn connect_timeout<A: ToSocketAddrs>(addr: A, timeout: Duration) -> std::io::Result<TcpStream> {
    let timeout_time = open_coroutine_core::common::get_timeout_time(timeout);
    let mut last_err = None;
    for addr in addr.to_socket_addrs()? {
        loop {
            let left_time = timeout_time.saturating_sub(open_coroutine_core::common::now());
            if 0 == left_time {
                break;
            }
            match TcpStream::connect_timeout(&addr, Duration::from_nanos(left_time).min(SLICE)) {
                Ok(l) => return Ok(l),
                Err(e) => last_err = Some(e),
            }
        }
    }
    Err(last_err.unwrap_or_else(|| {
        Error::new(
            ErrorKind::InvalidInput,
            "could not resolve to any addresses",
        )
    }))
}

#[cfg(test)]
mod tests {
    use crate::{init, shutdown};
    use open_coroutine_core::net::config::Config;

    #[test]
    fn test() {
        init(Config::single());
        let join = task!(
            |_| {
                println!("Hello, world!");
            },
            (),
        );
        assert_eq!(Some(()), join.join().expect("join failed"));
        shutdown();
    }
}