2012-06-04 19:43:29 -04:00
|
|
|
{- locking between threads
|
|
|
|
-
|
2015-01-21 12:50:09 -04:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-06-04 19:43:29 -04:00
|
|
|
-
|
2014-05-10 11:01:27 -03:00
|
|
|
- License: BSD-2-clause
|
2012-06-04 19:43:29 -04:00
|
|
|
-}
|
|
|
|
|
2019-11-23 11:07:22 -04:00
|
|
|
module Utility.ThreadLock (
|
|
|
|
Lock,
|
|
|
|
newLock,
|
|
|
|
withLock,
|
|
|
|
) where
|
2012-06-04 19:43:29 -04: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
|