{-# LANGUAGE CPP, DeriveDataTypeable, NoImplicitPrelude, UnicodeSyntax #-}---------------------------------------------------------------------------------- |-- Module : Control.Concurrent.STM.Lock-- Copyright : (c) 2010-2011 Bas van Dijk & Roel van Dijk-- License : BSD3 (see the file LICENSE)-- Maintainer : Bas van Dijk <v.dijk.bas@gmail.com>-- , Roel van Dijk <vandijk.roel@gmail.com>---- 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 ( ... )-- @----------------------------------------------------------------------------------moduleControl.Concurrent.STM.Lock(Lock-- * Creating locks,new,newAcquired-- * Locking and unlocking,acquire,tryAcquire,release-- * Convenience functions,with,tryWith,wait-- * Querying locks,locked)where---------------------------------------------------------------------------------- Imports---------------------------------------------------------------------------------- from base:importControl.Applicative(liftA2)importControl.Exception(bracket_,onException)importControl.Monad(Monad,return,(>>),when)importData.Bool(Bool,not)#ifdef __HADDOCK__importData.Bool(Bool(False,True))#endifimportData.Eq(Eq)importData.Function(($))importData.Functor(fmap,(<$>))importData.Maybe(Maybe(Nothing,Just),isJust)importData.Typeable(Typeable)importPrelude(error)importSystem.IO(IO)#if __GLASGOW_HASKELL__ < 700importControl.Monad((>>=),fail)#endif-- from stm:importControl.Concurrent.STM(STM,atomically)#ifdef __HADDOCK__importControl.Concurrent.STM(retry)#endifimportControl.Concurrent.STM.TMVar(TMVar,newTMVar,newEmptyTMVar,takeTMVar,tryTakeTMVar,putTMVar,tryPutTMVar,isEmptyTMVar)-- from base-unicode-symbols:importData.Function.Unicode((∘))-- from concurrent-extra (this package):importUtils(mask)---------------------------------------------------------------------------------- Locks---------------------------------------------------------------------------------- | A lock is in one of two states: \"locked\" or \"unlocked\".newtypeLock=Lock{un∷TMVar()}deriving(Typeable,Eq)---------------------------------------------------------------------------------- Creating locks---------------------------------------------------------------------------------- | Create a lock in the \"unlocked\" state.new∷STMLocknew=Lock<$>newTMVar()-- | Create a lock in the \"locked\" state.newAcquired∷STMLocknewAcquired=Lock<$>newEmptyTMVar---------------------------------------------------------------------------------- Locking and unlocking--------------------------------------------------------------------------------{-|
* When the state is \"locked\" @acquire@ will 'retry' the transaction.
* When the state is \"unlocked\" @acquire@ will change the state to \"locked\".
-}acquire∷Lock→STM()acquire=takeTMVar∘un{-|
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'.
-}tryAcquire∷Lock→STMBooltryAcquire=fmapisJust∘tryTakeTMVar∘un{-|
@release@ changes the state to \"unlocked\" and returns immediately.
Note that it is an error to release a lock in the \"unlocked\" state!
-}release∷Lock→STM()release(Locktmv)=dob←tryPutTMVartmv()when(notb)$error"Control.Concurrent.STM.Lock.release: Can't release unlocked Lock!"---------------------------------------------------------------------------------- Convenience functions--------------------------------------------------------------------------------{-|
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.
-}with∷Lock→IOa→IOawith=liftA2bracket_(atomically∘acquire)(atomically∘release){-|
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.
-}tryWith∷Lock→IOα→IO(Maybeα)tryWithla=mask$\restore→doacquired←atomically(tryAcquirel)ifacquiredthendor←restorea`onException`atomically(releasel)atomically(releasel)return$JustrelsereturnNothing{-|
* 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@
-}wait∷Lock→STM()wait(Locktmv)=takeTMVartmv>>putTMVartmv()---------------------------------------------------------------------------------- Querying locks--------------------------------------------------------------------------------{-|
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.
-}locked∷Lock→STMBoollocked=isEmptyTMVar∘un