]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
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. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | #include <libkern/OSDebug.h> | |
29 | ||
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> | |
35 | ||
36 | #define super IOEventSource | |
37 | ||
38 | OSDefineMetaClassAndStructors(IOCommandGate, IOEventSource) | |
39 | #if __LP64__ | |
40 | OSMetaClassDefineReservedUnused(IOCommandGate, 0); | |
41 | #else | |
42 | OSMetaClassDefineReservedUsed(IOCommandGate, 0); | |
43 | #endif | |
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); | |
51 | ||
52 | bool IOCommandGate::checkForWork() { return false; } | |
53 | ||
54 | bool IOCommandGate::init(OSObject *inOwner, Action inAction) | |
55 | { | |
56 | return super::init(inOwner, (IOEventSource::Action) inAction); | |
57 | } | |
58 | ||
59 | IOCommandGate * | |
60 | IOCommandGate::commandGate(OSObject *inOwner, Action inAction) | |
61 | { | |
62 | IOCommandGate *me = new IOCommandGate; | |
63 | ||
64 | if (me && !me->init(inOwner, inAction)) { | |
65 | me->release(); | |
66 | return 0; | |
67 | } | |
68 | ||
69 | return me; | |
70 | } | |
71 | ||
72 | /* virtual */ void IOCommandGate::disable() | |
73 | { | |
74 | if (workLoop && !workLoop->inGate()) | |
75 | OSReportWithBacktrace("IOCommandGate::disable() called when not gated"); | |
76 | ||
77 | super::disable(); | |
78 | } | |
79 | ||
80 | /* virtual */ void IOCommandGate::enable() | |
81 | { | |
82 | if (workLoop) { | |
83 | closeGate(); | |
84 | super::enable(); | |
85 | wakeupGate(&enabled, /* oneThread */ false); // Unblock sleeping threads | |
86 | openGate(); | |
87 | } | |
88 | } | |
89 | ||
90 | /* virtual */ void IOCommandGate::free() | |
91 | { | |
92 | setWorkLoop(0); | |
93 | super::free(); | |
94 | } | |
95 | ||
96 | /* virtual */ void IOCommandGate::setWorkLoop(IOWorkLoop *inWorkLoop) | |
97 | { | |
98 | uintptr_t *sleepersP = (uintptr_t *) &reserved; | |
99 | if (!inWorkLoop && workLoop) { // tearing down | |
100 | closeGate(); | |
101 | *sleepersP |= 1; | |
102 | while (*sleepersP >> 1) { | |
103 | thread_wakeup_with_result(&enabled, THREAD_INTERRUPTED); | |
104 | sleepGate(sleepersP, THREAD_UNINT); | |
105 | } | |
106 | *sleepersP = 0; | |
107 | openGate(); | |
108 | } | |
109 | else | |
110 | ||
111 | super::setWorkLoop(inWorkLoop); | |
112 | } | |
113 | ||
114 | IOReturn IOCommandGate::runCommand(void *arg0, void *arg1, | |
115 | void *arg2, void *arg3) | |
116 | { | |
117 | return runAction((Action) action, arg0, arg1, arg2, arg3); | |
118 | } | |
119 | ||
120 | IOReturn IOCommandGate::attemptCommand(void *arg0, void *arg1, | |
121 | void *arg2, void *arg3) | |
122 | { | |
123 | return attemptAction((Action) action, arg0, arg1, arg2, arg3); | |
124 | } | |
125 | ||
126 | IOReturn IOCommandGate::runAction(Action inAction, | |
127 | void *arg0, void *arg1, | |
128 | void *arg2, void *arg3) | |
129 | { | |
130 | if (!inAction) | |
131 | return kIOReturnBadArgument; | |
132 | ||
133 | // closeGate is recursive needn't worry if we already hold the lock. | |
134 | closeGate(); | |
135 | ||
136 | // If the command gate is disabled and we aren't on the workloop thread | |
137 | // itself then sleep until we get enabled. | |
138 | IOReturn res; | |
139 | if (!workLoop->onThread()) { | |
140 | while (!enabled) { | |
141 | uintptr_t *sleepersP = (uintptr_t *) &reserved; | |
142 | ||
143 | *sleepersP += 2; | |
144 | IOReturn res = sleepGate(&enabled, THREAD_ABORTSAFE); | |
145 | *sleepersP -= 2; | |
146 | ||
147 | bool wakeupTearDown = (*sleepersP & 1); | |
148 | if (res || wakeupTearDown) { | |
149 | openGate(); | |
150 | ||
151 | if (wakeupTearDown) | |
152 | commandWakeup(sleepersP); // No further resources used | |
153 | ||
154 | return kIOReturnAborted; | |
155 | } | |
156 | } | |
157 | } | |
158 | ||
159 | bool trace = ( gIOKitTrace & kIOTraceCommandGates ) ? true : false; | |
160 | ||
161 | if (trace) | |
162 | IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION), | |
163 | (uintptr_t) inAction, (uintptr_t) owner); | |
164 | ||
165 | // Must be gated and on the work loop or enabled | |
166 | res = (*inAction)(owner, arg0, arg1, arg2, arg3); | |
167 | ||
168 | if (trace) | |
169 | IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION), | |
170 | (uintptr_t) inAction, (uintptr_t) owner); | |
171 | ||
172 | openGate(); | |
173 | ||
174 | return res; | |
175 | } | |
176 | ||
177 | IOReturn IOCommandGate::attemptAction(Action inAction, | |
178 | void *arg0, void *arg1, | |
179 | void *arg2, void *arg3) | |
180 | { | |
181 | IOReturn res; | |
182 | ||
183 | if (!inAction) | |
184 | return kIOReturnBadArgument; | |
185 | ||
186 | // Try to close the gate if can't get return immediately. | |
187 | if (!tryCloseGate()) | |
188 | return kIOReturnCannotLock; | |
189 | ||
190 | // If the command gate is disabled then sleep until we get a wakeup | |
191 | if (!workLoop->onThread() && !enabled) | |
192 | res = kIOReturnNotPermitted; | |
193 | else { | |
194 | ||
195 | bool trace = ( gIOKitTrace & kIOTraceCommandGates ) ? true : false; | |
196 | ||
197 | if (trace) | |
198 | IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION), | |
199 | (uintptr_t) inAction, (uintptr_t) owner); | |
200 | ||
201 | res = (*inAction)(owner, arg0, arg1, arg2, arg3); | |
202 | ||
203 | if (trace) | |
204 | IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION), | |
205 | (uintptr_t) inAction, (uintptr_t) owner); | |
206 | } | |
207 | ||
208 | openGate(); | |
209 | ||
210 | return res; | |
211 | } | |
212 | ||
213 | IOReturn IOCommandGate::commandSleep(void *event, UInt32 interruptible) | |
214 | { | |
215 | if (!workLoop->inGate()) | |
216 | return kIOReturnNotPermitted; | |
217 | ||
218 | return sleepGate(event, interruptible); | |
219 | } | |
220 | ||
221 | IOReturn IOCommandGate::commandSleep(void *event, AbsoluteTime deadline, UInt32 interruptible) | |
222 | { | |
223 | if (!workLoop->inGate()) | |
224 | return kIOReturnNotPermitted; | |
225 | ||
226 | return sleepGate(event, deadline, interruptible); | |
227 | } | |
228 | ||
229 | void IOCommandGate::commandWakeup(void *event, bool oneThread) | |
230 | { | |
231 | wakeupGate(event, oneThread); | |
232 | } |