-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathXTaskQueue.h
More file actions
335 lines (304 loc) · 13.2 KB
/
XTaskQueue.h
File metadata and controls
335 lines (304 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright (c) Microsoft Corporation
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#if !defined(__cplusplus)
#error C++11 required
#endif
#pragma once
#include <stdint.h>
extern "C"
{
/// <summary>
/// A task queue contains and dispatches callbacks. A task queue has two ports: a worker port and
/// a completion port. Each port can have different rules for how queued calls
/// are dispatched.
/// </summary>
typedef struct XTaskQueueObject* XTaskQueueHandle;
/// <summary>
/// A task queue port represents one port of a task queue. A port's lifetime is controlled
/// by its owning task queue.
/// </summary>
typedef struct XTaskQueuePortObject* XTaskQueuePortHandle;
/// <summary>
/// Describes how task queue callbacks are processed.
/// </summary>
enum class XTaskQueueDispatchMode : uint32_t
{
/// <summary>
/// Callbacks are invoked manually by XTaskQueueDispatch
/// </summary>
Manual,
/// <summary>
/// Callbacks are queued to the system thread pool and will
/// be processed in order by the thread pool across multiple thread
/// pool threads.
/// </summary>
ThreadPool,
/// <summary>
/// Callbacks are queued to the system thread pool and
/// will be processed one at a time.
/// </summary>
SerializedThreadPool,
/// <summary>
/// Callbacks are not queued at all but are dispatched
/// immediately by the thread that submits them.
/// </summary>
Immediate
};
/// <summary>
/// Declares which port of a task queue to dispatch or submit
/// callbacks to.
/// </summary>
enum class XTaskQueuePort : uint32_t
{
/// <summary>
/// Work callbacks
/// </summary>
Work,
/// <summary>
/// Completion callbacks after work is done
/// </summary>
Completion
};
/// <summary>
/// A token returned when registering a callback to identify the registration. This token
/// is later used to unregister the callback.
/// </summary>
struct XTaskQueueRegistrationToken
{
uint64_t token;
};
/// <summary>
/// A callback that is invoked by the task queue.
/// </summary>
/// <param name='context'>A context pointer that was passed during XTaskQueueSubmitCallback.</param>
/// <param name='canceled'>If true, callbacks are being canceled because the queue is terminating.</param>
/// <seealso cref='XTaskQueueSubmitCallback' />
typedef void CALLBACK XTaskQueueCallback(_In_opt_ void* context, _In_ bool canceled);
/// <summary>
/// A callback that is invoked by the task queue whenever an item is submitted for execution.
/// </summary>
/// <param name='context'>A context pointer that was passed during XTaskQueueRegisterMonitor.</param>
/// <param name='queue'>The task queue where the callback was submitted.</param>
/// <param name='port'>The port the callback was submitted to.</param>
/// <seealso cref='XTaskQueueRegisterMonitor' />
typedef void CALLBACK XTaskQueueMonitorCallback(_In_opt_ void* context, _In_ XTaskQueueHandle queue, _In_ XTaskQueuePort port);
/// <summary>
/// A callback that is invoked when a task queue is terminated.
/// </summary>
/// <param name='context'>A context pointer that was passed during XTaskQueueTerminate.</param>
typedef void CALLBACK XTaskQueueTerminatedCallback(_In_opt_ void* context);
/// <summary>
/// Creates a Task Queue, which can be used to queue
/// and dispatch calls. Task Queues are
/// reference counted objects. Release the reference
/// by calling XTaskQueueCloseHandle.
/// </summary>
/// <param name='workDispatchMode'>The dispatch mode for the "work" port of the queue.</param>
/// <param name='completionDispatchMode'>The dispatch mode for the "completion" port of the queue.</param>
/// <param name='queue'>The newly created queue.</param>
STDAPI XTaskQueueCreate(
_In_ XTaskQueueDispatchMode workDispatchMode,
_In_ XTaskQueueDispatchMode completionDispatchMode,
_Out_ XTaskQueueHandle* queue
) noexcept;
/// <summary>
/// Creates a task queue composed of ports of other
/// task queues. A composite task queue will duplicate
/// the handles of queues that own the provided ports.
/// </summary>
/// <param name='workPort'>The port to use for queuing work callbacks.</param>
/// <param name='completionPort'>The port to use for queuing completion callbacks.</param>
STDAPI XTaskQueueCreateComposite(
_In_ XTaskQueuePortHandle workPort,
_In_ XTaskQueuePortHandle completionPort,
_Out_ XTaskQueueHandle* queue
) noexcept;
/// <summary>
/// Returns the task queue port handle for the given
/// port. Task queue port handles are owned by the
/// task queue and do not have to be closed. They are
/// used when creating composite task queues.
/// </summary>
/// <param name='queue'>The task queue to get the port from.</param>
/// <param name='port'>The port to get.</param>
STDAPI XTaskQueueGetPort(
_In_ XTaskQueueHandle queue,
_In_ XTaskQueuePort port,
_Out_ XTaskQueuePortHandle* portHandle
) noexcept;
/// <summary>
/// Duplicates the XTaskQueueHandle object. Use XTaskQueueCloseHandle to close it.
/// </summary>
/// <param name='queueHandle'>The queue to reference.</param>
/// <param name='duplicatedHandle'>The duplicated queue handle.</param>
STDAPI XTaskQueueDuplicateHandle(
_In_ XTaskQueueHandle queueHandle,
_Out_ XTaskQueueHandle* duplicatedHandle
) noexcept;
/// <summary>
/// Processes an item in the task queue for the given port. If an item
/// is processed this will return true. If there are no items to process
/// this returns false. You can pass a timeout, which will cause
/// XTaskQueueDispatch to wait for something to arrive in the queue.
/// </summary>
/// <param name='queue'>The queue to dispatch work on.</param>
/// <param name='port'>The port to dispatch.</param>
/// <param name='timeoutInMs'>The number of ms to wait for work to arrive before returning false.</param>
STDAPI_(bool) XTaskQueueDispatch(
_In_ XTaskQueueHandle queue,
_In_ XTaskQueuePort port,
_In_ uint32_t timeoutInMs
) noexcept;
/// <summary>
/// Closes the task queue. The queue is not actually destroyed until
/// all handles are closed and there are no outstanding callbacks
/// enqueued.
/// </summary>
/// <param name='queue'>The queue to close.</param>
STDAPI_(void) XTaskQueueCloseHandle(
_In_ XTaskQueueHandle queue
) noexcept;
/// <summary>
/// Terminates a task queue by canceling all pending items and
/// preventing new items from being queued. Once a queue is terminated
/// its handle can be closed. New items cannot be enqueued to a task
/// queue that has been terminated.
///
/// Each task queue terminates independently. For composite queues created
/// with XTaskQueueCreateComposite, terminating a composite queue does NOT
/// automatically terminate other queues sharing the same ports.
///
/// When wait=true:
/// - Blocks until this queue's termination callback completes
/// - Does NOT wait for other independent queues (including composite delegates)
/// - Ensures this queue's termination callback has finished executing
/// - Safe to unload code/modules after this returns
///
/// When wait=false:
/// - Returns immediately after initiating termination
/// - The termination callback will be invoked asynchronously when termination completes
///
/// The termination callback is invoked after all work and completion callbacks
/// have been canceled or completed. After the termination callback returns, the
/// implementation will no longer access the queue's internal state.
///
/// For coordinated shutdown of multiple queues sharing ports, use termination
/// callbacks to track completion of each queue before performing final cleanup.
/// </summary>
/// <param name='queue'>The queue to terminate.</param>
/// <param name='wait'>True to wait for the termination to complete.</param>
/// <param name='callbackContext'>An optional context pointer to pass to the callback.</param>
/// <param name='callback'>An optional callback that will be called when the queue has terminated.</param>
STDAPI XTaskQueueTerminate(
_In_ XTaskQueueHandle queue,
_In_ bool wait,
_In_opt_ void* callbackContext,
_In_opt_ XTaskQueueTerminatedCallback* callback
) noexcept;
/// <summary>
/// Submits a callback to the queue for the given port.
/// </summary>
/// <param name='queue'>The queue to submit the callback to.</param>
/// <param name='port'>The port to submit the callback to.</param>
/// <param name='callbackContext'>An optional context pointer that will be passed to the callback.</param>
/// <param name='callback'>A pointer to the callback function.</param>
STDAPI XTaskQueueSubmitCallback(
_In_ XTaskQueueHandle queue,
_In_ XTaskQueuePort port,
_In_opt_ void* callbackContext,
_In_ XTaskQueueCallback* callback
) noexcept;
/// <summary>
/// Submits a callback to the queue for the given port. The callback will be added
/// to the queue after delayMs milliseconds.
/// </summary>
/// <param name='queue'>The queue to submit the callback to.</param>
/// <param name='port'>The port to submit the callback to.</param>
/// <param name='delayMs'>The number of milliseconds to delay before submitting the callback to the queue.</param>
/// <param name='callbackContext'>An optional context pointer that will be passed to the callback.</param>
/// <param name='callback'>A pointer to the callback function.</param>
STDAPI XTaskQueueSubmitDelayedCallback(
_In_ XTaskQueueHandle queue,
_In_ XTaskQueuePort port,
_In_ uint32_t delayMs,
_In_opt_ void* callbackContext,
_In_ XTaskQueueCallback* callback
) noexcept;
/// <summary>
/// Registers a wait handle with the task queue. When the wait handle
/// is satisfied the task queue will invoke the given callback. This
/// provides an efficient way to add items to a task queue in
/// response to handles becoming signaled.
/// </summary>
/// <param name='queue'>The queue to submit the callback to.</param>
/// <param name='port'>The port to invoke the callback on.</param>
/// <param name='waitHandle'>The handle to monitor.</param>
/// <param name='callbackContext'>An optional context pointer that will be passed to the callback.</param>
/// <param name='callback'>A pointer to the callback function.</param>
/// <param name='token'>A registration token.</param>
STDAPI XTaskQueueRegisterWaiter(
_In_ XTaskQueueHandle queue,
_In_ XTaskQueuePort port,
_In_ HANDLE waitHandle,
_In_opt_ void* callbackContext,
_In_ XTaskQueueCallback* callback,
_Out_ XTaskQueueRegistrationToken* token
) noexcept;
/// <summary>
/// Unregisters a previously registered task queue waiter.
/// </summary>
/// <param name='queue'>The queue to remove the waiter from.</param>
/// <param name='token'>The token returned from XTaskQueueRegisterWaiter.</param>
STDAPI_(void) XTaskQueueUnregisterWaiter(
_In_ XTaskQueueHandle queue,
_In_ XTaskQueueRegistrationToken token
) noexcept;
/// <summary>
/// Registers a callback that will be invoked whenever a callback
/// is submitted to this queue.
/// </summary>
/// <param name='queue'>The queue to register the submit callback to.</param>
/// <param name='callbackContext'>An optional context pointer to be passed to the callback.</param>
/// <param name='callback'>A callback that will be invoked when a new callback is submitted to the queue.</param>
/// <param name='token'>A token used in a later call to XTaskQueueUnregisterMonitor to remove the callback.</param>
STDAPI XTaskQueueRegisterMonitor(
_In_ XTaskQueueHandle queue,
_In_opt_ void* callbackContext,
_In_ XTaskQueueMonitorCallback* callback,
_Out_ XTaskQueueRegistrationToken* token
) noexcept;
/// <summary>
/// Unregisters a previously registered callback. This blocks if there are outstanding montior
/// callbacks.
/// </summary>
/// <param name='queue'>The queue to remove the submit callback from.</param>
/// <param name='token'>The token returned from XTaskQueueRegisterMonitor.</param>
STDAPI_(void) XTaskQueueUnregisterMonitor(
_In_ XTaskQueueHandle queue,
_In_ XTaskQueueRegistrationToken token
) noexcept;
/// <summary>
/// Obtains a handle to the process task queue, or nullptr if there is no
/// process task queue. By default, there is a process task queue
/// that uses the thread pool for both work and completion ports. You
/// can replace the default process task queue by calling
/// XTaskQueueSetCurrentProcessTaskQueue, and you can prevent callers using
/// the process task queue by calling XTaskQueueSetCurrentProcessTaskQueue
/// with a null queue parameter.
///
/// This API returns true if there is a process task queue available.
/// You are responsible for calling XTaskQueueCloseHandle on the handle
/// returned from this API.
/// </summary>
STDAPI_(bool) XTaskQueueGetCurrentProcessTaskQueue(_Out_ XTaskQueueHandle* queue) noexcept;
/// <summary>
/// Sets the given task queue as the process wide task queue. The
/// queue can be set to nullptr, in which case XTaskQueueGetCurrentProcessTaskQueue will
/// also return nullptr. The provided queue will have its handle duplicated
/// and any existing process task queue will have its handle closed.
/// </summary>
/// <param name='queue'>The queue to set up as the default task queue for the procces.</param>
STDAPI_(void) XTaskQueueSetCurrentProcessTaskQueue(
_In_ XTaskQueueHandle queue
) noexcept;
} // extern "C"