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