Commit e703a8e9 authored by Petr Pudlak's avatar Petr Pudlak

Add a module for simple synchronization locks

It just encapsulates 'MVar ()', but hides the implementation and
enforces that the only way how to use a lock is to (internally) use
'bracket'. This ensures that the lock never becomes locked forever due
to a programing error or an exception.
Signed-off-by: default avatarPetr Pudlak <>
Reviewed-by: default avatarKlaus Aehlig <>
parent 01a8d300
......@@ -863,6 +863,7 @@ HS_LIB_SRCS = \
src/Ganeti/Utils/IORef.hs \
src/Ganeti/Utils/Livelock.hs \
src/Ganeti/Utils/MonadPlus.hs \
src/Ganeti/Utils/MVarLock.hs \
src/Ganeti/VCluster.hs \
src/Ganeti/WConfd/ConfigState.hs \
src/Ganeti/WConfd/ConfigWriter.hs \
{-# LANGUAGE FlexibleContexts #-}
{-| Utility functions for using MVars as simple locks. -}
Copyright (C) 2014 Google Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
module Ganeti.Utils.MVarLock
( Lock()
, newLock
, withLock
) where
import Control.Exception.Lifted
import Control.Concurrent.MVar.Lifted
import Control.Monad
import Control.Monad.Base (MonadBase(..))
import Control.Monad.Trans.Control (MonadBaseControl(..))
newtype Lock = MVarLock (MVar ())
newLock :: (MonadBase IO m) => m Lock
newLock = MVarLock `liftM` newMVar ()
withLock :: (MonadBaseControl IO m) => Lock -> m a -> m a
withLock (MVarLock l) = bracket_ (takeMVar l) (putMVar l ())
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment