struct Syn::Core::Mutex

Overview

A Fiber aware, mutually exclusive lock.

Prevents two or more fibers to access the same non concurrent piece of code (e.g. mutating a shared object) at the same time.

There are three types of mutexes.

This is a smaller and fully contained alternative to the ::Mutex class in stdlib that doesn't allocate extraneous objects. It also exposes the quick #try_lock? that won't block.

TODO check whether the owning fiber is still alive or not (i.e. EOWNERDEAD)

Included Modules

Defined in:

core/mutex.cr

Constructors

Instance Method Summary

Instance methods inherited from module Syn::Core::Lockable

lock : Nil lock, unlock : Nil unlock

Constructor Detail

def self.new(type : Type = :checked) #

[View source]

Instance Method Detail

def lock(timeout : Time::Span) : Bool #

Identical to #lock but aborts if the lock couldn't be acquired until timeout is reached, in which case it returns false (failed to acquire lock). Returns true if the lock was acquired.

EXPERIMENTAL The timeout feature is experimental.


[View source]
def lock : Nil #

Acquires the lock, suspending the current fiber until the lock can be acquired.

If the mutex is unchecked, trying to re-lock while the current fiber is already holding the lock will result in a deadlock. If checked it will raise an Error. If reentrant, the counter will be incremented and the method will return.


[View source]
def synchronize(timeout : Time::Span, &) : Bool #

Identical to #synchronize but aborts if the lock couldn't be acquired until timeout is reached, in which case it returns false.

NOTE unlike #synchronize it doesn't return the block's value!

EXPERIMENTAL The timeout feature is experimental.


[View source]
def synchronize(& : -> U) : U forall U #

Acquires the lock, yields, then releases the lock, even if the block raised an exception.


[View source]
def try_lock? : Bool #

Returns true if the lock could be acquired, otherwise immediately returns false without blocking.

Merely returns false whenever the lock if already held. Doesn't check if the current fiber currently holds the lock, doesn't raise and also doesn't increment the counter for reentrant mutexes (unless it's the initial lock).


[View source]
def unlock : Nil #

Releases the lock.

If unchecked, any fiber can unlock the mutex and the mutex doesn't even need to be locked.

If checked or reentrant, the mutex must have been locked and only the fiber holding the lock is allowed otherwise Error exceptions will be raised. If reentrant the counter will be decremented and the lock only released when the counter reaches zero (i.e. you must call #unlock as many times as #lock was called.


[View source]