coroutines - modrpc/info GitHub Wiki

Table of Contents

Coroutines

Python

asyncio

3.7 asyncio basics

  • three ways to run a couroutine
    • asyncio.run(coro())
    • await coro()
    • task1 = asyncio.create_task(coro()); await task1
  • three awaitable objects: can be used in an await expression
    • coroutine
    • asyncio.Task
    • asyncio.Future

gevent

MicroPython

uasyncio

CircuitPython

QuickThreads

QuickThreads Internals

LLVM Coroutine

Libtask

Node.js

SystemC

sc_cor_pkg abstract class

class sc_cor_pkg {
  public:
   ...        
   virtual sc_cor* create(size_t stack_size,sc_cor_fn* fn, void* arg );
   virtual void yield( sc_cor* next_cor ); // yield to function assoc. with next_cor
   virtual void abort( sc_cor* next_cor ); // quit and transfer control to next_cor
   ...
};

sc_cor abstract class

  • wait: suspend a thread -- internally calls sc_cor_pkg::yield

UNIX SYSV: setcontext

data structure

typedef struct ucontext {
  // context which will be resumed when current context terminates.
  // in case the current context was created using makecontext 
  struct ucontext *uc_link;      
  sigset_t         uc_sigmask;  // in <signal.h>
  stack_t          uc_stack;    // in <signal.h>
  // machine-specific representation of saved context, including calling
  // thread's machine registers
  mcontext_t       uc_mcontext; 
  ...
} ucontext_t;

getcontext(ucontext_t *ucp)

  • save current context into "ucp"

setcontext(ucontext_t *ucp)

  • restores the user context pointed at by ucp
  • successful call does not return (just jump!)
  • ucp should have been obtained by
    • getcontext() or
    • makecontext() or
    • passed as third argument to a signal handler

makecontext(ucontext_t *ucp, void (*func)(), int argc, ...)

  • initial setup:
    • getcontext(ucp): saves current context into ucp
    • allocate a stack space in ucp->uc_stack
    • define successor context and assign it to ucp->uc_link
    • makecontext(ucp, foo, args):
  • later:
  • setcontext(ucp) is called or swapcontext() is called
  • foo(args) is called
  • when foo(args) returns, successor context (uc_link) is activated

UNIX: SETJMP

⚠️ **GitHub.com Fallback** ⚠️