xref: /xnu-11215.81.4/iokit/DriverKit/IODispatchQueue.iig (revision d4514f0bc1d3f944c22d92e68b646ac3fb40d452)
1*d4514f0bSApple OSS Distributions/*
2*d4514f0bSApple OSS Distributions * Copyright (c) 2019-2019 Apple Inc. All rights reserved.
3*d4514f0bSApple OSS Distributions *
4*d4514f0bSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*d4514f0bSApple OSS Distributions *
6*d4514f0bSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*d4514f0bSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*d4514f0bSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*d4514f0bSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*d4514f0bSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*d4514f0bSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*d4514f0bSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*d4514f0bSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*d4514f0bSApple OSS Distributions *
15*d4514f0bSApple OSS Distributions * Please obtain a copy of the License at
16*d4514f0bSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*d4514f0bSApple OSS Distributions *
18*d4514f0bSApple OSS Distributions * The Original Code and all software distributed under the License are
19*d4514f0bSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*d4514f0bSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*d4514f0bSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*d4514f0bSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*d4514f0bSApple OSS Distributions * Please see the License for the specific language governing rights and
24*d4514f0bSApple OSS Distributions * limitations under the License.
25*d4514f0bSApple OSS Distributions *
26*d4514f0bSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*d4514f0bSApple OSS Distributions */
28*d4514f0bSApple OSS Distributions
29*d4514f0bSApple OSS Distributions#ifndef _IOKIT_UIODISPATCHQUEUE_H
30*d4514f0bSApple OSS Distributions#define _IOKIT_UIODISPATCHQUEUE_H
31*d4514f0bSApple OSS Distributions
32*d4514f0bSApple OSS Distributions#include <DriverKit/OSObject.iig>
33*d4514f0bSApple OSS Distributions#include <DriverKit/OSAction.iig>
34*d4514f0bSApple OSS Distributions#include <DriverKit/IODispatchSource.iig>
35*d4514f0bSApple OSS Distributions
36*d4514f0bSApple OSS Distributionstypedef int (*IODispatchLogFunction)(const char *format, ...);
37*d4514f0bSApple OSS Distributionstypedef void (^IODispatchBlock)(void);
38*d4514f0bSApple OSS Distributionstypedef void (*IODispatchFunction)(void * context);
39*d4514f0bSApple OSS Distributionstypedef kern_return_t (^IODispatchAction)(void);
40*d4514f0bSApple OSS Distributions
41*d4514f0bSApple OSS Distributionstypedef void (^IODispatchQueueCancelHandler)(void);
42*d4514f0bSApple OSS Distributions
43*d4514f0bSApple OSS Distributions
44*d4514f0bSApple OSS Distributions// options for Create()
45*d4514f0bSApple OSS Distributionsenum {
46*d4514f0bSApple OSS Distributions	kIODispatchQueueReentrant              = 0x00000001,
47*d4514f0bSApple OSS Distributions	kIODispatchQueueMethodsNotSynchronized = 0x00000002,
48*d4514f0bSApple OSS Distributions};
49*d4514f0bSApple OSS Distributions
50*d4514f0bSApple OSS Distributions// options for WakeupWithOptions()
51*d4514f0bSApple OSS Distributionsenum {
52*d4514f0bSApple OSS Distributions	kIODispatchQueueWakeupAll              = 0x00000001,
53*d4514f0bSApple OSS Distributions};
54*d4514f0bSApple OSS Distributions
55*d4514f0bSApple OSS Distributions// options for SleepWithDeadline()
56*d4514f0bSApple OSS Distributionsenum {
57*d4514f0bSApple OSS Distributions	// Sleep on an event which other threads are still waiting on, but has already been signaled
58*d4514f0bSApple OSS Distributions	kIODispatchQueueSleepReuseEvent        = 0x00000100,
59*d4514f0bSApple OSS Distributions};
60*d4514f0bSApple OSS Distributions
61*d4514f0bSApple OSS Distributions/*!
62*d4514f0bSApple OSS Distributions * @class IODispatchQueue
63*d4514f0bSApple OSS Distributions *
64*d4514f0bSApple OSS Distributions * @abstract
65*d4514f0bSApple OSS Distributions * IODispatchQueue provides a queue for ordered execution of blocks.
66*d4514f0bSApple OSS Distributions *
67*d4514f0bSApple OSS Distributions * @discussion
68*d4514f0bSApple OSS Distributions * All blocks submitted to dispatch queues are dequeued in FIFO order.
69*d4514f0bSApple OSS Distributions * By default the queue is serial and will execute one block at a time.
70*d4514f0bSApple OSS Distributions */
71*d4514f0bSApple OSS Distributions
72*d4514f0bSApple OSS Distributionsclass NATIVE KERNEL IODispatchQueue : public OSObject
73*d4514f0bSApple OSS Distributions{
74*d4514f0bSApple OSS Distributionspublic:
75*d4514f0bSApple OSS Distributions    /*!
76*d4514f0bSApple OSS Distributions     * @brief       Creates a new dispatch queue object.
77*d4514f0bSApple OSS Distributions     * @discussion  Creates a new dispatch queue object. All queues are currently serial, executing one block at time
78*d4514f0bSApple OSS Distributions     *              FIFO order. The new object has retain count 1 and should be released by the caller.
79*d4514f0bSApple OSS Distributions     * @param       options
80*d4514f0bSApple OSS Distributions                    kIODispatchQueueReentrant Creates a queue that allows release with the Sleep
81*d4514f0bSApple OSS Distributions                    method, so that other threads and callers may acquire the queue.
82*d4514f0bSApple OSS Distributions     * @param       priority No priorities are currently defined, pass zero.
83*d4514f0bSApple OSS Distributions     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
84*d4514f0bSApple OSS Distributions     */
85*d4514f0bSApple OSS Distributions	static kern_return_t
86*d4514f0bSApple OSS Distributions	Create(
87*d4514f0bSApple OSS Distributions		const IODispatchQueueName name,
88*d4514f0bSApple OSS Distributions		uint64_t                  options,
89*d4514f0bSApple OSS Distributions		uint64_t                  priority,
90*d4514f0bSApple OSS Distributions		IODispatchQueue        ** queue) LOCAL;
91*d4514f0bSApple OSS Distributions
92*d4514f0bSApple OSS Distributions	virtual bool
93*d4514f0bSApple OSS Distributions	init() override;
94*d4514f0bSApple OSS Distributions
95*d4514f0bSApple OSS Distributions	virtual void
96*d4514f0bSApple OSS Distributions	free() override;
97*d4514f0bSApple OSS Distributions
98*d4514f0bSApple OSS Distributions    /*!
99*d4514f0bSApple OSS Distributions     * @brief       Determines if the current thread is running on the queue.
100*d4514f0bSApple OSS Distributions     * @discussion  Determines if the current thread is running on the queue, including if the queue invoked a
101*d4514f0bSApple OSS Distributions     *              second queue (ie. OnQueue can return true for more than one queue in a given context.)
102*d4514f0bSApple OSS Distributions     * @return      bool true if current thread is running on this queue.
103*d4514f0bSApple OSS Distributions     */
104*d4514f0bSApple OSS Distributions	bool
105*d4514f0bSApple OSS Distributions	OnQueue() LOCALONLY;
106*d4514f0bSApple OSS Distributions
107*d4514f0bSApple OSS Distributions    /*!
108*d4514f0bSApple OSS Distributions     * @brief       Return the name the queue was created with.
109*d4514f0bSApple OSS Distributions     * @discussion  Returns a pointer to the queues name. Only valid while the queue is retained.
110*d4514f0bSApple OSS Distributions     * @return      C-string pointer in the queues internal storage.
111*d4514f0bSApple OSS Distributions     */
112*d4514f0bSApple OSS Distributions	const char *
113*d4514f0bSApple OSS Distributions	GetName() LOCALONLY;
114*d4514f0bSApple OSS Distributions
115*d4514f0bSApple OSS Distributions    /*!
116*d4514f0bSApple OSS Distributions     * @brief       Stop the queue from executing futher work.
117*d4514f0bSApple OSS Distributions     * @discussion  Stops the queue from dequeuing work, and on completion of any block currently being executed,
118*d4514f0bSApple OSS Distributions     *              invokes a callback block. Canceling is asynchronous.
119*d4514f0bSApple OSS Distributions     * @param       handler Block that will executed when the queue has completed any inflight work
120*d4514f0bSApple OSS Distributions     *              and will not execute further work.
121*d4514f0bSApple OSS Distributions     * @return      C-string pointer in the queues internal storage.
122*d4514f0bSApple OSS Distributions     */
123*d4514f0bSApple OSS Distributions	kern_return_t
124*d4514f0bSApple OSS Distributions	Cancel(IODispatchQueueCancelHandler handler) LOCALONLY;
125*d4514f0bSApple OSS Distributions
126*d4514f0bSApple OSS Distributions    /*!
127*d4514f0bSApple OSS Distributions     * @brief       Schedule a block to be executed on the queue asynchronously.
128*d4514f0bSApple OSS Distributions     * @discussion  Schedules work to be done on the queue without waiting for it to complete. The queue will be
129*d4514f0bSApple OSS Distributions     *              retained until the block completes.
130*d4514f0bSApple OSS Distributions     * @param       block Block that will executed on the queue, not in the context of the caller.
131*d4514f0bSApple OSS Distributions     */
132*d4514f0bSApple OSS Distributions	void
133*d4514f0bSApple OSS Distributions	DispatchAsync(IODispatchBlock block) LOCALONLY;
134*d4514f0bSApple OSS Distributions
135*d4514f0bSApple OSS Distributions    /*!
136*d4514f0bSApple OSS Distributions     * @brief       C-function callback version of DispatchAsync.
137*d4514f0bSApple OSS Distributions	 */
138*d4514f0bSApple OSS Distributions	void
139*d4514f0bSApple OSS Distributions	DispatchAsync_f(void * context, IODispatchFunction function) LOCALONLY;
140*d4514f0bSApple OSS Distributions
141*d4514f0bSApple OSS Distributions    /*!
142*d4514f0bSApple OSS Distributions     * @brief       Schedule a block to be executed concurrently & asynchronously.
143*d4514f0bSApple OSS Distributions     * @discussion  Schedules work to be done on the queue without waiting for it to complete, and
144*d4514f0bSApple OSS Distributions     *              concurrently with other blocks executed with DispatchConcurrent. The queue will be
145*d4514f0bSApple OSS Distributions     *              retained until the block completes. May only be used with a queue created with
146*d4514f0bSApple OSS Distributions     *              the kIODispatchQueueReentrant option.
147*d4514f0bSApple OSS Distributions     * @param       block Block that will executed on the queue, not in the context of the caller.
148*d4514f0bSApple OSS Distributions     */
149*d4514f0bSApple OSS Distributions	void
150*d4514f0bSApple OSS Distributions	DispatchConcurrent(IODispatchBlock block) LOCALONLY;
151*d4514f0bSApple OSS Distributions
152*d4514f0bSApple OSS Distributions    /*!
153*d4514f0bSApple OSS Distributions     * @brief       C-function callback version of DispatchConcurrent.
154*d4514f0bSApple OSS Distributions	 */
155*d4514f0bSApple OSS Distributions	void
156*d4514f0bSApple OSS Distributions	DispatchConcurrent_f(void * context, IODispatchFunction function) LOCALONLY;
157*d4514f0bSApple OSS Distributions
158*d4514f0bSApple OSS Distributions    /*!
159*d4514f0bSApple OSS Distributions     * @brief       Execute a block on the queue synchronously.
160*d4514f0bSApple OSS Distributions     * @discussion  Execute a block on the queue synchronously.
161*d4514f0bSApple OSS Distributions     * @param       block Block that will executed on the queue.
162*d4514f0bSApple OSS Distributions     */
163*d4514f0bSApple OSS Distributions	void
164*d4514f0bSApple OSS Distributions	DispatchSync(IODispatchBlock block) LOCALONLY;
165*d4514f0bSApple OSS Distributions
166*d4514f0bSApple OSS Distributions    /*!
167*d4514f0bSApple OSS Distributions     * @brief       C-function callback version of DispatchSync.
168*d4514f0bSApple OSS Distributions	 */
169*d4514f0bSApple OSS Distributions	void
170*d4514f0bSApple OSS Distributions	DispatchSync_f(void * context, IODispatchFunction function) LOCALONLY;
171*d4514f0bSApple OSS Distributions
172*d4514f0bSApple OSS Distributions    /*!
173*d4514f0bSApple OSS Distributions     * @brief       Log the current execution context with respect to any queues the current thread holds.
174*d4514f0bSApple OSS Distributions     * @param       output printf like output function. The address of IOLog is suitable to be used.
175*d4514f0bSApple OSS Distributions	 */
176*d4514f0bSApple OSS Distributions	static void
177*d4514f0bSApple OSS Distributions	Log(const char * message, IODispatchLogFunction output) LOCALONLY;
178*d4514f0bSApple OSS Distributions
179*d4514f0bSApple OSS Distributions    /*!
180*d4514f0bSApple OSS Distributions     * @brief       Version of DispatchSync that returns a kern_return_t status.
181*d4514f0bSApple OSS Distributions	 */
182*d4514f0bSApple OSS Distributions	kern_return_t
183*d4514f0bSApple OSS Distributions	RunAction(IODispatchAction action) LOCALONLY;
184*d4514f0bSApple OSS Distributions
185*d4514f0bSApple OSS Distributions    /*!
186*d4514f0bSApple OSS Distributions     * @brief       Put a thread that is currently running the queue to sleep, releasing the queue.
187*d4514f0bSApple OSS Distributions     * @discussion  Put a thread to sleep waiting for an event but release the queue first.
188*d4514f0bSApple OSS Distributions     *              In all cases (signal, timeout or error), the caller resumes running on the queue.
189*d4514f0bSApple OSS Distributions	 *              The caller must be currently running on the queue to call Sleep().
190*d4514f0bSApple OSS Distributions     * @param       event A unique token matching one later passed to Wakeup().
191*d4514f0bSApple OSS Distributions     * @param       options Pass one of the kIOTimerClock* options to specify the timebase for the
192*d4514f0bSApple OSS Distributions     *              deadline, or zero for no timeout.
193*d4514f0bSApple OSS Distributions     * @param       deadline Clock deadline to timeout the sleep.
194*d4514f0bSApple OSS Distributions     * @return 		kIOReturnSuccess  or kIOReturnTimeout
195*d4514f0bSApple OSS Distributions     */
196*d4514f0bSApple OSS Distributions	kern_return_t
197*d4514f0bSApple OSS Distributions	SleepWithDeadline(void * event, uint64_t options, uint64_t deadline) LOCALONLY;
198*d4514f0bSApple OSS Distributions
199*d4514f0bSApple OSS Distributions    /*!
200*d4514f0bSApple OSS Distributions     * @brief       Put a thread that is currently running the queue to sleep, releasing the queue.
201*d4514f0bSApple OSS Distributions     * @discussion  Put a thread to sleep waiting for an event but release the queue first.
202*d4514f0bSApple OSS Distributions     *              In all cases (signal, timeout or error), the caller resumes running on the queue.
203*d4514f0bSApple OSS Distributions	 *              The caller must be currently running on the queue to call Sleep().
204*d4514f0bSApple OSS Distributions     * @param       event A unique token matching one later passed to Wakeup().
205*d4514f0bSApple OSS Distributions     * @param       timeout Clock delay to timeout the sleep.
206*d4514f0bSApple OSS Distributions     * @return 		kIOReturnSuccess  or kIOReturnTimeout
207*d4514f0bSApple OSS Distributions     */
208*d4514f0bSApple OSS Distributions	kern_return_t
209*d4514f0bSApple OSS Distributions	SleepWithTimeout(void * event, uint64_t timeout) LOCALONLY;
210*d4514f0bSApple OSS Distributions
211*d4514f0bSApple OSS Distributions    /*!
212*d4514f0bSApple OSS Distributions     * @brief       Synonym for SleepWithTimeout()
213*d4514f0bSApple OSS Distributions     */
214*d4514f0bSApple OSS Distributions	kern_return_t
215*d4514f0bSApple OSS Distributions	Sleep(void * event, uint64_t timeout) LOCALONLY;
216*d4514f0bSApple OSS Distributions
217*d4514f0bSApple OSS Distributions    /*!
218*d4514f0bSApple OSS Distributions     * @brief       Wakes a thread that is blocked in Sleep().
219*d4514f0bSApple OSS Distributions     * @discussion  Signals a thread that gave up the queue with Sleep() to continue running.
220*d4514f0bSApple OSS Distributions	 *              The caller must be currently running on the queue.
221*d4514f0bSApple OSS Distributions     * @param       event A unique token matching one passed to Sleep().
222*d4514f0bSApple OSS Distributions     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
223*d4514f0bSApple OSS Distributions     */
224*d4514f0bSApple OSS Distributions	kern_return_t
225*d4514f0bSApple OSS Distributions	Wakeup(void * event) LOCALONLY;
226*d4514f0bSApple OSS Distributions
227*d4514f0bSApple OSS Distributions    /*!
228*d4514f0bSApple OSS Distributions     * @brief       Wakes a thread that is blocked in Sleep().
229*d4514f0bSApple OSS Distributions     * @discussion  Signals a thread that gave up the queue with Sleep() to continue running.
230*d4514f0bSApple OSS Distributions	 *              The caller must be currently running on the queue.
231*d4514f0bSApple OSS Distributions     * @param       event A unique token matching one passed to Sleep().
232*d4514f0bSApple OSS Distributions     * @param       options
233*d4514f0bSApple OSS Distributions					kIODispatchQueueWakeupAll wake all threads waiting in Sleep().
234*d4514f0bSApple OSS Distributions					The default is to wake only one of any waiting threads.
235*d4514f0bSApple OSS Distributions     * @return      kIOReturnSuccess on success. See IOReturn.h for error codes.
236*d4514f0bSApple OSS Distributions     */
237*d4514f0bSApple OSS Distributions	kern_return_t
238*d4514f0bSApple OSS Distributions	WakeupWithOptions(void * event, uint64_t options) LOCALONLY;
239*d4514f0bSApple OSS Distributions};
240*d4514f0bSApple OSS Distributions
241*d4514f0bSApple OSS Distributions#if DRIVERKIT_PRIVATE
242*d4514f0bSApple OSS Distributionsclass EXTENDS (IODispatchQueue) IODispatchQueuePrivate
243*d4514f0bSApple OSS Distributions{
244*d4514f0bSApple OSS Distributions	virtual kern_return_t
245*d4514f0bSApple OSS Distributions	SetPort(
246*d4514f0bSApple OSS Distributions		mach_port_t port PORTMAKESEND);
247*d4514f0bSApple OSS Distributions};
248*d4514f0bSApple OSS Distributions#endif
249*d4514f0bSApple OSS Distributions
250*d4514f0bSApple OSS Distributions#endif /* ! _IOKIT_UIODISPATCH_H */
251