|
| 1 | +# open-coroutine |
| 2 | + |
| 3 | +[](https://crates.io/crates/open-coroutine) |
| 4 | +[](https://docs.rs/open-coroutine) |
| 5 | +[](https://github.com/acl-dev/open-coroutine/blob/master/LICENSE-APACHE) |
| 6 | +[](https://github.com/acl-dev/open-coroutine/actions) |
| 7 | +[](https://codecov.io/github/acl-dev/open-coroutine) |
| 8 | +[](http://isitmaintained.com/project/acl-dev/open-coroutine "Average time to resolve an issue") |
| 9 | +[](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) |
0 commit comments