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 panicking,
134}
135
136impl SyscallName {
137 #[must_use]
139 pub fn nio() -> Self {
140 cfg_if::cfg_if! {
141 if #[cfg(target_os = "linux")] {
142 Self::epoll_wait
143 } else if #[cfg(any(
144 target_os = "macos",
145 target_os = "ios",
146 target_os = "tvos",
147 target_os = "watchos",
148 target_os = "freebsd",
149 target_os = "dragonfly",
150 target_os = "openbsd",
151 target_os = "netbsd"
152 ))] {
153 Self::kevent
154 } else if #[cfg(windows)] {
155 Self::iocp
156 } else {
157 compile_error!("unsupported")
158 }
159 }
160 }
161}
162
163impl_display_by_debug!(SyscallName);
164
165impl From<SyscallName> for &str {
166 fn from(val: SyscallName) -> Self {
167 format!("{val}").leak()
168 }
169}
170
171#[repr(C)]
173#[derive(Debug, Copy, Clone, Eq, PartialEq)]
174pub enum SyscallState {
175 Executing,
177 Suspend(u64),
179 Timeout,
182 Callback,
184}
185
186impl_display_by_debug!(SyscallState);
187
188#[repr(C)]
190#[derive(Debug, Copy, Clone, Eq, PartialEq)]
191pub enum CoroutineState<Y, R> {
192 Ready,
194 Running,
196 Suspend(Y, u64),
198 Syscall(Y, SyscallName, SyscallState),
200 Cancelled,
202 Complete(R),
204 Error(&'static str),
206}
207
208impl_display_by_debug!(CoroutineState<Y, R>);