Skip to content

Commit 282fd31

Browse files
committed
hook more syscall
1 parent 4301874 commit 282fd31

File tree

14 files changed

+341
-53
lines changed

14 files changed

+341
-53
lines changed

core/docs/en/coroutine.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,35 @@ resumed.
1515

1616
The above is excerpted from [corosensei](https://github.com/Amanieu/corosensei).
1717

18+
## Usage
19+
20+
```rust
21+
use open_coroutine_core::common::constants::CoroutineState;
22+
use open_coroutine_core::coroutine::Coroutine;
23+
24+
fn main() -> std::io::Result<()> {
25+
let mut co = Coroutine::new(
26+
None,
27+
|suspender, input| {
28+
assert_eq!(1, input);
29+
assert_eq!(3, suspender.suspend_with(2));
30+
4
31+
},
32+
None,
33+
None,
34+
)?;
35+
// Macro `co!` is equivalent to the code above
36+
// let mut co = open_coroutine_core::co!(|suspender, input| {
37+
// assert_eq!(1, input);
38+
// assert_eq!(3, suspender.suspend_with(2));
39+
// 4
40+
// })?;
41+
assert_eq!(CoroutineState::Suspend(2, 0), co.resume_with(1)?);
42+
assert_eq!(CoroutineState::Complete(4), co.resume_with(3)?);
43+
Ok(())
44+
}
45+
```
46+
1847
## State in open-coroutine
1948

2049
```text

core/docs/en/monitor.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ The `preemptive` feature currently supports the following targets:
2121

2222
✅ Tested and stable; ⚠️ Tested but unstable; ❌ Not supported.
2323

24+
[//]: # (## Why preempt)
25+
2426
## How it works
2527

2628
```mermaid

core/src/coroutine/korosensei.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,20 +381,23 @@ where
381381
///# Errors
382382
/// if stack allocate failed.
383383
pub fn new<F>(
384-
name: String,
384+
name: Option<String>,
385385
f: F,
386-
stack_size: usize,
386+
stack_size: Option<usize>,
387387
priority: Option<c_longlong>,
388388
) -> std::io::Result<Self>
389389
where
390390
F: FnOnce(&Suspender<Param, Yield>, Param) -> Return + 'static,
391391
{
392-
let stack_size = stack_size.max(crate::common::page_size());
392+
let stack_size = stack_size
393+
.unwrap_or(crate::common::constants::DEFAULT_STACK_SIZE)
394+
.max(crate::common::page_size());
393395
let stack = DefaultStack::new(stack_size)?;
394396
let stack_infos = UnsafeCell::new(VecDeque::from([StackInfo {
395397
stack_top: stack.base().get(),
396398
stack_bottom: stack.limit().get(),
397399
}]));
400+
let name = name.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
398401
let co_name = name.clone().leak();
399402
let inner = corosensei::Coroutine::with_stack(stack, move |y, p| {
400403
catch!(

core/src/coroutine/mod.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,19 @@ macro_rules! co {
2929
$crate::coroutine::Coroutine::new($name, $f, $size, $priority)
3030
};
3131
($f:expr, $size:literal, $priority:literal $(,)?) => {
32-
$crate::coroutine::Coroutine::new(
33-
uuid::Uuid::new_v4().to_string(),
34-
$f,
35-
$size,
36-
Some($priority),
37-
)
32+
$crate::coroutine::Coroutine::new(None, $f, $size, Some($priority))
3833
};
3934
($name:expr, $f:expr, $size:expr $(,)?) => {
4035
$crate::coroutine::Coroutine::new($name, $f, $size, None)
4136
};
4237
($f:expr, $size:literal $(,)?) => {
43-
$crate::coroutine::Coroutine::new(uuid::Uuid::new_v4().to_string(), $f, $size, None)
38+
$crate::coroutine::Coroutine::new(None, $f, $size, None)
4439
};
4540
($name:expr, $f:expr $(,)?) => {
46-
$crate::coroutine::Coroutine::new(
47-
$name,
48-
$f,
49-
$crate::common::constants::DEFAULT_STACK_SIZE,
50-
None,
51-
)
41+
$crate::coroutine::Coroutine::new($name, $f, None, None)
5242
};
5343
($f:expr $(,)?) => {
54-
$crate::coroutine::Coroutine::new(
55-
uuid::Uuid::new_v4().to_string(),
56-
$f,
57-
$crate::common::constants::DEFAULT_STACK_SIZE,
58-
None,
59-
)
44+
$crate::coroutine::Coroutine::new(None, $f, None, None)
6045
};
6146
}
6247

core/src/scheduler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ impl<'s> Scheduler<'s> {
177177
priority: Option<c_longlong>,
178178
) -> std::io::Result<()> {
179179
self.submit_raw_co(co!(
180-
format!("{}@{}", self.name(), uuid::Uuid::new_v4()),
180+
Some(format!("{}@{}", self.name(), uuid::Uuid::new_v4())),
181181
f,
182-
stack_size.unwrap_or(self.stack_size()),
182+
Some(stack_size.unwrap_or(self.stack_size())),
183183
priority
184184
)?)
185185
}

core/src/syscall/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
macro_rules! syscall_mod {
2-
($($mod_name: ident);*) => {
2+
($($mod_name: ident);*$(;)?) => {
33
$(
44
pub use $mod_name::$mod_name;
55
mod $mod_name;

core/src/syscall/unix/fsync.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use once_cell::sync::Lazy;
2+
use std::ffi::c_int;
3+
4+
#[must_use]
5+
pub extern "C" fn fsync(
6+
fn_ptr: Option<&extern "C" fn(c_int) -> c_int>,
7+
fd: c_int,
8+
) -> c_int {
9+
cfg_if::cfg_if! {
10+
if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
11+
static CHAIN: Lazy<FsyncSyscallFacade<IoUringFsyncSyscall<RawFsyncSyscall>>> =
12+
Lazy::new(Default::default);
13+
} else {
14+
static CHAIN: Lazy<FsyncSyscallFacade<RawFsyncSyscall>> = Lazy::new(Default::default);
15+
}
16+
}
17+
CHAIN.fsync(fn_ptr, fd)
18+
}
19+
20+
trait FsyncSyscall {
21+
extern "C" fn fsync(
22+
&self,
23+
fn_ptr: Option<&extern "C" fn(c_int) -> c_int>,
24+
fd: c_int,
25+
) -> c_int;
26+
}
27+
28+
impl_facade!(FsyncSyscallFacade, FsyncSyscall, fsync(fd: c_int) -> c_int);
29+
30+
impl_io_uring!(IoUringFsyncSyscall, FsyncSyscall, fsync(fd: c_int) -> c_int);
31+
32+
impl_raw!(RawFsyncSyscall, FsyncSyscall, fsync(fd: c_int) -> c_int);

core/src/syscall/unix/mkdirat.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use libc::mode_t;
2+
use once_cell::sync::Lazy;
3+
use std::ffi::{c_char, c_int};
4+
5+
#[must_use]
6+
pub extern "C" fn mkdirat(
7+
fn_ptr: Option<&extern "C" fn(c_int, *const c_char, mode_t) -> c_int>,
8+
dirfd: c_int,
9+
pathname: *const c_char,
10+
mode: mode_t,
11+
) -> c_int {
12+
cfg_if::cfg_if! {
13+
if #[cfg(all(target_os = "linux", feature = "io_uring"))] {
14+
static CHAIN: Lazy<MkdiratSyscallFacade<IoUringMkdiratSyscall<RawMkdiratSyscall>>> =
15+
Lazy::new(Default::default);
16+
} else {
17+
static CHAIN: Lazy<MkdiratSyscallFacade<RawMkdiratSyscall>> = Lazy::new(Default::default);
18+
}
19+
}
20+
CHAIN.mkdirat(fn_ptr, dirfd, pathname, mode)
21+
}
22+
23+
trait MkdiratSyscall {
24+
extern "C" fn mkdirat(
25+
&self,
26+
fn_ptr: Option<&extern "C" fn(c_int, *const c_char, mode_t) -> c_int>,
27+
dirfd: c_int,
28+
pathname: *const c_char,
29+
mode: mode_t,
30+
) -> c_int;
31+
}
32+
33+
impl_facade!(MkdiratSyscallFacade, MkdiratSyscall,
34+
mkdirat(dirfd: c_int, pathname: *const c_char, mode: mode_t) -> c_int
35+
);
36+
37+
impl_io_uring!(IoUringMkdiratSyscall, MkdiratSyscall,
38+
mkdirat(dirfd: c_int, pathname: *const c_char, mode: mode_t) -> c_int
39+
);
40+
41+
impl_raw!(RawMkdiratSyscall, MkdiratSyscall,
42+
mkdirat(dirfd: c_int, pathname: *const c_char, mode: mode_t) -> c_int
43+
);

0 commit comments

Comments
 (0)