]>
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
)
39 OSMetaClassDefineReservedUnused(IOCommandGate
, 0);
41 OSMetaClassDefineReservedUsed(IOCommandGate
, 0);
43 OSMetaClassDefineReservedUnused(IOCommandGate
, 1);
44 OSMetaClassDefineReservedUnused(IOCommandGate
, 2);
45 OSMetaClassDefineReservedUnused(IOCommandGate
, 3);
46 OSMetaClassDefineReservedUnused(IOCommandGate
, 4);
47 OSMetaClassDefineReservedUnused(IOCommandGate
, 5);
48 OSMetaClassDefineReservedUnused(IOCommandGate
, 6);
49 OSMetaClassDefineReservedUnused(IOCommandGate
, 7);
51 bool IOCommandGate::checkForWork() { return false; }
53 bool IOCommandGate::init(OSObject
*inOwner
, Action inAction
)
55 return super::init(inOwner
, (IOEventSource::Action
) inAction
);
59 IOCommandGate::commandGate(OSObject
*inOwner
, Action inAction
)
61 IOCommandGate
*me
= new IOCommandGate
;
63 if (me
&& !me
->init(inOwner
, inAction
)) {
71 /* virtual */ void IOCommandGate::disable()
73 if (workLoop
&& !workLoop
->inGate())
74 OSReportWithBacktrace("IOCommandGate::disable() called when not gated");
79 /* virtual */ void IOCommandGate::enable()
84 wakeupGate(&enabled
, /* oneThread */ false); // Unblock sleeping threads
89 /* virtual */ void IOCommandGate::free()
95 /* virtual */ void IOCommandGate::setWorkLoop(IOWorkLoop
*inWorkLoop
)
97 uintptr_t *sleepersP
= (uintptr_t *) &reserved
;
98 if (!inWorkLoop
&& workLoop
) { // tearing down
101 while (*sleepersP
>> 1) {
102 thread_wakeup_with_result(&enabled
, THREAD_INTERRUPTED
);
103 sleepGate(sleepersP
, THREAD_UNINT
);
110 super::setWorkLoop(inWorkLoop
);
113 IOReturn
IOCommandGate::runCommand(void *arg0
, void *arg1
,
114 void *arg2
, void *arg3
)
116 return runAction((Action
) action
, arg0
, arg1
, arg2
, arg3
);
119 IOReturn
IOCommandGate::attemptCommand(void *arg0
, void *arg1
,
120 void *arg2
, void *arg3
)
122 return attemptAction((Action
) action
, arg0
, arg1
, arg2
, arg3
);
125 IOReturn
IOCommandGate::runAction(Action inAction
,
126 void *arg0
, void *arg1
,
127 void *arg2
, void *arg3
)
130 return kIOReturnBadArgument
;
132 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
133 (uintptr_t) inAction
, (uintptr_t) owner
);
135 // closeGate is recursive needn't worry if we already hold the lock.
138 // If the command gate is disabled and we aren't on the workloop thread
139 // itself then sleep until we get enabled.
141 if (!workLoop
->onThread()) {
143 uintptr_t *sleepersP
= (uintptr_t *) &reserved
;
146 IOReturn res
= sleepGate(&enabled
, THREAD_ABORTSAFE
);
149 bool wakeupTearDown
= (*sleepersP
& 1);
150 if (res
|| wakeupTearDown
) {
154 commandWakeup(sleepersP
); // No further resources used
156 return kIOReturnAborted
;
161 // Must be gated and on the work loop or enabled
162 res
= (*inAction
)(owner
, arg0
, arg1
, arg2
, arg3
);
168 IOReturn
IOCommandGate::attemptAction(Action inAction
,
169 void *arg0
, void *arg1
,
170 void *arg2
, void *arg3
)
175 return kIOReturnBadArgument
;
177 // Try to close the gate if can't get return immediately.
179 return kIOReturnCannotLock
;
181 // If the command gate is disabled then sleep until we get a wakeup
182 if (!workLoop
->onThread() && !enabled
)
183 res
= kIOReturnNotPermitted
;
185 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
186 (uintptr_t) inAction
, (uintptr_t) owner
);
188 res
= (*inAction
)(owner
, arg0
, arg1
, arg2
, arg3
);
196 IOReturn
IOCommandGate::commandSleep(void *event
, UInt32 interruptible
)
198 if (!workLoop
->inGate())
199 return kIOReturnNotPermitted
;
201 return sleepGate(event
, interruptible
);
204 IOReturn
IOCommandGate::commandSleep(void *event
, AbsoluteTime deadline
, UInt32 interruptible
)
206 if (!workLoop
->inGate())
207 return kIOReturnNotPermitted
;
209 return sleepGate(event
, deadline
, interruptible
);
212 void IOCommandGate::commandWakeup(void *event
, bool oneThread
)
214 wakeupGate(event
, oneThread
);