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