As discussed offline, I'd suggest to put a warning to the documentation of the exported functions that they release all pending requests, so that it's immediately obvious for people that use them.
Otherwise LGTM (no need to resend). On Fri, Apr 11, 2014 at 12:43 PM, Klaus Aehlig <[email protected]> wrote: > Add convenience functions for downgrading and freeing > locks in a waiting structure. As these functions are > guaranteed to always succeed, they will also drop any > pending request of the owner. > > Signed-off-by: Klaus Aehlig <[email protected]> > --- > src/Ganeti/Locking/Waiting.hs | 29 ++++++++++++++++++++++++++++- > 1 file changed, 28 insertions(+), 1 deletion(-) > > diff --git a/src/Ganeti/Locking/Waiting.hs b/src/Ganeti/Locking/Waiting.hs > index 4fe25fe..1fdd602 100644 > --- a/src/Ganeti/Locking/Waiting.hs > +++ b/src/Ganeti/Locking/Waiting.hs > @@ -36,9 +36,11 @@ module Ganeti.Locking.Waiting > , getPendingRequests > , extRepr > , fromExtRepr > + , freeLocksPredicate > + , downGradeLocksPredicate > ) where > > -import Control.Arrow ((&&&)) > +import Control.Arrow ((&&&), second) > import Control.Monad (liftM) > import qualified Data.Map as M > import Data.Maybe (fromMaybe) > @@ -282,3 +284,28 @@ instance (Lock a, J.JSON a, Ord b, J.JSON b, Show b, > Ord c, J.JSON c) > => J.JSON (LockWaiting a b c) where > showJSON = J.showJSON . extRepr > readJSON = liftM fromExtRepr . J.readJSON > + > +-- | Manipulate a all locks of an owner that have a given property. > s/ a / / > +manipulateLocksPredicate :: (Lock a, Ord b, Ord c) > + => (a -> L.LockRequest a) > + -> (a -> Bool) > + -> b > + -> LockWaiting a b c -> (LockWaiting a b c, > S.Set b) > +manipulateLocksPredicate req prop owner state = > + second snd . flip (updateLocks owner) (removePendingRequest owner state) > + . map req . filter prop . M.keys > + . L.listLocks owner $ getAllocation state > + > +-- | Free all Locks of a given owner satisfying a given predicate. > +freeLocksPredicate :: (Lock a, Ord b, Ord c) > + => (a -> Bool) > + -> b > + -> LockWaiting a b c -> (LockWaiting a b c, S.Set b) > +freeLocksPredicate = manipulateLocksPredicate L.requestRelease > + > +-- | Downgrade all locks of a given owner that satisfy a given predicate > +downGradeLocksPredicate :: (Lock a, Ord b, Ord c) > + => (a -> Bool) > + -> b > + -> LockWaiting a b c -> (LockWaiting a b c, S.Set > b) > +downGradeLocksPredicate = manipulateLocksPredicate L.requestShared > -- > 1.9.1.423.g4596e3a > >
