HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleconcurrent-extra-0.7.0.12Haskell98

Control.Concurrent.STM.Lock

This module provides an STM version of Control.Concurrent.Lock.

This module is intended to be imported qualified. We suggest importing it like:

import           Control.Concurrent.STM.Lock         ( Lock )
import qualified Control.Concurrent.STM.Lock as Lock ( ... )
  • 1 type
  • 9 values
newtypenewtype Lock
#

A lock is in one of two states: "locked" or "unlocked".

Instances1Eq
  • Eq LockDefined in concurrent-extra-0.7.0.12 · Control.Concurrent.STM.Lock

Creating locks

2 declarations
valuenew :: STM Lock
#

Create a lock in the "unlocked" state.

Locking and unlocking

3 declarations
valueacquire :: Lock -> STM ()
#
  • When the state is "locked" acquire will retry the transaction.

  • When the state is "unlocked" acquire will change the state to "locked".

valuetryAcquire :: Lock -> STM Bool
#

A non-blocking acquire.

  • When the state is "unlocked" tryAcquire changes the state to "locked" and returns True.

  • When the state is "locked" tryAcquire leaves the state unchanged and returns False.

valuerelease :: Lock -> STM ()
#

release changes the state to "unlocked" and returns immediately.

Note that it is an error to release a lock in the "unlocked" state!

Convenience functions

3 declarations
valuewith :: Lock -> IO a -> IO a
#

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.

valuetryWith :: Lock -> IO a -> IO (Maybe a)
#

A non-blocking with. tryWith is a convenience function which first tries to acquire the lock. If that fails, Nothing is returned. If it succeeds, the computation is performed. When the computation terminates, whether normally or by raising an exception, the lock is released and Just the result of the computation is returned.

valuewait :: Lock -> STM ()
#
  • When the state is "locked", wait will retry the transaction

  • When the state is "unlocked" wait returns immediately.

wait does not alter the state of the lock.

Note that wait is just a convenience function which can be defined as:

wait l = acquire l >> release l

Querying locks

1 declaration
valuelocked :: Lock -> STM Bool
#

Determines if the lock is in the "locked" state.

Note that this is only a snapshot of the state. By the time a program reacts on its result it may already be out of date.