2 * Copyright (c) 1998-2000, 2009-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <libkern/OSDebug.h>
30 #include <IOKit/IOCommandGate.h>
31 #include <IOKit/IOWorkLoop.h>
32 #include <IOKit/IOReturn.h>
33 #include <IOKit/IOTimeStamp.h>
34 #include <IOKit/IOKitDebug.h>
36 #define super IOEventSource
38 OSDefineMetaClassAndStructors(IOCommandGate
, IOEventSource
)
40 OSMetaClassDefineReservedUnused(IOCommandGate
, 0);
42 OSMetaClassDefineReservedUsed(IOCommandGate
, 0);
44 OSMetaClassDefineReservedUnused(IOCommandGate
, 1);
45 OSMetaClassDefineReservedUnused(IOCommandGate
, 2);
46 OSMetaClassDefineReservedUnused(IOCommandGate
, 3);
47 OSMetaClassDefineReservedUnused(IOCommandGate
, 4);
48 OSMetaClassDefineReservedUnused(IOCommandGate
, 5);
49 OSMetaClassDefineReservedUnused(IOCommandGate
, 6);
50 OSMetaClassDefineReservedUnused(IOCommandGate
, 7);
54 #define IOStatisticsInitializeCounter() \
56 IOStatistics::setCounterType(IOEventSource::reserved->counter, kIOStatisticsCommandGateCounter); \
59 #define IOStatisticsActionCall() \
61 IOStatistics::countCommandGateActionCall(IOEventSource::reserved->counter); \
66 #define IOStatisticsInitializeCounter()
67 #define IOStatisticsActionCall()
69 #endif /* IOKITSTATS */
72 IOCommandGate::init(OSObject
*inOwner
, Action inAction
)
74 bool res
= super::init(inOwner
, (IOEventSource::Action
) inAction
);
76 IOStatisticsInitializeCounter();
83 IOCommandGate::commandGate(OSObject
*inOwner
, Action inAction
)
85 IOCommandGate
*me
= new IOCommandGate
;
87 if (me
&& !me
->init(inOwner
, inAction
)) {
96 IOCommandGate::disable()
98 if (workLoop
&& !workLoop
->inGate()) {
99 OSReportWithBacktrace("IOCommandGate::disable() called when not gated");
106 IOCommandGate::enable()
111 wakeupGate(&enabled
, /* oneThread */ false); // Unblock sleeping threads
117 IOCommandGate::free()
126 kSleepersRemoved
= 0x00000001,
127 kSleepersWaitEnabled
= 0x00000002,
128 kSleepersActions
= 0x00000100,
129 kSleepersActionsMask
= 0xffffff00,
133 IOCommandGate::setWorkLoop(IOWorkLoop
*inWorkLoop
)
136 uintptr_t * sleepersP
= (uintptr_t *) &reserved
;
139 if (!inWorkLoop
&& (wl
= workLoop
)) { // tearing down
141 *sleepersP
|= kSleepersRemoved
;
142 while (*sleepersP
& kSleepersWaitEnabled
) {
143 thread_wakeup_with_result(&enabled
, THREAD_INTERRUPTED
);
144 sleepGate(sleepersP
, THREAD_UNINT
);
146 *sleepersP
&= ~kSleepersWaitEnabled
;
147 defer
= (0 != (kSleepersActionsMask
& *sleepersP
));
149 super::setWorkLoop(NULL
);
150 *sleepersP
&= ~kSleepersRemoved
;
156 super::setWorkLoop(inWorkLoop
);
160 IOCommandGate::runCommand(void *arg0
, void *arg1
,
161 void *arg2
, void *arg3
)
163 return runAction((Action
) action
, arg0
, arg1
, arg2
, arg3
);
167 IOCommandGate::attemptCommand(void *arg0
, void *arg1
,
168 void *arg2
, void *arg3
)
170 return attemptAction((Action
) action
, arg0
, arg1
, arg2
, arg3
);
175 IOCommandGateActionToBlock(OSObject
*owner
,
176 void *arg0
, void *arg1
,
177 void *arg2
, void *arg3
)
179 return ((IOEventSource::ActionBlock
) arg0
)();
183 IOCommandGate::runActionBlock(ActionBlock _action
)
185 return runAction(&IOCommandGateActionToBlock
, _action
);
189 IOCommandGate::runAction(Action inAction
,
190 void *arg0
, void *arg1
,
191 void *arg2
, void *arg3
)
194 uintptr_t * sleepersP
;
197 return kIOReturnBadArgument
;
199 if (!(wl
= workLoop
)) {
200 return kIOReturnNotReady
;
203 // closeGate is recursive needn't worry if we already hold the lock.
205 sleepersP
= (uintptr_t *) &reserved
;
207 // If the command gate is disabled and we aren't on the workloop thread
208 // itself then sleep until we get enabled.
210 if (!wl
->onThread()) {
212 IOReturn sleepResult
= kIOReturnSuccess
;
214 *sleepersP
|= kSleepersWaitEnabled
;
215 sleepResult
= wl
->sleepGate(&enabled
, THREAD_INTERRUPTIBLE
);
216 *sleepersP
&= ~kSleepersWaitEnabled
;
218 bool wakeupTearDown
= (!workLoop
|| (0 != (*sleepersP
& kSleepersRemoved
)));
219 if ((kIOReturnSuccess
!= sleepResult
) || wakeupTearDown
) {
222 if (wakeupTearDown
) {
223 wl
->wakeupGate(sleepersP
, false); // No further resources used
225 return kIOReturnAborted
;
230 bool trace
= (gIOKitTrace
& kIOTraceCommandGates
) ? true : false;
233 IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
234 VM_KERNEL_ADDRHIDE(inAction
), VM_KERNEL_ADDRHIDE(owner
));
237 IOStatisticsActionCall();
239 // Must be gated and on the work loop or enabled
241 *sleepersP
+= kSleepersActions
;
242 res
= (*inAction
)(owner
, arg0
, arg1
, arg2
, arg3
);
243 *sleepersP
-= kSleepersActions
;
246 IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
247 VM_KERNEL_ADDRHIDE(inAction
), VM_KERNEL_ADDRHIDE(owner
));
250 if (kSleepersRemoved
== ((kSleepersActionsMask
| kSleepersRemoved
) & *sleepersP
)) {
251 // no actions outstanding
252 *sleepersP
&= ~kSleepersRemoved
;
253 super::setWorkLoop(NULL
);
262 IOCommandGate::attemptAction(Action inAction
,
263 void *arg0
, void *arg1
,
264 void *arg2
, void *arg3
)
270 return kIOReturnBadArgument
;
272 if (!(wl
= workLoop
)) {
273 return kIOReturnNotReady
;
276 // Try to close the gate if can't get return immediately.
277 if (!wl
->tryCloseGate()) {
278 return kIOReturnCannotLock
;
281 // If the command gate is disabled then sleep until we get a wakeup
282 if (!wl
->onThread() && !enabled
) {
283 res
= kIOReturnNotPermitted
;
285 bool trace
= (gIOKitTrace
& kIOTraceCommandGates
) ? true : false;
288 IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
289 VM_KERNEL_ADDRHIDE(inAction
), VM_KERNEL_ADDRHIDE(owner
));
292 IOStatisticsActionCall();
294 res
= (*inAction
)(owner
, arg0
, arg1
, arg2
, arg3
);
297 IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
298 VM_KERNEL_ADDRHIDE(inAction
), VM_KERNEL_ADDRHIDE(owner
));
308 IOCommandGate::commandSleep(void *event
, UInt32 interruptible
)
310 if (!workLoop
->inGate()) {
311 /* The equivalent of 'msleep' while not holding the mutex is invalid */
312 panic("invalid commandSleep while not holding the gate");
315 return sleepGate(event
, interruptible
);
319 IOCommandGate::commandSleep(void *event
, AbsoluteTime deadline
, UInt32 interruptible
)
321 if (!workLoop
->inGate()) {
322 /* The equivalent of 'msleep' while not holding the mutex is invalid */
323 panic("invalid commandSleep while not holding the gate");
326 return sleepGate(event
, deadline
, interruptible
);
330 IOCommandGate::commandWakeup(void *event
, bool oneThread
)
332 wakeupGate(event
, oneThread
);