2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[Concurrent]{Concurrent Haskell constructs}
7 A common interface to a collection of useful concurrency abstractions.
8 Currently, the collection only contains the abstractions found in the
9 {\em Concurrent Haskell} paper (presented at the Haskell Workshop
10 1995, draft available via \tr{ftp} from
11 \tr{ftp.dcs.gla.ac.uk/pub/glasgow-fp/drafts}.) plus a couple of
12 others. See the paper and the individual files containing the module
13 definitions for explanation on what they do.
24 -- Forking and suchlike
25 , forkIO -- :: IO () -> IO ThreadId
26 , myThreadId -- :: IO ThreadId
27 , killThread -- :: ThreadId -> IO ()
28 , raiseInThread -- :: ThreadId -> Exception -> IO ()
29 , par -- :: a -> b -> b
30 , seq -- :: a -> b -> b
31 , fork -- :: a -> b -> b
34 , threadDelay -- :: Int -> IO ()
35 , threadWaitRead -- :: Int -> IO ()
36 , threadWaitWrite -- :: Int -> IO ()
40 , newMVar -- :: a -> IO (MVar a)
41 , newEmptyMVar -- :: IO (MVar a)
42 , takeMVar -- :: MVar a -> IO a
43 , putMVar -- :: MVar a -> a -> IO ()
44 , readMVar -- :: MVar a -> IO a
45 , swapMVar -- :: MVar a -> a -> IO a
46 , isEmptyMVar -- :: MVar a -> IO Bool
49 , mergeIO -- :: [a] -> [a] -> IO [a]
50 , nmergeIO -- :: [[a]] -> IO [a]
59 import PrelHandle ( topHandler )
61 import PrelIOBase ( IO(..) )
63 import PrelAddr ( Addr )
64 import PrelArr ( ByteArray )
65 import PrelPack ( packString )
66 import PrelIOBase ( unsafePerformIO , unsafeInterleaveIO )
67 import PrelBase ( fork# )
68 import PrelGHC ( Addr#, unsafeCoerce# )
73 Thread Ids, specifically the instances of Eq and Ord for these things.
74 The ThreadId type itself is defined in std/PrelConc.lhs.
76 Rather than define a new primitve, we use a little helper function
77 cmp_thread in the RTS.
80 foreign import ccall "cmp_thread" unsafe cmp_thread :: Addr# -> Addr# -> Int
83 cmpThread :: ThreadId -> ThreadId -> Ordering
84 cmpThread (ThreadId t1) (ThreadId t2) =
85 case cmp_thread (unsafeCoerce# t1) (unsafeCoerce# t2) of
90 instance Eq ThreadId where
92 case t1 `cmpThread` t2 of
96 instance Ord ThreadId where
101 forkIO :: IO () -> IO ThreadId
102 forkIO action = IO $ \ s ->
103 case (fork# action_plus s) of (# s1, id #) -> (# s1, ThreadId id #)
106 catchException action
107 (topHandler False{-don't quit on exception raised-})
111 fork x y = unsafePerformIO (forkIO (x `seq` return ())) `seq` y
119 mergeIO :: [a] -> [a] -> IO [a]
120 nmergeIO :: [[a]] -> IO [a]
123 = newEmptyMVar >>= \ tail_node ->
124 newMVar tail_node >>= \ tail_list ->
125 newQSem max_buff_size >>= \ e ->
126 newMVar 2 >>= \ branches_running ->
130 forkIO (suckIO branches_running buff ls) >>
131 forkIO (suckIO branches_running buff rs) >>
132 takeMVar tail_node >>= \ val ->
137 = (MVar (MVar [a]), QSem)
139 suckIO :: MVar Int -> Buffer a -> [a] -> IO ()
141 suckIO branches_running buff@(tail_list,e) vs
143 [] -> takeMVar branches_running >>= \ val ->
145 takeMVar tail_list >>= \ node ->
147 putMVar tail_list node
149 putMVar branches_running (val-1)
152 takeMVar tail_list >>= \ node ->
153 newEmptyMVar >>= \ next_node ->
155 takeMVar next_node >>= \ x ->
157 return x) >>= \ next_node_val ->
158 putMVar node (x:next_node_val) >>
159 putMVar tail_list next_node >>
160 suckIO branches_running buff xs
166 newEmptyMVar >>= \ tail_node ->
167 newMVar tail_node >>= \ tail_list ->
168 newQSem max_buff_size >>= \ e ->
169 newMVar len >>= \ branches_running ->
173 mapIO (\ x -> forkIO (suckIO branches_running buff x)) lss >>
174 takeMVar tail_node >>= \ val ->
178 mapIO f xs = sequence (map f xs)