I really like the manner in which C# async tasks work. And while building Voron, I run into a scenario in which I could really make use of Windows async API. This is exposed via the Overlapped I/O. The problem is that those are pretty different models, and they don’t appear to want to play together very nicely.
Since I don’t feel like having those two cohabitate in my codebase, I decided to see if I could write a TPL wrapper that would provide nice API on top of the underlying Overlapped I/O implementation.
Here is what I ended up with:
1: public unsafe class Win32DirectFile : IDisposable
3: private readonly SafeFileHandle _handle;
5: public Win32DirectFile(string filename)
7: _handle = NativeFileMethods.CreateFile(filename,
8: NativeFileAccess.GenericWrite | NativeFileAccess.GenericWrite, NativeFileShare.None, IntPtr.Zero,
10: NativeFileAttributes.Write_Through | NativeFileAttributes.NoBuffering | NativeFileAttributes.Overlapped, IntPtr.Zero);
12: if (_handle.IsInvalid)
13: throw new Win32Exception();
15: if(ThreadPool.BindHandle(_handle) == false)
16: throw new InvalidOperationException("Could not bind the handle to the thread pool");
Note that I create the file with overlapped enabled, as well as write_through & no buffering (I need them for something else, not relevant for now).
It it important to note that I bind the handle (which effectively issue a BindIoCompletionCallback under the cover, I think), so we won’t have to use events, but can use callbacks. This is much more natural manner to work when using the TPL.
Then, we can just issue the actual work:
1: public Task WriteAsync(long position, byte* ptr, uint length)
3: var tcs = new TaskCompletionSource<object>();
5: var nativeOverlapped = CreateNativeOverlapped(position, tcs);
7: uint written;
8: var result = NativeFileMethods.WriteFile(_handle, ptr, length, out written, nativeOverlapped);
10: return HandleResponse(result, nativeOverlapped, tcs);
As you can see, all the actual details are handled in the helper functions, we can just run the code we need, passing it the overlapped structure it requires. Now, let us look at those functions:
1: private static NativeOverlapped* CreateNativeOverlapped(long position, TaskCompletionSource<object> tcs)
3: var o = new Overlapped((int) (position & 0xffffffff), (int) (position >> 32), IntPtr.Zero, null);
4: var nativeOverlapped = o.Pack((code, bytes, overlap) =>
8: switch (code)
10: case ERROR_SUCCESS:
13: case ERROR_OPERATION_ABORTED:
17: tcs.TrySetException(new Win32Exception((int) code));
26: }, null);
27: return nativeOverlapped;
30: private static Task HandleResponse(bool completedSyncronously, NativeOverlapped* nativeOverlapped, TaskCompletionSource<object> tcs)
32: if (completedSyncronously)
37: return tcs.Task;
40: var lastWin32Error = Marshal.GetLastWin32Error();
41: if (lastWin32Error == ERROR_IO_PENDING)
42: return tcs.Task;
46: throw new Win32Exception(lastWin32Error);
The complexity here is that we need to handle 3 cases:
- Successful completion
- Error (no pending work)
- Error (actually success, work is done in an async manner).
But that seems to be working quite nicely for me so far.