-
Notifications
You must be signed in to change notification settings - Fork 2
coroutines
modrpc edited this page Oct 8, 2023
·
6 revisions
- Coroutines: https://en.wikipedia.org/wiki/Coroutine
- Fibers: https://en.wikipedia.org/wiki/Fiber_(computer_science)
- asyncio: https://docs.python.org/3/library/asyncio.html
- coroutines: https://docs.python.org/3/library/asyncio-task.html
- internals
- tutorials
- 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
- tutorials
- gevent for the workking programmers: http://sdiehl.github.io/gevent-tutorial/
- qt: use version inside SystemC package
- David Keppel's TR
- SystemC: quickthreads vs setcontext
- Coroutines in LLVM: http://llvm.org/docs/Coroutines.html
- Coroutine library for C/Unix: https://swtch.com/libtask -- not sure if this is a good library
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
...
};
-
wait
: suspend a thread -- internally callssc_cor_pkg::yield
- user-level context switching facility
- setcontext wiki: https://en.wikipedia.org/wiki/Setcontext
- Copmplete Context Control: http://www.delorie.com/gnu/docs/glibc/libc_465.html
- Non-Local Exits (GNU C Library): http://www.delorie.com/gnu/docs/glibc/libc_461.html
- boost.Context: http://www.boost.org/doc/libs/1_55_0/libs/context/doc/html/index.html
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;
- save current context into "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
- 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