open_coroutine_core/common/
constants.rs1use crate::impl_display_by_debug;
2use once_cell::sync::Lazy;
3use std::time::Duration;
4
5pub const DEFAULT_STACK_SIZE: usize = 128 * 1024;
7
8#[cfg(all(target_os = "linux", feature = "io_uring"))]
10pub const IO_URING_TIMEOUT_USERDATA: usize = usize::MAX - 1;
11
12pub const COROUTINE_GLOBAL_QUEUE_BEAN: &str = "coroutineGlobalQueueBean";
14
15pub const TASK_GLOBAL_QUEUE_BEAN: &str = "taskGlobalQueueBean";
17
18pub const MONITOR_BEAN: &str = "monitorBean";
20
21pub const SLICE: Duration = Duration::from_millis(10);
23
24#[must_use]
26pub fn cpu_count() -> usize {
27 static CPU_COUNT: Lazy<usize> = Lazy::new(num_cpus::get);
28 *CPU_COUNT
29}
30
31#[repr(C)]
33#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
34pub enum PoolState {
35 Running,
37 Stopping,
39 Stopped,
41}
42
43impl_display_by_debug!(PoolState);
44
45#[allow(non_camel_case_types, missing_docs)]
47#[repr(C)]
48#[derive(Debug, Copy, Clone, Eq, PartialEq)]
49pub enum SyscallName {
50 #[cfg(windows)]
51 Sleep,
52 sleep,
53 usleep,
54 nanosleep,
55 poll,
56 select,
57 #[cfg(target_os = "linux")]
58 accept4,
59 #[cfg(target_os = "linux")]
60 epoll_ctl,
61 #[cfg(target_os = "linux")]
62 epoll_wait,
63 #[cfg(target_os = "linux")]
64 io_uring_enter,
65 #[cfg(any(
66 target_os = "macos",
67 target_os = "ios",
68 target_os = "tvos",
69 target_os = "watchos",
70 target_os = "freebsd",
71 target_os = "dragonfly",
72 target_os = "openbsd",
73 target_os = "netbsd"
74 ))]
75 kevent,
76 #[cfg(windows)]
77 iocp,
78 setsockopt,
79 recv,
80 #[cfg(windows)]
81 WSARecv,
82 recvfrom,
83 read,
84 pread,
85 readv,
86 preadv,
87 recvmsg,
88 connect,
89 listen,
90 accept,
91 #[cfg(windows)]
92 WSAAccept,
93 shutdown,
94 close,
95 socket,
96 #[cfg(windows)]
97 WSASocketW,
98 #[cfg(windows)]
99 ioctlsocket,
100 send,
101 #[cfg(windows)]
102 WSASend,
103 sendto,
104 write,
105 pwrite,
106 writev,
107 pwritev,
108 sendmsg,
109 fsync,
110 renameat,
111 #[cfg(target_os = "linux")]
112 renameat2,
113 mkdir,
114 mkdirat,
115 rmdir,
116 lseek,
117 openat,
118 link,
119 unlink,
120 pthread_cond_timedwait,
121 pthread_mutex_trylock,
122 pthread_mutex_lock,
123 pthread_mutex_unlock,
124 #[cfg(windows)]
125 CreateFileW,
126 #[cfg(windows)]
127 SetFilePointerEx,
128 #[cfg(windows)]
129 WaitOnAddress,
130 #[cfg(windows)]
131 WSAPoll,
132}
133
134impl SyscallName {
135 #[must_use]
137 pub fn nio() -> Self {
138 cfg_if::cfg_if! {
139 if #[cfg(target_os = "linux")] {
140 Self::epoll_wait
141 } else if #[cfg(any(
142 target_os = "macos",
143 target_os = "ios",
144 target_os = "tvos",
145 target_os = "watchos",
146 target_os = "freebsd",
147 target_os = "dragonfly",
148 target_os = "openbsd",
149 target_os = "netbsd"
150 ))] {
151 Self::kevent
152 } else if #[cfg(windows)] {
153 Self::iocp
154 } else {
155 compile_error!("unsupported")
156 }
157 }
158 }
159}
160
161impl_display_by_debug!(SyscallName);
162
163impl From<SyscallName> for &str {
164 fn from(val: SyscallName) -> Self {
165 format!("{val}").leak()
166 }
167}
168
169#[repr(C)]
171#[derive(Debug, Copy, Clone, Eq, PartialEq)]
172pub enum SyscallState {
173 Executing,
175 Suspend(u64),
177 Timeout,
180 Callback,
182}
183
184impl_display_by_debug!(SyscallState);
185
186#[repr(C)]
188#[derive(Debug, Copy, Clone, Eq, PartialEq)]
189pub enum CoroutineState<Y, R> {
190 Ready,
192 Running,
194 Suspend(Y, u64),
196 Syscall(Y, SyscallName, SyscallState),
198 Complete(R),
200 Error(&'static str),
202}
203
204impl_display_by_debug!(CoroutineState<Y, R>);