Module

Threading.Data.Mutex

Package
purescript-threading
Repository
cakekindel/purescript-threading

A Mutex allows any number of threads to share mutable state, with at most 1 thread having read or write access at a time.

Threads can access the data with lock or tryLock, which both return a Guard.

The holder of a Guard is guaranteed exclusive read & write access to the data contained in the Mutex.

#Mutex Source

data Mutex t0

#Guard Source

data Guard t0

A lock to a Mutex.

Guards may be read from, written to, and released. Guards must be released in order for other blocking threads to continue.

Note: If a Guard reclaimed by the garbage collector without being released, its Mutex will notice and behave as if the Guard was explicitly released. This will hopefully catch deadlocks caused by threads that have exited while holding a Guard.

#mutex Source

mutex :: forall a. a -> Effect (Mutex a)

Create a new Mutex

#lock Source

lock :: forall a. Mutex a -> Aff (Guard a)

Acquire a lock, blocking if another thread currently holds a lock.

If multiple threads invoke lock, they will be unlocked in the order that they blocked on lock.

#tryLock Source

tryLock :: forall a. Mutex a -> Effect (Maybe (Guard a))

Attempt to acquire a lock without blocking.

If the Mutex is currently locked, this will return Nothing.

#locked Source

locked :: forall a. Mutex a -> Effect Boolean

Is the Mutex currently locked?

#release Source

release :: forall a. Guard a -> Effect Unit

Release the lock

Attempting to read or write this Guard will throw an exception.

Repeated invocations of release are ignored.

#modify Source

modify :: forall a. Mutex a -> (a -> a) -> Aff a

Modify the value contained in a Mutex

This is a shorthand for acquiring a lock, reading from it, writing to it, then immediately releasing it.

Returns the new value.

#modify_ Source

modify_ :: forall a. Mutex a -> (a -> a) -> Aff Unit

modify with its return value ignored.

#write Source

write :: forall a. Guard a -> a -> Effect Unit

Write a new value into the Mutex via the Guard

#read Source

read :: forall a. Guard a -> Effect a

Read the value in the Mutex via the Guard

#get Source

get :: forall a. Mutex a -> Aff a

Take a snapshot of the value in a Mutex

This is a shorthand for acquiring a lock, reading it, then immediately releasing the lock.

#put Source

put :: forall a. Mutex a -> a -> Aff Unit

Write a new value to a Mutex

This is a shorthand for acquiring a lock, writing to it, then immediately releasing the lock.