Module molly.thread

Module with thread implementation.

The module stores an active thread type and delegates generic thread operations (new, yield, scheduler) and synchronization primitives to the corresponding implementation:

  • molly.compat.thread_fiber - threads, based on Tarantool fibers.
  • molly.compat.thread_coroutine - threads, based on Lua coroutines.

This is the single entry point for thread synchronization primitives: a barrier, a mutex and a wait group. All of them are bound to the active thread backend through yield, so they behave identically for fibers and coroutines.

All primitives are designed for a cooperative environment: a thread that cannot proceed yet yields control to a scheduler instead of blocking an OS thread. Semantics follow well-known counterparts from other languages:

  • a barrier resembles pthread_barrier_wait from POSIX threads;
  • a mutex resembles sync.Mutex from Go;
  • a wait group resembles sync.WaitGroup from Go.

Primitives are plain shared objects and are not tied to the lifetime of a thread that created or uses them.

A barrier releases a group of threads once every thread reaches it:

 local thread = require('molly.thread')
 -- 'coroutine' is used by default.
 thread.set_type('fiber')
 local n = 3
 local barrier = thread.barrier_new(n)

 local function worker(thread_id, opts)
     -- Wait until every thread has finished the first stage.
     barrier:wait()
     -- ... run the second stage ...
 end

A mutex serializes access to shared data and a wait group waits until all threads are done:

 local mutex = thread.mutex_new()
 mutex:lock()
 -- ... protected shared data ...
 mutex:unlock()

 local wg = thread.wg_new()
 wg:add(n)
 -- ... each thread calls wg:done() ...
 wg:wait()

See also:

Functions

barrier_new (n) Create a barrier that releases a group of n threads.
mutex_new () Create an unlocked mutex.
wg_new () Create a wait group with a zero counter.

Local Functions

set_type (thread_type) Set an active thread type.
new (thread_id) Create a new thread of the active type.
yield () Yield control to a scheduler of the active thread type.
scheduler () Drive coroutine threads to completion.


Functions

barrier_new (n)
Create a barrier that releases a group of n threads. A barrier is reusable: when the last thread arrives, all threads are released and the barrier starts to count a next round.

A barrier has a single method, wait(), that blocks the calling thread until all n threads arrive at the barrier.

Parameters:

  • n number A number of threads.

Returns:

    barrier
mutex_new ()
Create an unlocked mutex.

A mutex has the following methods:

  • lock() - acquire the mutex, yielding until it becomes available;
  • unlock() - release the mutex, an error to unlock an unlocked mutex;
  • trylock() - try to acquire the mutex without blocking and report whether it succeeded.

A mutex does not track its owner: any thread may call unlock(). If a thread is terminated while holding a mutex, the mutex stays locked and lock() yields forever, so use a non-blocking trylock() to detect such a state.

Returns:

    mutex
wg_new ()

Create a wait group with a zero counter.

A wait group has the following methods:

  • add(delta) - add delta, which may be negative, to the counter;
  • done() - decrement the counter by one;
  • wait() - block until the counter is zero.

Returns:

    wg

Local Functions

set_type (thread_type)
Set an active thread type. Raises an error for an unknown type or for a type with no implementation in the current runtime, e.g. 'fiber' under LuaJIT.

Parameters:

  • thread_type string Thread type: 'fiber' or 'coroutine'.

Returns:

    an active thread implementation
new (thread_id)
Create a new thread of the active type.

Parameters:

  • thread_id number A thread identifier.

Returns:

    a thread object
yield ()
Yield control to a scheduler of the active thread type.

Returns:

    true
scheduler ()
Drive coroutine threads to completion. A no-op for fiber threads, which are scheduled by the runtime.
generated by LDoc 1.5.0 Last updated 2026-10-22 11:22:25