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