Fix QSem and QSemN: Initial amount must be non-negative
authorIan Lynagh <igloo@earth.li>
Fri, 10 Apr 2009 16:40:13 +0000 (16:40 +0000)
committerIan Lynagh <igloo@earth.li>
Fri, 10 Apr 2009 16:40:13 +0000 (16:40 +0000)
Control/Concurrent/QSem.hs
Control/Concurrent/QSemN.hs

index 87f5543..d069b89 100644 (file)
@@ -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 ()
index 014a72c..7ee6142 100644 (file)
@@ -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 ()