]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOCommandGate.cpp
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 */
71 bool IOCommandGate::init(OSObject
*inOwner
, Action inAction
)
73 bool res
= super::init(inOwner
, (IOEventSource::Action
) inAction
);
75 IOStatisticsInitializeCounter();
82 IOCommandGate::commandGate(OSObject
*inOwner
, Action inAction
)
84 IOCommandGate
*me
= new IOCommandGate
;
86 if (me
&& !me
->init(inOwner
, inAction
)) {
94 /* virtual */ void IOCommandGate::disable()
96 if (workLoop
&& !workLoop
->inGate())
97 OSReportWithBacktrace("IOCommandGate::disable() called when not gated");
102 /* virtual */ void IOCommandGate::enable()
107 wakeupGate(&enabled
, /* oneThread */ false); // Unblock sleeping threads
112 /* virtual */ void IOCommandGate::free()
118 /* virtual */ void IOCommandGate::setWorkLoop(IOWorkLoop
*inWorkLoop
)
120 uintptr_t *sleepersP
= (uintptr_t *) &reserved
;
121 if (!inWorkLoop
&& workLoop
) { // tearing down
124 while (*sleepersP
>> 1) {
125 thread_wakeup_with_result(&enabled
, THREAD_INTERRUPTED
);
126 sleepGate(sleepersP
, THREAD_UNINT
);
133 super::setWorkLoop(inWorkLoop
);
136 IOReturn
IOCommandGate::runCommand(void *arg0
, void *arg1
,
137 void *arg2
, void *arg3
)
139 return runAction((Action
) action
, arg0
, arg1
, arg2
, arg3
);
142 IOReturn
IOCommandGate::attemptCommand(void *arg0
, void *arg1
,
143 void *arg2
, void *arg3
)
145 return attemptAction((Action
) action
, arg0
, arg1
, arg2
, arg3
);
148 IOReturn
IOCommandGate::runAction(Action inAction
,
149 void *arg0
, void *arg1
,
150 void *arg2
, void *arg3
)
153 return kIOReturnBadArgument
;
155 // closeGate is recursive needn't worry if we already hold the lock.
158 // If the command gate is disabled and we aren't on the workloop thread
159 // itself then sleep until we get enabled.
161 if (!workLoop
->onThread()) {
163 uintptr_t *sleepersP
= (uintptr_t *) &reserved
;
166 IOReturn res
= sleepGate(&enabled
, THREAD_ABORTSAFE
);
169 bool wakeupTearDown
= (*sleepersP
& 1);
170 if (res
|| wakeupTearDown
) {
174 commandWakeup(sleepersP
); // No further resources used
176 return kIOReturnAborted
;
181 bool trace
= ( gIOKitTrace
& kIOTraceCommandGates
) ? true : false;
184 IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
185 VM_KERNEL_UNSLIDE(inAction
), (uintptr_t) owner
);
187 IOStatisticsActionCall();
189 // Must be gated and on the work loop or enabled
190 res
= (*inAction
)(owner
, arg0
, arg1
, arg2
, arg3
);
193 IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
194 VM_KERNEL_UNSLIDE(inAction
), (uintptr_t) owner
);
201 IOReturn
IOCommandGate::attemptAction(Action inAction
,
202 void *arg0
, void *arg1
,
203 void *arg2
, void *arg3
)
208 return kIOReturnBadArgument
;
210 // Try to close the gate if can't get return immediately.
212 return kIOReturnCannotLock
;
214 // If the command gate is disabled then sleep until we get a wakeup
215 if (!workLoop
->onThread() && !enabled
)
216 res
= kIOReturnNotPermitted
;
219 bool trace
= ( gIOKitTrace
& kIOTraceCommandGates
) ? true : false;
222 IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
223 VM_KERNEL_UNSLIDE(inAction
), (uintptr_t) owner
);
225 IOStatisticsActionCall();
227 res
= (*inAction
)(owner
, arg0
, arg1
, arg2
, arg3
);
230 IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
231 VM_KERNEL_UNSLIDE(inAction
), (uintptr_t) owner
);
239 IOReturn
IOCommandGate::commandSleep(void *event
, UInt32 interruptible
)
241 if (!workLoop
->inGate())
242 return kIOReturnNotPermitted
;
244 return sleepGate(event
, interruptible
);
247 IOReturn
IOCommandGate::commandSleep(void *event
, AbsoluteTime deadline
, UInt32 interruptible
)
249 if (!workLoop
->inGate())
250 return kIOReturnNotPermitted
;
252 return sleepGate(event
, deadline
, interruptible
);
255 void IOCommandGate::commandWakeup(void *event
, bool oneThread
)
257 wakeupGate(event
, oneThread
);