X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Control%2FConcurrent%2FSampleVar.hs;h=ca68a38f76714e7d9d1761411ff68c7e9df4123f;hb=41e8fba828acbae1751628af50849f5352b27873;hp=ad89a953e0b8890c6bc3a1f5cb7cf9834c99abc0;hpb=4799550f7f8fc40eed4d11203a8a1eb83a2a5d2e;p=ghc-base.git diff --git a/Control/Concurrent/SampleVar.hs b/Control/Concurrent/SampleVar.hs index ad89a95..ca68a38 100644 --- a/Control/Concurrent/SampleVar.hs +++ b/Control/Concurrent/SampleVar.hs @@ -1,3 +1,5 @@ +{-# LANGUAGE CPP #-} + ----------------------------------------------------------------------------- -- | -- Module : Control.Concurrent.SampleVar @@ -30,10 +32,14 @@ import Prelude import Control.Concurrent.MVar -import Control.Exception ( block ) +import Control.Exception ( mask_ ) import Data.Functor ( (<$>) ) +import Data.Typeable + +#include "Typeable.h" + -- | -- Sample variables are slightly different from a normal 'MVar': -- @@ -58,6 +64,8 @@ newtype SampleVar a = SampleVar ( MVar ( Int -- 1 == full ) deriving (Eq) +INSTANCE_TYPEABLE1(SampleVar,sampleVarTc,"SampleVar") + -- |Build a new, empty, 'SampleVar' newEmptySampleVar :: IO (SampleVar a) newEmptySampleVar = do @@ -72,8 +80,8 @@ newSampleVar a = do -- |If the SampleVar is full, leave it empty. Otherwise, do nothing. emptySampleVar :: SampleVar a -> IO () -emptySampleVar (SampleVar v) = block $ do - s@(readers, var) <- block $ takeMVar v +emptySampleVar (SampleVar v) = mask_ $ do + s@(readers, var) <- takeMVar v if readers > 0 then do _ <- takeMVar var putMVar v (0,var) @@ -82,7 +90,7 @@ emptySampleVar (SampleVar v) = block $ do -- |Wait for a value to become available, then take it and return. readSampleVar :: SampleVar a -> IO a -readSampleVar (SampleVar svar) = block $ do +readSampleVar (SampleVar svar) = mask_ $ do -- -- filled => make empty and grab sample -- not filled => try to grab value, empty when read val. @@ -95,7 +103,7 @@ readSampleVar (SampleVar svar) = block $ do -- |Write a value into the 'SampleVar', overwriting any previous value that -- was there. writeSampleVar :: SampleVar a -> a -> IO () -writeSampleVar (SampleVar svar) v = block $ do +writeSampleVar (SampleVar svar) v = mask_ $ do -- -- filled => overwrite -- not filled => fill, write val @@ -120,5 +128,5 @@ writeSampleVar (SampleVar svar) v = block $ do isEmptySampleVar :: SampleVar a -> IO Bool isEmptySampleVar (SampleVar svar) = do (readers, _) <- readMVar svar - return (readers == 0) + return (readers <= 0)