# std/coro

Theme:

- Manual
- Standard library
- Index
- Compiler docs
- Fusion docs
- devel, stable

Search:
Source Edit

Nim coroutines implementation, supports several context switching methods:

ucontext | available on unix and alike (default)
setjmp | available on unix and alike (x86/64 only)
fibers | available and required on windows.

`-d:nimCoroutines`
Required to build this module.
`-d:nimCoroutinesUcontext`
Use ucontext backend.
`-d:nimCoroutinesSetjmp`
Use setjmp backend.
`-d:nimCoroutinesSetjmpBundled`
Use bundled setjmp implementation.

Unstable API.

Timer support for the realtime GC. Based on https://github.com/jckarter/clay/blob/master/compiler/hirestimer.cpp

# Imports

coro_detection, os, lists, assertions

# Types

    CoroutineRef = ref object

  CoroutineRef holds a pointer to actual coroutine object. Public API always returns CoroutineRef instead of CoroutinePtr in order to allow holding a reference to coroutine object while it can be safely deallocated by coroutine scheduler loop. In this case Coroutine.reference.coro is set to nil. Public API checks for it being nil and gracefully fails if it is nil. Source Edit

# Procs

    proc alive(c: CoroutineRef): bool {....raises: [], tags: [], forbids: [].}

  Returns `true` if coroutine has not returned, `false` otherwise. Source Edit

    proc nimGC_setStackBottom(theStackBottom: pointer) {....raises: [], tags: [],
        forbids: [].}

  Source Edit

    proc run() {....raises: [], tags: [TimeEffect], forbids: [].}

  Starts main coroutine scheduler loop which exits when all coroutines exit. Calling this proc starts execution of first coroutine. Source Edit

    proc start(c: proc (); stacksize: int = defaultStackSize): CoroutineRef {.
        discardable, ...raises: [], tags: [], forbids: [].}

  Schedule coroutine for execution. It does not run immediately. Source Edit

    proc suspend(sleepTime: float = 0.0) {....raises: [], tags: [], forbids: [].}

  Stops coroutine execution and resumes no sooner than after `sleeptime` seconds. Until then other coroutines are executed. Source Edit

    proc wait(c: CoroutineRef; interval = 0.01) {....raises: [], tags: [], forbids: [].}

  Returns only after coroutine `c` has returned. `interval` is time in seconds how often. Source Edit