]>
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 | ||
35 | #define super IOEventSource | |
36 | ||
37 | OSDefineMetaClassAndStructors(IOCommandGate, IOEventSource) | |
38 | #if __LP64__ | |
39 | OSMetaClassDefineReservedUnused(IOCommandGate, 0); | |
40 | #else | |
41 | OSMetaClassDefineReservedUsed(IOCommandGate, 0); | |
42 | #endif | |
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 | ||
53 | bool IOCommandGate::init(OSObject *inOwner, Action inAction) | |
54 | { | |
55 | return super::init(inOwner, (IOEventSource::Action) inAction); | |
56 | } | |
57 | ||
58 | IOCommandGate * | |
59 | IOCommandGate::commandGate(OSObject *inOwner, Action inAction) | |
60 | { | |
61 | IOCommandGate *me = new IOCommandGate; | |
62 | ||
63 | if (me && !me->init(inOwner, inAction)) { | |
64 | me->release(); | |
65 | return 0; | |
66 | } | |
67 | ||
68 | return me; | |
69 | } | |
70 | ||
71 | /* virtual */ void IOCommandGate::disable() | |
72 | { | |
73 | if (workLoop && !workLoop->inGate()) | |
74 | OSReportWithBacktrace("IOCommandGate::disable() called when not gated"); | |
75 | ||
76 | super::disable(); | |
77 | } | |
78 | ||
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 | } | |
88 | ||
89 | /* virtual */ void IOCommandGate::free() | |
90 | { | |
91 | setWorkLoop(0); | |
92 | super::free(); | |
93 | } | |
94 | ||
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 | |
109 | ||
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); | |
123 | } | |
124 | ||
125 | IOReturn IOCommandGate::runAction(Action inAction, | |
126 | void *arg0, void *arg1, | |
127 | void *arg2, void *arg3) | |
128 | { | |
129 | if (!inAction) | |
130 | return kIOReturnBadArgument; | |
131 | ||
132 | IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION), | |
133 | (uintptr_t) inAction, (uintptr_t) owner); | |
134 | ||
135 | // closeGate is recursive needn't worry if we already hold the lock. | |
136 | closeGate(); | |
137 | ||
138 | // If the command gate is disabled and we aren't on the workloop thread | |
139 | // itself then sleep until we get enabled. | |
140 | IOReturn res; | |
141 | if (!workLoop->onThread()) { | |
142 | while (!enabled) { | |
143 | uintptr_t *sleepersP = (uintptr_t *) &reserved; | |
144 | ||
145 | *sleepersP += 2; | |
146 | IOReturn res = sleepGate(&enabled, THREAD_ABORTSAFE); | |
147 | *sleepersP -= 2; | |
148 | ||
149 | bool wakeupTearDown = (*sleepersP & 1); | |
150 | if (res || wakeupTearDown) { | |
151 | openGate(); | |
152 | ||
153 | if (wakeupTearDown) | |
154 | commandWakeup(sleepersP); // No further resources used | |
155 | ||
156 | return kIOReturnAborted; | |
157 | } | |
158 | } | |
159 | } | |
160 | ||
161 | // Must be gated and on the work loop or enabled | |
162 | res = (*inAction)(owner, arg0, arg1, arg2, arg3); | |
163 | openGate(); | |
164 | ||
165 | return res; | |
166 | } | |
167 | ||
168 | IOReturn IOCommandGate::attemptAction(Action inAction, | |
169 | void *arg0, void *arg1, | |
170 | void *arg2, void *arg3) | |
171 | { | |
172 | IOReturn res; | |
173 | ||
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 | ||
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), | |
186 | (uintptr_t) inAction, (uintptr_t) owner); | |
187 | ||
188 | res = (*inAction)(owner, arg0, arg1, arg2, arg3); | |
189 | } | |
190 | ||
191 | openGate(); | |
192 | ||
193 | return res; | |
194 | } | |
195 | ||
196 | IOReturn IOCommandGate::commandSleep(void *event, UInt32 interruptible) | |
197 | { | |
198 | if (!workLoop->inGate()) | |
199 | return kIOReturnNotPermitted; | |
200 | ||
201 | return sleepGate(event, interruptible); | |
202 | } | |
203 | ||
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 | ||
212 | void IOCommandGate::commandWakeup(void *event, bool oneThread) | |
213 | { | |
214 | wakeupGate(event, oneThread); | |
215 | } |