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