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