annotate win32-mingw/include/kj/async-win32.h @ 79:91c729825bca pa_catalina

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