LeeReindeer/ROS

Code Review 2: 增加任务间通信能力

Opened this issue · 1 comments

目前 ROS 提供的系统调用有如下四个:

  • ros_init(): 初始化系统时钟等
  • ros_create_task(*task, task_func, priority, stack, stack_size): 创建任务
  • ros_delay(ticks): 挂起任务一段时间,主动让出CPU
  • ros_schedule(): 任务调度,不必主动调用

功能十分有限和简洁简陋,之后会考虑提供更多系统调用,丰富任务间协同通信的能力:

  • ros_yield(x): 挂起任务并返回值,唤醒任意其他的任务
  • ros_join(*task): 等待其他任务结束
  • Channel: 任务间数据传递,类似 Go 中的 chan
  • Mutex: 互斥锁

需要声明的是, ROS 实现的协程是 symmetric coroutine。因此任务不能嵌套调用(不存在调用者和被调用者的关系),任务都是在同一层级进行调度的。

在论文 Revisiting Coroutines 中又对其的明确定义:

A well-known classification of coroutines concerns the control-transfer operations that are provided and distinguishes the concepts of symmetric and asymmetric coroutines. Symmetric coroutine facilities provide a single control-transfer operation that allows coroutines to explicitly pass control between themselves. Asymmetric coroutine mechanisms (more commonly denoted as semi-symmetric or semi coroutines) provide two control-transfer operations: one for invoking a coroutine and one for suspending it, the latter returning control to the coroutine invoker. While symmetric coroutines operate at the same hierarchical level, an asymmetric coroutine can be regarded as subordinate to its caller, the relationship between them being somewhat similar to that between a called and a calling routine.