rcore-os / rcore-os/rCore-Tutorial-v3

协程的一些想法

Open
#124 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
2.1k
Forks
560
PR merge metrics
No merged PRs in 30d

Description

无栈协程的方案之一 -- 达夫设备 ( 从语言的角度来看 )

这是属于第八章的主题,是自己学习协程的一点心得,希望对相应的工作有所帮助.

前置的一些文章 ( 建议看一下 )

达夫设备的出现是个十分偶然的"故事",但是这个"故事"却比较好的给出了一个在栈中捕获状态机的方案, 即无栈协程的方案: 在栈中利用 switch(in C++), loop(Rust) . 前者Boost(v1.66)已经有相关的设计实现,其源码在boost/asio/coroutine.hppboost/asio/yield.hpp,中, 后者则是自己偶然的一个Rust尝试,见如下代码:

#[allow(unreachable_code)]

const STATE_NUM: i32 = 68;

fn _fun_duff_device_coroutine() {
  let mut state: i32 = 0;
  let mut i: i32 = 0;
  'outer: loop {
    loop {
      match state {
        0 => {
          state = 1;
          continue 'outer;
        }
        _ => {
          loop {
            state = 1; i += 1;
            if i >= STATE_NUM { break 'outer; }
            else { continue 'outer; }
          }
        }
      }
    }
  }
}

协程是为了更好复用线程资源.对于有栈线程,代码工作者,往往需要自行设计合适的异步结构,同步结构的存储队列,唤醒机构和执行机构,工作的难度主要也来自前述的四个方面,需要应对不同的现实环境组织四个方面的"交互".而无栈的协程,从代码上看,虽然没有有栈的复杂,但也需要控制好相应的状态转换,无栈的方案(达夫设备)依赖语言设计者或无意或有意留下的"语言缺口".

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the issue text and its linked articles, then inspect how chapter eight is organized in the tutorial. Clarify whether this coroutine discussion is intended as a tutorial contribution and where it belongs. Done means an agreed scope and an accepted, reviewable chapter change.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
documentation, operating-systems
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.