1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 {-# OPTIONS_GHC -fno-warn-missing-signatures #-}
3 {-# OPTIONS_HADDOCK not-home #-}
4 -----------------------------------------------------------------------------
6 -- Module : GHC.Conc.IO
7 -- Copyright : (c) The University of Glasgow, 1994-2002
8 -- License : see libraries/base/LICENSE
10 -- Maintainer : cvs-ghc@haskell.org
11 -- Stability : internal
12 -- Portability : non-portable (GHC extensions)
14 -- Basic concurrency stuff.
16 -----------------------------------------------------------------------------
18 -- No: #hide, because bits of this module are exposed by the stm package.
19 -- However, we don't want this module to be the home location for the
20 -- bits it exports, we'd rather have Control.Concurrent and the other
21 -- higher level modules be the home. Hence:
27 ( ensureIOManagerIsRunning
30 , threadDelay -- :: Int -> IO ()
31 , registerDelay -- :: Int -> IO (TVar Bool)
32 , threadWaitRead -- :: Int -> IO ()
33 , threadWaitWrite -- :: Int -> IO ()
34 , closeFd -- :: (Int -> IO ()) -> Int -> IO ()
36 #ifdef mingw32_HOST_OS
37 , asyncRead -- :: Int -> Int -> Int -> Ptr a -> IO (Int, Int)
38 , asyncWrite -- :: Int -> Int -> Int -> Ptr a -> IO (Int, Int)
39 , asyncDoProc -- :: FunPtr (Ptr a -> IO Int) -> Ptr a -> IO Int
41 , asyncReadBA -- :: Int -> Int -> Int -> Int -> MutableByteArray# RealWorld -> IO (Int, Int)
42 , asyncWriteBA -- :: Int -> Int -> Int -> Int -> MutableByteArray# RealWorld -> IO (Int, Int)
52 import GHC.Conc.Sync as Sync
53 import GHC.Real ( fromIntegral )
54 import System.Posix.Types
56 #ifdef mingw32_HOST_OS
57 import qualified GHC.Conc.Windows as Windows
58 import GHC.Conc.Windows (asyncRead, asyncWrite, asyncDoProc, asyncReadBA,
59 asyncWriteBA, ConsoleEvent(..), win32ConsoleHandler,
62 import qualified System.Event.Thread as Event
65 ensureIOManagerIsRunning :: IO ()
66 #ifndef mingw32_HOST_OS
67 ensureIOManagerIsRunning = Event.ensureIOManagerIsRunning
69 ensureIOManagerIsRunning = Windows.ensureIOManagerIsRunning
72 -- | Block the current thread until data is available to read on the
73 -- given file descriptor (GHC only).
74 threadWaitRead :: Fd -> IO ()
76 #ifndef mingw32_HOST_OS
77 | threaded = Event.threadWaitRead fd
79 | otherwise = IO $ \s ->
80 case fromIntegral fd of { I# fd# ->
81 case waitRead# fd# s of { s' -> (# s', () #)
84 -- | Block the current thread until data can be written to the
85 -- given file descriptor (GHC only).
87 -- This will throw an 'IOError' if the file descriptor was closed
88 -- while this thread was blocked.
89 threadWaitWrite :: Fd -> IO ()
91 #ifndef mingw32_HOST_OS
92 | threaded = Event.threadWaitWrite fd
94 | otherwise = IO $ \s ->
95 case fromIntegral fd of { I# fd# ->
96 case waitWrite# fd# s of { s' -> (# s', () #)
99 -- | Close a file descriptor in a concurrency-safe way (GHC only). If
100 -- you are using 'threadWaitRead' or 'threadWaitWrite' to perform
101 -- blocking I\/O, you /must/ use this function to close file
102 -- descriptors, or blocked threads may not be woken.
104 -- Any threads that are blocked on the file descriptor via
105 -- 'threadWaitRead' or 'threadWaitWrite' will be unblocked by having
106 -- IO exceptions thrown.
107 closeFd :: (Fd -> IO ()) -- ^ Low-level action that performs the real close.
108 -> Fd -- ^ File descriptor to close.
111 #ifndef mingw32_HOST_OS
112 | threaded = Event.closeFd close fd
114 | otherwise = close fd
116 -- | Suspends the current thread for a given number of microseconds
119 -- There is no guarantee that the thread will be rescheduled promptly
120 -- when the delay has expired, but the thread will never continue to
121 -- run /earlier/ than specified.
123 threadDelay :: Int -> IO ()
125 #ifdef mingw32_HOST_OS
126 | threaded = Windows.threadDelay time
128 | threaded = Event.threadDelay time
130 | otherwise = IO $ \s ->
131 case time of { I# time# ->
132 case delay# time# s of { s' -> (# s', () #)
135 -- | Set the value of returned TVar to True after a given number of
136 -- microseconds. The caveats associated with threadDelay also apply.
138 registerDelay :: Int -> IO (TVar Bool)
140 #ifdef mingw32_HOST_OS
141 | threaded = Windows.registerDelay usecs
143 | threaded = Event.registerDelay usecs
145 | otherwise = error "registerDelay: requires -threaded"
147 foreign import ccall unsafe "rtsSupportsBoundThreads" threaded :: Bool