-{-# OPTIONS -fno-implicit-prelude -#include "HsBase.h" #-}
+{-# OPTIONS_GHC -fno-implicit-prelude -#include "HsBase.h" #-}
#undef DEBUG_DUMP
commitBuffer', -- hack, see below
hGetcBuffered, -- needed by ghc/compiler/utils/StringBuffer.lhs
hGetBuf, hGetBufNonBlocking, hPutBuf, hPutBufNonBlocking, slurpFile,
-{- NOTE: As far as I can tell, not defined.
- createPipe, createPipeEx,
--}
memcpy_ba_baoff,
memcpy_ptr_baoff,
memcpy_baoff_ba,
memcpy_baoff_ptr,
) where
-#include "config.h"
-
import Foreign
import Foreign.C
import GHC.Show
import GHC.List
import GHC.Exception ( ioError, catch )
+
+#ifdef mingw32_HOST_OS
import GHC.Conc
+#endif
-- ---------------------------------------------------------------------------
-- Simple input operations
-- It returns 'True' as soon as input is available on @hdl@,
-- or 'False' if no input is available within @t@ milliseconds.
--
+-- If @t@ is less than zero, then @hWaitForInput@ waits indefinitely.
+-- NOTE: in the current implementation, this is the only case that works
+-- correctly (if @t@ is non-zero, then all other concurrent threads are
+-- blocked until data is available).
+--
-- This operation may fail with:
--
-- * 'isEOFError' if the end of file has been reached.
then return True
else do
- r <- throwErrnoIfMinus1Retry "hWaitForInput"
- (inputReady (fromIntegral (haFD handle_)) (fromIntegral msecs) (haIsStream handle_))
- return (r /= 0)
-
-foreign import ccall unsafe "inputReady"
+ if msecs < 0
+ then do buf' <- fillReadBuffer (haFD handle_) True
+ (haIsStream handle_) buf
+ writeIORef ref buf'
+ return True
+ else do r <- throwErrnoIfMinus1Retry "hWaitForInput" $
+ inputReady (fromIntegral (haFD handle_))
+ (fromIntegral msecs) (haIsStream handle_)
+ return (r /= 0)
+
+foreign import ccall safe "inputReady"
inputReady :: CInt -> CInt -> Bool -> IO CInt
-- ---------------------------------------------------------------------------
LineBuffering -> hPutcBuffered handle_ True c
BlockBuffering _ -> hPutcBuffered handle_ False c
NoBuffering ->
- withObject (castCharToCChar c) $ \buf -> do
+ with (castCharToCChar c) $ \buf -> do
writeRawBufferPtr "hPutChar" (fromIntegral fd) (haIsStream handle_) buf 0 1
return ()
commitBuffer hdl raw sz@(I# _) count@(I# _) flush release = do
wantWritableHandle "commitAndReleaseBuffer" hdl $
- commitBuffer' hdl raw sz count flush release
+ commitBuffer' raw sz count flush release
-- Explicitly lambda-lift this function to subvert GHC's full laziness
-- optimisations, which otherwise tends to float out subexpressions
--
-- This hack is a fairly big win for hPutStr performance. --SDM 18/9/2001
--
-commitBuffer' hdl raw sz@(I# _) count@(I# _) flush release
+commitBuffer' raw sz@(I# _) count@(I# _) flush release
handle_@Handle__{ haFD=fd, haBuffer=ref, haBuffers=spare_buf_ref } = do
#ifdef DEBUG_DUMP
loop :: Int -> Int -> IO Int
loop off bytes | bytes <= 0 = return off
loop off bytes = do
-#ifndef mingw32_TARGET_OS
+#ifndef mingw32_HOST_OS
ssize <- c_write (fromIntegral fd) (ptr `plusPtr` off) (fromIntegral bytes)
let r = fromIntegral ssize :: Int
if (r == -1)
-- is closed, 'hGetBuf' will behave as if EOF was reached.
hGetBuf :: Handle -> Ptr a -> Int -> IO Int
-hGetBuf h ptr count = hGetBuf' h ptr count True
-
-hGetBufNonBlocking :: Handle -> Ptr a -> Int -> IO Int
-hGetBufNonBlocking h ptr count = hGetBuf' h ptr count False
-
-hGetBuf' :: Handle -> Ptr a -> Int -> Bool -> IO Int
-hGetBuf' handle ptr count can_block
+hGetBuf h ptr count
| count == 0 = return 0
- | count < 0 = illegalBufferSize handle "hGetBuf" count
+ | count < 0 = illegalBufferSize h "hGetBuf" count
| otherwise =
- wantReadableHandle "hGetBuf" handle $
+ wantReadableHandle "hGetBuf" h $
\ handle_@Handle__{ haFD=fd, haBuffer=ref, haIsStream=is_stream } -> do
- bufRead fd ref is_stream ptr 0 count can_block
+ bufRead fd ref is_stream ptr 0 count
-bufRead fd ref is_stream ptr so_far count can_block =
+-- small reads go through the buffer, large reads are satisfied by
+-- taking data first from the buffer and then direct from the file
+-- descriptor.
+bufRead fd ref is_stream ptr so_far count =
seq fd $ seq so_far $ seq count $ do -- strictness hack
buf@Buffer{ bufBuf=raw, bufWPtr=w, bufRPtr=r, bufSize=sz } <- readIORef ref
if bufferEmpty buf
- then if so_far > 0 then return so_far else
- if count < sz
- then do
- mb_buf <- maybeFillReadBuffer fd (not can_block) is_stream buf
- case mb_buf of
- Nothing -> return 0
- Just new_buf -> do
- writeIORef ref new_buf
- bufRead fd ref is_stream ptr so_far count can_block
- else if can_block
- then readChunk fd is_stream ptr count
- else readChunkNonBlocking fd is_stream ptr count
+ then if count > sz -- small read?
+ then do rest <- readChunk fd is_stream ptr count
+ return (so_far + rest)
+ else do mb_buf <- maybeFillReadBuffer fd True is_stream buf
+ case mb_buf of
+ Nothing -> return so_far -- got nothing, we're done
+ Just buf' -> do
+ writeIORef ref buf'
+ bufRead fd ref is_stream ptr so_far count
else do
- let avail = w - r
+ let avail = w - r
if (count == avail)
then do
memcpy_ptr_baoff ptr raw r (fromIntegral count)
writeIORef ref buf{ bufRPtr = r + count }
return (so_far + count)
else do
-
+
memcpy_ptr_baoff ptr raw r (fromIntegral avail)
writeIORef ref buf{ bufWPtr=0, bufRPtr=0 }
-
let remaining = count - avail
so_far' = so_far + avail
ptr' = ptr `plusPtr` avail
if remaining < sz
- then bufRead fd ref is_stream ptr' so_far' remaining can_block
+ then bufRead fd ref is_stream ptr' so_far' remaining
else do
- rest <- if can_block
- then readChunk fd is_stream ptr' remaining
- else readChunkNonBlocking fd is_stream ptr' remaining
+ rest <- readChunk fd is_stream ptr' remaining
return (so_far' + rest)
readChunk :: FD -> Bool -> Ptr a -> Int -> IO Int
then return off
else loop (off + r) (bytes - r)
+
+-- | 'hGetBufNonBlocking' @hdl buf count@ reads data from the handle @hdl@
+-- into the buffer @buf@ until either EOF is reached, or
+-- @count@ 8-bit bytes have been read, or there is no more data available
+-- to read immediately.
+--
+-- 'hGetBufNonBlocking' is identical to 'hGetBuf', except that it will
+-- never block waiting for data to become available, instead it returns
+-- only whatever data is available. To wait for data to arrive before
+-- calling 'hGetBufNonBlocking', use 'hWaitForInput'.
+--
+-- If the handle is a pipe or socket, and the writing end
+-- is closed, 'hGetBufNonBlocking' will behave as if EOF was reached.
+--
+hGetBufNonBlocking :: Handle -> Ptr a -> Int -> IO Int
+hGetBufNonBlocking h ptr count
+ | count == 0 = return 0
+ | count < 0 = illegalBufferSize h "hGetBufNonBlocking" count
+ | otherwise =
+ wantReadableHandle "hGetBufNonBlocking" h $
+ \ handle_@Handle__{ haFD=fd, haBuffer=ref, haIsStream=is_stream } -> do
+ bufReadNonBlocking fd ref is_stream ptr 0 count
+
+bufReadNonBlocking fd ref is_stream ptr so_far count =
+ seq fd $ seq so_far $ seq count $ do -- strictness hack
+ buf@Buffer{ bufBuf=raw, bufWPtr=w, bufRPtr=r, bufSize=sz } <- readIORef ref
+ if bufferEmpty buf
+ then if count > sz -- large read?
+ then do rest <- readChunkNonBlocking fd is_stream ptr count
+ return (so_far + rest)
+ else do buf' <- fillReadBufferWithoutBlocking fd is_stream buf
+ case buf' of { Buffer{ bufWPtr=w } ->
+ if (w == 0)
+ then return so_far
+ else do writeIORef ref buf'
+ bufReadNonBlocking fd ref is_stream ptr
+ so_far (min count w)
+ -- NOTE: new count is 'min count w'
+ -- so we will just copy the contents of the
+ -- buffer in the recursive call, and not
+ -- loop again.
+ }
+ else do
+ let avail = w - r
+ if (count == avail)
+ then do
+ memcpy_ptr_baoff ptr raw r (fromIntegral count)
+ writeIORef ref buf{ bufWPtr=0, bufRPtr=0 }
+ return (so_far + count)
+ else do
+ if (count < avail)
+ then do
+ memcpy_ptr_baoff ptr raw r (fromIntegral count)
+ writeIORef ref buf{ bufRPtr = r + count }
+ return (so_far + count)
+ else do
+
+ memcpy_ptr_baoff ptr raw r (fromIntegral avail)
+ writeIORef ref buf{ bufWPtr=0, bufRPtr=0 }
+ let remaining = count - avail
+ so_far' = so_far + avail
+ ptr' = ptr `plusPtr` avail
+
+ -- we haven't attempted to read anything yet if we get to here.
+ if remaining < sz
+ then bufReadNonBlocking fd ref is_stream ptr' so_far' remaining
+ else do
+
+ rest <- readChunkNonBlocking fd is_stream ptr' remaining
+ return (so_far' + rest)
+
+
readChunkNonBlocking :: FD -> Bool -> Ptr a -> Int -> IO Int
-readChunkNonBlocking fd is_stream ptr bytes = loop 0 bytes
- where
- loop :: Int -> Int -> IO Int
- loop off bytes | bytes <= 0 = return off
- loop off bytes = do
-#ifndef mingw32_TARGET_OS
- ssize <- c_read (fromIntegral fd) (ptr `plusPtr` off) (fromIntegral bytes)
+readChunkNonBlocking fd is_stream ptr bytes = do
+#ifndef mingw32_HOST_OS
+ ssize <- c_read (fromIntegral fd) (castPtr ptr) (fromIntegral bytes)
let r = fromIntegral ssize :: Int
if (r == -1)
then do errno <- getErrno
if (errno == eAGAIN || errno == eWOULDBLOCK)
- then return off
+ then return 0
else throwErrno "readChunk"
- else if (r == 0)
- then return off
- else loop (off + r) (bytes - r)
+ else return r
#else
(ssize, rc) <- asyncRead fd (fromIntegral $ fromEnum is_stream)
- (fromIntegral bytes)
- (ptr `plusPtr` off)
+ (fromIntegral bytes) ptr
let r = fromIntegral ssize :: Int
if r == (-1)
then ioError (errnoToIOError "hGetBufNonBlocking" (Errno (fromIntegral rc)) Nothing Nothing)
- else if (r == 0)
- then return off
- else loop (off + r) (bytes - r)
+ else return r
#endif
slurpFile :: FilePath -> IO (Ptr (), Int)
return (chunk, r)
-- ---------------------------------------------------------------------------
--- pipes
-
-{-|
-(@createPipe@) creates an anonymous /pipe/ and returns a pair of
-handles, the first for reading and the second for writing. Both
-pipe ends can be inherited by a child process.
-
-> createPipe = createPipeEx (BinaryMode AppendMode)
--}
-createPipe :: IO (Handle,Handle)
-createPipe = createPipeEx AppendMode
-
-{-|
-(@createPipeEx modeEx@) creates an anonymous /pipe/ and returns a pair of
-handles, the first for reading and the second for writing.
-The pipe mode @modeEx@ can be:
-
- * @'TextMode' mode@ -- the pipe is opened in text mode.
-
- * @'BinaryMode' mode@ -- the pipe is opened in binary mode.
-
-The @mode@ determines if child processes can inherit the pipe handles:
-
- * 'ReadMode' -- The /read/ handle of the pipe is private to this process.
-
- * 'WriteMode' -- The /write/ handle of the pipe is private to this process.
-
- * 'ReadWriteMode' -- Both handles are private to this process.
-
- * 'AppendMode' -- Both handles are available (inheritable) to child processes.
- This mode can be used to /append/ (|) two seperate child processes.
-
-If a broken pipe is read, an end-of-file ('GHC.IOBase.EOF')
-exception is raised. If a broken pipe is written to, an invalid argument exception
-is raised ('GHC.IOBase.InvalidArgument').
--}
-createPipeEx :: IOMode -> IO (Handle,Handle)
-createPipeEx mode = do
-#if 1
- return (error "createPipeEx")
-#else
-
-#ifndef mingw32_TARGET_OS
- -- ignore modeEx for Unix: just always inherit the descriptors
- allocaArray 2 $ \p -> do
- throwErrnoIfMinus1 "createPipe" (c_pipe p)
- r <- peekElemOff p 0
- hr <- openFd (fromIntegral r) (Just Stream) ("<fd="++show r++")>") ReadMode
- False{-text mode-} False{-don't truncate-}
- w <- peekElemOff p 1
- hw <- openFd (fromIntegral w) (Just Stream) ("<fd="++show r++")>") WriteMode
- False{-text mode-} False{-don't truncate-}
- return (hr,hw)
-#else
-
- alloca $ \pFdRead ->
- alloca $ \pFdWrite ->
- do{ r <- winCreatePipe (fromIntegral textmode) (fromIntegral inherit) 4096 pFdRead pFdWrite
- ; when (r/=0) (ioError (userError ("unable to create pipe")))
- ; fdRead <- do{ fd <- peek pFdRead
- ; case mode of
- WriteMode -> inheritFd fd -- a child process must be able to read from it
- other -> return fd
- }
- ; fdWrite <- do{ fd <- peek pFdWrite
- ; case mode of
- ReadMode -> inheritFd fd -- a child process must be able to write to it
- other -> return fd
- }
- ; hRead <- openFd (fromIntegral fd) (Just Stream)
- "<pipe(read)>" ReadMode textmode False
- ; hWrite <- openFd (fromIntegral fd) (Just Stream)
- "<pipe(write)>" WriteMode textmode False
- ; return (hRead,hWrite)
- }
- where
- (mode,textmode) = case modeEx of
- TextMode mode -> (mode,1::Int)
- BinaryMode mode -> (mode,0::Int)
-
- inherit :: Int
- inherit = case mode of
- ReadMode -> 0 -- not inheritable
- WriteMode -> 0 -- not inheritable
- ReadWriteMode -> 0 -- not inheritable
- AppendMode -> 1 -- both inheritable
-
-inheritFd :: CInt -> IO CInt
-inheritFd fd0
- = do{ fd1 <- c_dup fd0 -- dup() makes a file descriptor inheritable
- ; c_close fd0
- ; return fd1
- }
-#endif
-#endif /* mingw32_TARGET_OS */
-
--- ---------------------------------------------------------------------------
-- memcpy wrappers
foreign import ccall unsafe "__hscore_memcpy_src_off"