1 {-# OPTIONS_GHC -fno-warn-unused-imports #-}
2 -----------------------------------------------------------------------------
4 -- Module : Control.Concurrent
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : experimental
10 -- Portability : non-portable (concurrency)
12 -- A common interface to a collection of useful concurrency
15 -----------------------------------------------------------------------------
17 module Control.Concurrent (
18 -- * Concurrent Haskell
22 -- * Basic concurrency operations
25 #ifdef __GLASGOW_HASKELL__
30 #ifdef __GLASGOW_HASKELL__
44 #ifdef __GLASGOW_HASKELL__
46 threadDelay, -- :: Int -> IO ()
47 threadWaitRead, -- :: Int -> IO ()
48 threadWaitWrite, -- :: Int -> IO ()
51 -- * Communication abstractions
53 module Control.Concurrent.MVar,
54 module Control.Concurrent.Chan,
55 module Control.Concurrent.QSem,
56 module Control.Concurrent.QSemN,
57 module Control.Concurrent.SampleVar,
59 -- * Merging of streams
61 mergeIO, -- :: [a] -> [a] -> IO [a]
62 nmergeIO, -- :: [[a]] -> IO [a]
66 #ifdef __GLASGOW_HASKELL__
69 rtsSupportsBoundThreads,
76 -- * GHC's implementation of concurrency
78 -- |This section describes features specific to GHC's
79 -- implementation of Concurrent Haskell.
81 -- ** Haskell threads and Operating System threads
85 -- ** Terminating the program
96 import Control.Exception.Base as Exception
98 #ifdef __GLASGOW_HASKELL__
100 import GHC.Conc ( ThreadId(..), myThreadId, killThread, yield,
101 threadDelay, forkIO, childHandler )
102 import qualified GHC.Conc
103 import GHC.IOBase ( IO(..) )
104 import GHC.IOBase ( unsafeInterleaveIO )
105 import GHC.IOBase ( newIORef, readIORef, writeIORef )
108 import System.Posix.Types ( Fd )
109 import Foreign.StablePtr
110 import Foreign.C.Types ( CInt )
111 import Control.Monad ( when )
113 #ifdef mingw32_HOST_OS
124 import Control.Concurrent.MVar
125 import Control.Concurrent.Chan
126 import Control.Concurrent.QSem
127 import Control.Concurrent.QSemN
128 import Control.Concurrent.SampleVar
136 The concurrency extension for Haskell is described in the paper
138 <http://www.haskell.org/ghc/docs/papers/concurrent-haskell.ps.gz>.
140 Concurrency is \"lightweight\", which means that both thread creation
141 and context switching overheads are extremely low. Scheduling of
142 Haskell threads is done internally in the Haskell runtime system, and
143 doesn't make use of any operating system-supplied thread packages.
145 However, if you want to interact with a foreign library that expects your
146 program to use the operating system-supplied thread package, you can do so
147 by using 'forkOS' instead of 'forkIO'.
149 Haskell threads can communicate via 'MVar's, a kind of synchronised
150 mutable variable (see "Control.Concurrent.MVar"). Several common
151 concurrency abstractions can be built from 'MVar's, and these are
152 provided by the "Control.Concurrent" library.
153 In GHC, threads may also communicate via exceptions.
158 Scheduling may be either pre-emptive or co-operative,
159 depending on the implementation of Concurrent Haskell (see below
160 for information related to specific compilers). In a co-operative
161 system, context switches only occur when you use one of the
162 primitives defined in this module. This means that programs such
166 > main = forkIO (write 'a') >> write 'b'
167 > where write c = putChar c >> write c
169 will print either @aaaaaaaaaaaaaa...@ or @bbbbbbbbbbbb...@,
170 instead of some random interleaving of @a@s and @b@s. In
171 practice, cooperative multitasking is sufficient for writing
172 simple graphical user interfaces.
176 Different Haskell implementations have different characteristics with
177 regard to which operations block /all/ threads.
179 Using GHC without the @-threaded@ option, all foreign calls will block
180 all other Haskell threads in the system, although I\/O operations will
181 not. With the @-threaded@ option, only foreign calls with the @unsafe@
182 attribute will block all other threads.
184 Using Hugs, all I\/O operations and foreign calls will block all other
192 mergeIO :: [a] -> [a] -> IO [a]
193 nmergeIO :: [[a]] -> IO [a]
196 -- The 'mergeIO' and 'nmergeIO' functions fork one thread for each
197 -- input list that concurrently evaluates that list; the results are
198 -- merged into a single output list.
200 -- Note: Hugs does not provide these functions, since they require
201 -- preemptive multitasking.
204 = newEmptyMVar >>= \ tail_node ->
205 newMVar tail_node >>= \ tail_list ->
206 newQSem max_buff_size >>= \ e ->
207 newMVar 2 >>= \ branches_running ->
211 forkIO (suckIO branches_running buff ls) >>
212 forkIO (suckIO branches_running buff rs) >>
213 takeMVar tail_node >>= \ val ->
218 = (MVar (MVar [a]), QSem)
220 suckIO :: MVar Int -> Buffer a -> [a] -> IO ()
222 suckIO branches_running buff@(tail_list,e) vs
224 [] -> takeMVar branches_running >>= \ val ->
226 takeMVar tail_list >>= \ node ->
228 putMVar tail_list node
230 putMVar branches_running (val-1)
233 takeMVar tail_list >>= \ node ->
234 newEmptyMVar >>= \ next_node ->
236 takeMVar next_node >>= \ y ->
238 return y) >>= \ next_node_val ->
239 putMVar node (x:next_node_val) >>
240 putMVar tail_list next_node >>
241 suckIO branches_running buff xs
247 newEmptyMVar >>= \ tail_node ->
248 newMVar tail_node >>= \ tail_list ->
249 newQSem max_buff_size >>= \ e ->
250 newMVar len >>= \ branches_running ->
254 mapIO (\ x -> forkIO (suckIO branches_running buff x)) lss >>
255 takeMVar tail_node >>= \ val ->
259 mapIO f xs = sequence (map f xs)
260 #endif /* __HUGS__ */
262 #ifdef __GLASGOW_HASKELL__
263 -- ---------------------------------------------------------------------------
269 Support for multiple operating system threads and bound threads as described
270 below is currently only available in the GHC runtime system if you use the
271 /-threaded/ option when linking.
273 Other Haskell systems do not currently support multiple operating system threads.
275 A bound thread is a haskell thread that is /bound/ to an operating system
276 thread. While the bound thread is still scheduled by the Haskell run-time
277 system, the operating system thread takes care of all the foreign calls made
280 To a foreign library, the bound thread will look exactly like an ordinary
281 operating system thread created using OS functions like @pthread_create@
284 Bound threads can be created using the 'forkOS' function below. All foreign
285 exported functions are run in a bound thread (bound to the OS thread that
286 called the function). Also, the @main@ action of every Haskell program is
287 run in a bound thread.
289 Why do we need this? Because if a foreign library is called from a thread
290 created using 'forkIO', it won't have access to any /thread-local state/ -
291 state variables that have specific values for each OS thread
292 (see POSIX's @pthread_key_create@ or Win32's @TlsAlloc@). Therefore, some
293 libraries (OpenGL, for example) will not work from a thread created using
294 'forkIO'. They work fine in threads created using 'forkOS' or when called
295 from @main@ or from a @foreign export@.
297 In terms of performance, 'forkOS' (aka bound) threads are much more
298 expensive than 'forkIO' (aka unbound) threads, because a 'forkOS'
299 thread is tied to a particular OS thread, whereas a 'forkIO' thread
300 can be run by any OS thread. Context-switching between a 'forkOS'
301 thread and a 'forkIO' thread is many times more expensive than between
302 two 'forkIO' threads.
304 Note in particular that the main program thread (the thread running
305 @Main.main@) is always a bound thread, so for good concurrency
306 performance you should ensure that the main thread is not doing
307 repeated communication with other threads in the system. Typically
308 this means forking subthreads to do the work using 'forkIO', and
309 waiting for the results in the main thread.
313 -- | 'True' if bound threads are supported.
314 -- If @rtsSupportsBoundThreads@ is 'False', 'isCurrentThreadBound'
315 -- will always return 'False' and both 'forkOS' and 'runInBoundThread' will
317 foreign import ccall rtsSupportsBoundThreads :: Bool
321 Like 'forkIO', this sparks off a new thread to run the 'IO'
322 computation passed as the first argument, and returns the 'ThreadId'
323 of the newly created thread.
325 However, 'forkOS' creates a /bound/ thread, which is necessary if you
326 need to call foreign (non-Haskell) libraries that make use of
327 thread-local state, such as OpenGL (see "Control.Concurrent#boundthreads").
329 Using 'forkOS' instead of 'forkIO' makes no difference at all to the
330 scheduling behaviour of the Haskell runtime system. It is a common
331 misconception that you need to use 'forkOS' instead of 'forkIO' to
332 avoid blocking all the Haskell threads when making a foreign call;
333 this isn't the case. To allow foreign calls to be made without
334 blocking all the Haskell threads (with GHC), it is only necessary to
335 use the @-threaded@ option when linking your program, and to make sure
336 the foreign import is not marked @unsafe@.
339 forkOS :: IO () -> IO ThreadId
341 foreign export ccall forkOS_entry
342 :: StablePtr (IO ()) -> IO ()
344 foreign import ccall "forkOS_entry" forkOS_entry_reimported
345 :: StablePtr (IO ()) -> IO ()
347 forkOS_entry :: StablePtr (IO ()) -> IO ()
348 forkOS_entry stableAction = do
349 action <- deRefStablePtr stableAction
352 foreign import ccall forkOS_createThread
353 :: StablePtr (IO ()) -> IO CInt
355 failNonThreaded :: IO a
356 failNonThreaded = fail $ "RTS doesn't support multiple OS threads "
357 ++"(use ghc -threaded when linking)"
360 | rtsSupportsBoundThreads = do
362 b <- Exception.blocked
364 -- async exceptions are blocked in the child if they are blocked
365 -- in the parent, as for forkIO (see #1048). forkOS_createThread
366 -- creates a thread with exceptions blocked by default.
367 action1 | b = action0
368 | otherwise = unblock action0
370 action_plus = Exception.catch action1 childHandler
372 entry <- newStablePtr (myThreadId >>= putMVar mv >> action_plus)
373 err <- forkOS_createThread entry
374 when (err /= 0) $ fail "Cannot create OS thread."
378 | otherwise = failNonThreaded
380 -- | Returns 'True' if the calling thread is /bound/, that is, if it is
381 -- safe to use foreign libraries that rely on thread-local state from the
383 isCurrentThreadBound :: IO Bool
384 isCurrentThreadBound = IO $ \ s# ->
385 case isCurrentThreadBound# s# of
386 (# s2#, flg #) -> (# s2#, not (flg ==# 0#) #)
390 Run the 'IO' computation passed as the first argument. If the calling thread
391 is not /bound/, a bound thread is created temporarily. @runInBoundThread@
392 doesn't finish until the 'IO' computation finishes.
394 You can wrap a series of foreign function calls that rely on thread-local state
395 with @runInBoundThread@ so that you can use them without knowing whether the
396 current thread is /bound/.
398 runInBoundThread :: IO a -> IO a
400 runInBoundThread action
401 | rtsSupportsBoundThreads = do
402 bound <- isCurrentThreadBound
406 ref <- newIORef undefined
407 let action_plus = Exception.try action >>= writeIORef ref
409 bracket (newStablePtr action_plus)
411 (\cEntry -> forkOS_entry_reimported cEntry >> readIORef ref)
412 case resultOrException of
413 Left exception -> Exception.throw (exception :: SomeException)
414 Right result -> return result
415 | otherwise = failNonThreaded
418 Run the 'IO' computation passed as the first argument. If the calling thread
419 is /bound/, an unbound thread is created temporarily using 'forkIO'.
420 @runInBoundThread@ doesn't finish until the 'IO' computation finishes.
422 Use this function /only/ in the rare case that you have actually observed a
423 performance loss due to the use of bound threads. A program that
424 doesn't need it's main thread to be bound and makes /heavy/ use of concurrency
425 (e.g. a web server), might want to wrap it's @main@ action in
426 @runInUnboundThread@.
428 runInUnboundThread :: IO a -> IO a
430 runInUnboundThread action = do
431 bound <- isCurrentThreadBound
435 forkIO (Exception.try action >>= putMVar mv)
436 takeMVar mv >>= \ei -> case ei of
437 Left exception -> Exception.throw (exception :: SomeException)
438 Right result -> return result
441 #endif /* __GLASGOW_HASKELL__ */
443 #ifdef __GLASGOW_HASKELL__
444 -- ---------------------------------------------------------------------------
445 -- threadWaitRead/threadWaitWrite
447 -- | Block the current thread until data is available to read on the
448 -- given file descriptor (GHC only).
449 threadWaitRead :: Fd -> IO ()
451 #ifdef mingw32_HOST_OS
452 -- we have no IO manager implementing threadWaitRead on Windows.
453 -- fdReady does the right thing, but we have to call it in a
454 -- separate thread, otherwise threadWaitRead won't be interruptible,
455 -- and this only works with -threaded.
456 | threaded = withThread (waitFd fd 0)
457 | otherwise = case fd of
458 0 -> do hWaitForInput stdin (-1); return ()
459 -- hWaitForInput does work properly, but we can only
460 -- do this for stdin since we know its FD.
461 _ -> error "threadWaitRead requires -threaded on Windows, or use System.IO.hWaitForInput"
463 = GHC.Conc.threadWaitRead fd
466 -- | Block the current thread until data can be written to the
467 -- given file descriptor (GHC only).
468 threadWaitWrite :: Fd -> IO ()
470 #ifdef mingw32_HOST_OS
471 | threaded = withThread (waitFd fd 1)
472 | otherwise = error "threadWaitWrite requires -threaded on Windows"
474 = GHC.Conc.threadWaitWrite fd
477 #ifdef mingw32_HOST_OS
478 foreign import ccall unsafe "rtsSupportsBoundThreads" threaded :: Bool
480 withThread :: IO a -> IO a
483 forkIO $ try io >>= putMVar m
487 Left e -> throwIO (e :: IOException)
489 waitFd :: Fd -> CInt -> IO ()
491 throwErrnoIfMinus1 "fdReady" $
492 fdReady (fromIntegral fd) write (fromIntegral iNFINITE) 0
496 iNFINITE = 0xFFFFFFFF -- urgh
498 foreign import ccall safe "fdReady"
499 fdReady :: CInt -> CInt -> CInt -> CInt -> IO CInt
502 -- ---------------------------------------------------------------------------
507 #osthreads# In GHC, threads created by 'forkIO' are lightweight threads, and
508 are managed entirely by the GHC runtime. Typically Haskell
509 threads are an order of magnitude or two more efficient (in
510 terms of both time and space) than operating system threads.
512 The downside of having lightweight threads is that only one can
513 run at a time, so if one thread blocks in a foreign call, for
514 example, the other threads cannot continue. The GHC runtime
515 works around this by making use of full OS threads where
516 necessary. When the program is built with the @-threaded@
517 option (to link against the multithreaded version of the
518 runtime), a thread making a @safe@ foreign call will not block
519 the other threads in the system; another OS thread will take
520 over running Haskell threads until the original call returns.
521 The runtime maintains a pool of these /worker/ threads so that
522 multiple Haskell threads can be involved in external calls
525 The "System.IO" library manages multiplexing in its own way. On
526 Windows systems it uses @safe@ foreign calls to ensure that
527 threads doing I\/O operations don't block the whole runtime,
528 whereas on Unix systems all the currently blocked I\/O reqwests
529 are managed by a single thread (the /IO manager thread/) using
532 The runtime will run a Haskell thread using any of the available
533 worker OS threads. If you need control over which particular OS
534 thread is used to run a given Haskell thread, perhaps because
535 you need to call a foreign library that uses OS-thread-local
536 state, then you need bound threads (see "Control.Concurrent#boundthreads").
538 If you don't use the @-threaded@ option, then the runtime does
539 not make use of multiple OS threads. Foreign calls will block
540 all other running Haskell threads until the call returns. The
541 "System.IO" library still does multiplexing, so there can be multiple
542 threads doing I\/O, and this is handled internally by the runtime using
548 In a standalone GHC program, only the main thread is
549 required to terminate in order for the process to terminate.
550 Thus all other forked threads will simply terminate at the same
551 time as the main thread (the terminology for this kind of
552 behaviour is \"daemonic threads\").
554 If you want the program to wait for child threads to
555 finish before exiting, you need to program this yourself. A
556 simple mechanism is to have each child thread write to an
557 'MVar' when it completes, and have the main
558 thread wait on all the 'MVar's before
561 > myForkIO :: IO () -> IO (MVar ())
563 > mvar <- newEmptyMVar
564 > forkIO (io `finally` putMVar mvar ())
567 Note that we use 'finally' from the
568 "Control.Exception" module to make sure that the
569 'MVar' is written to even if the thread dies or
570 is killed for some reason.
572 A better method is to keep a global list of all child
573 threads which we should wait for at the end of the program:
575 > children :: MVar [MVar ()]
576 > children = unsafePerformIO (newMVar [])
578 > waitForChildren :: IO ()
579 > waitForChildren = do
580 > cs <- takeMVar children
584 > putMVar children ms
588 > forkChild :: IO () -> IO ThreadId
590 > mvar <- newEmptyMVar
591 > childs <- takeMVar children
592 > putMVar children (mvar:childs)
593 > forkIO (io `finally` putMVar mvar ())
596 > later waitForChildren $
599 The main thread principle also applies to calls to Haskell from
600 outside, using @foreign export@. When the @foreign export@ed
601 function is invoked, it starts a new main thread, and it returns
602 when this main thread terminates. If the call causes new
603 threads to be forked, they may remain in the system after the
604 @foreign export@ed function has returned.
609 GHC implements pre-emptive multitasking: the execution of
610 threads are interleaved in a random fashion. More specifically,
611 a thread may be pre-empted whenever it allocates some memory,
612 which unfortunately means that tight loops which do no
613 allocation tend to lock out other threads (this only seems to
614 happen with pathological benchmark-style code, however).
616 The rescheduling timer runs on a 20ms granularity by
617 default, but this may be altered using the
618 @-i\<n\>@ RTS option. After a rescheduling
619 \"tick\" the running thread is pre-empted as soon as
623 @aaaa@ @bbbb@ example may not
624 work too well on GHC (see Scheduling, above), due
625 to the locking on a 'System.IO.Handle'. Only one thread
626 may hold the lock on a 'System.IO.Handle' at any one
627 time, so if a reschedule happens while a thread is holding the
628 lock, the other thread won't be able to run. The upshot is that
629 the switch from @aaaa@ to
630 @bbbbb@ happens infrequently. It can be
631 improved by lowering the reschedule tick period. We also have a
632 patch that causes a reschedule whenever a thread waiting on a
633 lock is woken up, but haven't found it to be useful for anything
634 other than this example :-)
636 #endif /* __GLASGOW_HASKELL__ */