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