named-lock-0.1: A named lock that is created on demand.





This module implements "named locks".

A named lock is like a normal lock (MVar ()) but is created on demand. This is useful when you have a potentially infinite number of resources that should not be used concurrently.

For example, in a web-server you might create a new lock for each database query so that the same query is only run once.

Named locks are allocated in a LockPool. Names are arbitrary, well-behaved instances of the Ord class.


Creating Lock Pools

newLockPool :: IO (LockPool name)Source

Create a new, empty, lock pool.

data LockPool name Source

Working with Named Locks

grabNamedLock :: Ord name => LockPool name -> name -> IO ()Source

Grab the lock with given name. Blocks until the lock becomes available.

releaseNamedLock :: Ord name => LockPool name -> name -> IO ()Source

Release the lock with the given name.

The released lock must have previously been grabbed via grabNamedLock.

withNamedLock :: Ord name => LockPool name -> name -> IO a -> IO aSource

Hold the lock while running the action.

If the action throws an exception, the lock is released an the exception propagated. Returns the result of the action.