2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[PrelConc]{Module @PrelConc@}
7 Basic concurrency stuff
10 {-# OPTIONS -fno-implicit-prelude #-}
17 -- Forking and suchlike
21 {-threadDelay, threadWaitRead, threadWaitWrite, -}
24 MVar, newMVar, newEmptyMVar, takeMVar, putMVar, readMVar, swapMVar
28 import {-# SOURCE #-} PrelErr ( parError )
29 import PrelST ( ST(..), STret(..), liftST )
30 import PrelIOBase ( IO(..), MVar(..), liftIO, unsafePerformIO )
31 import PrelErr ( parError )
32 import PrelBase ( Int(..) )
33 import PrelErr ( seqError )
35 infixr 0 `par`, `fork`
38 %************************************************************************
40 \subsection{@ThreadId@, @par@, and @fork@}
42 %************************************************************************
45 data ThreadId = ThreadId ThreadId#
46 -- ToDo: data ThreadId = ThreadId (WeakPair ThreadId# ())
47 -- But since ThreadId# is unlifted, the WeakPair type must use open
50 forkIO :: IO () -> IO ThreadId
51 forkIO action = IO $ \ s ->
52 case (fork# action s) of (# s, id #) -> (# s, ThreadId id #)
54 killThread :: ThreadId -> IO ()
55 killThread (ThreadId id) = IO $ \ s ->
56 case (killThread# id s) of s -> (# s, () #)
58 -- "seq" is defined a bit wierdly (see below)
60 -- The reason for the strange "0# -> parError" case is that
61 -- it fools the compiler into thinking that seq is non-strict in
62 -- its second argument (even if it inlines seq at the call site).
63 -- If it thinks seq is strict in "y", then it often evaluates
64 -- "y" before "x", which is totally wrong.
66 -- Just before converting from Core to STG there's a bit of magic
67 -- that recognises the seq# and eliminates the duff case.
71 seq x y = case (seq# x) of { 0# -> seqError; _ -> y }
73 par, fork :: a -> b -> b
77 #if defined(__PARALLEL_HASKELL__) || defined (__GRANSIM__)
78 par x y = case (par# x) of { 0# -> parError; _ -> y }
83 fork x y = unsafePerformIO (forkIO (x `seq` return ())) `seq` y
87 %************************************************************************
89 \subsection[mvars]{M-Structures}
91 %************************************************************************
93 M-Vars are rendezvous points for concurrent threads. They begin
94 empty, and any attempt to read an empty M-Var blocks. When an M-Var
95 is written, a single blocked thread may be freed. Reading an M-Var
96 toggles its state from full back to empty. Therefore, any value
97 written to an M-Var may only be read once. Multiple reads and writes
98 are allowed, but there must be at least one read between any two
102 --Defined in IOBase to avoid cycle: data MVar a = MVar (SynchVar# RealWorld a)
104 instance Eq (MVar a) where
105 (MVar mvar1#) == (MVar mvar2#) = sameMVar# mvar1# mvar2#
107 newEmptyMVar :: IO (MVar a)
109 newEmptyMVar = IO $ \ s# ->
111 (# s2#, svar# #) -> (# s2#, MVar svar# #)
113 takeMVar :: MVar a -> IO a
115 takeMVar (MVar mvar#) = IO $ \ s# -> takeMVar# mvar# s#
117 putMVar :: MVar a -> a -> IO ()
119 putMVar (MVar mvar#) x = IO $ \ s# ->
120 case putMVar# mvar# x s# of
123 newMVar :: a -> IO (MVar a)
126 newEmptyMVar >>= \ mvar ->
127 putMVar mvar value >>
130 readMVar :: MVar a -> IO a
133 takeMVar mvar >>= \ value ->
134 putMVar mvar value >>
137 swapMVar :: MVar a -> a -> IO a
140 takeMVar mvar >>= \ old ->
146 %************************************************************************
148 \subsection{Thread waiting}
150 %************************************************************************
152 @threadDelay@ delays rescheduling of a thread until the indicated
153 number of microseconds have elapsed. Generally, the microseconds are
154 counted by the context switch timer, which ticks in virtual time;
155 however, when there are no runnable threads, we don't accumulate any
156 virtual time, so we start ticking in real time. (The granularity is
157 the effective resolution of the context switch timer, so it is
158 affected by the RTS -C option.)
160 @threadWaitRead@ delays rescheduling of a thread until input on the
161 specified file descriptor is available for reading (just like select).
162 @threadWaitWrite@ is similar, but for writing on a file descriptor.
166 threadDelay, threadWaitRead, threadWaitWrite :: Int -> IO ()
168 threadDelay (I# x#) = IO $ \ s# ->
172 threadWaitRead (I# x#) = IO $ \ s# ->
173 case waitRead# x# s# of
176 threadWaitWrite (I# x#) = IO $ \ s# ->
177 case waitWrite# x# s# of