#7353: Make system IO interruptible on Windows
---------------------------------+------------------------------------------
Reporter: joeyadams | Owner:
Type: bug | Status: new
Priority: normal | Milestone: 7.8.1
Component: libraries/base | Version: 7.6.1
Keywords: | Os: Windows
Architecture: Unknown/Multiple | Failure: Incorrect result at runtime
Difficulty: Unknown | Testcase:
Blockedby: | Blocking:
Related: |
---------------------------------+------------------------------------------
Comment(by joeyadams):
My IOCP manager, https://github.com/joeyadams/haskell-iocp, is now fairly
complete, and supports updatable timeouts like GHC.Event has. Where
credit is due:
* Felix Martini, author of the [http://hackage.haskell.org/package/winio
winio package]. This code clarified how to use the completion port API,
and gave me the idea to use
[http://hackage.haskell.org/packages/archive/base/latest/doc/html/Foreign-
StablePtr.html StablePtr] to carry context alongside the `OVERLAPPED`
structure.
* Authors of GHC.Event, for paving the way and identifying issues such as
#3838 . The updatable timeout support in haskell-iocp is largely based on
that from GHC.
Quick overview of the I/O manager design:
At the core is a thread which alternates between waiting for completions
and calling expired timeout callbacks (much like GHC.Event does). Each
completion carries a callback that the I/O manager thread simply executes.
`registerTimeout` sends a callback to the I/O manager telling it to update
its state. This eliminates the need for an `IORef` to hold the timeout
queue.
Overlapped I/O operations, on the other hand, do have to fight over an
IORef. Because [http://msdn.microsoft.com/en-
us/library/windows/desktop/aa363791%28v=vs.85%29.aspx CancelIo] does not
let us specify the operation to cancel, we have to use separate worker
threads if there are multiple concurrent operations on the same handle.
`withOverlapped` wraps system calls that support overlapped I/O, using the
I/O manager to wait for completion. It basically works as follows:
* Select a worker thread from which to initiate the I/O. Use the main
I/O manager thread (same thread that waits for completions and handles
timeouts) whenever it is available, to avoid an expensive context switch.
* In this worker thread, allocate an `OVERLAPPED` with a callback that
signals completion to the application thread. Call the `StartCallback`
with this `OVERLAPPED`.
* If `withOverlapped` is interrupted by an asynchronous exception, issue
`CancelIo` from the same worker thread that started the I/O.
* Do not let the application thread proceed until we are done operating
on the handle.
Can we add this I/O manager to the base package? If so, what should the
package be named? `GHC.Event.IOCP` ?
My goal is to make socket operations interruptible on Windows with
`-threaded`. I don't have time to extend it to file I/O right now.
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/7353#comment:8>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs