Skip to content

Commit d92351e

Browse files
committed
add doc
1 parent d9a05c7 commit d92351e

File tree

3 files changed

+96
-6
lines changed

3 files changed

+96
-6
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010

1111
The `open-coroutine` is a simple, efficient and generic stackful-coroutine library.
1212

13+
English | [中文](README_ZH.md)
14+
1315
## 🚀 Features
1416

1517
- [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);
1618
- [x] Hook: you are free to use most of the slow system calls in coroutine;
1719
- [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);
1820
- [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;
1921
- [x] Priority: support custom task and coroutine priority;
20-
- [x] Work Stealing: internally using a lock free work steel queue;
21-
- [x] Compatibility: the implementation of open-coroutine is no async, but it is compatible with async, which means you can use this crate in tokeno/sync-std/smol/...;
22-
- [x] Platforms: running on Linux, MacOS and Windows;
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/...;
24+
- [x] Platforms: running on Linux, macOS and Windows;
2325

2426
## 🕊 Roadmap
2527

@@ -74,7 +76,7 @@ fn main() {
7476
})
7577
.expect("allocate stack failed")
7678
}
77-
println!("[coroutine] launched");
79+
println!("[task] launched");
7880
// Use ~500KB of stack.
7981
recurse(50, &mut [0; 10240]);
8082
}, ());

README_ZH.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# open-coroutine
2+
3+
[![crates.io](https://img.shields.io/crates/v/open-coroutine.svg)](https://crates.io/crates/open-coroutine)
4+
[![docs.rs](https://img.shields.io/badge/docs-release-blue)](https://docs.rs/open-coroutine)
5+
[![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)
6+
[![Build Status](https://github.com/acl-dev/open-coroutine/workflows/CI/badge.svg)](https://github.com/acl-dev/open-coroutine/actions)
7+
[![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")
10+
11+
`open-coroutine`是一个简单、高效、通用的有栈协程库。
12+
13+
[English](README.md) | 中文
14+
15+
## 🚀 当前特性
16+
17+
- [x] 抢占调度(`不支持windows`): 即使协程进入死循环,它仍能被抢占,查看[例子](https://github.com/loongs-zhang/open-coroutine/blob/master/open-coroutine/examples/preemptive.rs);
18+
- [x] Hook: 您可以在协程中自由使用大多数慢系统调用;
19+
- [x] 可伸缩栈: 协程栈的大小支持无限制扩容而没有复制堆栈的开销,查看[例子](https://github.com/loongs-zhang/open-coroutine/blob/master/open-coroutine/examples/scalable_stack.rs);
20+
- [x] io_uring(`只支持linux`): 在本地文件IO和网络IO方面支持并兼容io_uring。如果您的系统不支持,它将回退到NIO;
21+
- [x] 优先级: 支持自定义任务和协程的优先级;
22+
- [x] 任务窃取: 内部使用无锁任务窃取队列;
23+
- [x] 兼容性: open-coroutine的实现是No async的,但它与async兼容,这意味着您可以在tokio/sync-std/smol/...中使用这个crate;
24+
- [x] 跨平台: 支持Linux、macOS和Windows;
25+
26+
## 🕊 未来计划
27+
28+
- [ ] 支持`#[open_coroutine::all_join]``#[open_coroutine::any_join]`宏;
29+
- [ ] 增加并发工具包;
30+
- [ ] 支持AF_XDP套接字;
31+
32+
## 📖 快速接入
33+
34+
### step1: 在你的Cargo.toml中添加依赖
35+
36+
```toml
37+
[dependencies]
38+
# check https://crates.io/crates/open-coroutine
39+
open-coroutine = "x.y.z"
40+
```
41+
42+
### step2: 添加宏
43+
44+
```rust
45+
#[open_coroutine::main]
46+
fn main() {
47+
//......
48+
}
49+
```
50+
51+
### step3: 创建任务
52+
53+
```rust
54+
#[open_coroutine::main]
55+
fn main() {
56+
let task = open_coroutine::task!(|param| {
57+
assert_eq!(param, 1);
58+
}, 1);
59+
task.timeout_join(std::time::Duration::from_secs(1)).expect("timeout");
60+
}
61+
```
62+
63+
### step4: 扩容栈(可选)
64+
65+
```rust
66+
#[open_coroutine::main]
67+
fn main() {
68+
_ = open_coroutine::task!(|_| {
69+
fn recurse(i: u32, p: &mut [u8; 10240]) {
70+
open_coroutine::maybe_grow!(|| {
71+
// Ensure the stack allocation isn't optimized away.
72+
unsafe { _ = std::ptr::read_volatile(&p) };
73+
if i > 0 {
74+
recurse(i - 1, &mut [0; 10240]);
75+
}
76+
})
77+
.expect("allocate stack failed")
78+
}
79+
println!("[task] launched");
80+
// Use ~500KB of stack.
81+
recurse(50, &mut [0; 10240]);
82+
}, ());
83+
}
84+
```
85+
86+
## ⚓ 学习更多
87+
88+
[我有故事,你有酒吗?](https://github.com/acl-dev/open-coroutine-docs)

core/src/coroutine/korosensei.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,6 @@ impl<'c, Param, Yield, Return> Coroutine<'c, Param, Yield, Return> {
307307
stack_size: usize,
308308
callback: F,
309309
) -> std::io::Result<R> {
310-
// if we can't guess the remaining stack (unsupported on some platforms) we immediately grow
311-
// the stack and then cache the new stack size (which we do know now because we allocated it.
312310
if let Some(co) = Self::current() {
313311
let remaining_stack = unsafe { co.remaining_stack() };
314312
if remaining_stack >= red_zone {
@@ -325,6 +323,8 @@ impl<'c, Param, Yield, Return> Coroutine<'c, Param, Yield, Return> {
325323
});
326324
}
327325
// in thread
326+
// if we can't guess the remaining stack (unsupported on some platforms) we immediately grow
327+
// the stack and then cache the new stack size (which we do know now because we allocated it).
328328
thread_local! {
329329
#[allow(clippy::missing_const_for_thread_local)]
330330
static STACK_INFOS: RefCell<VecDeque<StackInfo>> = const { RefCell::new(VecDeque::new()) };

0 commit comments

Comments
 (0)