Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | /* * Copyright (c) 1998-2004 Apple Computer, Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * The contents of this file constitute Original Code as defined in and * are subject to the Apple Public Source License Version 1.1 (the * "License"). You may not use this file except in compliance with the * License. Please obtain a copy of the License at * http://www.apple.com/publicsource and read it before using this file. * * This Original Code and all software distributed under the License are * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the * License for the specific language governing rights and limitations * under the License. * * @APPLE_LICENSE_HEADER_END@ */ /* Copyright (c) 1998 Apple Computer, Inc. All rights reserved. HISTORY 1998-7-13 Godfrey van der Linden(gvdl) Created. ]*/ #include <IOKit/IOCommandQueue.h> #include <IOKit/IOWorkLoop.h> #include <IOKit/IOTimeStamp.h> #include <mach/sync_policy.h> #define NUM_FIELDS_IN_COMMAND 4 typedef struct commandEntryTag { void *f[NUM_FIELDS_IN_COMMAND]; } commandEntryT; #define super IOEventSource OSDefineMetaClassAndStructors(IOCommandQueue, IOEventSource) /*[ Instance Methods initWithNext:owner:action:size: - initWithNext: (IOEventSource *) inNext owner: (id) inOwner action: (SEL) inAction size: (int) inSize; Primary initialiser for the IOCommandQueue class. Returns an IOCommandQueue object that is initialised with the next object in the chain and the owner and action. On return the signalWorkAvailableIMP has been cached for this function. If the object fails to initialise for some reason then [self free] will be called and nil will be returned. See also: initWithNext:owner:action:(IOEventSource) ]*/ bool IOCommandQueue::init(OSObject *inOwner, IOCommandQueueAction inAction, int inSize) { if ( !super::init(inOwner, (IOEventSourceAction) inAction) ) return false; if (KERN_SUCCESS != semaphore_create(kernel_task, &producerSema, SYNC_POLICY_FIFO, inSize)) return false; size = inSize + 1; /* Allocate one more entry than needed */ queue = (void *)kalloc(size * sizeof(commandEntryT)); if (!queue) return false; producerLock = IOLockAlloc(); if (!producerLock) return false; producerIndex = consumerIndex = 0; return true; } IOCommandQueue * IOCommandQueue::commandQueue(OSObject *inOwner, IOCommandQueueAction inAction, int inSize) { IOCommandQueue *me = new IOCommandQueue; if (me && !me->init(inOwner, inAction, inSize)) { me->free(); return 0; } return me; } /*[ free - free Mandatory free of the object independent of the current retain count. Returns nil. ]*/ void IOCommandQueue::free() { if (queue) kfree(queue, size * sizeof(commandEntryT)); if (producerSema) semaphore_destroy(kernel_task, producerSema); if (producerLock) IOLockFree(producerLock); super::free(); } #if NUM_FIELDS_IN_COMMAND != 4 #error IOCommandQueue::checkForWork needs to be updated for new command size #endif bool IOCommandQueue::checkForWork() { void *field0, *field1, *field2, *field3; if (!enabled || consumerIndex == producerIndex) return false; { commandEntryT *q = (commandEntryT *) queue; int localIndex = consumerIndex; field0 = q[localIndex].f[0]; field1 = q[localIndex].f[1]; field2 = q[localIndex].f[2]; field3 = q[localIndex].f[3]; semaphore_signal(producerSema); } if (++consumerIndex >= size) consumerIndex = 0; IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION), (unsigned int) action, (unsigned int) owner); (*(IOCommandQueueAction) action)(owner, field0, field1, field2, field3); return (consumerIndex != producerIndex); } /*[ enqueueSleep:command: - (kern_return_t) enqueueSleepRaw: (BOOL) gotoSleep field0: (void *) field0 field1: (void *) field1 field2: (void *) field2 field3: (void *) field3; Key method that enqueues the four input fields onto the command queue and calls signalWorkAvailable to indicate that work is available to the consumer. This routine is safe against multiple threaded producers. A family of convenience functions have been provided to assist with the enqueueing of an method selector and an integer tag. This relies on the IODevice rawCommandOccurred... command to forward on the requests. See also: signalWorkAvailable, checkForWork ]*/ #if NUM_FIELDS_IN_COMMAND != 4 #error IOCommandQueue::enqueueCommand needs to be updated #endif kern_return_t IOCommandQueue::enqueueCommand(bool gotoSleep, void *field0, void *field1, void *field2, void *field3) { kern_return_t rtn = KERN_SUCCESS; int retry; /* Make sure there is room in the queue before doing anything else */ if (gotoSleep) { retry = 0; do rtn = semaphore_wait(producerSema); while( (KERN_SUCCESS != rtn) && (KERN_OPERATION_TIMED_OUT != rtn) && (KERN_SEMAPHORE_DESTROYED != rtn) && (KERN_TERMINATED != rtn) && ((retry++) < 4)); } else rtn = semaphore_timedwait(producerSema, MACH_TIMESPEC_ZERO); if (KERN_SUCCESS != rtn) return rtn; /* Block other producers */ IOTakeLock(producerLock); /* * Make sure that we update the current producer entry before we * increment the producer pointer. This avoids a nasty race as the * as the test for work is producerIndex != consumerIndex and a signal. */ { commandEntryT *q = (commandEntryT *) queue; int localIndex = producerIndex; q[localIndex].f[0] = field0; q[localIndex].f[1] = field1; q[localIndex].f[2] = field2; q[localIndex].f[3] = field3; } if (++producerIndex >= size) producerIndex = 0; /* Clear to allow other producers to go now */ IOUnlock(producerLock); /* * Right we have created some new work, we had better make sure that * we notify the work loop that it has to test producerIndex. */ signalWorkAvailable(); return rtn; } int IOCommandQueue::performAndFlush(OSObject *target, IOCommandQueueAction inAction) { int numEntries; kern_return_t rtn; // Set the defaults if necessary if (!target) target = owner; if (!inAction) inAction = (IOCommandQueueAction) action; // Lock out the producers first do { rtn = semaphore_timedwait(producerSema, MACH_TIMESPEC_ZERO); } while (rtn == KERN_SUCCESS); // now step over all remaining entries in the command queue for (numEntries = 0; consumerIndex != producerIndex; ) { void *field0, *field1, *field2, *field3; { commandEntryT *q = (commandEntryT *) queue; int localIndex = consumerIndex; field0 = q[localIndex].f[0]; field1 = q[localIndex].f[1]; field2 = q[localIndex].f[2]; field3 = q[localIndex].f[3]; } if (++consumerIndex >= size) consumerIndex = 0; (*inAction)(target, field0, field1, field2, field3); } // finally refill the producer semaphore to size - 1 for (int i = 1; i < size; i++) semaphore_signal(producerSema); return numEntries; } |