Module mutex
Mutex operations.
Mutexes are used to ensure portions of code are accessed by a single task at a time. Said portions are called "critical sections", and are delimited by a lock acquisition at the beginning, and a lock release at the end. Only one task can acquire a lock at a time, so there is only one task inside the critical section at a time. Notice that Lumen being a cooperative scheduler, it will never preempt control from a task. Thus mutexes can be needed only if the fragment of code being locked contains a call that explicitly relinquish control, such as sched.sleep(), sched.signal() or sched.wait().
Usage:
local mutex = require 'lumen.mutex' ocal mx = mutex.new() ocal function critical() mx:acquire() ... mx:release() nd ocal critical = mx:synchronize(function() ... end) --other method
Functions
new () | Create a new mutex. |
acquire (mutexd) | Acquire a lock. |
release (mutexd) | Releases a lock. |
synchronize (mutexd, f) | Generate a synchronizzed version of a function. |
Functions
- new ()
-
Create a new mutex.
Returns:
-
a mutexd object.
- acquire (mutexd)
-
Acquire a lock.
If the lock is already acquired, this call will block until the task that
holds it releases the lock or finishes. Can be invoked in OO fashion
as mutexd:acquire().
Parameters:
- mutexd a mutexd object.
- release (mutexd)
-
Releases a lock.
A task can only release a lock it acquired before, otherwise a
error is triggered. If a task finishes or is killed, all locks it held will be released automatically.
Can be invoked in OO fashion as mutexd:release().
Parameters:
- mutexd a mutexd object.
- synchronize (mutexd, f)
-
Generate a synchronizzed version of a function.
This is a helper that takes a function, and returns a wrapper that is locked with
the mutex.
Can be invoked in OO fashion as mutexd:synchronize(f).
Parameters:
- mutexd a mutexd object.
- f function to be locked.