From f779a4eeb052cfbe9eeaffd1366c8a4ff3b25703 Mon Sep 17 00:00:00 2001 From: Ian Lynagh Date: Fri, 10 Apr 2009 16:40:13 +0000 Subject: [PATCH] Fix QSem and QSemN: Initial amount must be non-negative --- Control/Concurrent/QSem.hs | 8 +++++--- Control/Concurrent/QSemN.hs | 10 ++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Control/Concurrent/QSem.hs b/Control/Concurrent/QSem.hs index 87f5543..d069b89 100644 --- a/Control/Concurrent/QSem.hs +++ b/Control/Concurrent/QSem.hs @@ -41,9 +41,11 @@ INSTANCE_TYPEABLE0(QSem,qSemTc,"QSem") -- |Build a new 'QSem' newQSem :: Int -> IO QSem -newQSem initial = do - sem <- newMVar (initial, []) - return (QSem sem) +newQSem initial = + if initial < 0 + then fail "newQSem: Initial quantity must be non-negative" + else do sem <- newMVar (initial, []) + return (QSem sem) -- |Wait for a unit to become available waitQSem :: QSem -> IO () diff --git a/Control/Concurrent/QSemN.hs b/Control/Concurrent/QSemN.hs index 014a72c..7ee6142 100644 --- a/Control/Concurrent/QSemN.hs +++ b/Control/Concurrent/QSemN.hs @@ -35,10 +35,12 @@ newtype QSemN = QSemN (MVar (Int,[(Int,MVar ())])) INSTANCE_TYPEABLE0(QSemN,qSemNTc,"QSemN") -- |Build a new 'QSemN' with a supplied initial quantity. -newQSemN :: Int -> IO QSemN -newQSemN initial = do - sem <- newMVar (initial, []) - return (QSemN sem) +newQSemN :: Int -> IO QSemN +newQSemN initial = + if initial < 0 + then fail "newQSemN: Initial quantity must be non-negative" + else do sem <- newMVar (initial, []) + return (QSemN sem) -- |Wait for the specified quantity to become available waitQSemN :: QSemN -> Int -> IO () -- 1.7.10.4