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@
29 #define IOKIT_ENABLE_SHARED_PTR
31 #include <libkern/OSDebug.h>
32 #include <libkern/c++/OSSharedPtr.h>
34 #include <IOKit/IOCommandGate.h>
35 #include <IOKit/IOWorkLoop.h>
36 #include <IOKit/IOReturn.h>
37 #include <IOKit/IOTimeStamp.h>
38 #include <IOKit/IOKitDebug.h>
40 #define super IOEventSource
42 OSDefineMetaClassAndStructorsWithZone(IOCommandGate
, IOEventSource
, ZC_NONE
)
44 OSMetaClassDefineReservedUnused(IOCommandGate
, 0);
46 OSMetaClassDefineReservedUsedX86(IOCommandGate
, 0);
48 OSMetaClassDefineReservedUnused(IOCommandGate
, 1);
49 OSMetaClassDefineReservedUnused(IOCommandGate
, 2);
50 OSMetaClassDefineReservedUnused(IOCommandGate
, 3);
51 OSMetaClassDefineReservedUnused(IOCommandGate
, 4);
52 OSMetaClassDefineReservedUnused(IOCommandGate
, 5);
53 OSMetaClassDefineReservedUnused(IOCommandGate
, 6);
54 OSMetaClassDefineReservedUnused(IOCommandGate
, 7);
58 #define IOStatisticsInitializeCounter() \
60 IOStatistics::setCounterType(IOEventSource::reserved->counter, kIOStatisticsCommandGateCounter); \
63 #define IOStatisticsActionCall() \
65 IOStatistics::countCommandGateActionCall(IOEventSource::reserved->counter); \
70 #define IOStatisticsInitializeCounter()
71 #define IOStatisticsActionCall()
73 #endif /* IOKITSTATS */
76 IOCommandGate::init(OSObject
*inOwner
, Action inAction
)
78 bool res
= super::init(inOwner
, (IOEventSource::Action
) inAction
);
80 IOStatisticsInitializeCounter();
86 OSSharedPtr
<IOCommandGate
>
87 IOCommandGate::commandGate(OSObject
*inOwner
, Action inAction
)
89 OSSharedPtr
<IOCommandGate
> me
= OSMakeShared
<IOCommandGate
>();
91 if (me
&& !me
->init(inOwner
, inAction
)) {
99 IOCommandGate::disable()
101 if (workLoop
&& !workLoop
->inGate()) {
102 OSReportWithBacktrace("IOCommandGate::disable() called when not gated");
109 IOCommandGate::enable()
114 wakeupGate(&enabled
, /* oneThread */ false); // Unblock sleeping threads
120 IOCommandGate::free()
129 kSleepersRemoved
= 0x00000001,
130 kSleepersWaitEnabled
= 0x00000002,
131 kSleepersActions
= 0x00000100,
132 kSleepersActionsMask
= 0xffffff00,
136 IOCommandGate::setWorkLoop(IOWorkLoop
*inWorkLoop
)
139 uintptr_t * sleepersP
= (uintptr_t *) &reserved
;
142 if (!inWorkLoop
&& (wl
= workLoop
)) { // tearing down
144 *sleepersP
|= kSleepersRemoved
;
145 while (*sleepersP
& kSleepersWaitEnabled
) {
146 thread_wakeup_with_result(&enabled
, THREAD_INTERRUPTED
);
147 sleepGate(sleepersP
, THREAD_UNINT
);
149 *sleepersP
&= ~kSleepersWaitEnabled
;
150 defer
= (0 != (kSleepersActionsMask
& *sleepersP
));
152 super::setWorkLoop(NULL
);
153 *sleepersP
&= ~kSleepersRemoved
;
159 super::setWorkLoop(inWorkLoop
);
163 IOCommandGate::runCommand(void *arg0
, void *arg1
,
164 void *arg2
, void *arg3
)
166 return runAction((Action
) action
, arg0
, arg1
, arg2
, arg3
);
170 IOCommandGate::attemptCommand(void *arg0
, void *arg1
,
171 void *arg2
, void *arg3
)
173 return attemptAction((Action
) action
, arg0
, arg1
, arg2
, arg3
);
178 IOCommandGateActionToBlock(OSObject
*owner
,
179 void *arg0
, void *arg1
,
180 void *arg2
, void *arg3
)
182 return ((IOEventSource::ActionBlock
) arg0
)();
186 IOCommandGate::runActionBlock(ActionBlock _action
)
188 return runAction(&IOCommandGateActionToBlock
, _action
);
192 IOCommandGate::runAction(Action inAction
,
193 void *arg0
, void *arg1
,
194 void *arg2
, void *arg3
)
197 uintptr_t * sleepersP
;
200 return kIOReturnBadArgument
;
202 if (!(wl
= workLoop
)) {
203 return kIOReturnNotReady
;
206 // closeGate is recursive needn't worry if we already hold the lock.
208 sleepersP
= (uintptr_t *) &reserved
;
210 // If the command gate is disabled and we aren't on the workloop thread
211 // itself then sleep until we get enabled.
213 if (!wl
->onThread()) {
215 IOReturn sleepResult
= kIOReturnSuccess
;
217 *sleepersP
|= kSleepersWaitEnabled
;
218 sleepResult
= wl
->sleepGate(&enabled
, THREAD_INTERRUPTIBLE
);
219 *sleepersP
&= ~kSleepersWaitEnabled
;
221 bool wakeupTearDown
= (!workLoop
|| (0 != (*sleepersP
& kSleepersRemoved
)));
222 if ((kIOReturnSuccess
!= sleepResult
) || wakeupTearDown
) {
225 if (wakeupTearDown
) {
226 wl
->wakeupGate(sleepersP
, false); // No further resources used
228 return kIOReturnAborted
;
233 bool trace
= (gIOKitTrace
& kIOTraceCommandGates
) ? true : false;
236 IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
237 VM_KERNEL_ADDRHIDE(inAction
), VM_KERNEL_ADDRHIDE(owner
));
240 IOStatisticsActionCall();
242 // Must be gated and on the work loop or enabled
244 *sleepersP
+= kSleepersActions
;
245 res
= (*inAction
)(owner
, arg0
, arg1
, arg2
, arg3
);
246 *sleepersP
-= kSleepersActions
;
249 IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
250 VM_KERNEL_ADDRHIDE(inAction
), VM_KERNEL_ADDRHIDE(owner
));
253 if (kSleepersRemoved
== ((kSleepersActionsMask
| kSleepersRemoved
) & *sleepersP
)) {
254 // no actions outstanding
255 *sleepersP
&= ~kSleepersRemoved
;
256 super::setWorkLoop(NULL
);
265 IOCommandGate::attemptAction(Action inAction
,
266 void *arg0
, void *arg1
,
267 void *arg2
, void *arg3
)
273 return kIOReturnBadArgument
;
275 if (!(wl
= workLoop
)) {
276 return kIOReturnNotReady
;
279 // Try to close the gate if can't get return immediately.
280 if (!wl
->tryCloseGate()) {
281 return kIOReturnCannotLock
;
284 // If the command gate is disabled then sleep until we get a wakeup
285 if (!wl
->onThread() && !enabled
) {
286 res
= kIOReturnNotPermitted
;
288 bool trace
= (gIOKitTrace
& kIOTraceCommandGates
) ? true : false;
291 IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
292 VM_KERNEL_ADDRHIDE(inAction
), VM_KERNEL_ADDRHIDE(owner
));
295 IOStatisticsActionCall();
297 res
= (*inAction
)(owner
, arg0
, arg1
, arg2
, arg3
);
300 IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
301 VM_KERNEL_ADDRHIDE(inAction
), VM_KERNEL_ADDRHIDE(owner
));
311 IOCommandGate::commandSleep(void *event
, UInt32 interruptible
)
313 if (!workLoop
->inGate()) {
314 /* The equivalent of 'msleep' while not holding the mutex is invalid */
315 panic("invalid commandSleep while not holding the gate");
318 return sleepGate(event
, interruptible
);
322 IOCommandGate::commandSleep(void *event
, AbsoluteTime deadline
, UInt32 interruptible
)
324 if (!workLoop
->inGate()) {
325 /* The equivalent of 'msleep' while not holding the mutex is invalid */
326 panic("invalid commandSleep while not holding the gate");
329 return sleepGate(event
, deadline
, interruptible
);
333 IOCommandGate::commandWakeup(void *event
, bool oneThread
)
335 wakeupGate(event
, oneThread
);