]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOWorkLoop.cpp
xnu-517.tar.gz
[apple/xnu.git] / iokit / Kernel / IOWorkLoop.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
27
28 HISTORY
29 1998-7-13 Godfrey van der Linden(gvdl)
30 Created.
31 */
32 #include <IOKit/IOWorkLoop.h>
33 #include <IOKit/IOEventSource.h>
34 #include <IOKit/IOInterruptEventSource.h>
35 #include <IOKit/IOCommandGate.h>
36 #include <IOKit/IOTimeStamp.h>
37
38 #define super OSObject
39
40 OSDefineMetaClassAndStructors(IOWorkLoop, OSObject);
41
42 // Block of unused functions intended for future use
43 OSMetaClassDefineReservedUsed(IOWorkLoop, 0);
44
45 OSMetaClassDefineReservedUnused(IOWorkLoop, 1);
46 OSMetaClassDefineReservedUnused(IOWorkLoop, 2);
47 OSMetaClassDefineReservedUnused(IOWorkLoop, 3);
48 OSMetaClassDefineReservedUnused(IOWorkLoop, 4);
49 OSMetaClassDefineReservedUnused(IOWorkLoop, 5);
50 OSMetaClassDefineReservedUnused(IOWorkLoop, 6);
51 OSMetaClassDefineReservedUnused(IOWorkLoop, 7);
52
53 enum IOWorkLoopState { kLoopRestart = 0x1, kLoopTerminate = 0x2 };
54 static inline void SETP(void *addr, unsigned int flag)
55 { unsigned int *num = (unsigned int *) addr; *num |= flag; }
56 static inline void CLRP(void *addr, unsigned int flag)
57 { unsigned int *num = (unsigned int *) addr; *num &= ~flag; }
58 static inline bool ISSETP(void *addr, unsigned int flag)
59 { unsigned int *num = (unsigned int *) addr; return (*num & flag) != 0; }
60
61 #define fFlags loopRestart
62
63 void IOWorkLoop::launchThreadMain(void *self)
64 {
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->release();
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 thread_set_cont_arg((int) this);
301 thread_block(&threadMainContinuation);
302 /* NOTREACHED */
303 }
304
305 // At this point we either have work to do or we need
306 // to commit suicide. But no matter
307 // Clear the simple lock and retore the interrupt state
308 IOSimpleLockUnlockEnableInterrupt(workToDoLock, is);
309 if (workToDo)
310 continue;
311 else
312 break;
313 }
314
315 exitThread:
316 workThread = 0; // Say we don't have a loop and free ourselves
317 free();
318 IOExitThread();
319 }
320
321 IOThread IOWorkLoop::getThread() const
322 {
323 return workThread;
324 }
325
326 bool IOWorkLoop::onThread() const
327 {
328 return (IOThreadSelf() == workThread);
329 }
330
331 bool IOWorkLoop::inGate() const
332 {
333 return IORecursiveLockHaveLock(gateLock);
334 }
335
336 // Internal APIs used by event sources to control the thread
337 void IOWorkLoop::signalWorkAvailable()
338 {
339 if (workToDoLock) {
340 IOInterruptState is = IOSimpleLockLockDisableInterrupt(workToDoLock);
341 workToDo = true;
342 thread_wakeup_one((void *) &workToDo);
343 IOSimpleLockUnlockEnableInterrupt(workToDoLock, is);
344 }
345 }
346
347 void IOWorkLoop::openGate()
348 {
349 IORecursiveLockUnlock(gateLock);
350 }
351
352 void IOWorkLoop::closeGate()
353 {
354 IORecursiveLockLock(gateLock);
355 }
356
357 bool IOWorkLoop::tryCloseGate()
358 {
359 return IORecursiveLockTryLock(gateLock) != 0;
360 }
361
362 int IOWorkLoop::sleepGate(void *event, UInt32 interuptibleType)
363 {
364 return IORecursiveLockSleep(gateLock, event, interuptibleType);
365 }
366
367 void IOWorkLoop::wakeupGate(void *event, bool oneThread)
368 {
369 IORecursiveLockWakeup(gateLock, event, oneThread);
370 }
371
372 IOReturn IOWorkLoop::runAction(Action inAction, OSObject *target,
373 void *arg0, void *arg1,
374 void *arg2, void *arg3)
375 {
376 IOReturn res;
377
378 // closeGate is recursive so don't worry if we already hold the lock.
379 closeGate();
380 res = (*inAction)(target, arg0, arg1, arg2, arg3);
381 openGate();
382
383 return res;
384 }
385
386 IOReturn IOWorkLoop::_maintRequest(void *inC, void *inD, void *, void *)
387 {
388 maintCommandEnum command = (maintCommandEnum) (vm_address_t) inC;
389 IOEventSource *inEvent = (IOEventSource *) inD;
390 IOReturn res = kIOReturnSuccess;
391
392 switch (command)
393 {
394 case mAddEvent:
395 if (!inEvent->getWorkLoop()) {
396 SETP(&fFlags, kLoopRestart);
397
398 inEvent->retain();
399 inEvent->setWorkLoop(this);
400 inEvent->setNext(0);
401
402 if (!eventChain)
403 eventChain = inEvent;
404 else {
405 IOEventSource *event, *next;
406
407 for (event = eventChain; (next = event->getNext()); event = next)
408 ;
409 event->setNext(inEvent);
410 }
411 }
412 break;
413
414 case mRemoveEvent:
415 if (inEvent->getWorkLoop()) {
416 if (eventChain == inEvent)
417 eventChain = inEvent->getNext();
418 else {
419 IOEventSource *event, *next;
420
421 event = eventChain;
422 while ((next = event->getNext()) && next != inEvent)
423 event = next;
424
425 if (!next) {
426 res = kIOReturnBadArgument;
427 break;
428 }
429 event->setNext(inEvent->getNext());
430 }
431
432 inEvent->setWorkLoop(0);
433 inEvent->setNext(0);
434 inEvent->release();
435 SETP(&fFlags, kLoopRestart);
436 }
437 break;
438
439 default:
440 return kIOReturnUnsupported;
441 }
442
443 return res;
444 }