2012-06-04 23:43:29 +00:00
|
|
|
{- locking between threads
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-06-04 23:43:29 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-06-04 23:43:29 +00:00
|
|
|
-}
|
|
|
|
|
2019-11-23 15:07:22 +00:00
|
|
|
module Utility.ThreadLock (
|
|
|
|
Lock,
|
|
|
|
newLock,
|
|
|
|
withLock,
|
|
|
|
) where
|
2012-06-04 23:43:29 +00:00
|
|
|
|
|
|
|
import Control.Concurrent.MVar
|
|
|
|
|
|
|
|
type Lock = MVar ()
|
|
|
|
|
|
|
|
newLock :: IO Lock
|
|
|
|
newLock = newMVar ()
|
|
|
|
|
|
|
|
{- Runs an action with a lock held, so only one thread at a time can run it. -}
|
|
|
|
withLock :: Lock -> IO a -> IO a
|
|
|
|
withLock lock = withMVar lock . const
|