annotate win32-mingw/include/kj/async-win32.h @ 150:0a1a4a299a5d

OSX binaries for Cap'n Proto
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 05 Jul 2017 09:46:34 +0100
parents 279b18cc7785
children
rev   line source
cannam@149 1 // Copyright (c) 2016 Sandstorm Development Group, Inc. and contributors
cannam@149 2 // Licensed under the MIT License:
cannam@149 3 //
cannam@149 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@149 5 // of this software and associated documentation files (the "Software"), to deal
cannam@149 6 // in the Software without restriction, including without limitation the rights
cannam@149 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@149 8 // copies of the Software, and to permit persons to whom the Software is
cannam@149 9 // furnished to do so, subject to the following conditions:
cannam@149 10 //
cannam@149 11 // The above copyright notice and this permission notice shall be included in
cannam@149 12 // all copies or substantial portions of the Software.
cannam@149 13 //
cannam@149 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@149 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@149 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@149 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@149 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@149 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@149 20 // THE SOFTWARE.
cannam@149 21
cannam@149 22 #ifndef KJ_ASYNC_WIN32_H_
cannam@149 23 #define KJ_ASYNC_WIN32_H_
cannam@149 24
cannam@149 25 #if !_WIN32
cannam@149 26 #error "This file is Windows-specific. On Unix, include async-unix.h instead."
cannam@149 27 #endif
cannam@149 28
cannam@149 29 #include "async.h"
cannam@149 30 #include "time.h"
cannam@149 31 #include "io.h"
cannam@149 32 #include <atomic>
cannam@149 33 #include <inttypes.h>
cannam@149 34
cannam@149 35 // Include windows.h as lean as possible. (If you need more of the Windows API for your app,
cannam@149 36 // #include windows.h yourself before including this header.)
cannam@149 37 #define WIN32_LEAN_AND_MEAN 1
cannam@149 38 #define NOSERVICE 1
cannam@149 39 #define NOMCX 1
cannam@149 40 #define NOIME 1
cannam@149 41 #include <windows.h>
cannam@149 42 #include "windows-sanity.h"
cannam@149 43
cannam@149 44 namespace kj {
cannam@149 45
cannam@149 46 class Win32EventPort: public EventPort {
cannam@149 47 // Abstract base interface for EventPorts that can listen on Win32 event types. Due to the
cannam@149 48 // absurd complexity of the Win32 API, it's not possible to standardize on a single
cannam@149 49 // implementation of EventPort. In particular, there is no way for a single thread to use I/O
cannam@149 50 // completion ports (the most efficient way of handling I/O) while at the same time waiting for
cannam@149 51 // signalable handles or UI messages.
cannam@149 52 //
cannam@149 53 // Note that UI messages are not supported at all by this interface because the message queue
cannam@149 54 // is implemented by user32.dll and we want libkj to depend only on kernel32.dll. A separate
cannam@149 55 // compat library could provide a Win32EventPort implementation that works with the UI message
cannam@149 56 // queue.
cannam@149 57
cannam@149 58 public:
cannam@149 59 // ---------------------------------------------------------------------------
cannam@149 60 // overlapped I/O
cannam@149 61
cannam@149 62 struct IoResult {
cannam@149 63 DWORD errorCode;
cannam@149 64 DWORD bytesTransferred;
cannam@149 65 };
cannam@149 66
cannam@149 67 class IoOperation {
cannam@149 68 public:
cannam@149 69 virtual LPOVERLAPPED getOverlapped() = 0;
cannam@149 70 // Gets the OVERLAPPED structure to pass to the Win32 I/O call. Do NOT modify it; just pass it
cannam@149 71 // on.
cannam@149 72
cannam@149 73 virtual Promise<IoResult> onComplete() = 0;
cannam@149 74 // After making the Win32 call, if the return value indicates that the operation was
cannam@149 75 // successfully queued (i.e. the completion event will definitely occur), call this to wait
cannam@149 76 // for completion.
cannam@149 77 //
cannam@149 78 // You MUST call this if the operation was successfully queued, and you MUST NOT call this
cannam@149 79 // otherwise. If the Win32 call failed (without queuing any operation or event) then you should
cannam@149 80 // simply drop the IoOperation object.
cannam@149 81 //
cannam@149 82 // Dropping the returned Promise cancels the operation via Win32's CancelIoEx(). The destructor
cannam@149 83 // will wait for the cancellation to complete, such that after dropping the proimse it is safe
cannam@149 84 // to free the buffer that the operation was reading from / writing to.
cannam@149 85 //
cannam@149 86 // You may safely drop the `IoOperation` while still waiting for this promise. You may not,
cannam@149 87 // however, drop the `IoObserver`.
cannam@149 88 };
cannam@149 89
cannam@149 90 class IoObserver {
cannam@149 91 public:
cannam@149 92 virtual Own<IoOperation> newOperation(uint64_t offset) = 0;
cannam@149 93 // Begin an I/O operation. For file operations, `offset` is the offset within the file at
cannam@149 94 // which the operation will start. For stream operations, `offset` is ignored.
cannam@149 95 };
cannam@149 96
cannam@149 97 virtual Own<IoObserver> observeIo(HANDLE handle) = 0;
cannam@149 98 // Given a handle which supports overlapped I/O, arrange to receive I/O completion events via
cannam@149 99 // this EventPort.
cannam@149 100 //
cannam@149 101 // Different Win32EventPort implementations may handle this in different ways, such as by using
cannam@149 102 // completion routines (APCs) or by using I/O completion ports. The caller should not assume
cannam@149 103 // any particular technique.
cannam@149 104 //
cannam@149 105 // WARNING: It is only safe to call observeIo() on a particular handle once during its lifetime.
cannam@149 106 // You cannot observe the same handle from multiple Win32EventPorts, even if not at the same
cannam@149 107 // time. This is because the Win32 API provides no way to disassociate a handle from an I/O
cannam@149 108 // completion port once it is associated.
cannam@149 109
cannam@149 110 // ---------------------------------------------------------------------------
cannam@149 111 // signalable handles
cannam@149 112 //
cannam@149 113 // Warning: Due to limitations in the Win32 API, implementations of EventPort may be forced to
cannam@149 114 // spawn additional threads to wait for signaled objects. This is necessary if the EventPort
cannam@149 115 // implementation is based on I/O completion ports, or if you need to wait on more than 64
cannam@149 116 // handles at once.
cannam@149 117
cannam@149 118 class SignalObserver {
cannam@149 119 public:
cannam@149 120 virtual Promise<void> onSignaled() = 0;
cannam@149 121 // Returns a promise that completes the next time the handle enters the signaled state.
cannam@149 122 //
cannam@149 123 // Depending on the type of handle, the handle may automatically be reset to a non-signaled
cannam@149 124 // state before the promise resolves. The underlying implementaiton uses WaitForSingleObject()
cannam@149 125 // or an equivalent wait call, so check the documentation for that to understand the semantics.
cannam@149 126 //
cannam@149 127 // If the handle is a mutex and it is abandoned without being unlocked, the promise breaks with
cannam@149 128 // an exception.
cannam@149 129
cannam@149 130 virtual Promise<bool> onSignaledOrAbandoned() = 0;
cannam@149 131 // Like onSingaled(), but instead of throwing when a mutex is abandoned, resolves to `true`.
cannam@149 132 // Resolves to `false` for non-abandoned signals.
cannam@149 133 };
cannam@149 134
cannam@149 135 virtual Own<SignalObserver> observeSignalState(HANDLE handle) = 0;
cannam@149 136 // Given a handle that supports waiting for it to become "signaled" via WaitForSingleObject(),
cannam@149 137 // return an object that can wait for this state using the EventPort.
cannam@149 138
cannam@149 139 // ---------------------------------------------------------------------------
cannam@149 140 // APCs
cannam@149 141
cannam@149 142 virtual void allowApc() = 0;
cannam@149 143 // If this is ever called, the Win32EventPort will switch modes so that APCs can be scheduled
cannam@149 144 // on the thread, e.g. through the Win32 QueueUserAPC() call. In the future, this may be enabled
cannam@149 145 // by default. However, as of this writing, Wine does not support the necessary
cannam@149 146 // GetQueuedCompletionStatusEx() call, thus allowApc() breaks Wine support. (Tested on Wine
cannam@149 147 // 1.8.7.)
cannam@149 148 //
cannam@149 149 // If the event port implementation can't support APCs for some reason, this throws.
cannam@149 150
cannam@149 151 // ---------------------------------------------------------------------------
cannam@149 152 // time
cannam@149 153
cannam@149 154 virtual Timer& getTimer() = 0;
cannam@149 155 };
cannam@149 156
cannam@149 157 class Win32WaitObjectThreadPool {
cannam@149 158 // Helper class that implements Win32EventPort::observeSignalState() by spawning additional
cannam@149 159 // threads as needed to perform the actual waiting.
cannam@149 160 //
cannam@149 161 // This class is intended to be used to assist in building Win32EventPort implementations.
cannam@149 162
cannam@149 163 public:
cannam@149 164 Win32WaitObjectThreadPool(uint mainThreadCount = 0);
cannam@149 165 // `mainThreadCount` indicates the number of objects the main thread is able to listen on
cannam@149 166 // directly. Typically this would be zero (e.g. if the main thread watches an I/O completion
cannam@149 167 // port) or MAXIMUM_WAIT_OBJECTS (e.g. if the main thread is a UI thread but can use
cannam@149 168 // MsgWaitForMultipleObjectsEx() to wait on some handles at the same time as messages).
cannam@149 169
cannam@149 170 Own<Win32EventPort::SignalObserver> observeSignalState(HANDLE handle);
cannam@149 171 // Implemetns Win32EventPort::observeSignalState().
cannam@149 172
cannam@149 173 uint prepareMainThreadWait(HANDLE* handles[]);
cannam@149 174 // Call immediately before invoking WaitForMultipleObjects() or similar in the main thread.
cannam@149 175 // Fills in `handles` with the handle pointers to wait on, and returns the number of handles
cannam@149 176 // in this array. (The array should be allocated to be at least the size passed to the
cannam@149 177 // constructor).
cannam@149 178 //
cannam@149 179 // There's no need to call this if `mainThreadCount` as passed to the constructor was zero.
cannam@149 180
cannam@149 181 bool finishedMainThreadWait(DWORD returnCode);
cannam@149 182 // Call immediately after invoking WaitForMultipleObjects() or similar in the main thread,
cannam@149 183 // passing the value returend by that call. Returns true if the event indicated by `returnCode`
cannam@149 184 // has been handled (i.e. it was WAIT_OBJECT_n or WAIT_ABANDONED_n where n is in-range for the
cannam@149 185 // last call to prepareMainThreadWait()).
cannam@149 186 };
cannam@149 187
cannam@149 188 class Win32IocpEventPort final: public Win32EventPort {
cannam@149 189 // An EventPort implementation which uses Windows I/O completion ports to listen for events.
cannam@149 190 //
cannam@149 191 // With this implementation, observeSignalState() requires spawning a separate thread.
cannam@149 192
cannam@149 193 public:
cannam@149 194 Win32IocpEventPort();
cannam@149 195 ~Win32IocpEventPort() noexcept(false);
cannam@149 196
cannam@149 197 // implements EventPort ------------------------------------------------------
cannam@149 198 bool wait() override;
cannam@149 199 bool poll() override;
cannam@149 200 void wake() const override;
cannam@149 201
cannam@149 202 // implements Win32IocpEventPort ---------------------------------------------
cannam@149 203 Own<IoObserver> observeIo(HANDLE handle) override;
cannam@149 204 Own<SignalObserver> observeSignalState(HANDLE handle) override;
cannam@149 205 Timer& getTimer() override { return timerImpl; }
cannam@149 206 void allowApc() override { isAllowApc = true; }
cannam@149 207
cannam@149 208 private:
cannam@149 209 class IoPromiseAdapter;
cannam@149 210 class IoOperationImpl;
cannam@149 211 class IoObserverImpl;
cannam@149 212
cannam@149 213 AutoCloseHandle iocp;
cannam@149 214 AutoCloseHandle thread;
cannam@149 215 Win32WaitObjectThreadPool waitThreads;
cannam@149 216 TimerImpl timerImpl;
cannam@149 217 mutable std::atomic<bool> sentWake {false};
cannam@149 218 bool isAllowApc = false;
cannam@149 219
cannam@149 220 static TimePoint readClock();
cannam@149 221
cannam@149 222 void waitIocp(DWORD timeoutMs);
cannam@149 223 // Wait on the I/O completion port for up to timeoutMs and pump events. Does not advance the
cannam@149 224 // timer; caller must do that.
cannam@149 225
cannam@149 226 bool receivedWake();
cannam@149 227
cannam@149 228 static AutoCloseHandle newIocpHandle();
cannam@149 229 static AutoCloseHandle openCurrentThread();
cannam@149 230 };
cannam@149 231
cannam@149 232 } // namespace kj
cannam@149 233
cannam@149 234 #endif // KJ_ASYNC_WIN32_H_