e327827f48c3b8a16bf53f531f0342a9eb03ced3
[ghc-hetmet.git] / ghc / lib / std / PrelConc.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1996
3 %
4
5 \section[PrelConc]{Module @PrelConc@}
6
7 Basic concurrency stuff
8
9 \begin{code}
10 {-# OPTIONS -fno-implicit-prelude #-}
11
12 module PrelConc
13         ( ThreadId(..)
14
15         -- Forking and suchlike
16         , myThreadId    -- :: IO ThreadId
17         , killThread    -- :: ThreadId -> IO ()
18         , raiseInThread -- :: ThreadId -> Exception -> IO ()
19         , par           -- :: a -> b -> b
20         , seq           -- :: a -> b -> b
21         , yield         -- :: IO ()
22
23         -- Waiting
24         , threadDelay           -- :: Int -> IO ()
25         , threadWaitRead        -- :: Int -> IO ()
26         , threadWaitWrite       -- :: Int -> IO ()
27
28         -- MVars
29         , MVar          -- abstract
30         , newMVar       -- :: a -> IO (MVar a)
31         , newEmptyMVar  -- :: IO (MVar a)
32         , takeMVar      -- :: MVar a -> IO a
33         , putMVar       -- :: MVar a -> a -> IO ()
34         , readMVar      -- :: MVar a -> IO a
35         , swapMVar      -- :: MVar a -> a -> IO a
36         , isEmptyMVar   -- :: MVar a -> IO Bool
37
38     ) where
39
40 import PrelBase
41 import PrelErr ( parError, seqError )
42 import PrelST           ( liftST )
43 import PrelIOBase       ( IO(..), MVar(..), unsafePerformIO )
44 import PrelBase         ( Int(..) )
45 import PrelException    ( Exception(..), AsyncException(..) )
46
47 infixr 0 `par`
48 \end{code}
49
50 %************************************************************************
51 %*                                                                      *
52 \subsection{@ThreadId@, @par@, and @fork@}
53 %*                                                                      *
54 %************************************************************************
55
56 \begin{code}
57 data ThreadId = ThreadId ThreadId#
58 -- ToDo: data ThreadId = ThreadId (Weak ThreadId#)
59 -- But since ThreadId# is unlifted, the Weak type must use open
60 -- type variables.
61
62 --forkIO has now been hoisted out into the Concurrent library.
63
64 killThread :: ThreadId -> IO ()
65 killThread (ThreadId id) = IO $ \ s ->
66    case (killThread# id (AsyncException ThreadKilled) s) of s1 -> (# s1, () #)
67
68 raiseInThread :: ThreadId -> Exception -> IO ()
69 raiseInThread (ThreadId id) ex = IO $ \ s ->
70    case (killThread# id ex s) of s1 -> (# s1, () #)
71
72 myThreadId :: IO ThreadId
73 myThreadId = IO $ \s ->
74    case (myThreadId# s) of (# s1, id #) -> (# s1, ThreadId id #)
75
76 yield :: IO ()
77 yield = IO $ \s -> 
78    case (yield# s) of s1 -> (# s1, () #)
79
80 -- "seq" is defined a bit wierdly (see below)
81 --
82 -- The reason for the strange "0# -> parError" case is that
83 -- it fools the compiler into thinking that seq is non-strict in
84 -- its second argument (even if it inlines seq at the call site).
85 -- If it thinks seq is strict in "y", then it often evaluates
86 -- "y" before "x", which is totally wrong.  
87 --
88 -- Just before converting from Core to STG there's a bit of magic
89 -- that recognises the seq# and eliminates the duff case.
90
91 {-# INLINE seq  #-}
92 seq :: a -> b -> b
93 seq  x y = case (seq#  x) of { 0# -> seqError; _ -> y }
94
95 par :: a -> b -> b
96
97 {-# INLINE par  #-}
98 #if defined(__PARALLEL_HASKELL__) || defined (__GRANSIM__)
99 par  x y = case (par# x) of { 0# -> parError; _ -> y }
100 #else
101 par  _ y = y
102 #endif
103 \end{code}
104
105 %************************************************************************
106 %*                                                                      *
107 \subsection[mvars]{M-Structures}
108 %*                                                                      *
109 %************************************************************************
110
111 M-Vars are rendezvous points for concurrent threads.  They begin
112 empty, and any attempt to read an empty M-Var blocks.  When an M-Var
113 is written, a single blocked thread may be freed.  Reading an M-Var
114 toggles its state from full back to empty.  Therefore, any value
115 written to an M-Var may only be read once.  Multiple reads and writes
116 are allowed, but there must be at least one read between any two
117 writes.
118
119 \begin{code}
120 --Defined in IOBase to avoid cycle: data MVar a = MVar (SynchVar# RealWorld a)
121
122 newEmptyMVar  :: IO (MVar a)
123
124 newEmptyMVar = IO $ \ s# ->
125     case newMVar# s# of
126          (# s2#, svar# #) -> (# s2#, MVar svar# #)
127
128 takeMVar :: MVar a -> IO a
129
130 takeMVar (MVar mvar#) = IO $ \ s# -> takeMVar# mvar# s#
131
132 putMVar  :: MVar a -> a -> IO ()
133
134 putMVar (MVar mvar#) x = IO $ \ s# ->
135     case putMVar# mvar# x s# of
136         s2# -> (# s2#, () #)
137
138 newMVar :: a -> IO (MVar a)
139
140 newMVar value =
141     newEmptyMVar        >>= \ mvar ->
142     putMVar mvar value  >>
143     return mvar
144
145 readMVar :: MVar a -> IO a
146
147 readMVar mvar =
148     takeMVar mvar       >>= \ value ->
149     putMVar mvar value  >>
150     return value
151
152 swapMVar :: MVar a -> a -> IO a
153
154 swapMVar mvar new =
155     takeMVar mvar       >>= \ old ->
156     putMVar mvar new    >>
157     return old
158
159 {- 
160  Low-level op. for checking whether an MVar is filled-in or not.
161  Notice that the boolean value returned  is just a snapshot of
162  the state of the MVar. By the time you get to react on its result,
163  the MVar may have been filled (or emptied) - so be extremely
164  careful when using this operation.
165
166  If you can re-work your abstractions to avoid having to
167  depend on isEmptyMVar, then you're encouraged to do so,
168  i.e., consider yourself warned about the imprecision in
169  general of isEmptyMVar :-)
170 -}
171 isEmptyMVar :: MVar a -> IO Bool
172 isEmptyMVar (MVar mv#) = IO $ \ s# -> 
173     case isEmptyMVar# mv# s# of
174         (# s2#, flg #) -> (# s2#, not (flg ==# 0#) #)
175 \end{code}
176
177
178 %************************************************************************
179 %*                                                                      *
180 \subsection{Thread waiting}
181 %*                                                                      *
182 %************************************************************************
183
184 @threadDelay@ delays rescheduling of a thread until the indicated
185 number of microseconds have elapsed.  Generally, the microseconds are
186 counted by the context switch timer, which ticks in virtual time;
187 however, when there are no runnable threads, we don't accumulate any
188 virtual time, so we start ticking in real time.  (The granularity is
189 the effective resolution of the context switch timer, so it is
190 affected by the RTS -C option.)
191
192 @threadWaitRead@ delays rescheduling of a thread until input on the
193 specified file descriptor is available for reading (just like select).
194 @threadWaitWrite@ is similar, but for writing on a file descriptor.
195
196 \begin{code}
197 threadDelay, threadWaitRead, threadWaitWrite :: Int -> IO ()
198
199 threadDelay     (I# ms) = IO $ \s -> case delay# ms s     of s -> (# s, () #)
200 threadWaitRead  (I# fd) = IO $ \s -> case waitRead# fd s  of s -> (# s, () #)
201 threadWaitWrite (I# fd) = IO $ \s -> case waitWrite# fd s of s -> (# s, () #)
202 \end{code}