open_coroutine_core/common/
constants.rsuse crate::impl_display_by_debug;
use once_cell::sync::Lazy;
use std::time::Duration;
pub const DEFAULT_STACK_SIZE: usize = 128 * 1024;
#[cfg(all(target_os = "linux", feature = "io_uring"))]
pub const IO_URING_TIMEOUT_USERDATA: usize = usize::MAX - 1;
pub const COROUTINE_GLOBAL_QUEUE_BEAN: &str = "coroutineGlobalQueueBean";
pub const TASK_GLOBAL_QUEUE_BEAN: &str = "taskGlobalQueueBean";
pub const MONITOR_BEAN: &str = "monitorBean";
pub const SLICE: Duration = Duration::from_millis(10);
#[must_use]
pub fn cpu_count() -> usize {
static CPU_COUNT: Lazy<usize> = Lazy::new(num_cpus::get);
*CPU_COUNT
}
#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum PoolState {
Running,
Stopping,
Stopped,
}
impl_display_by_debug!(PoolState);
#[allow(non_camel_case_types, missing_docs)]
#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SyscallName {
#[cfg(windows)]
Sleep,
sleep,
usleep,
nanosleep,
poll,
select,
#[cfg(target_os = "linux")]
accept4,
#[cfg(target_os = "linux")]
epoll_ctl,
#[cfg(target_os = "linux")]
epoll_wait,
#[cfg(target_os = "linux")]
io_uring_enter,
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
))]
kevent,
#[cfg(windows)]
iocp,
setsockopt,
recv,
#[cfg(windows)]
WSARecv,
recvfrom,
read,
pread,
readv,
preadv,
recvmsg,
connect,
listen,
accept,
#[cfg(windows)]
WSAAccept,
shutdown,
close,
socket,
#[cfg(windows)]
WSASocketW,
#[cfg(windows)]
ioctlsocket,
send,
#[cfg(windows)]
WSASend,
sendto,
write,
pwrite,
writev,
pwritev,
sendmsg,
fsync,
renameat,
#[cfg(target_os = "linux")]
renameat2,
mkdir,
mkdirat,
rmdir,
lseek,
openat,
link,
unlink,
pthread_cond_timedwait,
pthread_mutex_trylock,
pthread_mutex_lock,
pthread_mutex_unlock,
#[cfg(windows)]
CreateFileW,
#[cfg(windows)]
SetFilePointerEx,
#[cfg(windows)]
WaitOnAddress,
#[cfg(windows)]
WSAPoll,
}
impl SyscallName {
#[must_use]
pub fn nio() -> Self {
cfg_if::cfg_if! {
if #[cfg(target_os = "linux")] {
Self::epoll_wait
} else if #[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "openbsd",
target_os = "netbsd"
))] {
Self::kevent
} else if #[cfg(windows)] {
Self::iocp
} else {
compile_error!("unsupported")
}
}
}
}
impl_display_by_debug!(SyscallName);
impl From<SyscallName> for &str {
fn from(val: SyscallName) -> Self {
format!("{val}").leak()
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SyscallState {
Executing,
Suspend(u64),
Timeout,
Callback,
}
impl_display_by_debug!(SyscallState);
#[repr(C)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum CoroutineState<Y, R> {
Ready,
Running,
Suspend(Y, u64),
Syscall(Y, SyscallName, SyscallState),
Complete(R),
Error(&'static str),
}
impl_display_by_debug!(CoroutineState<Y, R>);