Skip to content

Commit 900d52a

Browse files
committed
add docs
1 parent 0765016 commit 900d52a

39 files changed

+370
-221
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@
7979
- [x] higher level coroutine abstraction supported
8080
- [x] preemptive scheduling supported
8181
- [x] work stealing supported
82-
- [x] sleep system call hooks supported
82+
- [x] sleep syscall hooks supported

README.md

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ English | [中文](README_ZH.md)
1515
## 🚀 Features
1616

1717
- [x] Preemptive(`not supported in windows`): even if the coroutine enters a dead loop, it can still be seized, see [example](https://github.com/loongs-zhang/open-coroutine/blob/master/open-coroutine/examples/preemptive.rs);
18-
- [x] Hook: you are free to use most of the slow system calls in coroutine;
18+
- [x] Hook: you are free to use most of the slow syscall in coroutine, see supported syscall on [unix](https://github.com/acl-dev/open-coroutine/blob/master/hook/src/syscall/unix.rs)/[windows](https://github.com/acl-dev/open-coroutine/blob/master/hook/src/syscall/windows.rs);
1919
- [x] Scalable: the size of the coroutine stack supports unlimited expansion without the cost of copying stack, and immediately shrinks to the original size after use, see [example](https://github.com/loongs-zhang/open-coroutine/blob/master/open-coroutine/examples/scalable_stack.rs);
2020
- [x] io_uring(`only in linux`): supports and is compatible with io_uring in terms of local file IO and network IO. If it's not supported on your system, it will fall back to non-blocking IO;
21-
- [x] Priority: support custom task and coroutine priority;
22-
- [x] Work Stealing: internally using a lock free work steal queue;
23-
- [x] Compatibility: the implementation of open-coroutine is no async, but it is compatible with async, which means you can use this crate in tokio/async-std/smol/...;
21+
- [x] Priority: support custom task priority, note that coroutine priority is not open to users;
22+
- [x] Work Steal: internally using a lock free work steal queue;
23+
- [x] Compatibility: the implementation of open-coroutine is no async, but it is compatible with async, which means you can use this crate in `tokio/async-std/smol/...`;
2424
- [x] Platforms: running on Linux, macOS and Windows;
2525

2626
## 🕊 Roadmap
2727

28-
- [ ] support `#[open_coroutine::all_join]` and `#[open_coroutine::any_join]` macro to wait coroutines;
28+
- [ ] cancel coroutine/task;
29+
- [ ] add metrics;
2930
- [ ] add synchronization toolkit;
3031
- [ ] support and compatibility for AF_XDP socket;
3132

@@ -39,7 +40,7 @@ English | [中文](README_ZH.md)
3940
open-coroutine = "x.y.z"
4041
```
4142

42-
### step2: add macro
43+
### step2: add `open_coroutine::main` macro
4344

4445
```rust
4546
#[open_coroutine::main]
@@ -50,17 +51,39 @@ fn main() {
5051

5152
### step3: create a task
5253

54+
```rust
55+
#[open_coroutine::main]
56+
fn main() {
57+
_ = open_coroutine::task!(|param| {
58+
assert_eq!(param, "param");
59+
}, "param");
60+
}
61+
```
62+
63+
### create a task with priority(optional)
64+
65+
```rust
66+
#[open_coroutine::main]
67+
fn main() {
68+
_ = open_coroutine::task!(|param| {
69+
assert_eq!(param, "param");
70+
}, "param", 1/*priority*/);
71+
}
72+
```
73+
74+
### wait until the task is completed or timed out(optional)
75+
5376
```rust
5477
#[open_coroutine::main]
5578
fn main() {
5679
let task = open_coroutine::task!(|param| {
57-
assert_eq!(param, 1);
58-
}, 1);
80+
assert_eq!(param, "param");
81+
}, "param", 1);
5982
task.timeout_join(std::time::Duration::from_secs(1)).expect("timeout");
6083
}
6184
```
6285

63-
### step4: scalable stack(optional)
86+
### scalable stack(optional)
6487

6588
```rust
6689
#[open_coroutine::main]
@@ -83,6 +106,6 @@ fn main() {
83106
}
84107
```
85108

86-
## ⚓ Learn
109+
## ⚓ Learn More
87110

88111
[我有故事,你有酒吗?](https://github.com/acl-dev/open-coroutine-docs)

README_ZH.md

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
[![LICENSE](https://img.shields.io/github/license/acl-dev/open-coroutine.svg?style=flat-square)](https://github.com/acl-dev/open-coroutine/blob/master/LICENSE-APACHE)
66
[![Build Status](https://github.com/acl-dev/open-coroutine/workflows/CI/badge.svg)](https://github.com/acl-dev/open-coroutine/actions)
77
[![Codecov](https://codecov.io/github/acl-dev/open-coroutine/graph/badge.svg?token=MSM3R7CBEX)](https://codecov.io/github/acl-dev/open-coroutine)
8-
[![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/acl-dev/open-coroutine.svg)](http://isitmaintained.com/project/acl-dev/open-coroutine "Average time to resolve an issue")
9-
[![Percentage of issues still open](http://isitmaintained.com/badge/open/acl-dev/open-coroutine.svg)](http://isitmaintained.com/project/acl-dev/open-coroutine "Percentage of issues still open")
8+
[![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/acl-dev/open-coroutine.svg)](http://isitmaintained.com/project/acl-dev/open-coroutine "解决issue的平均时间")
9+
[![Percentage of issues still open](http://isitmaintained.com/badge/open/acl-dev/open-coroutine.svg)](http://isitmaintained.com/project/acl-dev/open-coroutine "仍未关闭issue的百分比")
1010

1111
`open-coroutine`是一个简单、高效、通用的有栈协程库。
1212

@@ -15,17 +15,18 @@
1515
## 🚀 当前特性
1616

1717
- [x] 抢占调度(`不支持windows`): 即使协程进入死循环,它仍能被抢占,查看[例子](https://github.com/loongs-zhang/open-coroutine/blob/master/open-coroutine/examples/preemptive.rs);
18-
- [x] Hook: 您可以在协程中自由使用大多数慢系统调用;
18+
- [x] Hook: 您可以在协程中自由使用大多数慢系统调用,查看支持的系统调用[unix](https://github.com/acl-dev/open-coroutine/blob/master/hook/src/syscall/unix.rs)/[windows](https://github.com/acl-dev/open-coroutine/blob/master/hook/src/syscall/windows.rs);
1919
- [x] 可伸缩栈: 协程栈的大小支持无限制扩容而没有复制堆栈的开销,查看[例子](https://github.com/loongs-zhang/open-coroutine/blob/master/open-coroutine/examples/scalable_stack.rs);
2020
- [x] io_uring(`只支持linux`): 在本地文件IO和网络IO方面支持并兼容io_uring。如果您的系统不支持,它将回退到NIO;
21-
- [x] 优先级: 支持自定义任务和协程的优先级;
21+
- [x] 优先级: 支持自定义任务优先级,注意协程优先级未对用户开放;
2222
- [x] 任务窃取: 内部使用无锁任务窃取队列;
23-
- [x] 兼容性: open-coroutine的实现是No async的,但它与async兼容,这意味着您可以在tokio/sync-std/smol/...中使用这个crate;
23+
- [x] 兼容性: open-coroutine的实现是No async的,但它与async兼容,这意味着您可以在`tokio/sync-std/smol/...`中使用这个crate;
2424
- [x] 跨平台: 支持Linux、macOS和Windows;
2525

2626
## 🕊 未来计划
2727

28-
- [ ] 支持`#[open_coroutine::all_join]``#[open_coroutine::any_join]`宏;
28+
- [ ] 取消协程/任务;
29+
- [ ] 增加性能指标;
2930
- [ ] 增加并发工具包;
3031
- [ ] 支持AF_XDP套接字;
3132

@@ -39,7 +40,7 @@
3940
open-coroutine = "x.y.z"
4041
```
4142

42-
### step2: 添加宏
43+
### step2: 添加`open_coroutine::main`
4344

4445
```rust
4546
#[open_coroutine::main]
@@ -50,17 +51,39 @@ fn main() {
5051

5152
### step3: 创建任务
5253

54+
```rust
55+
#[open_coroutine::main]
56+
fn main() {
57+
_ = open_coroutine::task!(|param| {
58+
assert_eq!(param, "param");
59+
}, "param");
60+
}
61+
```
62+
63+
### 创建具有优先级的任务(可选)
64+
65+
```rust
66+
#[open_coroutine::main]
67+
fn main() {
68+
_ = open_coroutine::task!(|param| {
69+
assert_eq!(param, "param");
70+
}, "param", 1);
71+
}
72+
```
73+
74+
### 等待任务完成或超时(可选)
75+
5376
```rust
5477
#[open_coroutine::main]
5578
fn main() {
5679
let task = open_coroutine::task!(|param| {
57-
assert_eq!(param, 1);
58-
}, 1);
80+
assert_eq!(param, "param");
81+
}, "param", 1);
5982
task.timeout_join(std::time::Duration::from_secs(1)).expect("timeout");
6083
}
6184
```
6285

63-
### step4: 扩容栈(可选)
86+
### 扩容栈(可选)
6487

6588
```rust
6689
#[open_coroutine::main]
@@ -83,6 +106,9 @@ fn main() {
83106
}
84107
```
85108

86-
## ⚓ 学习更多
109+
## ⚓ 了解更多
110+
111+
- [诞生之因](docs/cn/background.md)
112+
- [语言选择](docs/cn/why-rust.md)
87113

88114
[我有故事,你有酒吗?](https://github.com/acl-dev/open-coroutine-docs)

core/src/co_pool/creator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl Listener<(), Option<usize>> for CoroutineCreator {
1717
new_state: SchedulableCoroutineState,
1818
) {
1919
match new_state {
20-
CoroutineState::Suspend((), _) | CoroutineState::SystemCall((), _, _) => {
20+
CoroutineState::Suspend((), _) | CoroutineState::Syscall((), _, _) => {
2121
if let Some(pool) = CoroutinePool::current() {
2222
_ = pool.try_grow();
2323
}

core/src/common/constants.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl_display_by_debug!(PoolState);
4646
#[allow(non_camel_case_types, missing_docs)]
4747
#[repr(C)]
4848
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
49-
pub enum Syscall {
49+
pub enum SyscallName {
5050
#[cfg(windows)]
5151
Sleep,
5252
sleep,
@@ -131,7 +131,7 @@ pub enum Syscall {
131131
WSAPoll,
132132
}
133133

134-
impl Syscall {
134+
impl SyscallName {
135135
/// Get the `NIO` syscall.
136136
#[must_use]
137137
pub fn nio() -> Self {
@@ -158,10 +158,10 @@ impl Syscall {
158158
}
159159
}
160160

161-
impl_display_by_debug!(Syscall);
161+
impl_display_by_debug!(SyscallName);
162162

163-
impl From<Syscall> for &str {
164-
fn from(val: Syscall) -> Self {
163+
impl From<SyscallName> for &str {
164+
fn from(val: SyscallName) -> Self {
165165
format!("{val}").leak()
166166
}
167167
}
@@ -193,8 +193,8 @@ pub enum CoroutineState<Y, R> {
193193
Running,
194194
///The coroutine resumes execution after the specified time has been suspended(with a given value).
195195
Suspend(Y, u64),
196-
///The coroutine enters the system call.
197-
SystemCall(Y, Syscall, SyscallState),
196+
///The coroutine enters the syscall.
197+
Syscall(Y, SyscallName, SyscallState),
198198
/// The coroutine completed with a return value.
199199
Complete(R),
200200
/// The coroutine completed with a error message.

core/src/config.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct Config {
1616
impl Config {
1717
#[must_use]
1818
pub fn single() -> Self {
19-
Self::new(1, DEFAULT_STACK_SIZE, 0, 65536, 0, 0, 10_000_000_000, true)
19+
Self::new(1, DEFAULT_STACK_SIZE, 0, 65536, 0, 0, 0, true)
2020
}
2121

2222
#[allow(clippy::too_many_arguments)]
@@ -136,15 +136,6 @@ impl Config {
136136

137137
impl Default for Config {
138138
fn default() -> Self {
139-
Self::new(
140-
cpu_count(),
141-
DEFAULT_STACK_SIZE,
142-
0,
143-
65536,
144-
0,
145-
0,
146-
10_000_000_000,
147-
true,
148-
)
139+
Self::new(cpu_count(), DEFAULT_STACK_SIZE, 0, 65536, 0, 0, 0, true)
149140
}
150141
}

core/src/coroutine/korosensei.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@ where
447447
self.suspend(y, timestamp)?;
448448
Ok(CoroutineState::Suspend(y, timestamp))
449449
}
450-
CoroutineState::SystemCall(y, syscall, state) => {
451-
Ok(CoroutineState::SystemCall(y, syscall, state))
450+
CoroutineState::Syscall(y, syscall, state) => {
451+
Ok(CoroutineState::Syscall(y, syscall, state))
452452
}
453453
_ => Err(Error::new(
454454
ErrorKind::Other,

core/src/coroutine/stack_pool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ unsafe impl Sync for MemoryPool {}
159159

160160
impl Default for MemoryPool {
161161
fn default() -> Self {
162-
Self::new(0, 10_000_000_000)
162+
Self::new(0, 0)
163163
}
164164
}
165165

core/src/coroutine/state.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::common::constants::{CoroutineState, Syscall, SyscallState};
1+
use crate::common::constants::{CoroutineState, SyscallName, SyscallState};
22
use crate::common::now;
33
use crate::coroutine::listener::Listener;
44
use crate::coroutine::Coroutine;
@@ -67,7 +67,7 @@ where
6767
let current = self.state();
6868
match current {
6969
CoroutineState::Running => return Ok(()),
70-
CoroutineState::Ready | CoroutineState::SystemCall(_, _, SyscallState::Executing) => {
70+
CoroutineState::Ready | CoroutineState::Syscall(_, _, SyscallState::Executing) => {
7171
let new_state = CoroutineState::Running;
7272
let old_state = self.change_state(new_state);
7373
self.on_running(self, old_state);
@@ -82,7 +82,7 @@ where
8282
return Ok(());
8383
}
8484
}
85-
CoroutineState::SystemCall(_, _, SyscallState::Callback | SyscallState::Timeout) => {
85+
CoroutineState::Syscall(_, _, SyscallState::Callback | SyscallState::Timeout) => {
8686
return Ok(());
8787
}
8888
_ => {}
@@ -127,20 +127,20 @@ where
127127
pub fn syscall(
128128
&self,
129129
val: Yield,
130-
syscall: Syscall,
130+
syscall: SyscallName,
131131
syscall_state: SyscallState,
132132
) -> std::io::Result<()> {
133133
let current = self.state();
134134
match current {
135135
CoroutineState::Running => {
136-
let new_state = CoroutineState::SystemCall(val, syscall, syscall_state);
136+
let new_state = CoroutineState::Syscall(val, syscall, syscall_state);
137137
let old_state = self.change_state(new_state);
138138
self.on_syscall(self, old_state);
139139
return Ok(());
140140
}
141-
CoroutineState::SystemCall(_, original_syscall, _) => {
141+
CoroutineState::Syscall(_, original_syscall, _) => {
142142
if original_syscall == syscall {
143-
let new_state = CoroutineState::SystemCall(val, syscall, syscall_state);
143+
let new_state = CoroutineState::Syscall(val, syscall, syscall_state);
144144
let old_state = self.change_state(new_state);
145145
self.on_syscall(self, old_state);
146146
return Ok(());
@@ -153,7 +153,7 @@ where
153153
format!(
154154
"{} unexpected {current}->{:?}",
155155
self.name(),
156-
CoroutineState::<Yield, Return>::SystemCall(val, syscall, syscall_state)
156+
CoroutineState::<Yield, Return>::Syscall(val, syscall, syscall_state)
157157
),
158158
))
159159
}
@@ -250,13 +250,13 @@ mod tests {
250250
let co = co!(|_: &Suspender<(), ()>, ()| {})?;
251251
assert_eq!(CoroutineState::Ready, co.state());
252252
co.running()?;
253-
co.syscall((), Syscall::nanosleep, SyscallState::Executing)?;
253+
co.syscall((), SyscallName::nanosleep, SyscallState::Executing)?;
254254
assert_eq!(
255-
CoroutineState::SystemCall((), Syscall::nanosleep, SyscallState::Executing),
255+
CoroutineState::Syscall((), SyscallName::nanosleep, SyscallState::Executing),
256256
co.state()
257257
);
258258
assert!(co
259-
.syscall((), Syscall::sleep, SyscallState::Executing)
259+
.syscall((), SyscallName::sleep, SyscallState::Executing)
260260
.is_err());
261261
Ok(())
262262
}

core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub mod common;
6060
#[allow(missing_docs)]
6161
pub mod config;
6262

63-
/// Coroutine impls.
63+
#[doc = include_str!("../../docs/en/coroutine.md")]
6464
pub mod coroutine;
6565

6666
/// Make the coroutine automatically yield.

0 commit comments

Comments
 (0)