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