z3-408.2: Bindings for the Z3 Theorem Prover
Copyright(c) 2010-2011 Bas van Dijk & Roel van Dijk
LicenseBSD3
MaintainerIago Abal <mail@iagoabal.eu>, David Castro <david.castro.dcp@gmail.com>
Safe HaskellSafe-Inferred
LanguageHaskell2010

Z3.RLock

Description

A minimal implementation of a re-entrant lock, adapted from https://github.com/basvandijk/concurrent-extra

Synopsis

Documentation

newtype RLock Source #

A reentrant lock is in one of two states: "locked" or "unlocked". When the lock is in the "locked" state it has two additional properties:

  • Its owner: the thread that acquired the lock.
  • Its acquired count: how many times its owner acquired the lock.

Constructors

RLock 

Fields

Instances

Instances details
Eq RLock Source # 
Instance details

Defined in Z3.RLock

Methods

(==) :: RLock -> RLock -> Bool #

(/=) :: RLock -> RLock -> Bool #

type State = Maybe (ThreadId, Integer) Source #

The state of an RLock.

  • Nothing indicates an "unlocked" state.
  • Just (tid, n) indicates a "locked" state where the thread identified by tid acquired the lock n times.

new :: IO RLock Source #

Create a reentrant lock in the "unlocked" state.

acquire :: RLock -> IO () Source #

Acquires the RLock. Blocks if another thread has acquired the RLock. acquire behaves as follows:

  • When the state is "unlocked", acquire changes the state to "locked" with the current thread as owner and an acquired count of 1.
  • When the state is "locked" and the current thread owns the lock acquire only increments the acquired count.
  • When the state is "locked" and the current thread does not own the lock acquire blocks until the owner releases the lock. If the thread that called acquire is woken upon release of the lock it will take ownership and change the state to "locked" with an acquired count of 1.

There are two further important properties of acquire:

  • acquire is single-wakeup. That is, if there are multiple threads blocked on acquire, and the lock is released, only one thread will be woken up. The runtime guarantees that the woken thread completes its acquire operation.
  • When multiple threads are blocked on acquire they are woken up in FIFO order. This is useful for providing fairness properties of abstractions built using locks. (Note that this differs from the Python implementation where the wake-up order is undefined.)

release :: RLock -> IO () Source #

release decrements the acquired count. When a lock is released with an acquired count of 1 its state is changed to "unlocked".

Note that it is both an error to release a lock in the "unlocked" state and to release a lock that is not owned by the current thread.

If there are any threads blocked on acquire the thread that first called acquire will be woken up.

with :: RLock -> IO a -> IO a Source #

A convenience function which first acquires the lock and then performs the computation. When the computation terminates, whether normally or by raising an exception, the lock is released.

Note that: with = liftA2 bracket_ acquire release.