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