]>
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_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 #include <libkern/OSDebug.h>
24 #include <IOKit/IOCommandGate.h>
25 #include <IOKit/IOWorkLoop.h>
26 #include <IOKit/IOReturn.h>
27 #include <IOKit/IOTimeStamp.h>
29 #define super IOEventSource
31 OSDefineMetaClassAndStructors(IOCommandGate
, IOEventSource
)
32 OSMetaClassDefineReservedUnused(IOCommandGate
, 0);
33 OSMetaClassDefineReservedUnused(IOCommandGate
, 1);
34 OSMetaClassDefineReservedUnused(IOCommandGate
, 2);
35 OSMetaClassDefineReservedUnused(IOCommandGate
, 3);
36 OSMetaClassDefineReservedUnused(IOCommandGate
, 4);
37 OSMetaClassDefineReservedUnused(IOCommandGate
, 5);
38 OSMetaClassDefineReservedUnused(IOCommandGate
, 6);
39 OSMetaClassDefineReservedUnused(IOCommandGate
, 7);
41 bool IOCommandGate::checkForWork() { return false; }
43 bool IOCommandGate::init(OSObject
*inOwner
, Action inAction
)
45 return super::init(inOwner
, (IOEventSource::Action
) inAction
);
49 IOCommandGate::commandGate(OSObject
*inOwner
, Action inAction
)
51 IOCommandGate
*me
= new IOCommandGate
;
53 if (me
&& !me
->init(inOwner
, inAction
)) {
61 /* virtual */ void IOCommandGate::disable()
63 if (workLoop
&& !workLoop
->inGate())
64 OSReportWithBacktrace("IOCommandGate::disable() called when not gated");
69 /* virtual */ void IOCommandGate::enable()
74 wakeupGate(&enabled
, /* oneThread */ false); // Unblock sleeping threads
79 /* virtual */ void IOCommandGate::free()
85 /* virtual */ void IOCommandGate::setWorkLoop(IOWorkLoop
*inWorkLoop
)
87 uintptr_t *sleepersP
= (uintptr_t *) &reserved
;
88 if (!inWorkLoop
&& workLoop
) { // tearing down
91 while (*sleepersP
>> 1) {
92 thread_wakeup_with_result(&enabled
, THREAD_INTERRUPTED
);
93 sleepGate(sleepersP
, THREAD_UNINT
);
100 super::setWorkLoop(inWorkLoop
);
103 IOReturn
IOCommandGate::runCommand(void *arg0
, void *arg1
,
104 void *arg2
, void *arg3
)
106 return runAction((Action
) action
, arg0
, arg1
, arg2
, arg3
);
109 IOReturn
IOCommandGate::attemptCommand(void *arg0
, void *arg1
,
110 void *arg2
, void *arg3
)
112 return attemptAction((Action
) action
, arg0
, arg1
, arg2
, arg3
);
115 IOReturn
IOCommandGate::runAction(Action inAction
,
116 void *arg0
, void *arg1
,
117 void *arg2
, void *arg3
)
120 return kIOReturnBadArgument
;
122 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
123 (unsigned int) inAction
, (unsigned int) owner
);
125 // closeGate is recursive needn't worry if we already hold the lock.
128 // If the command gate is disabled and we aren't on the workloop thread
129 // itself then sleep until we get enabled.
131 if (!workLoop
->onThread()) {
133 uintptr_t *sleepersP
= (uintptr_t *) &reserved
;
136 IOReturn res
= sleepGate(&enabled
, THREAD_ABORTSAFE
);
139 bool wakeupTearDown
= (*sleepersP
& 1);
140 if (res
|| wakeupTearDown
) {
144 commandWakeup(sleepersP
); // No further resources used
146 return kIOReturnAborted
;
151 // Must be gated and on the work loop or enabled
152 res
= (*inAction
)(owner
, arg0
, arg1
, arg2
, arg3
);
158 IOReturn
IOCommandGate::attemptAction(Action inAction
,
159 void *arg0
, void *arg1
,
160 void *arg2
, void *arg3
)
165 return kIOReturnBadArgument
;
167 // Try to close the gate if can't get return immediately.
169 return kIOReturnCannotLock
;
171 // If the command gate is disabled then sleep until we get a wakeup
172 if (!workLoop
->onThread() && !enabled
)
173 res
= kIOReturnNotPermitted
;
175 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION
),
176 (unsigned int) inAction
, (unsigned int) owner
);
178 res
= (*inAction
)(owner
, arg0
, arg1
, arg2
, arg3
);
186 IOReturn
IOCommandGate::commandSleep(void *event
, UInt32 interruptible
)
188 if (!workLoop
->inGate())
189 return kIOReturnNotPermitted
;
191 return sleepGate(event
, interruptible
);
194 void IOCommandGate::commandWakeup(void *event
, bool oneThread
)
196 wakeupGate(event
, oneThread
);