]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
37839358 A |
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. | |
1c79356b | 11 | * |
37839358 A |
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 | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
37839358 A |
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. | |
1c79356b A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
c0fea474 A |
22 | #include <libkern/OSDebug.h> |
23 | ||
1c79356b A |
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 | ||
55e303ae | 43 | bool IOCommandGate::init(OSObject *inOwner, Action inAction) |
1c79356b A |
44 | { |
45 | return super::init(inOwner, (IOEventSource::Action) inAction); | |
46 | } | |
47 | ||
48 | IOCommandGate * | |
55e303ae | 49 | IOCommandGate::commandGate(OSObject *inOwner, Action inAction) |
1c79356b A |
50 | { |
51 | IOCommandGate *me = new IOCommandGate; | |
52 | ||
53 | if (me && !me->init(inOwner, inAction)) { | |
55e303ae | 54 | me->release(); |
1c79356b A |
55 | return 0; |
56 | } | |
57 | ||
58 | return me; | |
59 | } | |
60 | ||
c0fea474 | 61 | /* virtual */ void IOCommandGate::disable() |
1c79356b | 62 | { |
c0fea474 A |
63 | if (workLoop && !workLoop->inGate()) |
64 | OSReportWithBacktrace("IOCommandGate::disable() called when not gated"); | |
1c79356b | 65 | |
c0fea474 A |
66 | super::disable(); |
67 | } | |
1c79356b | 68 | |
c0fea474 A |
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 | } | |
1c79356b | 78 | |
c0fea474 A |
79 | /* virtual */ void IOCommandGate::free() |
80 | { | |
81 | setWorkLoop(0); | |
82 | super::free(); | |
83 | } | |
1c79356b | 84 | |
c0fea474 A |
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 | |
1c79356b | 99 | |
c0fea474 A |
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); | |
1c79356b A |
113 | } |
114 | ||
115 | IOReturn IOCommandGate::runAction(Action inAction, | |
55e303ae A |
116 | void *arg0, void *arg1, |
117 | void *arg2, void *arg3) | |
1c79356b | 118 | { |
1c79356b A |
119 | if (!inAction) |
120 | return kIOReturnBadArgument; | |
121 | ||
122 | IOTimeStampConstant(IODBG_CMDQ(IOCMDQ_ACTION), | |
123 | (unsigned int) inAction, (unsigned int) owner); | |
124 | ||
c0fea474 | 125 | // closeGate is recursive needn't worry if we already hold the lock. |
1c79356b | 126 | closeGate(); |
1c79356b | 127 | |
c0fea474 A |
128 | // If the command gate is disabled and we aren't on the workloop thread |
129 | // itself then sleep until we get enabled. | |
1c79356b | 130 | IOReturn res; |
c0fea474 A |
131 | if (!workLoop->onThread()) { |
132 | while (!enabled) { | |
133 | uintptr_t *sleepersP = (uintptr_t *) &reserved; | |
1c79356b | 134 | |
c0fea474 A |
135 | *sleepersP += 2; |
136 | IOReturn res = sleepGate(&enabled, THREAD_ABORTSAFE); | |
137 | *sleepersP -= 2; | |
1c79356b | 138 | |
c0fea474 A |
139 | bool wakeupTearDown = (*sleepersP & 1); |
140 | if (res || wakeupTearDown) { | |
141 | openGate(); | |
1c79356b | 142 | |
c0fea474 A |
143 | if (wakeupTearDown) |
144 | commandWakeup(sleepersP); // No further resources used | |
1c79356b | 145 | |
c0fea474 A |
146 | return kIOReturnAborted; |
147 | } | |
148 | } | |
149 | } | |
1c79356b | 150 | |
c0fea474 A |
151 | // Must be gated and on the work loop or enabled |
152 | res = (*inAction)(owner, arg0, arg1, arg2, arg3); | |
1c79356b A |
153 | openGate(); |
154 | ||
155 | return res; | |
156 | } | |
157 | ||
158 | IOReturn IOCommandGate::attemptAction(Action inAction, | |
55e303ae A |
159 | void *arg0, void *arg1, |
160 | void *arg2, void *arg3) | |
1c79356b A |
161 | { |
162 | IOReturn res; | |
163 | ||
1c79356b A |
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 | ||
c0fea474 A |
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 | } | |
1c79356b | 180 | |
1c79356b A |
181 | openGate(); |
182 | ||
183 | return res; | |
184 | } | |
185 | ||
186 | IOReturn IOCommandGate::commandSleep(void *event, UInt32 interruptible) | |
187 | { | |
1c79356b A |
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 | } |