]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOCommandGate.cpp
xnu-792.25.20.tar.gz
[apple/xnu.git] / iokit / Kernel / IOCommandGate.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 #include <libkern/OSDebug.h>
23
24 #include <IOKit/IOCommandGate.h>
25 #include <IOKit/IOWorkLoop.h>
26 #include <IOKit/IOReturn.h>
27 #include <IOKit/IOTimeStamp.h>
28
29 #define super IOEventSource
30
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);
40
41 bool IOCommandGate::checkForWork() { return false; }
42
43 bool IOCommandGate::init(OSObject *inOwner, Action inAction)
44 {
45 return super::init(inOwner, (IOEventSource::Action) inAction);
46 }
47
48 IOCommandGate *
49 IOCommandGate::commandGate(OSObject *inOwner, Action inAction)
50 {
51 IOCommandGate *me = new IOCommandGate;
52
53 if (me && !me->init(inOwner, inAction)) {
54 me->release();
55 return 0;
56 }
57
58 return me;
59 }
60
61 /* virtual */ void IOCommandGate::disable()
62 {
63 if (workLoop && !workLoop->inGate())
64 OSReportWithBacktrace("IOCommandGate::disable() called when not gated");
65
66 super::disable();
67 }
68
69 /* virtual */ void IOCommandGate::enable()
70 {
71 if (workLoop) {
72 closeGate();
73 super::enable();
74 wakeupGate(&enabled, /* oneThread */ false); // Unblock sleeping threads
75 openGate();
76 }
77 }
78
79 /* virtual */ void IOCommandGate::free()
80 {
81 setWorkLoop(0);
82 super::free();
83 }
84
85 /* virtual */ void IOCommandGate::setWorkLoop(IOWorkLoop *inWorkLoop)
86 {
87 uintptr_t *sleepersP = (uintptr_t *) &reserved;
88 if (!inWorkLoop && workLoop) { // tearing down
89 closeGate();
90 *sleepersP |= 1;
91 while (*sleepersP >> 1) {
92 thread_wakeup_with_result(&enabled, THREAD_INTERRUPTED);
93 sleepGate(sleepersP, THREAD_UNINT);
94 }
95 *sleepersP = 0;
96 openGate();
97 }
98 else
99
100 super::setWorkLoop(inWorkLoop);
101 }
102
103 IOReturn IOCommandGate::runCommand(void *arg0, void *arg1,
104 void *arg2, void *arg3)
105 {
106 return runAction((Action) action, arg0, arg1, arg2, arg3);
107 }
108
109 IOReturn IOCommandGate::attemptCommand(void *arg0, void *arg1,
110 void *arg2, void *arg3)
111 {
112 return attemptAction((Action) action, arg0, arg1, arg2, arg3);
113 }
114
115 IOReturn IOCommandGate::runAction(Action inAction,
116 void *arg0, void *arg1,
117 void *arg2, void *arg3)
118 {
119 if (!inAction)
120 return kIOReturnBadArgument;
121
122 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION),
123 (unsigned int) inAction, (unsigned int) owner);
124
125 // closeGate is recursive needn't worry if we already hold the lock.
126 closeGate();
127
128 // If the command gate is disabled and we aren't on the workloop thread
129 // itself then sleep until we get enabled.
130 IOReturn res;
131 if (!workLoop->onThread()) {
132 while (!enabled) {
133 uintptr_t *sleepersP = (uintptr_t *) &reserved;
134
135 *sleepersP += 2;
136 IOReturn res = sleepGate(&enabled, THREAD_ABORTSAFE);
137 *sleepersP -= 2;
138
139 bool wakeupTearDown = (*sleepersP & 1);
140 if (res || wakeupTearDown) {
141 openGate();
142
143 if (wakeupTearDown)
144 commandWakeup(sleepersP); // No further resources used
145
146 return kIOReturnAborted;
147 }
148 }
149 }
150
151 // Must be gated and on the work loop or enabled
152 res = (*inAction)(owner, arg0, arg1, arg2, arg3);
153 openGate();
154
155 return res;
156 }
157
158 IOReturn IOCommandGate::attemptAction(Action inAction,
159 void *arg0, void *arg1,
160 void *arg2, void *arg3)
161 {
162 IOReturn res;
163
164 if (!inAction)
165 return kIOReturnBadArgument;
166
167 // Try to close the gate if can't get return immediately.
168 if (!tryCloseGate())
169 return kIOReturnCannotLock;
170
171 // If the command gate is disabled then sleep until we get a wakeup
172 if (!workLoop->onThread() && !enabled)
173 res = kIOReturnNotPermitted;
174 else {
175 IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION),
176 (unsigned int) inAction, (unsigned int) owner);
177
178 res = (*inAction)(owner, arg0, arg1, arg2, arg3);
179 }
180
181 openGate();
182
183 return res;
184 }
185
186 IOReturn IOCommandGate::commandSleep(void *event, UInt32 interruptible)
187 {
188 if (!workLoop->inGate())
189 return kIOReturnNotPermitted;
190
191 return sleepGate(event, interruptible);
192 }
193
194 void IOCommandGate::commandWakeup(void *event, bool oneThread)
195 {
196 wakeupGate(event, oneThread);
197 }