]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b | 27 | */ |
0c530ab8 A |
28 | #include <libkern/OSDebug.h> |
29 | ||
1c79356b A |
30 | #include <IOKit/IOCommandGate.h> |
31 | #include <IOKit/IOWorkLoop.h> | |
32 | #include <IOKit/IOReturn.h> | |
33 | #include <IOKit/IOTimeStamp.h> | |
060df5ea | 34 | #include <IOKit/IOKitDebug.h> |
1c79356b A |
35 | |
36 | #define super IOEventSource | |
37 | ||
38 | OSDefineMetaClassAndStructors(IOCommandGate, IOEventSource) | |
b0d623f7 | 39 | #if __LP64__ |
1c79356b | 40 | OSMetaClassDefineReservedUnused(IOCommandGate, 0); |
b0d623f7 A |
41 | #else |
42 | OSMetaClassDefineReservedUsed(IOCommandGate, 0); | |
43 | #endif | |
1c79356b A |
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 | ||
55e303ae | 54 | bool IOCommandGate::init(OSObject *inOwner, Action inAction) |
1c79356b A |
55 | { |
56 | return super::init(inOwner, (IOEventSource::Action) inAction); | |
57 | } | |
58 | ||
59 | IOCommandGate * | |
55e303ae | 60 | IOCommandGate::commandGate(OSObject *inOwner, Action inAction) |
1c79356b A |
61 | { |
62 | IOCommandGate *me = new IOCommandGate; | |
63 | ||
64 | if (me && !me->init(inOwner, inAction)) { | |
55e303ae | 65 | me->release(); |
1c79356b A |
66 | return 0; |
67 | } | |
68 | ||
69 | return me; | |
70 | } | |
71 | ||
0c530ab8 | 72 | /* virtual */ void IOCommandGate::disable() |
89b3af67 | 73 | { |
0c530ab8 A |
74 | if (workLoop && !workLoop->inGate()) |
75 | OSReportWithBacktrace("IOCommandGate::disable() called when not gated"); | |
5d5c5d0d | 76 | |
0c530ab8 A |
77 | super::disable(); |
78 | } | |
5d5c5d0d | 79 | |
0c530ab8 A |
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 | } | |
89b3af67 | 89 | |
0c530ab8 A |
90 | /* virtual */ void IOCommandGate::free() |
91 | { | |
92 | setWorkLoop(0); | |
93 | super::free(); | |
94 | } | |
4452a7af | 95 | |
0c530ab8 A |
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 | |
4452a7af | 110 | |
0c530ab8 A |
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); | |
1c79356b A |
124 | } |
125 | ||
126 | IOReturn IOCommandGate::runAction(Action inAction, | |
55e303ae A |
127 | void *arg0, void *arg1, |
128 | void *arg2, void *arg3) | |
1c79356b | 129 | { |
1c79356b A |
130 | if (!inAction) |
131 | return kIOReturnBadArgument; | |
132 | ||
0c530ab8 | 133 | // closeGate is recursive needn't worry if we already hold the lock. |
1c79356b | 134 | closeGate(); |
1c79356b | 135 | |
0c530ab8 A |
136 | // If the command gate is disabled and we aren't on the workloop thread |
137 | // itself then sleep until we get enabled. | |
1c79356b | 138 | IOReturn res; |
0c530ab8 A |
139 | if (!workLoop->onThread()) { |
140 | while (!enabled) { | |
141 | uintptr_t *sleepersP = (uintptr_t *) &reserved; | |
1c79356b | 142 | |
0c530ab8 A |
143 | *sleepersP += 2; |
144 | IOReturn res = sleepGate(&enabled, THREAD_ABORTSAFE); | |
145 | *sleepersP -= 2; | |
1c79356b | 146 | |
0c530ab8 A |
147 | bool wakeupTearDown = (*sleepersP & 1); |
148 | if (res || wakeupTearDown) { | |
149 | openGate(); | |
1c79356b | 150 | |
0c530ab8 A |
151 | if (wakeupTearDown) |
152 | commandWakeup(sleepersP); // No further resources used | |
1c79356b | 153 | |
0c530ab8 A |
154 | return kIOReturnAborted; |
155 | } | |
156 | } | |
157 | } | |
1c79356b | 158 | |
060df5ea A |
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 | ||
0c530ab8 A |
165 | // Must be gated and on the work loop or enabled |
166 | res = (*inAction)(owner, arg0, arg1, arg2, arg3); | |
060df5ea A |
167 | |
168 | if (trace) | |
169 | IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION), | |
170 | (uintptr_t) inAction, (uintptr_t) owner); | |
171 | ||
1c79356b A |
172 | openGate(); |
173 | ||
174 | return res; | |
175 | } | |
176 | ||
177 | IOReturn IOCommandGate::attemptAction(Action inAction, | |
55e303ae A |
178 | void *arg0, void *arg1, |
179 | void *arg2, void *arg3) | |
1c79356b A |
180 | { |
181 | IOReturn res; | |
182 | ||
1c79356b A |
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 | ||
0c530ab8 A |
190 | // If the command gate is disabled then sleep until we get a wakeup |
191 | if (!workLoop->onThread() && !enabled) | |
192 | res = kIOReturnNotPermitted; | |
193 | else { | |
060df5ea A |
194 | |
195 | bool trace = ( gIOKitTrace & kIOTraceCommandGates ) ? true : false; | |
196 | ||
197 | if (trace) | |
198 | IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION), | |
b0d623f7 | 199 | (uintptr_t) inAction, (uintptr_t) owner); |
0c530ab8 A |
200 | |
201 | res = (*inAction)(owner, arg0, arg1, arg2, arg3); | |
060df5ea A |
202 | |
203 | if (trace) | |
204 | IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION), | |
205 | (uintptr_t) inAction, (uintptr_t) owner); | |
0c530ab8 | 206 | } |
1c79356b | 207 | |
1c79356b A |
208 | openGate(); |
209 | ||
210 | return res; | |
211 | } | |
212 | ||
213 | IOReturn IOCommandGate::commandSleep(void *event, UInt32 interruptible) | |
214 | { | |
1c79356b A |
215 | if (!workLoop->inGate()) |
216 | return kIOReturnNotPermitted; | |
217 | ||
218 | return sleepGate(event, interruptible); | |
219 | } | |
220 | ||
b0d623f7 A |
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 | ||
1c79356b A |
229 | void IOCommandGate::commandWakeup(void *event, bool oneThread) |
230 | { | |
231 | wakeupGate(event, oneThread); | |
232 | } |