]>
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> | |
34 | ||
35 | #define super IOEventSource | |
36 | ||
37 | OSDefineMetaClassAndStructors(IOCommandGate, IOEventSource) | |
b0d623f7 | 38 | #if __LP64__ |
1c79356b | 39 | OSMetaClassDefineReservedUnused(IOCommandGate, 0); |
b0d623f7 A |
40 | #else |
41 | OSMetaClassDefineReservedUsed(IOCommandGate, 0); | |
42 | #endif | |
1c79356b A |
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); | |
50 | ||
51 | bool IOCommandGate::checkForWork() { return false; } | |
52 | ||
55e303ae | 53 | bool IOCommandGate::init(OSObject *inOwner, Action inAction) |
1c79356b A |
54 | { |
55 | return super::init(inOwner, (IOEventSource::Action) inAction); | |
56 | } | |
57 | ||
58 | IOCommandGate * | |
55e303ae | 59 | IOCommandGate::commandGate(OSObject *inOwner, Action inAction) |
1c79356b A |
60 | { |
61 | IOCommandGate *me = new IOCommandGate; | |
62 | ||
63 | if (me && !me->init(inOwner, inAction)) { | |
55e303ae | 64 | me->release(); |
1c79356b A |
65 | return 0; |
66 | } | |
67 | ||
68 | return me; | |
69 | } | |
70 | ||
0c530ab8 | 71 | /* virtual */ void IOCommandGate::disable() |
89b3af67 | 72 | { |
0c530ab8 A |
73 | if (workLoop && !workLoop->inGate()) |
74 | OSReportWithBacktrace("IOCommandGate::disable() called when not gated"); | |
5d5c5d0d | 75 | |
0c530ab8 A |
76 | super::disable(); |
77 | } | |
5d5c5d0d | 78 | |
0c530ab8 A |
79 | /* virtual */ void IOCommandGate::enable() |
80 | { | |
81 | if (workLoop) { | |
82 | closeGate(); | |
83 | super::enable(); | |
84 | wakeupGate(&enabled, /* oneThread */ false); // Unblock sleeping threads | |
85 | openGate(); | |
86 | } | |
87 | } | |
89b3af67 | 88 | |
0c530ab8 A |
89 | /* virtual */ void IOCommandGate::free() |
90 | { | |
91 | setWorkLoop(0); | |
92 | super::free(); | |
93 | } | |
4452a7af | 94 | |
0c530ab8 A |
95 | /* virtual */ void IOCommandGate::setWorkLoop(IOWorkLoop *inWorkLoop) |
96 | { | |
97 | uintptr_t *sleepersP = (uintptr_t *) &reserved; | |
98 | if (!inWorkLoop && workLoop) { // tearing down | |
99 | closeGate(); | |
100 | *sleepersP |= 1; | |
101 | while (*sleepersP >> 1) { | |
102 | thread_wakeup_with_result(&enabled, THREAD_INTERRUPTED); | |
103 | sleepGate(sleepersP, THREAD_UNINT); | |
104 | } | |
105 | *sleepersP = 0; | |
106 | openGate(); | |
107 | } | |
108 | else | |
4452a7af | 109 | |
0c530ab8 A |
110 | super::setWorkLoop(inWorkLoop); |
111 | } | |
112 | ||
113 | IOReturn IOCommandGate::runCommand(void *arg0, void *arg1, | |
114 | void *arg2, void *arg3) | |
115 | { | |
116 | return runAction((Action) action, arg0, arg1, arg2, arg3); | |
117 | } | |
118 | ||
119 | IOReturn IOCommandGate::attemptCommand(void *arg0, void *arg1, | |
120 | void *arg2, void *arg3) | |
121 | { | |
122 | return attemptAction((Action) action, arg0, arg1, arg2, arg3); | |
1c79356b A |
123 | } |
124 | ||
125 | IOReturn IOCommandGate::runAction(Action inAction, | |
55e303ae A |
126 | void *arg0, void *arg1, |
127 | void *arg2, void *arg3) | |
1c79356b | 128 | { |
1c79356b A |
129 | if (!inAction) |
130 | return kIOReturnBadArgument; | |
131 | ||
132 | IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION), | |
b0d623f7 | 133 | (uintptr_t) inAction, (uintptr_t) owner); |
1c79356b | 134 | |
0c530ab8 | 135 | // closeGate is recursive needn't worry if we already hold the lock. |
1c79356b | 136 | closeGate(); |
1c79356b | 137 | |
0c530ab8 A |
138 | // If the command gate is disabled and we aren't on the workloop thread |
139 | // itself then sleep until we get enabled. | |
1c79356b | 140 | IOReturn res; |
0c530ab8 A |
141 | if (!workLoop->onThread()) { |
142 | while (!enabled) { | |
143 | uintptr_t *sleepersP = (uintptr_t *) &reserved; | |
1c79356b | 144 | |
0c530ab8 A |
145 | *sleepersP += 2; |
146 | IOReturn res = sleepGate(&enabled, THREAD_ABORTSAFE); | |
147 | *sleepersP -= 2; | |
1c79356b | 148 | |
0c530ab8 A |
149 | bool wakeupTearDown = (*sleepersP & 1); |
150 | if (res || wakeupTearDown) { | |
151 | openGate(); | |
1c79356b | 152 | |
0c530ab8 A |
153 | if (wakeupTearDown) |
154 | commandWakeup(sleepersP); // No further resources used | |
1c79356b | 155 | |
0c530ab8 A |
156 | return kIOReturnAborted; |
157 | } | |
158 | } | |
159 | } | |
1c79356b | 160 | |
0c530ab8 A |
161 | // Must be gated and on the work loop or enabled |
162 | res = (*inAction)(owner, arg0, arg1, arg2, arg3); | |
1c79356b A |
163 | openGate(); |
164 | ||
165 | return res; | |
166 | } | |
167 | ||
168 | IOReturn IOCommandGate::attemptAction(Action inAction, | |
55e303ae A |
169 | void *arg0, void *arg1, |
170 | void *arg2, void *arg3) | |
1c79356b A |
171 | { |
172 | IOReturn res; | |
173 | ||
1c79356b A |
174 | if (!inAction) |
175 | return kIOReturnBadArgument; | |
176 | ||
177 | // Try to close the gate if can't get return immediately. | |
178 | if (!tryCloseGate()) | |
179 | return kIOReturnCannotLock; | |
180 | ||
0c530ab8 A |
181 | // If the command gate is disabled then sleep until we get a wakeup |
182 | if (!workLoop->onThread() && !enabled) | |
183 | res = kIOReturnNotPermitted; | |
184 | else { | |
185 | IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION), | |
b0d623f7 | 186 | (uintptr_t) inAction, (uintptr_t) owner); |
0c530ab8 A |
187 | |
188 | res = (*inAction)(owner, arg0, arg1, arg2, arg3); | |
189 | } | |
1c79356b | 190 | |
1c79356b A |
191 | openGate(); |
192 | ||
193 | return res; | |
194 | } | |
195 | ||
196 | IOReturn IOCommandGate::commandSleep(void *event, UInt32 interruptible) | |
197 | { | |
1c79356b A |
198 | if (!workLoop->inGate()) |
199 | return kIOReturnNotPermitted; | |
200 | ||
201 | return sleepGate(event, interruptible); | |
202 | } | |
203 | ||
b0d623f7 A |
204 | IOReturn IOCommandGate::commandSleep(void *event, AbsoluteTime deadline, UInt32 interruptible) |
205 | { | |
206 | if (!workLoop->inGate()) | |
207 | return kIOReturnNotPermitted; | |
208 | ||
209 | return sleepGate(event, deadline, interruptible); | |
210 | } | |
211 | ||
1c79356b A |
212 | void IOCommandGate::commandWakeup(void *event, bool oneThread) |
213 | { | |
214 | wakeupGate(event, oneThread); | |
215 | } |