open_coroutine_core/net/
mod.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
use crate::config::Config;
use crate::coroutine::suspender::Suspender;
use crate::net::event_loop::EventLoop;
use crate::net::join::JoinHandle;
use crate::{error, info};
use once_cell::sync::OnceCell;
use std::collections::VecDeque;
use std::ffi::{c_int, c_longlong};
use std::io::{Error, ErrorKind};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::time::Duration;

cfg_if::cfg_if! {
    if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
        use libc::{epoll_event, iovec, msghdr, off_t, size_t, sockaddr, socklen_t};
        use std::ffi::c_void;
    }
}

/// 做C兼容时会用到
pub type UserFunc = extern "C" fn(usize) -> usize;

mod selector;

#[allow(clippy::too_many_arguments)]
#[cfg(all(target_os = "linux", feature = "io_uring"))]
mod operator;

#[allow(missing_docs)]
pub mod event_loop;

/// Task join abstraction and impl.
pub mod join;

static INSTANCE: OnceCell<EventLoops> = OnceCell::new();

/// The manager for `EventLoop`.
#[repr(C)]
#[derive(Debug)]
pub struct EventLoops {
    index: AtomicUsize,
    loops: VecDeque<Arc<EventLoop<'static>>>,
    shared_stop: Arc<(Mutex<AtomicUsize>, Condvar)>,
}

unsafe impl Send for EventLoops {}

unsafe impl Sync for EventLoops {}

impl EventLoops {
    /// Init the `EventLoops`.
    pub fn init(config: &Config) {
        _ = INSTANCE.get_or_init(|| {
            #[cfg(feature = "ci")]
            crate::common::ci::init();
            let loops = Self::new(
                config.event_loop_size(),
                config.stack_size(),
                config.min_size(),
                config.max_size(),
                config.keep_alive_time(),
            )
            .expect("init default EventLoops failed !");
            #[cfg(feature = "log")]
            let _ = tracing_subscriber::fmt()
                .with_thread_names(true)
                .with_line_number(true)
                .with_timer(tracing_subscriber::fmt::time::OffsetTime::new(
                    time::UtcOffset::from_hms(8, 0, 0).expect("create UtcOffset failed !"),
                    time::format_description::well_known::Rfc2822,
                ))
                .try_init();
            info!("open-coroutine init with {config:#?}");
            loops
        });
    }

    /// Create a new `EventLoops`.
    pub fn new(
        event_loop_size: usize,
        stack_size: usize,
        min_size: usize,
        max_size: usize,
        keep_alive_time: u64,
    ) -> std::io::Result<Self> {
        let shared_stop = Arc::new((Mutex::new(AtomicUsize::new(0)), Condvar::new()));
        let mut loops = VecDeque::new();
        for i in 0..event_loop_size {
            loops.push_back(
                EventLoop::new(
                    format!("open-coroutine-event-loop-{i}"),
                    i,
                    stack_size,
                    min_size,
                    max_size,
                    keep_alive_time,
                    shared_stop.clone(),
                )?
                .start()?,
            );
        }
        Ok(Self {
            index: AtomicUsize::new(0),
            loops,
            shared_stop,
        })
    }

    fn round_robin() -> &'static Arc<EventLoop<'static>> {
        let instance = INSTANCE.get().expect("EventLoops not init !");
        let index = instance.index.fetch_add(1, Ordering::Release) % instance.loops.len();
        instance
            .loops
            .get(index)
            .unwrap_or_else(move || panic!("init event-loop-{index} failed!"))
    }

    /// Get a `EventLoop`, prefer current.
    fn event_loop() -> &'static EventLoop<'static> {
        EventLoop::current().unwrap_or_else(|| Self::round_robin())
    }

    /// Submit a new task to event-loop.
    ///
    /// Allow multiple threads to concurrently submit task to the pool,
    /// but only allow one thread to execute scheduling.
    pub fn submit_task(
        name: Option<String>,
        func: impl FnOnce(Option<usize>) -> Option<usize> + 'static,
        param: Option<usize>,
        priority: Option<c_longlong>,
    ) -> JoinHandle {
        let event_loop = Self::round_robin();
        event_loop
            .submit_task(name, func, param, priority)
            .map_or_else(
                |_| JoinHandle::err(event_loop),
                |n| JoinHandle::new(event_loop, n.as_str()),
            )
    }

    /// Submit a new coroutine to event-loop.
    ///
    /// Allow multiple threads to concurrently submit coroutine to the pool,
    /// but only allow one thread to execute scheduling.
    pub fn submit_co(
        f: impl FnOnce(&Suspender<(), ()>, ()) -> Option<usize> + 'static,
        stack_size: Option<usize>,
        priority: Option<c_longlong>,
    ) -> std::io::Result<()> {
        Self::round_robin().submit_co(f, stack_size, priority)
    }

    /// Waiting for read or write events to occur.
    /// This method can only be used in coroutines.
    pub fn wait_event(timeout: Option<Duration>) -> std::io::Result<()> {
        Self::event_loop().timed_wait_just(timeout)
    }

    /// Waiting for a read event to occur.
    /// This method can only be used in coroutines.
    pub fn wait_read_event(fd: c_int, timeout: Option<Duration>) -> std::io::Result<()> {
        let event_loop = Self::event_loop();
        event_loop.add_read_event(fd)?;
        event_loop.wait_just(timeout)
    }

    /// Waiting for a write event to occur.
    /// This method can only be used in coroutines.
    pub fn wait_write_event(fd: c_int, timeout: Option<Duration>) -> std::io::Result<()> {
        let event_loop = Self::event_loop();
        event_loop.add_write_event(fd)?;
        event_loop.wait_just(timeout)
    }

    /// Remove read and write event interests.
    /// This method can only be used in coroutines.
    pub fn del_event(fd: c_int) -> std::io::Result<()> {
        if let Some(event_loop) = EventLoop::current() {
            event_loop.del_event(fd)?;
        } else {
            let instance = INSTANCE.get().expect("EventLoops not init !");
            for event_loop in &instance.loops {
                event_loop.del_event(fd)?;
            }
        }
        Ok(())
    }

    /// Remove read event interest.
    /// This method can only be used in coroutines.
    pub fn del_read_event(fd: c_int) -> std::io::Result<()> {
        if let Some(event_loop) = EventLoop::current() {
            event_loop.del_read_event(fd)?;
        } else {
            let instance = INSTANCE.get().expect("EventLoops not init !");
            for event_loop in &instance.loops {
                event_loop.del_read_event(fd)?;
            }
        }
        Ok(())
    }

    /// Remove write event interest.
    /// This method can only be used in coroutines.
    pub fn del_write_event(fd: c_int) -> std::io::Result<()> {
        if let Some(event_loop) = EventLoop::current() {
            event_loop.del_write_event(fd)?;
        } else {
            let instance = INSTANCE.get().expect("EventLoops not init !");
            for event_loop in &instance.loops {
                event_loop.del_write_event(fd)?;
            }
        }
        Ok(())
    }

    /// Stop all `EventLoop`.
    pub fn stop(wait_time: Duration) -> std::io::Result<()> {
        if let Some(instance) = INSTANCE.get() {
            for i in &instance.loops {
                _ = i.stop(Duration::ZERO);
            }
            let (lock, cvar) = &*instance.shared_stop;
            let guard = lock
                .lock()
                .map_err(|_| Error::new(ErrorKind::TimedOut, "wait failed !"))?;
            let result = cvar
                .wait_timeout_while(guard, wait_time, |stopped| {
                    stopped.load(Ordering::Acquire) > 0
                })
                .map_err(|_| Error::new(ErrorKind::TimedOut, "wait failed !"))?;
            if result.1.timed_out() {
                error!("open-coroutine stop timeout !");
                return Err(Error::new(ErrorKind::TimedOut, "stop timeout !"));
            }
            #[cfg(all(unix, feature = "preemptive"))]
            crate::monitor::Monitor::stop();
        }
        Ok(())
    }
}

macro_rules! impl_io_uring {
    ( $syscall: ident($($arg: ident: $arg_type: ty),*) -> $result: ty ) => {
        #[cfg(all(target_os = "linux", feature = "io_uring"))]
        impl EventLoops {
            #[allow(missing_docs)]
            pub fn $syscall(
                $($arg: $arg_type),*
            ) -> std::io::Result<Arc<(Mutex<Option<c_longlong>>, Condvar)>> {
                Self::event_loop().$syscall($($arg, )*)
            }
        }
    }
}

impl_io_uring!(epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *mut epoll_event) -> c_int);
impl_io_uring!(socket(domain: c_int, ty: c_int, protocol: c_int) -> c_int);
impl_io_uring!(accept(fd: c_int, addr: *mut sockaddr, len: *mut socklen_t) -> c_int);
impl_io_uring!(accept4(fd: c_int, addr: *mut sockaddr, len: *mut socklen_t, flg: c_int) -> c_int);
impl_io_uring!(shutdown(fd: c_int, how: c_int) -> c_int);
impl_io_uring!(connect(fd: c_int, address: *const sockaddr, len: socklen_t) -> c_int);
impl_io_uring!(close(fd: c_int) -> c_int);
impl_io_uring!(recv(fd: c_int, buf: *mut c_void, len: size_t, flags: c_int) -> ssize_t);
impl_io_uring!(read(fd: c_int, buf: *mut c_void, count: size_t) -> ssize_t);
impl_io_uring!(pread(fd: c_int, buf: *mut c_void, count: size_t, offset: off_t) -> ssize_t);
impl_io_uring!(readv(fd: c_int, iov: *const iovec, iovcnt: c_int) -> ssize_t);
impl_io_uring!(preadv(fd: c_int, iov: *const iovec, iovcnt: c_int, offset: off_t) -> ssize_t);
impl_io_uring!(recvmsg(fd: c_int, msg: *mut msghdr, flags: c_int) -> ssize_t);
impl_io_uring!(send(fd: c_int, buf: *const c_void, len: size_t, flags: c_int) -> ssize_t);
impl_io_uring!(sendto(fd: c_int, buf: *const c_void, len: size_t, flags: c_int, addr: *const sockaddr, addrlen: socklen_t) -> ssize_t);
impl_io_uring!(write(fd: c_int, buf: *const c_void, count: size_t) -> ssize_t);
impl_io_uring!(pwrite(fd: c_int, buf: *const c_void, count: size_t, offset: off_t) -> ssize_t);
impl_io_uring!(writev(fd: c_int, iov: *const iovec, iovcnt: c_int) -> ssize_t);
impl_io_uring!(pwritev(fd: c_int, iov: *const iovec, iovcnt: c_int, offset: off_t) -> ssize_t);
impl_io_uring!(sendmsg(fd: c_int, msg: *const msghdr, flags: c_int) -> ssize_t);