Module

Threading.Data.RWLock

Package
purescript-threading
Repository
cakekindel/purescript-threading

A RWLock allows threads to share mutable state.

Any number of threads can concurrently read the state, when there isn't a thread with write access.

Get write access with lockWrite or tryLockWrite, or read access with lockRead or tryLockRead.

(try)lockWrite returns a WriteGuard, which guarantees no other threads have read or write access until it is released.

(try)lockRead returns a ReadGuard, which guarantees no threads have write access until it is released.

#RWLock Source

data RWLock a

A Read-Write lock

Ensures that there can be at most 1 thread with write access to the data contained in the RWLock, or any number of concurrent readers.

#ReadGuard Source

data ReadGuard a

A guard with read access to data of type a

Instances

#WriteGuard Source

data WriteGuard a

A guard with read+write access to data of type a

Instances

#rwLock Source

rwLock :: forall a. a -> Effect (RWLock a)

Create a new RWLock

#lockWrite Source

lockWrite :: forall a. RWLock a -> Aff (WriteGuard a)

Acquire a write-access lock to the data contained in the RWLock.

If another thread holds a ReadGuard or WriteGuard, this will block until the data is writable.

#tryLockWrite Source

tryLockWrite :: forall a. RWLock a -> Aff (Maybe (WriteGuard a))

Acquire a write-access lock to the data contained in the RWLock.

If another thread holds a ReadGuard or WriteGuard, this will return Nothing.

#lockRead Source

lockRead :: forall a. RWLock a -> Aff (ReadGuard a)

Acquire a read-access lock to the data contained in the RWLock.

If another thread holds a WriteGuard, this will block until the data is readable.

#tryLockRead Source

tryLockRead :: forall a. RWLock a -> Aff (Maybe (ReadGuard a))

Acquire a read-access lock to the data contained in the RWLock.

If another thread holds a WriteGuard, this will return Nothing.

#locked Source

locked :: forall a. RWLock a -> Effect Locked

Asks what state the RWLock is currently in

#Locked Source

data Locked

The lock state of the RWLock

Constructors

  • Unlocked

    There are no readers or writers.

  • LockedWriting

    There is a writer, and the RWLock is not currently readable or writable.

  • LockedReading

    There is at least one reader, and the RWLock is not currently writable.

Instances

#get Source

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

Get the value currently in the RWLock.

Shorthand for lockRead rw >>= (\l -> read l <* release l)

#put Source

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

Write a new value to the RWLock.

Shorthand for lockWrite rw >>= (\l -> liftEffect (write l a) <* release l)

#modify Source

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

Modify the value in the RWLock using the provided function.

#modify_ Source

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

Shorthand for void $ modify rw f

#write Source

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

Writes a new value

#RWLockGuard Source

class RWLockGuard :: (Type -> Type) -> Constraintclass RWLockGuard g  where

Typeclass implemented by WriteGuard and ReadGuard allowing a common release + read function (as opposed to releaseRead, releaseWrite, etc.)

Members

Instances