25671bf1d8ada2e09ba39b65f5e6702fd293e134
[packages/base.git] / Control / Concurrent.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP
3 , ForeignFunctionInterface
4 , MagicHash
5 , UnboxedTuples
6 , ScopedTypeVariables
7 #-}
8 {-# OPTIONS_GHC -fno-warn-unused-imports #-}
9 {-# OPTIONS_GHC -fno-warn-deprecations #-}
10 -- kludge for the Control.Concurrent.QSem, Control.Concurrent.QSemN
11 -- and Control.Concurrent.SampleVar imports.
12
13 -----------------------------------------------------------------------------
14 -- |
15 -- Module : Control.Concurrent
16 -- Copyright : (c) The University of Glasgow 2001
17 -- License : BSD-style (see the file libraries/base/LICENSE)
18 --
19 -- Maintainer : libraries@haskell.org
20 -- Stability : experimental
21 -- Portability : non-portable (concurrency)
22 --
23 -- A common interface to a collection of useful concurrency
24 -- abstractions.
25 --
26 -----------------------------------------------------------------------------
27
28 module Control.Concurrent (
29 -- * Concurrent Haskell
30
31 -- $conc_intro
32
33 -- * Basic concurrency operations
34
35 ThreadId,
36 myThreadId,
37
38 forkIO,
39 forkFinally,
40 forkIOWithUnmask,
41 killThread,
42 throwTo,
43
44 -- ** Threads with affinity
45 forkOn,
46 forkOnWithUnmask,
47 getNumCapabilities,
48 setNumCapabilities,
49 threadCapability,
50
51 -- * Scheduling
52
53 -- $conc_scheduling
54 yield,
55
56 -- ** Blocking
57
58 -- $blocking
59
60 -- ** Waiting
61 threadDelay,
62 threadWaitRead,
63 threadWaitWrite,
64 threadWaitReadSTM,
65 threadWaitWriteSTM,
66
67 -- * Communication abstractions
68
69 module Control.Concurrent.MVar,
70 module Control.Concurrent.Chan,
71 module Control.Concurrent.QSem,
72 module Control.Concurrent.QSemN,
73
74 -- * Bound Threads
75 -- $boundthreads
76 rtsSupportsBoundThreads,
77 forkOS,
78 isCurrentThreadBound,
79 runInBoundThread,
80 runInUnboundThread,
81
82 -- * Weak references to ThreadIds
83 mkWeakThreadId,
84
85 -- * GHC's implementation of concurrency
86
87 -- |This section describes features specific to GHC's
88 -- implementation of Concurrent Haskell.
89
90 -- ** Haskell threads and Operating System threads
91
92 -- $osthreads
93
94 -- ** Terminating the program
95
96 -- $termination
97
98 -- ** Pre-emption
99
100 -- $preemption
101
102 -- ** Deadlock
103
104 -- $deadlock
105
106 ) where
107
108 import Prelude
109
110 import Control.Exception.Base as Exception
111
112 import GHC.Exception
113 import GHC.Conc hiding (threadWaitRead, threadWaitWrite,
114 threadWaitReadSTM, threadWaitWriteSTM)
115 import qualified GHC.Conc
116 import GHC.IO ( IO(..), unsafeInterleaveIO, unsafeUnmask )
117 import GHC.IORef ( newIORef, readIORef, writeIORef )
118 import GHC.Base
119
120 import System.Posix.Types ( Fd )
121 import Foreign.StablePtr
122 import Foreign.C.Types
123 import Control.Monad
124
125 #ifdef mingw32_HOST_OS
126 import Foreign.C
127 import System.IO
128 import Data.Maybe (Maybe(..))
129 #endif
130
131 import Control.Concurrent.MVar
132 import Control.Concurrent.Chan
133 import Control.Concurrent.QSem
134 import Control.Concurrent.QSemN
135
136 {- $conc_intro
137
138 The concurrency extension for Haskell is described in the paper
139 /Concurrent Haskell/
140 <http://www.haskell.org/ghc/docs/papers/concurrent-haskell.ps.gz>.
141
142 Concurrency is \"lightweight\", which means that both thread creation
143 and context switching overheads are extremely low. Scheduling of
144 Haskell threads is done internally in the Haskell runtime system, and
145 doesn't make use of any operating system-supplied thread packages.
146
147 However, if you want to interact with a foreign library that expects your
148 program to use the operating system-supplied thread package, you can do so
149 by using 'forkOS' instead of 'forkIO'.
150
151 Haskell threads can communicate via 'MVar's, a kind of synchronised
152 mutable variable (see "Control.Concurrent.MVar"). Several common
153 concurrency abstractions can be built from 'MVar's, and these are
154 provided by the "Control.Concurrent" library.
155 In GHC, threads may also communicate via exceptions.
156 -}
157
158 {- $conc_scheduling
159
160 Scheduling may be either pre-emptive or co-operative,
161 depending on the implementation of Concurrent Haskell (see below
162 for information related to specific compilers). In a co-operative
163 system, context switches only occur when you use one of the
164 primitives defined in this module. This means that programs such
165 as:
166
167
168 > main = forkIO (write 'a') >> write 'b'
169 > where write c = putChar c >> write c
170
171 will print either @aaaaaaaaaaaaaa...@ or @bbbbbbbbbbbb...@,
172 instead of some random interleaving of @a@s and @b@s. In
173 practice, cooperative multitasking is sufficient for writing
174 simple graphical user interfaces.
175 -}
176
177 {- $blocking
178 Different Haskell implementations have different characteristics with
179 regard to which operations block /all/ threads.
180
181 Using GHC without the @-threaded@ option, all foreign calls will block
182 all other Haskell threads in the system, although I\/O operations will
183 not. With the @-threaded@ option, only foreign calls with the @unsafe@
184 attribute will block all other threads.
185
186 -}
187
188 -- | fork a thread and call the supplied function when the thread is about
189 -- to terminate, with an exception or a returned value. The function is
190 -- called with asynchronous exceptions masked.
191 --
192 -- > forkFinally action and_then =
193 -- > mask $ \restore ->
194 -- > forkIO $ try (restore action) >>= and_then
195 --
196 -- This function is useful for informing the parent when a child
197 -- terminates, for example.
198 --
199 forkFinally :: IO a -> (Either SomeException a -> IO ()) -> IO ThreadId
200 forkFinally action and_then =
201 mask $ \restore ->
202 forkIO $ try (restore action) >>= and_then
203
204 -- ---------------------------------------------------------------------------
205 -- Bound Threads
206
207 {- $boundthreads
208 #boundthreads#
209
210 Support for multiple operating system threads and bound threads as described
211 below is currently only available in the GHC runtime system if you use the
212 /-threaded/ option when linking.
213
214 Other Haskell systems do not currently support multiple operating system threads.
215
216 A bound thread is a haskell thread that is /bound/ to an operating system
217 thread. While the bound thread is still scheduled by the Haskell run-time
218 system, the operating system thread takes care of all the foreign calls made
219 by the bound thread.
220
221 To a foreign library, the bound thread will look exactly like an ordinary
222 operating system thread created using OS functions like @pthread_create@
223 or @CreateThread@.
224
225 Bound threads can be created using the 'forkOS' function below. All foreign
226 exported functions are run in a bound thread (bound to the OS thread that
227 called the function). Also, the @main@ action of every Haskell program is
228 run in a bound thread.
229
230 Why do we need this? Because if a foreign library is called from a thread
231 created using 'forkIO', it won't have access to any /thread-local state/ -
232 state variables that have specific values for each OS thread
233 (see POSIX's @pthread_key_create@ or Win32's @TlsAlloc@). Therefore, some
234 libraries (OpenGL, for example) will not work from a thread created using
235 'forkIO'. They work fine in threads created using 'forkOS' or when called
236 from @main@ or from a @foreign export@.
237
238 In terms of performance, 'forkOS' (aka bound) threads are much more
239 expensive than 'forkIO' (aka unbound) threads, because a 'forkOS'
240 thread is tied to a particular OS thread, whereas a 'forkIO' thread
241 can be run by any OS thread. Context-switching between a 'forkOS'
242 thread and a 'forkIO' thread is many times more expensive than between
243 two 'forkIO' threads.
244
245 Note in particular that the main program thread (the thread running
246 @Main.main@) is always a bound thread, so for good concurrency
247 performance you should ensure that the main thread is not doing
248 repeated communication with other threads in the system. Typically
249 this means forking subthreads to do the work using 'forkIO', and
250 waiting for the results in the main thread.
251
252 -}
253
254 -- | 'True' if bound threads are supported.
255 -- If @rtsSupportsBoundThreads@ is 'False', 'isCurrentThreadBound'
256 -- will always return 'False' and both 'forkOS' and 'runInBoundThread' will
257 -- fail.
258 foreign import ccall rtsSupportsBoundThreads :: Bool
259
260
261 {- |
262 Like 'forkIO', this sparks off a new thread to run the 'IO'
263 computation passed as the first argument, and returns the 'ThreadId'
264 of the newly created thread.
265
266 However, 'forkOS' creates a /bound/ thread, which is necessary if you
267 need to call foreign (non-Haskell) libraries that make use of
268 thread-local state, such as OpenGL (see "Control.Concurrent#boundthreads").
269
270 Using 'forkOS' instead of 'forkIO' makes no difference at all to the
271 scheduling behaviour of the Haskell runtime system. It is a common
272 misconception that you need to use 'forkOS' instead of 'forkIO' to
273 avoid blocking all the Haskell threads when making a foreign call;
274 this isn't the case. To allow foreign calls to be made without
275 blocking all the Haskell threads (with GHC), it is only necessary to
276 use the @-threaded@ option when linking your program, and to make sure
277 the foreign import is not marked @unsafe@.
278 -}
279
280 forkOS :: IO () -> IO ThreadId
281
282 foreign export ccall forkOS_entry
283 :: StablePtr (IO ()) -> IO ()
284
285 foreign import ccall "forkOS_entry" forkOS_entry_reimported
286 :: StablePtr (IO ()) -> IO ()
287
288 forkOS_entry :: StablePtr (IO ()) -> IO ()
289 forkOS_entry stableAction = do
290 action <- deRefStablePtr stableAction
291 action
292
293 foreign import ccall forkOS_createThread
294 :: StablePtr (IO ()) -> IO CInt
295
296 failNonThreaded :: IO a
297 failNonThreaded = fail $ "RTS doesn't support multiple OS threads "
298 ++"(use ghc -threaded when linking)"
299
300 forkOS action0
301 | rtsSupportsBoundThreads = do
302 mv <- newEmptyMVar
303 b <- Exception.getMaskingState
304 let
305 -- async exceptions are masked in the child if they are masked
306 -- in the parent, as for forkIO (see #1048). forkOS_createThread
307 -- creates a thread with exceptions masked by default.
308 action1 = case b of
309 Unmasked -> unsafeUnmask action0
310 MaskedInterruptible -> action0
311 MaskedUninterruptible -> uninterruptibleMask_ action0
312
313 action_plus = Exception.catch action1 childHandler
314
315 entry <- newStablePtr (myThreadId >>= putMVar mv >> action_plus)
316 err <- forkOS_createThread entry
317 when (err /= 0) $ fail "Cannot create OS thread."
318 tid <- takeMVar mv
319 freeStablePtr entry
320 return tid
321 | otherwise = failNonThreaded
322
323 -- | Returns 'True' if the calling thread is /bound/, that is, if it is
324 -- safe to use foreign libraries that rely on thread-local state from the
325 -- calling thread.
326 isCurrentThreadBound :: IO Bool
327 isCurrentThreadBound = IO $ \ s# ->
328 case isCurrentThreadBound# s# of
329 (# s2#, flg #) -> (# s2#, isTrue# (flg /=# 0#) #)
330
331
332 {- |
333 Run the 'IO' computation passed as the first argument. If the calling thread
334 is not /bound/, a bound thread is created temporarily. @runInBoundThread@
335 doesn't finish until the 'IO' computation finishes.
336
337 You can wrap a series of foreign function calls that rely on thread-local state
338 with @runInBoundThread@ so that you can use them without knowing whether the
339 current thread is /bound/.
340 -}
341 runInBoundThread :: IO a -> IO a
342
343 runInBoundThread action
344 | rtsSupportsBoundThreads = do
345 bound <- isCurrentThreadBound
346 if bound
347 then action
348 else do
349 ref <- newIORef undefined
350 let action_plus = Exception.try action >>= writeIORef ref
351 bracket (newStablePtr action_plus)
352 freeStablePtr
353 (\cEntry -> forkOS_entry_reimported cEntry >> readIORef ref) >>=
354 unsafeResult
355 | otherwise = failNonThreaded
356
357 {- |
358 Run the 'IO' computation passed as the first argument. If the calling thread
359 is /bound/, an unbound thread is created temporarily using 'forkIO'.
360 @runInBoundThread@ doesn't finish until the 'IO' computation finishes.
361
362 Use this function /only/ in the rare case that you have actually observed a
363 performance loss due to the use of bound threads. A program that
364 doesn't need it's main thread to be bound and makes /heavy/ use of concurrency
365 (e.g. a web server), might want to wrap it's @main@ action in
366 @runInUnboundThread@.
367
368 Note that exceptions which are thrown to the current thread are thrown in turn
369 to the thread that is executing the given computation. This ensures there's
370 always a way of killing the forked thread.
371 -}
372 runInUnboundThread :: IO a -> IO a
373
374 runInUnboundThread action = do
375 bound <- isCurrentThreadBound
376 if bound
377 then do
378 mv <- newEmptyMVar
379 mask $ \restore -> do
380 tid <- forkIO $ Exception.try (restore action) >>= putMVar mv
381 let wait = takeMVar mv `Exception.catch` \(e :: SomeException) ->
382 Exception.throwTo tid e >> wait
383 wait >>= unsafeResult
384 else action
385
386 unsafeResult :: Either SomeException a -> IO a
387 unsafeResult = either Exception.throwIO return
388
389 -- ---------------------------------------------------------------------------
390 -- threadWaitRead/threadWaitWrite
391
392 -- | Block the current thread until data is available to read on the
393 -- given file descriptor (GHC only).
394 --
395 -- This will throw an 'IOError' if the file descriptor was closed
396 -- while this thread was blocked. To safely close a file descriptor
397 -- that has been used with 'threadWaitRead', use
398 -- 'GHC.Conc.closeFdWith'.
399 threadWaitRead :: Fd -> IO ()
400 threadWaitRead fd
401 #ifdef mingw32_HOST_OS
402 -- we have no IO manager implementing threadWaitRead on Windows.
403 -- fdReady does the right thing, but we have to call it in a
404 -- separate thread, otherwise threadWaitRead won't be interruptible,
405 -- and this only works with -threaded.
406 | threaded = withThread (waitFd fd 0)
407 | otherwise = case fd of
408 0 -> do _ <- hWaitForInput stdin (-1)
409 return ()
410 -- hWaitForInput does work properly, but we can only
411 -- do this for stdin since we know its FD.
412 _ -> error "threadWaitRead requires -threaded on Windows, or use System.IO.hWaitForInput"
413 #else
414 = GHC.Conc.threadWaitRead fd
415 #endif
416
417 -- | Block the current thread until data can be written to the
418 -- given file descriptor (GHC only).
419 --
420 -- This will throw an 'IOError' if the file descriptor was closed
421 -- while this thread was blocked. To safely close a file descriptor
422 -- that has been used with 'threadWaitWrite', use
423 -- 'GHC.Conc.closeFdWith'.
424 threadWaitWrite :: Fd -> IO ()
425 threadWaitWrite fd
426 #ifdef mingw32_HOST_OS
427 | threaded = withThread (waitFd fd 1)
428 | otherwise = error "threadWaitWrite requires -threaded on Windows"
429 #else
430 = GHC.Conc.threadWaitWrite fd
431 #endif
432
433 -- | Returns an STM action that can be used to wait for data
434 -- to read from a file descriptor. The second returned value
435 -- is an IO action that can be used to deregister interest
436 -- in the file descriptor.
437 threadWaitReadSTM :: Fd -> IO (STM (), IO ())
438 threadWaitReadSTM fd
439 #ifdef mingw32_HOST_OS
440 | threaded = do v <- newTVarIO Nothing
441 mask_ $ void $ forkIO $ do result <- try (waitFd fd 0)
442 atomically (writeTVar v $ Just result)
443 let waitAction = do result <- readTVar v
444 case result of
445 Nothing -> retry
446 Just (Right ()) -> return ()
447 Just (Left e) -> throwSTM (e :: IOException)
448 let killAction = return ()
449 return (waitAction, killAction)
450 | otherwise = error "threadWaitReadSTM requires -threaded on Windows"
451 #else
452 = GHC.Conc.threadWaitReadSTM fd
453 #endif
454
455 -- | Returns an STM action that can be used to wait until data
456 -- can be written to a file descriptor. The second returned value
457 -- is an IO action that can be used to deregister interest
458 -- in the file descriptor.
459 threadWaitWriteSTM :: Fd -> IO (STM (), IO ())
460 threadWaitWriteSTM fd
461 #ifdef mingw32_HOST_OS
462 | threaded = do v <- newTVarIO Nothing
463 mask_ $ void $ forkIO $ do result <- try (waitFd fd 1)
464 atomically (writeTVar v $ Just result)
465 let waitAction = do result <- readTVar v
466 case result of
467 Nothing -> retry
468 Just (Right ()) -> return ()
469 Just (Left e) -> throwSTM (e :: IOException)
470 let killAction = return ()
471 return (waitAction, killAction)
472 | otherwise = error "threadWaitWriteSTM requires -threaded on Windows"
473 #else
474 = GHC.Conc.threadWaitWriteSTM fd
475 #endif
476
477 #ifdef mingw32_HOST_OS
478 foreign import ccall unsafe "rtsSupportsBoundThreads" threaded :: Bool
479
480 withThread :: IO a -> IO a
481 withThread io = do
482 m <- newEmptyMVar
483 _ <- mask_ $ forkIO $ try io >>= putMVar m
484 x <- takeMVar m
485 case x of
486 Right a -> return a
487 Left e -> throwIO (e :: IOException)
488
489 waitFd :: Fd -> CInt -> IO ()
490 waitFd fd write = do
491 throwErrnoIfMinus1_ "fdReady" $
492 fdReady (fromIntegral fd) write iNFINITE 0
493
494 iNFINITE :: CInt
495 iNFINITE = 0xFFFFFFFF -- urgh
496
497 foreign import ccall safe "fdReady"
498 fdReady :: CInt -> CInt -> CInt -> CInt -> IO CInt
499 #endif
500
501 -- ---------------------------------------------------------------------------
502 -- More docs
503
504 {- $osthreads
505
506 #osthreads# In GHC, threads created by 'forkIO' are lightweight threads, and
507 are managed entirely by the GHC runtime. Typically Haskell
508 threads are an order of magnitude or two more efficient (in
509 terms of both time and space) than operating system threads.
510
511 The downside of having lightweight threads is that only one can
512 run at a time, so if one thread blocks in a foreign call, for
513 example, the other threads cannot continue. The GHC runtime
514 works around this by making use of full OS threads where
515 necessary. When the program is built with the @-threaded@
516 option (to link against the multithreaded version of the
517 runtime), a thread making a @safe@ foreign call will not block
518 the other threads in the system; another OS thread will take
519 over running Haskell threads until the original call returns.
520 The runtime maintains a pool of these /worker/ threads so that
521 multiple Haskell threads can be involved in external calls
522 simultaneously.
523
524 The "System.IO" library manages multiplexing in its own way. On
525 Windows systems it uses @safe@ foreign calls to ensure that
526 threads doing I\/O operations don't block the whole runtime,
527 whereas on Unix systems all the currently blocked I\/O requests
528 are managed by a single thread (the /IO manager thread/) using
529 a mechanism such as @epoll@ or @kqueue@, depending on what is
530 provided by the host operating system.
531
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").
537
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
543 @select@.
544 -}
545
546 {- $termination
547
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\").
553
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
559 exiting:
560
561 > myForkIO :: IO () -> IO (MVar ())
562 > myForkIO io = do
563 > mvar <- newEmptyMVar
564 > forkFinally io (\_ -> putMVar mvar ())
565 > return mvar
566
567 Note that we use 'forkFinally' to make sure that the
568 'MVar' is written to even if the thread dies or
569 is killed for some reason.
570
571 A better method is to keep a global list of all child
572 threads which we should wait for at the end of the program:
573
574 > children :: MVar [MVar ()]
575 > children = unsafePerformIO (newMVar [])
576 >
577 > waitForChildren :: IO ()
578 > waitForChildren = do
579 > cs <- takeMVar children
580 > case cs of
581 > [] -> return ()
582 > m:ms -> do
583 > putMVar children ms
584 > takeMVar m
585 > waitForChildren
586 >
587 > forkChild :: IO () -> IO ThreadId
588 > forkChild io = do
589 > mvar <- newEmptyMVar
590 > childs <- takeMVar children
591 > putMVar children (mvar:childs)
592 > forkFinally io (\_ -> putMVar mvar ())
593 >
594 > main =
595 > later waitForChildren $
596 > ...
597
598 The main thread principle also applies to calls to Haskell from
599 outside, using @foreign export@. When the @foreign export@ed
600 function is invoked, it starts a new main thread, and it returns
601 when this main thread terminates. If the call causes new
602 threads to be forked, they may remain in the system after the
603 @foreign export@ed function has returned.
604 -}
605
606 {- $preemption
607
608 GHC implements pre-emptive multitasking: the execution of
609 threads are interleaved in a random fashion. More specifically,
610 a thread may be pre-empted whenever it allocates some memory,
611 which unfortunately means that tight loops which do no
612 allocation tend to lock out other threads (this only seems to
613 happen with pathological benchmark-style code, however).
614
615 The rescheduling timer runs on a 20ms granularity by
616 default, but this may be altered using the
617 @-i\<n\>@ RTS option. After a rescheduling
618 \"tick\" the running thread is pre-empted as soon as
619 possible.
620
621 One final note: the
622 @aaaa@ @bbbb@ example may not
623 work too well on GHC (see Scheduling, above), due
624 to the locking on a 'System.IO.Handle'. Only one thread
625 may hold the lock on a 'System.IO.Handle' at any one
626 time, so if a reschedule happens while a thread is holding the
627 lock, the other thread won't be able to run. The upshot is that
628 the switch from @aaaa@ to
629 @bbbbb@ happens infrequently. It can be
630 improved by lowering the reschedule tick period. We also have a
631 patch that causes a reschedule whenever a thread waiting on a
632 lock is woken up, but haven't found it to be useful for anything
633 other than this example :-)
634 -}
635
636 {- $deadlock
637
638 GHC attempts to detect when threads are deadlocked using the garbage
639 collector. A thread that is not reachable (cannot be found by
640 following pointers from live objects) must be deadlocked, and in this
641 case the thread is sent an exception. The exception is either
642 'BlockedIndefinitelyOnMVar', 'BlockedIndefinitelyOnSTM',
643 'NonTermination', or 'Deadlock', depending on the way in which the
644 thread is deadlocked.
645
646 Note that this feature is intended for debugging, and should not be
647 relied on for the correct operation of your program. There is no
648 guarantee that the garbage collector will be accurate enough to detect
649 your deadlock, and no guarantee that the garbage collector will run in
650 a timely enough manner. Basically, the same caveats as for finalizers
651 apply to deadlock detection.
652
653 There is a subtle interaction between deadlock detection and
654 finalizers (as created by 'Foreign.Concurrent.newForeignPtr' or the
655 functions in "System.Mem.Weak"): if a thread is blocked waiting for a
656 finalizer to run, then the thread will be considered deadlocked and
657 sent an exception. So preferably don't do this, but if you have no
658 alternative then it is possible to prevent the thread from being
659 considered deadlocked by making a 'StablePtr' pointing to it. Don't
660 forget to release the 'StablePtr' later with 'freeStablePtr'.
661 -}