]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOCommandGate.cpp
0b823d2b69eaff02a7ecd8363201f29b101ad1c9
2 * Copyright (c) 1998-2000 Apple Computer, 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);
52 bool IOCommandGate::checkForWork() { return false; }
54 bool IOCommandGate::init(OSObject
*inOwner
, Action inAction
)
56 return super::init(inOwner
, (IOEventSource::Action
) inAction
);
60 IOCommandGate::commandGate(OSObject
*inOwner
, Action inAction
)
62 IOCommandGate
*me
= new IOCommandGate
;
64 if (me
&& !me
->init(inOwner
, inAction
)) {
72 /* virtual */ void IOCommandGate::disable()
74 if (workLoop
&& !workLoop
->inGate())
75 OSReportWithBacktrace("IOCommandGate::disable() called when not gated");
80 /* virtual */ void IOCommandGate::enable()
85 wakeupGate(&enabled
, /* oneThread */ false); // Unblock sleeping threads
90 /* virtual */ void IOCommandGate::free()
96 /* virtual */ void IOCommandGate::setWorkLoop(IOWorkLoop
*inWorkLoop
)
98 uintptr_t *sleepersP
= (uintptr_t *) &reserved
;
99 if (!inWorkLoop
&& workLoop
) { // tearing down
102 while (*sleepersP
>> 1) {
103 thread_wakeup_with_result(&enabled
, THREAD_INTERRUPTED
);
104 sleepGate(sleepersP
, THREAD_UNINT
);
111 super::setWorkLoop(inWorkLoop
);
114 IOReturn
IOCommandGate::runCommand(void *arg0
, void *arg1
,
115 void *arg2
, void *arg3
)
117 return runAction((Action
) action
, arg0
, arg1
, arg2
, arg3
);
120 IOReturn
IOCommandGate::attemptCommand(void *arg0
, void *arg1
,
121 void *arg2
, void *arg3
)
123 return attemptAction((Action
) action
, arg0
, arg1
, arg2
, arg3
);
126 IOReturn
IOCommandGate::runAction(Action inAction
,
127 void *arg0
, void *arg1
,
128 void *arg2
, void *arg3
)
131 return kIOReturnBadArgument
;
133 // closeGate is recursive needn't worry if we already hold the lock.
136 // If the command gate is disabled and we aren't on the workloop thread
137 // itself then sleep until we get enabled.
139 if (!workLoop
->onThread()) {
141 uintptr_t *sleepersP
= (uintptr_t *) &reserved
;
144 IOReturn res
= sleepGate(&enabled
, THREAD_ABORTSAFE
);
147 bool wakeupTearDown
= (*sleepersP
& 1);
148 if (res
|| wakeupTearDown
) {
152 commandWakeup(sleepersP
); // No further resources used
154 return kIOReturnAborted
;
159 bool trace
= ( gIOKitTrace
& kIOTraceCommandGates
) ? true : false;
162 IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
163 (uintptr_t) inAction
, (uintptr_t) owner
);
165 // Must be gated and on the work loop or enabled
166 res
= (*inAction
)(owner
, arg0
, arg1
, arg2
, arg3
);
169 IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
170 (uintptr_t) inAction
, (uintptr_t) owner
);
177 IOReturn
IOCommandGate::attemptAction(Action inAction
,
178 void *arg0
, void *arg1
,
179 void *arg2
, void *arg3
)
184 return kIOReturnBadArgument
;
186 // Try to close the gate if can't get return immediately.
188 return kIOReturnCannotLock
;
190 // If the command gate is disabled then sleep until we get a wakeup
191 if (!workLoop
->onThread() && !enabled
)
192 res
= kIOReturnNotPermitted
;
195 bool trace
= ( gIOKitTrace
& kIOTraceCommandGates
) ? true : false;
198 IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
199 (uintptr_t) inAction
, (uintptr_t) owner
);
201 res
= (*inAction
)(owner
, arg0
, arg1
, arg2
, arg3
);
204 IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
205 (uintptr_t) inAction
, (uintptr_t) owner
);
213 IOReturn
IOCommandGate::commandSleep(void *event
, UInt32 interruptible
)
215 if (!workLoop
->inGate())
216 return kIOReturnNotPermitted
;
218 return sleepGate(event
, interruptible
);
221 IOReturn
IOCommandGate::commandSleep(void *event
, AbsoluteTime deadline
, UInt32 interruptible
)
223 if (!workLoop
->inGate())
224 return kIOReturnNotPermitted
;
226 return sleepGate(event
, deadline
, interruptible
);
229 void IOCommandGate::commandWakeup(void *event
, bool oneThread
)
231 wakeupGate(event
, oneThread
);