Basic concurrency stuff
\begin{code}
-{-# OPTIONS -fno-implicit-prelude #-}
+{-# OPTIONS -fcompiling-prelude -fno-implicit-prelude #-}
module PrelConc
( ThreadId(..)
, seq -- :: a -> b -> b
, yield -- :: IO ()
- {-threadDelay, threadWaitRead, threadWaitWrite,-}
+ -- Waiting
+ , threadDelay -- :: Int -> IO ()
+ , threadWaitRead -- :: Int -> IO ()
+ , threadWaitWrite -- :: Int -> IO ()
-- MVars
, MVar -- abstract
, putMVar -- :: MVar a -> a -> IO ()
, readMVar -- :: MVar a -> IO a
, swapMVar -- :: MVar a -> a -> IO a
+ , takeMaybeMVar -- :: MVar a -> IO (Maybe a)
, isEmptyMVar -- :: MVar a -> IO Bool
) where
import PrelBase
+import PrelMaybe
import PrelErr ( parError, seqError )
import PrelST ( liftST )
import PrelIOBase ( IO(..), MVar(..), unsafePerformIO )
import PrelBase ( Int(..) )
import PrelException ( Exception(..), AsyncException(..) )
-infixr 0 `par`
+infixr 0 `par`, `seq`
\end{code}
%************************************************************************
-- But since ThreadId# is unlifted, the Weak type must use open
-- type variables.
---forkIO has now been hoisted out into the concurrent library.
+--forkIO has now been hoisted out into the Concurrent library.
killThread :: ThreadId -> IO ()
killThread (ThreadId id) = IO $ \ s ->
seq :: a -> b -> b
seq x y = case (seq# x) of { 0# -> seqError; _ -> y }
-par :: a -> b -> b
-
{-# INLINE par #-}
-#if defined(__PARALLEL_HASKELL__) || defined (__GRANSIM__)
+par :: a -> b -> b
par x y = case (par# x) of { 0# -> parError; _ -> y }
-#else
-par _ y = y
-#endif
-
\end{code}
%************************************************************************
\begin{code}
--Defined in IOBase to avoid cycle: data MVar a = MVar (SynchVar# RealWorld a)
-instance Eq (MVar a) where
- (MVar mvar1#) == (MVar mvar2#) = sameMVar# mvar1# mvar2#
-
newEmptyMVar :: IO (MVar a)
-
newEmptyMVar = IO $ \ s# ->
case newMVar# s# of
(# s2#, svar# #) -> (# s2#, MVar svar# #)
takeMVar :: MVar a -> IO a
-
takeMVar (MVar mvar#) = IO $ \ s# -> takeMVar# mvar# s#
putMVar :: MVar a -> a -> IO ()
-
putMVar (MVar mvar#) x = IO $ \ s# ->
case putMVar# mvar# x s# of
s2# -> (# s2#, () #)
newMVar :: a -> IO (MVar a)
-
newMVar value =
newEmptyMVar >>= \ mvar ->
putMVar mvar value >>
return mvar
readMVar :: MVar a -> IO a
-
readMVar mvar =
takeMVar mvar >>= \ value ->
putMVar mvar value >>
return value
swapMVar :: MVar a -> a -> IO a
-
swapMVar mvar new =
takeMVar mvar >>= \ old ->
putMVar mvar new >>
return old
+-- takeMaybeMVar is a non-blocking takeMVar
+takeMaybeMVar :: MVar a -> IO (Maybe a)
+takeMaybeMVar (MVar m) = IO $ \ s ->
+ case takeMaybeMVar# m s of
+ (# s, 0#, _ #) -> (# s, Nothing #) -- MVar is empty
+ (# s, _, a #) -> (# s, Just a #) -- MVar is full
+
{-
Low-level op. for checking whether an MVar is filled-in or not.
Notice that the boolean value returned is just a snapshot of
the state of the MVar. By the time you get to react on its result,
the MVar may have been filled (or emptied) - so be extremely
- careful when using this operation.
+ careful when using this operation.
+
+ Use takeMaybeMVar instead if possible.
If you can re-work your abstractions to avoid having to
depend on isEmptyMVar, then you're encouraged to do so,
@threadWaitWrite@ is similar, but for writing on a file descriptor.
\begin{code}
-{- Not yet -- SDM
threadDelay, threadWaitRead, threadWaitWrite :: Int -> IO ()
-threadDelay (I# x#) = IO $ \ s# ->
- case delay# x# s# of
- s2# -> (# s2#, () #)
-
-threadWaitRead (I# x#) = IO $ \ s# ->
- case waitRead# x# s# of
- s2# -> (# s2#, () #)
-
-threadWaitWrite (I# x#) = IO $ \ s# ->
- case waitWrite# x# s# of
- s2# -> (# s2#, () #)
--}
+threadDelay (I# ms) = IO $ \s -> case delay# ms s of s -> (# s, () #)
+threadWaitRead (I# fd) = IO $ \s -> case waitRead# fd s of s -> (# s, () #)
+threadWaitWrite (I# fd) = IO $ \s -> case waitWrite# fd s of s -> (# s, () #)
\end{code}