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