@@ -112,9 +112,10 @@ mkTransport = do
112 -- OPTION (1)
112 -- OPTION (1)
113 -- Here we protect from blocking other threads by running on a separate (OS) thread:
113 -- Here we protect from blocking other threads by running on a separate (OS) thread:
114-- mv <- onOSThread$ PIO.openFd filename PIO.ReadWrite Nothing fileFlags
114-- mv <- onOSThread$ PIO.openFd filename PIO.ReadWrite Nothing fileFlags
115+ dbgprint1$ "About to try opening writing end:"
115 mv <- onOSThread$ tryUntilNoIOErr $
116 mv <- onOSThread$ tryUntilNoIOErr $
116 PIO.openFd filename PIO.WriteOnly Nothing fileFlags
117 PIO.openFd filename PIO.WriteOnly Nothing fileFlags
117− fd <- takeMVar mv
118+-- fd <- takeMVar mv
119 -- OPTION (2) / (3)
120 -- OPTION (2) / (3)
120-- spinTillThere filename
121-- spinTillThere filename
@@ -143,7 +144,7 @@ mkTransport = do
143 dbgprint1$ " Final send msg: " ++ show finalmsg
144 dbgprint1$ " Final send msg: " ++ show finalmsg
145 -- OPTION 2: Speculative file opening, plus this synchronnization:
146 -- OPTION 2: Speculative file opening, plus this synchronnization:
146−-- fd <- readMVar mv
147+ fd <- readMVar mv
147 ----------------------------------------
148 ----------------------------------------
148 cnt <- PIO.fdWrite fd (fromBS finalmsg) -- inefficient to use String here!
149 cnt <- PIO.fdWrite fd (fromBS finalmsg) -- inefficient to use String here!
149 unless (fromIntegral cnt == BS.length finalmsg) $
150 unless (fromIntegral cnt == BS.length finalmsg) $
@@ -163,6 +164,7 @@ mkTransport = do
163 dbgprint2$ " (got lock)"
164 dbgprint2$ " (got lock)"
165 spinTillThere filename
166 spinTillThere filename
167+ -- Opening the file on the reader side should always succeed:
166 fd <- PIO.openFd filename PIO.ReadOnly Nothing fileFlags
168 fd <- PIO.openFd filename PIO.ReadOnly Nothing fileFlags
167-- fd <- PIO.openFd filename PIO.ReadWrite Nothing fileFlags
169-- fd <- PIO.openFd filename PIO.ReadWrite Nothing fileFlags
168-- mv <- onOSThread$ PIO.openFd filename PIO.ReadWrite Nothing fileFlags
170-- mv <- onOSThread$ PIO.openFd filename PIO.ReadWrite Nothing fileFlags
@@ -224,47 +226,38 @@ mkTransport = do
225spinTillThere :: String -> IO ()
227spinTillThere :: String -> IO ()
226spinTillThere filename =
228spinTillThere filename =
227− do dbgprint2$ " Spinning till file present: "++ filename
229+ do dbgprint2$ " Spinning till file present: "++ filename
228− loop 1
230+ mkBackoff >>= loop
231− loop t | t > maxwait = loop maxwait
232− loop t = do b <- doesFileExist filename
232+ loop bkoff = do b <- doesFileExist filename
233− unless b $ do
233+ unless b $ do bkoff; loop bkoff
239−-- loop $ round (fromIntegral t * 1.5)
241−data InfIO = InfIO (IO (InfIO))
243−-- mkBackoff :: IO (IO ())
236+mkBackoff :: IO (IO ())
245mkBackoff =
237mkBackoff =
246 do tref <- newIORef 1
238 do tref <- newIORef 1
247 return$ do t <- readIORef tref
239 return$ do t <- readIORef tref
248− writeIORef tref (min (10 * 1000) (2 * t))
240+ writeIORef tref (min maxwait (2 * t))
249 threadDelay t
241 threadDelay t
251tryUntilNoIOErr :: IO a -> IO a
245tryUntilNoIOErr :: IO a -> IO a
252tryUntilNoIOErr action = mkBackoff >>= loop
246tryUntilNoIOErr action = mkBackoff >>= loop
254 loop bkoff =
248 loop bkoff =
255− handle (\ (e :: IOException) -> bkoff >> loop bkoff) $
249+ handle (\ (e :: IOException) ->
251+ dbgprint2$ " got IO exn: " ++ show e
258−
255+-- Execute an action on its own OS thread. Return an MVar to synchronize on.
259−-- loop (InfIO bkoff) =
256+onOSThread :: IO a -> IO (MVar a)
260−-- handle (\ (e :: IOException) ->
257+onOSThread action = do
261−-- do InfIO nxt <- bkoff
258+ mv <- newEmptyMVar
262−-- loop nxt) $
259+ forkOS (action >>= putMVar mv )
263−-- action
260+ return mv
269#ifdef DEBUG
262#ifdef DEBUG
270dbgprint1 s = do BSS.hPutStrLn stderr (BSS.pack s); hFlush stderr;
263dbgprint1 s = do BSS.hPutStrLn stderr (BSS.pack s); hFlush stderr;
@@ -274,11 +267,3 @@ dbgprint1 _ = return ()
274dbgprint2 _ = return ()
267dbgprint2 _ = return ()
278−-- Execute an action on its own OS thread. Return an MVar to synchronize on.
279−onOSThread :: IO a -> IO (MVar a)
280−onOSThread action = do
282− forkOS (action >>= putMVar mv )
284−-- [2012.02.19] This didn't seem to help.