]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOCommandGate.cpp
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>
35 #define super IOEventSource
37 OSDefineMetaClassAndStructors(IOCommandGate
, IOEventSource
)
38 OSMetaClassDefineReservedUnused(IOCommandGate
, 0);
39 OSMetaClassDefineReservedUnused(IOCommandGate
, 1);
40 OSMetaClassDefineReservedUnused(IOCommandGate
, 2);
41 OSMetaClassDefineReservedUnused(IOCommandGate
, 3);
42 OSMetaClassDefineReservedUnused(IOCommandGate
, 4);
43 OSMetaClassDefineReservedUnused(IOCommandGate
, 5);
44 OSMetaClassDefineReservedUnused(IOCommandGate
, 6);
45 OSMetaClassDefineReservedUnused(IOCommandGate
, 7);
47 bool IOCommandGate::checkForWork() { return false; }
49 bool IOCommandGate::init(OSObject
*inOwner
, Action inAction
)
51 return super::init(inOwner
, (IOEventSource::Action
) inAction
);
55 IOCommandGate::commandGate(OSObject
*inOwner
, Action inAction
)
57 IOCommandGate
*me
= new IOCommandGate
;
59 if (me
&& !me
->init(inOwner
, inAction
)) {
67 /* virtual */ void IOCommandGate::disable()
69 if (workLoop
&& !workLoop
->inGate())
70 OSReportWithBacktrace("IOCommandGate::disable() called when not gated");
75 /* virtual */ void IOCommandGate::enable()
80 wakeupGate(&enabled
, /* oneThread */ false); // Unblock sleeping threads
85 /* virtual */ void IOCommandGate::free()
91 /* virtual */ void IOCommandGate::setWorkLoop(IOWorkLoop
*inWorkLoop
)
93 uintptr_t *sleepersP
= (uintptr_t *) &reserved
;
94 if (!inWorkLoop
&& workLoop
) { // tearing down
97 while (*sleepersP
>> 1) {
98 thread_wakeup_with_result(&enabled
, THREAD_INTERRUPTED
);
99 sleepGate(sleepersP
, THREAD_UNINT
);
106 super::setWorkLoop(inWorkLoop
);
109 IOReturn
IOCommandGate::runCommand(void *arg0
, void *arg1
,
110 void *arg2
, void *arg3
)
112 return runAction((Action
) action
, arg0
, arg1
, arg2
, arg3
);
115 IOReturn
IOCommandGate::attemptCommand(void *arg0
, void *arg1
,
116 void *arg2
, void *arg3
)
118 return attemptAction((Action
) action
, arg0
, arg1
, arg2
, arg3
);
121 IOReturn
IOCommandGate::runAction(Action inAction
,
122 void *arg0
, void *arg1
,
123 void *arg2
, void *arg3
)
126 return kIOReturnBadArgument
;
128 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
129 (unsigned int) inAction
, (unsigned int) owner
);
131 // closeGate is recursive needn't worry if we already hold the lock.
134 // If the command gate is disabled and we aren't on the workloop thread
135 // itself then sleep until we get enabled.
137 if (!workLoop
->onThread()) {
139 uintptr_t *sleepersP
= (uintptr_t *) &reserved
;
142 IOReturn res
= sleepGate(&enabled
, THREAD_ABORTSAFE
);
145 bool wakeupTearDown
= (*sleepersP
& 1);
146 if (res
|| wakeupTearDown
) {
150 commandWakeup(sleepersP
); // No further resources used
152 return kIOReturnAborted
;
157 // Must be gated and on the work loop or enabled
158 res
= (*inAction
)(owner
, arg0
, arg1
, arg2
, arg3
);
164 IOReturn
IOCommandGate::attemptAction(Action inAction
,
165 void *arg0
, void *arg1
,
166 void *arg2
, void *arg3
)
171 return kIOReturnBadArgument
;
173 // Try to close the gate if can't get return immediately.
175 return kIOReturnCannotLock
;
177 // If the command gate is disabled then sleep until we get a wakeup
178 if (!workLoop
->onThread() && !enabled
)
179 res
= kIOReturnNotPermitted
;
181 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
182 (unsigned int) inAction
, (unsigned int) owner
);
184 res
= (*inAction
)(owner
, arg0
, arg1
, arg2
, arg3
);
192 IOReturn
IOCommandGate::commandSleep(void *event
, UInt32 interruptible
)
194 if (!workLoop
->inGate())
195 return kIOReturnNotPermitted
;
197 return sleepGate(event
, interruptible
);
200 void IOCommandGate::commandWakeup(void *event
, bool oneThread
)
202 wakeupGate(event
, oneThread
);