throwErrnoIfMinus1 "startIOManagerThread" (c_pipe fds)
rd_end <- peekElemOff fds 0
wr_end <- peekElemOff fds 1
- setNonBlockingFD wr_end -- writes happen in a signal handler, we
- -- don't want them to block.
+ setNonBlockingFD wr_end True -- writes happen in a signal handler, we
+ -- don't want them to block.
setCloseOnExec rd_end
setCloseOnExec wr_end
writeIORef stick (fromIntegral wr_end)
c_dup2 (fdFD fd) (fdFD fdto)
return fd{ fdFD = fdFD fdto } -- original FD, with the new fdFD
-setNonBlockingMode :: FD -> IO ()
-setNonBlockingMode fd = setNonBlockingFD (fdFD fd)
+setNonBlockingMode :: FD -> Bool -> IO FD
+setNonBlockingMode fd set = do
+ setNonBlockingFD (fdFD fd) set
+ return fd{ fdIsNonBlocking = fromEnum set }
ready :: FD -> Bool -> Int -> IO Bool
ready fd write msecs = do
-> Maybe TextEncoding
-> IO Handle
-mkHandleFromFD fd fd_type filepath iomode set_non_blocking mb_codec
+mkHandleFromFD fd0 fd_type filepath iomode set_non_blocking mb_codec
= do
#ifndef mingw32_HOST_OS
- when set_non_blocking $ FD.setNonBlockingMode fd
-- turn on non-blocking mode
+ fd <- if set_non_blocking
+ then FD.setNonBlockingMode fd0 True
+ else return fd0
#else
let _ = set_non_blocking -- warning suppression
+ fd <- return fd0
#endif
let nl | isJust mb_codec = nativeNewlineMode
-- ---------------------------------------------------------------------------
-- Turning on non-blocking for a file descriptor
-setNonBlockingFD :: FD -> IO ()
+setNonBlockingFD :: FD -> Bool -> IO ()
#if !defined(mingw32_HOST_OS) && !defined(__MINGW32__)
-setNonBlockingFD fd = do
+setNonBlockingFD fd set = do
flags <- throwErrnoIfMinus1Retry "setNonBlockingFD"
(c_fcntl_read fd const_f_getfl)
-- An error when setting O_NONBLOCK isn't fatal: on some systems
-- there are certain file handles on which this will fail (eg. /dev/null
-- on FreeBSD) so we throw away the return code from fcntl_write.
- unless (testBit flags (fromIntegral o_NONBLOCK)) $ do
- c_fcntl_write fd const_f_setfl (fromIntegral (flags .|. o_NONBLOCK))
+ let flags' | set = flags .|. o_NONBLOCK
+ | otherwise = flags .&. complement o_NONBLOCK
+ unless (flags == flags') $ do
+ c_fcntl_write fd const_f_setfl (fromIntegral flags')
return ()
#else
-- bogus defns for win32
-setNonBlockingFD _ = return ()
+setNonBlockingFD _ _ = return ()
#endif