Module eco.sync

Coroutine synchronization primitives.

This module provides a small set of synchronization primitives designed for eco's cooperative coroutines (not OS threads):

All timeouts are expressed in seconds.

Functions

cond () Create a condition variable.
waitgroup () Create a wait group.
mutex () Create a mutex.

Class cond

cond:wait ([timeout]) Wait until signaled.
cond:signal ([data]) Wake one waiting coroutine.
cond:broadcast () Wake all waiting coroutines.

Class waitgroup

waitgroup:add (delta) Add delta to the wait group counter.
waitgroup:done () Decrement the wait group counter by one.
waitgroup:wait ([timeout]) Wait until the counter becomes zero.

Class mutex

mutex:lock ([timeout]) Lock the mutex.
mutex:unlock () Unlock the mutex.


Functions

cond ()
Create a condition variable.

Returns:

    cond
waitgroup ()
Create a wait group.

One coroutine calls waitgroup:add to set the number of coroutines to wait for. Then each of those coroutines runs and calls waitgroup:done when finished. Meanwhile, waitgroup:wait can be used to block until all of them have finished.

Returns:

    waitgroup
mutex ()
Create a mutex.

Returns:

    mutex

Class cond

Condition variable returned by sync.cond.

A condition variable is a rendezvous point for coroutines waiting for, or announcing, the occurrence of an event.

cond:wait ([timeout])
Wait until signaled.

If signaled with a non-nil truthy data, returns that data. Otherwise returns true.

Parameters:

  • timeout number Timeout in seconds. (optional)

Returns:

    any Data passed to cond:signal, or true.

Or

  1. nil On timeout.
  2. string Error message ('timeout').
cond:signal ([data])
Wake one waiting coroutine.

If data is provided and is truthy, the waiter will receive data as the return value of cond:wait; otherwise it will receive true.

Parameters:

  • data any Data passed to the awakened waiter. (optional)

Returns:

    boolean true if a waiter was awakened.
cond:broadcast ()
Wake all waiting coroutines.

All awakened waiters will receive true as the return value of cond:wait.

Returns:

    integer n Number of awakened coroutines.

Class waitgroup

Wait group returned by sync.waitgroup.

A wait group waits for a collection of coroutines to finish.

waitgroup:add (delta)
Add delta to the wait group counter.

A positive delta increments the number of workers to wait for. A negative delta decrements it.

Parameters:

  • delta integer

Raises:

If the counter would become negative.
waitgroup:done ()
Decrement the wait group counter by one.

When the counter reaches zero, all waiters are awakened.

Raises:

If the counter would become negative.
waitgroup:wait ([timeout])
Wait until the counter becomes zero.

Parameters:

  • timeout number Timeout in seconds. (optional)

Returns:

    boolean true When the counter becomes zero.

Or

  1. nil On timeout.
  2. string Error message ('timeout').

Class mutex

Mutex returned by sync.mutex.

A mutex provides mutual exclusion between coroutines.

mutex:lock ([timeout])
Lock the mutex.

If the mutex is already locked, the current coroutine waits until it becomes available or timeout expires.

Parameters:

  • timeout number Timeout in seconds. (optional)

Returns:

    boolean true On success.

Or

  1. nil On timeout.
  2. string Error message ('timeout').
mutex:unlock ()
Unlock the mutex.

Wakes one coroutine waiting in mutex:lock.

Raises:

If the mutex is not locked.
generated by LDoc 1.5.0 Last updated 2026-04-09 14:48:22