For the latest stable version, please use Emilua API 0.10!

mutex

local mutex = require('mutex')

local function ping_sender()
    sleep(30)
    scope(function()
        scope_cleanup_push(function() ws_write_mtx:unlock() end)
        ws_write_mtx:lock()
        ws:ping()
    end)
end

local function queue_consumer()
    scope(function()
        scope_cleanup_push(function() queue_mtx:unlock() end)
        queue_mtx:lock()
        while #queue == 0 do
            queue_cond:wait(queue_mtx)
        end
        for _, e in ipairs(queue) do
            consume_item(e)
        end
        queue = {}
    end)
end

A mutex.

Functions

new() → mutex

Constructor.

lock(self)

Locks the mutex.

This suspending function does not act as an interruption point.
This mutex applies dispatch semantics. That means no context switch to other ready fibers will take place if it’s possible to acquire the mutex immediately.

try_lock(self) → boolean

Tries to lock the mutex. Returns whether lock acquisition was successful.

It’s an error to call try_lock() if current fiber already owns the mutex (cf. recursive_mutex(3em) for an alternative).
The current fiber is never suspended.

unlock(self)

Unlocks the mutex.