]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
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. | |
11 | * | |
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 | |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
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. | |
19 | * | |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* | |
23 | Copyright (c) 1998 Apple Computer, Inc. All rights reserved. | |
24 | ||
25 | HISTORY | |
26 | 1998-7-13 Godfrey van der Linden(gvdl) | |
27 | Created. | |
28 | */ | |
29 | #include <IOKit/IOWorkLoop.h> | |
30 | #include <IOKit/IOEventSource.h> | |
31 | #include <IOKit/IOInterruptEventSource.h> | |
32 | #include <IOKit/IOCommandGate.h> | |
33 | #include <IOKit/IOTimeStamp.h> | |
34 | ||
35 | #define super OSObject | |
36 | ||
37 | OSDefineMetaClassAndStructors(IOWorkLoop, OSObject); | |
38 | ||
39 | // Block of unused functions intended for future use | |
40 | OSMetaClassDefineReservedUnused(IOWorkLoop, 0); | |
41 | OSMetaClassDefineReservedUnused(IOWorkLoop, 1); | |
42 | OSMetaClassDefineReservedUnused(IOWorkLoop, 2); | |
43 | OSMetaClassDefineReservedUnused(IOWorkLoop, 3); | |
44 | OSMetaClassDefineReservedUnused(IOWorkLoop, 4); | |
45 | OSMetaClassDefineReservedUnused(IOWorkLoop, 5); | |
46 | OSMetaClassDefineReservedUnused(IOWorkLoop, 6); | |
47 | OSMetaClassDefineReservedUnused(IOWorkLoop, 7); | |
48 | ||
49 | enum IOWorkLoopState { kLoopRestart = 0x1, kLoopTerminate = 0x2 }; | |
50 | static inline void SETP(void *addr, unsigned int flag) | |
51 | { unsigned int *num = (unsigned int *) addr; *num |= flag; } | |
52 | static inline void CLRP(void *addr, unsigned int flag) | |
53 | { unsigned int *num = (unsigned int *) addr; *num &= ~flag; } | |
54 | static inline bool ISSETP(void *addr, unsigned int flag) | |
55 | { unsigned int *num = (unsigned int *) addr; return (*num & flag) != 0; } | |
56 | ||
57 | #define fFlags loopRestart | |
58 | ||
59 | void IOWorkLoop::launchThreadMain(void *self) | |
60 | { | |
61 | register thread_t mythread = current_thread(); | |
62 | ||
63 | // Make sure that this thread always has a kernel stack | |
64 | stack_privilege(mythread); | |
65 | thread_set_cont_arg((int) self); | |
66 | threadMainContinuation(); | |
67 | } | |
68 | ||
69 | bool IOWorkLoop::init() | |
70 | { | |
71 | // The super init and gateLock allocation MUST be done first | |
72 | if ( !super::init() ) | |
73 | return false; | |
74 | ||
75 | if ( !(gateLock = IORecursiveLockAlloc()) ) | |
76 | return false; | |
77 | ||
78 | if ( !(workToDoLock = IOSimpleLockAlloc()) ) | |
79 | return false; | |
80 | ||
81 | controlG = IOCommandGate:: | |
82 | commandGate(this, (IOCommandGate::Action) &IOWorkLoop::_maintRequest); | |
83 | if ( !controlG ) | |
84 | return false; | |
85 | ||
86 | IOSimpleLockInit(workToDoLock); | |
87 | workToDo = false; | |
88 | ||
89 | // Point the controlGate at the workLoop. Usually addEventSource | |
90 | // does this automatically. The problem is in this case addEventSource | |
91 | // uses the control gate and it has to be bootstrapped. | |
92 | controlG->setWorkLoop(this); | |
93 | if (addEventSource(controlG) != kIOReturnSuccess) | |
94 | return false; | |
95 | ||
96 | workThread = IOCreateThread(launchThreadMain, (void *) this); | |
97 | if (!workThread) | |
98 | return false; | |
99 | ||
100 | return true; | |
101 | } | |
102 | ||
103 | IOWorkLoop * | |
104 | IOWorkLoop::workLoop() | |
105 | { | |
106 | IOWorkLoop *me = new IOWorkLoop; | |
107 | ||
108 | if (me && !me->init()) { | |
109 | me->free(); | |
110 | return 0; | |
111 | } | |
112 | ||
113 | return me; | |
114 | } | |
115 | ||
116 | // Free is called twice: | |
117 | // First when the atomic retainCount transitions from 1 -> 0 | |
118 | // Secondly when the work loop itself is commiting hari kari | |
119 | // Hence the each leg of the free must be single threaded. | |
120 | void IOWorkLoop::free() | |
121 | { | |
122 | if (workThread) { | |
123 | IOInterruptState is; | |
124 | ||
125 | // If we are here then we must be trying to shut down this work loop | |
126 | // in this case disable all of the event source, mark the loop for | |
127 | // as terminating and wakeup the work thread itself and return | |
128 | // Note: we hold the gate across the entire operation mainly for the | |
129 | // benefit of our event sources so we can disable them cleanly. | |
130 | closeGate(); | |
131 | ||
132 | disableAllEventSources(); | |
133 | ||
134 | is = IOSimpleLockLockDisableInterrupt(workToDoLock); | |
135 | SETP(&fFlags, kLoopTerminate); | |
136 | thread_wakeup_one((void *) &workToDo); | |
137 | IOSimpleLockUnlockEnableInterrupt(workToDoLock, is); | |
138 | ||
139 | openGate(); | |
140 | } | |
141 | else /* !workThread */ { | |
142 | IOEventSource *event, *next; | |
143 | ||
144 | for (event = eventChain; event; event = next) { | |
145 | next = event->getNext(); | |
146 | event->setWorkLoop(0); | |
147 | event->setNext(0); | |
148 | event->release(); | |
149 | } | |
150 | eventChain = 0; | |
151 | ||
152 | // Either we have a partial initialisation to clean up | |
153 | // or we the workThread itself is performing hari-kari. | |
154 | // either way clean up all of our resources and return. | |
155 | ||
156 | if (controlG) { | |
157 | controlG->release(); | |
158 | controlG = 0; | |
159 | } | |
160 | ||
161 | if (workToDoLock) { | |
162 | IOSimpleLockFree(workToDoLock); | |
163 | workToDoLock = 0; | |
164 | } | |
165 | ||
166 | if (gateLock) { | |
167 | IORecursiveLockFree(gateLock); | |
168 | gateLock = 0; | |
169 | } | |
170 | ||
171 | super::free(); | |
172 | } | |
173 | } | |
174 | ||
175 | IOReturn IOWorkLoop::addEventSource(IOEventSource *newEvent) | |
176 | { | |
177 | return controlG->runCommand((void *) mAddEvent, (void *) newEvent); | |
178 | } | |
179 | ||
180 | IOReturn IOWorkLoop::removeEventSource(IOEventSource *toRemove) | |
181 | { | |
182 | return controlG->runCommand((void *) mRemoveEvent, (void *) toRemove); | |
183 | } | |
184 | ||
185 | void IOWorkLoop::enableAllEventSources() const | |
186 | { | |
187 | IOEventSource *event; | |
188 | ||
189 | for (event = eventChain; event; event = event->getNext()) | |
190 | event->enable(); | |
191 | } | |
192 | ||
193 | void IOWorkLoop::disableAllEventSources() const | |
194 | { | |
195 | IOEventSource *event; | |
196 | ||
197 | for (event = eventChain; event; event = event->getNext()) | |
198 | if (event != controlG) // Don't disable the control gate | |
199 | event->disable(); | |
200 | } | |
201 | ||
202 | void IOWorkLoop::enableAllInterrupts() const | |
203 | { | |
204 | IOEventSource *event; | |
205 | ||
206 | for (event = eventChain; event; event = event->getNext()) | |
207 | if (OSDynamicCast(IOInterruptEventSource, event)) | |
208 | event->enable(); | |
209 | } | |
210 | ||
211 | void IOWorkLoop::disableAllInterrupts() const | |
212 | { | |
213 | IOEventSource *event; | |
214 | ||
215 | for (event = eventChain; event; event = event->getNext()) | |
216 | if (OSDynamicCast(IOInterruptEventSource, event)) | |
217 | event->disable(); | |
218 | } | |
219 | ||
220 | #if KDEBUG | |
221 | #define IOTimeClientS() \ | |
222 | do { \ | |
223 | IOTimeStampStart(IODBG_WORKLOOP(IOWL_CLIENT), \ | |
224 | (unsigned int) this, (unsigned int) event); \ | |
225 | } while(0) | |
226 | ||
227 | #define IOTimeClientE() \ | |
228 | do { \ | |
229 | IOTimeStampEnd(IODBG_WORKLOOP(IOWL_CLIENT), \ | |
230 | (unsigned int) this, (unsigned int) event); \ | |
231 | } while(0) | |
232 | ||
233 | #define IOTimeWorkS() \ | |
234 | do { \ | |
235 | IOTimeStampStart(IODBG_WORKLOOP(IOWL_WORK), (unsigned int) this); \ | |
236 | } while(0) | |
237 | ||
238 | #define IOTimeWorkE() \ | |
239 | do { \ | |
240 | IOTimeStampEnd(IODBG_WORKLOOP(IOWL_WORK),(unsigned int) this); \ | |
241 | } while(0) | |
242 | ||
243 | #else /* !KDEBUG */ | |
244 | ||
245 | #define IOTimeClientS() | |
246 | #define IOTimeClientE() | |
247 | #define IOTimeWorkS() | |
248 | #define IOTimeWorkE() | |
249 | ||
250 | #endif /* KDEBUG */ | |
251 | ||
252 | void IOWorkLoop::threadMainContinuation() | |
253 | { | |
254 | IOWorkLoop* self; | |
255 | self = (IOWorkLoop *) thread_get_cont_arg(); | |
256 | ||
257 | self->threadMain(); | |
258 | } | |
259 | ||
260 | void IOWorkLoop::threadMain() | |
261 | { | |
262 | CLRP(&fFlags, kLoopRestart); | |
263 | ||
264 | for (;;) { | |
265 | bool more; | |
266 | IOInterruptState is; | |
267 | ||
268 | IOTimeWorkS(); | |
269 | ||
270 | closeGate(); | |
271 | if (ISSETP(&fFlags, kLoopTerminate)) | |
272 | goto exitThread; | |
273 | ||
274 | do { | |
275 | workToDo = more = false; | |
276 | for (IOEventSource *event = eventChain; event; event = event->getNext()) { | |
277 | ||
278 | IOTimeClientS(); | |
279 | more |= event->checkForWork(); | |
280 | IOTimeClientE(); | |
281 | ||
282 | if (ISSETP(&fFlags, kLoopTerminate)) | |
283 | goto exitThread; | |
284 | else if (fFlags & kLoopRestart) { | |
285 | CLRP(&fFlags, kLoopRestart); | |
286 | continue; | |
287 | } | |
288 | } | |
289 | } while (more); | |
290 | ||
291 | IOTimeWorkE(); | |
292 | ||
293 | openGate(); | |
294 | ||
295 | is = IOSimpleLockLockDisableInterrupt(workToDoLock); | |
296 | if ( !ISSETP(&fFlags, kLoopTerminate) && !workToDo) { | |
297 | assert_wait((void *) &workToDo, false); | |
298 | IOSimpleLockUnlockEnableInterrupt(workToDoLock, is); | |
299 | ||
300 | #if defined (__i386__) | |
301 | thread_block(0); | |
302 | continue; | |
303 | #else | |
304 | thread_set_cont_arg((int) this); | |
305 | thread_block(&threadMainContinuation); | |
306 | #endif | |
307 | /* NOTREACHED */ | |
308 | } | |
309 | ||
310 | // At this point we either have work to do or we need | |
311 | // to commit suicide. But no matter | |
312 | // Clear the simple lock and retore the interrupt state | |
313 | IOSimpleLockUnlockEnableInterrupt(workToDoLock, is); | |
314 | if (workToDo) | |
315 | continue; | |
316 | else | |
317 | break; | |
318 | } | |
319 | ||
320 | exitThread: | |
321 | workThread = 0; // Say we don't have a loop and free ourselves | |
322 | free(); | |
323 | IOExitThread(0); | |
324 | } | |
325 | ||
326 | IOThread IOWorkLoop::getThread() const | |
327 | { | |
328 | return workThread; | |
329 | } | |
330 | ||
331 | bool IOWorkLoop::onThread() const | |
332 | { | |
333 | return (IOThreadSelf() == workThread); | |
334 | } | |
335 | ||
336 | bool IOWorkLoop::inGate() const | |
337 | { | |
338 | return IORecursiveLockHaveLock(gateLock); | |
339 | } | |
340 | ||
341 | // Internal APIs used by event sources to control the thread | |
342 | void IOWorkLoop::signalWorkAvailable() | |
343 | { | |
344 | if (workToDoLock) { | |
345 | IOInterruptState is = IOSimpleLockLockDisableInterrupt(workToDoLock); | |
346 | workToDo = true; | |
347 | thread_wakeup_one((void *) &workToDo); | |
348 | IOSimpleLockUnlockEnableInterrupt(workToDoLock, is); | |
349 | } | |
350 | } | |
351 | ||
352 | void IOWorkLoop::openGate() | |
353 | { | |
354 | IORecursiveLockUnlock(gateLock); | |
355 | } | |
356 | ||
357 | void IOWorkLoop::closeGate() | |
358 | { | |
359 | IORecursiveLockLock(gateLock); | |
360 | } | |
361 | ||
362 | bool IOWorkLoop::tryCloseGate() | |
363 | { | |
364 | return IORecursiveLockTryLock(gateLock) != 0; | |
365 | } | |
366 | ||
367 | int IOWorkLoop::sleepGate(void *event, UInt32 interuptibleType) | |
368 | { | |
369 | return IORecursiveLockSleep(gateLock, event, interuptibleType); | |
370 | } | |
371 | ||
372 | void IOWorkLoop::wakeupGate(void *event, bool oneThread) | |
373 | { | |
374 | IORecursiveLockWakeup(gateLock, event, oneThread); | |
375 | } | |
376 | ||
377 | IOReturn IOWorkLoop::_maintRequest(void *inC, void *inD, void *, void *) | |
378 | { | |
379 | maintCommandEnum command = (maintCommandEnum) (vm_address_t) inC; | |
380 | IOEventSource *inEvent = (IOEventSource *) inD; | |
381 | IOReturn res = kIOReturnSuccess; | |
382 | ||
383 | switch (command) | |
384 | { | |
385 | case mAddEvent: | |
386 | SETP(&fFlags, kLoopRestart); | |
387 | inEvent->retain(); | |
388 | inEvent->setWorkLoop(this); | |
389 | inEvent->setNext(0); | |
390 | ||
391 | if (!eventChain) | |
392 | eventChain = inEvent; | |
393 | else { | |
394 | IOEventSource *event, *next; | |
395 | ||
396 | for (event = eventChain; (next = event->getNext()); event = next) | |
397 | ; | |
398 | event->setNext(inEvent); | |
399 | } | |
400 | break; | |
401 | ||
402 | case mRemoveEvent: | |
403 | if (eventChain == inEvent) | |
404 | eventChain = inEvent->getNext(); | |
405 | else { | |
406 | IOEventSource *event, *next; | |
407 | ||
408 | event = eventChain; | |
409 | while ((next = event->getNext()) && next != inEvent) | |
410 | event = next; | |
411 | ||
412 | if (!next) { | |
413 | res = kIOReturnBadArgument; | |
414 | break; | |
415 | } | |
416 | event->setNext(inEvent->getNext()); | |
417 | } | |
418 | ||
419 | inEvent->setWorkLoop(0); | |
420 | inEvent->setNext(0); | |
421 | inEvent->release(); | |
422 | SETP(&fFlags, kLoopRestart); | |
423 | break; | |
424 | ||
425 | default: | |
426 | return kIOReturnUnsupported; | |
427 | } | |
428 | ||
429 | return res; | |
430 | } |