]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 1998-2010 Apple 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 | ||
29 | #include <pexpert/pexpert.h> | |
30 | #include <IOKit/IOWorkLoop.h> | |
31 | #include <IOKit/IOEventSource.h> | |
32 | #include <IOKit/IOInterruptEventSource.h> | |
33 | #include <IOKit/IOCommandGate.h> | |
34 | #include <IOKit/IOCommandPool.h> | |
35 | #include <IOKit/IOTimeStamp.h> | |
36 | #include <IOKit/IOKitDebug.h> | |
37 | #include <libkern/OSDebug.h> | |
38 | #include <kern/thread.h> | |
39 | ||
40 | #define super OSObject | |
41 | ||
42 | OSDefineMetaClassAndStructors(IOWorkLoop, OSObject); | |
43 | ||
44 | // Block of unused functions intended for future use | |
45 | #if __LP64__ | |
46 | OSMetaClassDefineReservedUnused(IOWorkLoop, 0); | |
47 | OSMetaClassDefineReservedUnused(IOWorkLoop, 1); | |
48 | OSMetaClassDefineReservedUnused(IOWorkLoop, 2); | |
49 | #else | |
50 | OSMetaClassDefineReservedUsed(IOWorkLoop, 0); | |
51 | OSMetaClassDefineReservedUsed(IOWorkLoop, 1); | |
52 | OSMetaClassDefineReservedUsed(IOWorkLoop, 2); | |
53 | #endif | |
54 | OSMetaClassDefineReservedUnused(IOWorkLoop, 3); | |
55 | OSMetaClassDefineReservedUnused(IOWorkLoop, 4); | |
56 | OSMetaClassDefineReservedUnused(IOWorkLoop, 5); | |
57 | OSMetaClassDefineReservedUnused(IOWorkLoop, 6); | |
58 | OSMetaClassDefineReservedUnused(IOWorkLoop, 7); | |
59 | ||
60 | enum IOWorkLoopState { kLoopRestart = 0x1, kLoopTerminate = 0x2 }; | |
61 | static inline void SETP(void *addr, unsigned int flag) | |
62 | { unsigned char *num = (unsigned char *) addr; *num |= flag; } | |
63 | static inline void CLRP(void *addr, unsigned int flag) | |
64 | { unsigned char *num = (unsigned char *) addr; *num &= ~flag; } | |
65 | static inline bool ISSETP(void *addr, unsigned int flag) | |
66 | { unsigned char *num = (unsigned char *) addr; return (*num & flag) != 0; } | |
67 | ||
68 | #define fFlags loopRestart | |
69 | ||
70 | #define passiveEventChain reserved->passiveEventChain | |
71 | ||
72 | #if IOKITSTATS | |
73 | ||
74 | #define IOStatisticsRegisterCounter() \ | |
75 | do { \ | |
76 | reserved->counter = IOStatistics::registerWorkLoop(this); \ | |
77 | } while(0) | |
78 | ||
79 | #define IOStatisticsUnregisterCounter() \ | |
80 | do { \ | |
81 | if (reserved) \ | |
82 | IOStatistics::unregisterWorkLoop(reserved->counter); \ | |
83 | } while(0) | |
84 | ||
85 | #define IOStatisticsOpenGate() \ | |
86 | do { \ | |
87 | IOStatistics::countWorkLoopOpenGate(reserved->counter); \ | |
88 | if (reserved->lockInterval) lockTime(); \ | |
89 | } while(0) | |
90 | #define IOStatisticsCloseGate() \ | |
91 | do { \ | |
92 | IOStatistics::countWorkLoopCloseGate(reserved->counter); \ | |
93 | if (reserved->lockInterval) reserved->lockTime = mach_absolute_time(); \ | |
94 | } while(0) | |
95 | ||
96 | #define IOStatisticsAttachEventSource() \ | |
97 | do { \ | |
98 | IOStatistics::attachWorkLoopEventSource(reserved->counter, inEvent->reserved->counter); \ | |
99 | } while(0) | |
100 | ||
101 | #define IOStatisticsDetachEventSource() \ | |
102 | do { \ | |
103 | IOStatistics::detachWorkLoopEventSource(reserved->counter, inEvent->reserved->counter); \ | |
104 | } while(0) | |
105 | ||
106 | #else | |
107 | ||
108 | #define IOStatisticsRegisterCounter() | |
109 | #define IOStatisticsUnregisterCounter() | |
110 | #define IOStatisticsOpenGate() | |
111 | #define IOStatisticsCloseGate() | |
112 | #define IOStatisticsAttachEventSource() | |
113 | #define IOStatisticsDetachEventSource() | |
114 | ||
115 | #endif /* IOKITSTATS */ | |
116 | ||
117 | bool IOWorkLoop::init() | |
118 | { | |
119 | // The super init and gateLock allocation MUST be done first. | |
120 | if ( !super::init() ) | |
121 | return false; | |
122 | ||
123 | // Allocate our ExpansionData if it hasn't been allocated already. | |
124 | if ( !reserved ) | |
125 | { | |
126 | reserved = IONew(ExpansionData,1); | |
127 | if ( !reserved ) | |
128 | return false; | |
129 | ||
130 | bzero(reserved,sizeof(ExpansionData)); | |
131 | } | |
132 | ||
133 | if ( gateLock == NULL ) { | |
134 | if ( !( gateLock = IORecursiveLockAlloc()) ) | |
135 | return false; | |
136 | } | |
137 | ||
138 | if ( workToDoLock == NULL ) { | |
139 | if ( !(workToDoLock = IOSimpleLockAlloc()) ) | |
140 | return false; | |
141 | IOSimpleLockInit(workToDoLock); | |
142 | workToDo = false; | |
143 | } | |
144 | ||
145 | IOStatisticsRegisterCounter(); | |
146 | ||
147 | if ( controlG == NULL ) { | |
148 | controlG = IOCommandGate::commandGate( | |
149 | this, | |
150 | OSMemberFunctionCast( | |
151 | IOCommandGate::Action, | |
152 | this, | |
153 | &IOWorkLoop::_maintRequest)); | |
154 | ||
155 | if ( !controlG ) | |
156 | return false; | |
157 | // Point the controlGate at the workLoop. Usually addEventSource | |
158 | // does this automatically. The problem is in this case addEventSource | |
159 | // uses the control gate and it has to be bootstrapped. | |
160 | controlG->setWorkLoop(this); | |
161 | if (addEventSource(controlG) != kIOReturnSuccess) | |
162 | return false; | |
163 | } | |
164 | ||
165 | if ( workThread == NULL ) { | |
166 | thread_continue_t cptr = OSMemberFunctionCast( | |
167 | thread_continue_t, | |
168 | this, | |
169 | &IOWorkLoop::threadMain); | |
170 | if (KERN_SUCCESS != kernel_thread_start(cptr, this, &workThread)) | |
171 | return false; | |
172 | } | |
173 | ||
174 | (void) thread_set_tag(workThread, THREAD_TAG_IOWORKLOOP); | |
175 | return true; | |
176 | } | |
177 | ||
178 | IOWorkLoop * | |
179 | IOWorkLoop::workLoop() | |
180 | { | |
181 | return IOWorkLoop::workLoopWithOptions(0); | |
182 | } | |
183 | ||
184 | IOWorkLoop * | |
185 | IOWorkLoop::workLoopWithOptions(IOOptionBits options) | |
186 | { | |
187 | IOWorkLoop *me = new IOWorkLoop; | |
188 | ||
189 | if (me && options) { | |
190 | me->reserved = IONew(ExpansionData,1); | |
191 | if (!me->reserved) { | |
192 | me->release(); | |
193 | return 0; | |
194 | } | |
195 | bzero(me->reserved,sizeof(ExpansionData)); | |
196 | me->reserved->options = options; | |
197 | } | |
198 | ||
199 | if (me && !me->init()) { | |
200 | me->release(); | |
201 | return 0; | |
202 | } | |
203 | ||
204 | return me; | |
205 | } | |
206 | ||
207 | // Free is called twice: | |
208 | // First when the atomic retainCount transitions from 1 -> 0 | |
209 | // Secondly when the work loop itself is commiting hari kari | |
210 | // Hence the each leg of the free must be single threaded. | |
211 | void IOWorkLoop::free() | |
212 | { | |
213 | if (workThread) { | |
214 | IOInterruptState is; | |
215 | ||
216 | // If we are here then we must be trying to shut down this work loop | |
217 | // in this case disable all of the event source, mark the loop | |
218 | // as terminating and wakeup the work thread itself and return | |
219 | // Note: we hold the gate across the entire operation mainly for the | |
220 | // benefit of our event sources so we can disable them cleanly. | |
221 | closeGate(); | |
222 | ||
223 | disableAllEventSources(); | |
224 | ||
225 | is = IOSimpleLockLockDisableInterrupt(workToDoLock); | |
226 | SETP(&fFlags, kLoopTerminate); | |
227 | thread_wakeup_one((void *) &workToDo); | |
228 | IOSimpleLockUnlockEnableInterrupt(workToDoLock, is); | |
229 | ||
230 | openGate(); | |
231 | } | |
232 | else /* !workThread */ { | |
233 | IOEventSource *event, *next; | |
234 | ||
235 | for (event = eventChain; event; event = next) { | |
236 | next = event->getNext(); | |
237 | event->setWorkLoop(0); | |
238 | event->setNext(0); | |
239 | event->release(); | |
240 | } | |
241 | eventChain = 0; | |
242 | ||
243 | for (event = passiveEventChain; event; event = next) { | |
244 | next = event->getNext(); | |
245 | event->setWorkLoop(0); | |
246 | event->setNext(0); | |
247 | event->release(); | |
248 | } | |
249 | passiveEventChain = 0; | |
250 | ||
251 | // Either we have a partial initialization to clean up | |
252 | // or the workThread itself is performing hari-kari. | |
253 | // Either way clean up all of our resources and return. | |
254 | ||
255 | if (controlG) { | |
256 | controlG->workLoop = 0; | |
257 | controlG->release(); | |
258 | controlG = 0; | |
259 | } | |
260 | ||
261 | if (workToDoLock) { | |
262 | IOSimpleLockFree(workToDoLock); | |
263 | workToDoLock = 0; | |
264 | } | |
265 | ||
266 | if (gateLock) { | |
267 | IORecursiveLockFree(gateLock); | |
268 | gateLock = 0; | |
269 | } | |
270 | ||
271 | IOStatisticsUnregisterCounter(); | |
272 | ||
273 | if (reserved) { | |
274 | IODelete(reserved, ExpansionData, 1); | |
275 | reserved = 0; | |
276 | } | |
277 | ||
278 | super::free(); | |
279 | } | |
280 | } | |
281 | ||
282 | IOReturn IOWorkLoop::addEventSource(IOEventSource *newEvent) | |
283 | { | |
284 | if ((workThread) | |
285 | && !thread_has_thread_name(workThread) | |
286 | && (newEvent->owner) | |
287 | && !OSDynamicCast(IOCommandPool, newEvent->owner)) { | |
288 | thread_set_thread_name(workThread, newEvent->owner->getMetaClass()->getClassName()); | |
289 | } | |
290 | ||
291 | return controlG->runCommand((void *) mAddEvent, (void *) newEvent); | |
292 | } | |
293 | ||
294 | IOReturn IOWorkLoop::removeEventSource(IOEventSource *toRemove) | |
295 | { | |
296 | return controlG->runCommand((void *) mRemoveEvent, (void *) toRemove); | |
297 | } | |
298 | ||
299 | void IOWorkLoop::enableAllEventSources() const | |
300 | { | |
301 | IOEventSource *event; | |
302 | ||
303 | for (event = eventChain; event; event = event->getNext()) | |
304 | event->enable(); | |
305 | ||
306 | for (event = passiveEventChain; event; event = event->getNext()) | |
307 | event->enable(); | |
308 | } | |
309 | ||
310 | void IOWorkLoop::disableAllEventSources() const | |
311 | { | |
312 | IOEventSource *event; | |
313 | ||
314 | for (event = eventChain; event; event = event->getNext()) | |
315 | event->disable(); | |
316 | ||
317 | /* NOTE: controlG is in passiveEventChain since it's an IOCommandGate */ | |
318 | for (event = passiveEventChain; event; event = event->getNext()) | |
319 | if (event != controlG) // Don't disable the control gate | |
320 | event->disable(); | |
321 | } | |
322 | ||
323 | void IOWorkLoop::enableAllInterrupts() const | |
324 | { | |
325 | IOEventSource *event; | |
326 | ||
327 | for (event = eventChain; event; event = event->getNext()) | |
328 | if (OSDynamicCast(IOInterruptEventSource, event)) | |
329 | event->enable(); | |
330 | } | |
331 | ||
332 | void IOWorkLoop::disableAllInterrupts() const | |
333 | { | |
334 | IOEventSource *event; | |
335 | ||
336 | for (event = eventChain; event; event = event->getNext()) | |
337 | if (OSDynamicCast(IOInterruptEventSource, event)) | |
338 | event->disable(); | |
339 | } | |
340 | ||
341 | ||
342 | /* virtual */ bool IOWorkLoop::runEventSources() | |
343 | { | |
344 | bool res = false; | |
345 | bool traceWL = (gIOKitTrace & kIOTraceWorkLoops) ? true : false; | |
346 | bool traceES = (gIOKitTrace & kIOTraceEventSources) ? true : false; | |
347 | ||
348 | closeGate(); | |
349 | if (ISSETP(&fFlags, kLoopTerminate)) | |
350 | goto abort; | |
351 | ||
352 | if (traceWL) | |
353 | IOTimeStampStartConstant(IODBG_WORKLOOP(IOWL_WORK), (uintptr_t) this); | |
354 | ||
355 | bool more; | |
356 | do { | |
357 | CLRP(&fFlags, kLoopRestart); | |
358 | more = false; | |
359 | IOInterruptState is = IOSimpleLockLockDisableInterrupt(workToDoLock); | |
360 | workToDo = false; | |
361 | IOSimpleLockUnlockEnableInterrupt(workToDoLock, is); | |
362 | /* NOTE: only loop over event sources in eventChain. Bypass "passive" event sources for performance */ | |
363 | for (IOEventSource *evnt = eventChain; evnt; evnt = evnt->getNext()) { | |
364 | ||
365 | if (traceES) | |
366 | IOTimeStampStartConstant(IODBG_WORKLOOP(IOWL_CLIENT), (uintptr_t) this, (uintptr_t) evnt); | |
367 | ||
368 | more |= evnt->checkForWork(); | |
369 | ||
370 | if (traceES) | |
371 | IOTimeStampEndConstant(IODBG_WORKLOOP(IOWL_CLIENT), (uintptr_t) this, (uintptr_t) evnt); | |
372 | ||
373 | if (ISSETP(&fFlags, kLoopTerminate)) | |
374 | goto abort; | |
375 | else if (fFlags & kLoopRestart) { | |
376 | more = true; | |
377 | break; | |
378 | } | |
379 | } | |
380 | } while (more); | |
381 | ||
382 | res = true; | |
383 | ||
384 | if (traceWL) | |
385 | IOTimeStampEndConstant(IODBG_WORKLOOP(IOWL_WORK), (uintptr_t) this); | |
386 | ||
387 | abort: | |
388 | openGate(); | |
389 | return res; | |
390 | } | |
391 | ||
392 | /* virtual */ void IOWorkLoop::threadMain() | |
393 | { | |
394 | restartThread: | |
395 | do { | |
396 | if ( !runEventSources() ) | |
397 | goto exitThread; | |
398 | ||
399 | IOInterruptState is = IOSimpleLockLockDisableInterrupt(workToDoLock); | |
400 | if ( !ISSETP(&fFlags, kLoopTerminate) && !workToDo) { | |
401 | assert_wait((void *) &workToDo, false); | |
402 | IOSimpleLockUnlockEnableInterrupt(workToDoLock, is); | |
403 | thread_continue_t cptr = NULL; | |
404 | if (!reserved || !(kPreciousStack & reserved->options)) | |
405 | cptr = OSMemberFunctionCast( | |
406 | thread_continue_t, this, &IOWorkLoop::threadMain); | |
407 | thread_block_parameter(cptr, this); | |
408 | goto restartThread; | |
409 | /* NOTREACHED */ | |
410 | } | |
411 | ||
412 | // At this point we either have work to do or we need | |
413 | // to commit suicide. But no matter | |
414 | // Clear the simple lock and retore the interrupt state | |
415 | IOSimpleLockUnlockEnableInterrupt(workToDoLock, is); | |
416 | } while(workToDo); | |
417 | ||
418 | exitThread: | |
419 | thread_t thread = workThread; | |
420 | workThread = 0; // Say we don't have a loop and free ourselves | |
421 | free(); | |
422 | ||
423 | thread_deallocate(thread); | |
424 | (void) thread_terminate(thread); | |
425 | } | |
426 | ||
427 | IOThread IOWorkLoop::getThread() const | |
428 | { | |
429 | return workThread; | |
430 | } | |
431 | ||
432 | bool IOWorkLoop::onThread() const | |
433 | { | |
434 | return (IOThreadSelf() == workThread); | |
435 | } | |
436 | ||
437 | bool IOWorkLoop::inGate() const | |
438 | { | |
439 | return IORecursiveLockHaveLock(gateLock); | |
440 | } | |
441 | ||
442 | // Internal APIs used by event sources to control the thread | |
443 | void IOWorkLoop::signalWorkAvailable() | |
444 | { | |
445 | if (workToDoLock) { | |
446 | IOInterruptState is = IOSimpleLockLockDisableInterrupt(workToDoLock); | |
447 | workToDo = true; | |
448 | thread_wakeup_one((void *) &workToDo); | |
449 | IOSimpleLockUnlockEnableInterrupt(workToDoLock, is); | |
450 | } | |
451 | } | |
452 | ||
453 | void IOWorkLoop::openGate() | |
454 | { | |
455 | IOStatisticsOpenGate(); | |
456 | IORecursiveLockUnlock(gateLock); | |
457 | } | |
458 | ||
459 | void IOWorkLoop::closeGate() | |
460 | { | |
461 | IORecursiveLockLock(gateLock); | |
462 | IOStatisticsCloseGate(); | |
463 | } | |
464 | ||
465 | bool IOWorkLoop::tryCloseGate() | |
466 | { | |
467 | bool res = (IORecursiveLockTryLock(gateLock) != 0); | |
468 | if (res) { | |
469 | IOStatisticsCloseGate(); | |
470 | } | |
471 | return res; | |
472 | } | |
473 | ||
474 | int IOWorkLoop::sleepGate(void *event, UInt32 interuptibleType) | |
475 | { | |
476 | int res; | |
477 | IOStatisticsOpenGate(); | |
478 | res = IORecursiveLockSleep(gateLock, event, interuptibleType); | |
479 | IOStatisticsCloseGate(); | |
480 | return res; | |
481 | } | |
482 | ||
483 | int IOWorkLoop::sleepGate(void *event, AbsoluteTime deadline, UInt32 interuptibleType) | |
484 | { | |
485 | int res; | |
486 | IOStatisticsOpenGate(); | |
487 | res = IORecursiveLockSleepDeadline(gateLock, event, deadline, interuptibleType); | |
488 | IOStatisticsCloseGate(); | |
489 | return res; | |
490 | } | |
491 | ||
492 | void IOWorkLoop::wakeupGate(void *event, bool oneThread) | |
493 | { | |
494 | IORecursiveLockWakeup(gateLock, event, oneThread); | |
495 | } | |
496 | ||
497 | IOReturn IOWorkLoop::runAction(Action inAction, OSObject *target, | |
498 | void *arg0, void *arg1, | |
499 | void *arg2, void *arg3) | |
500 | { | |
501 | IOReturn res; | |
502 | ||
503 | // closeGate is recursive so don't worry if we already hold the lock. | |
504 | closeGate(); | |
505 | res = (*inAction)(target, arg0, arg1, arg2, arg3); | |
506 | openGate(); | |
507 | ||
508 | return res; | |
509 | } | |
510 | ||
511 | IOReturn IOWorkLoop::_maintRequest(void *inC, void *inD, void *, void *) | |
512 | { | |
513 | maintCommandEnum command = (maintCommandEnum) (uintptr_t) inC; | |
514 | IOEventSource *inEvent = (IOEventSource *) inD; | |
515 | IOReturn res = kIOReturnSuccess; | |
516 | ||
517 | switch (command) | |
518 | { | |
519 | case mAddEvent: | |
520 | if (!inEvent->getWorkLoop()) { | |
521 | SETP(&fFlags, kLoopRestart); | |
522 | ||
523 | inEvent->retain(); | |
524 | inEvent->setWorkLoop(this); | |
525 | inEvent->setNext(0); | |
526 | ||
527 | /* Check if this is a passive or active event source being added */ | |
528 | if (eventSourcePerformsWork(inEvent)) { | |
529 | ||
530 | if (!eventChain) | |
531 | eventChain = inEvent; | |
532 | else { | |
533 | IOEventSource *event, *next; | |
534 | ||
535 | for (event = eventChain; (next = event->getNext()); event = next) | |
536 | ; | |
537 | event->setNext(inEvent); | |
538 | ||
539 | } | |
540 | ||
541 | } | |
542 | else { | |
543 | ||
544 | if (!passiveEventChain) | |
545 | passiveEventChain = inEvent; | |
546 | else { | |
547 | IOEventSource *event, *next; | |
548 | ||
549 | for (event = passiveEventChain; (next = event->getNext()); event = next) | |
550 | ; | |
551 | event->setNext(inEvent); | |
552 | ||
553 | } | |
554 | ||
555 | } | |
556 | IOStatisticsAttachEventSource(); | |
557 | } | |
558 | break; | |
559 | ||
560 | case mRemoveEvent: | |
561 | if (inEvent->getWorkLoop()) { | |
562 | IOStatisticsDetachEventSource(); | |
563 | ||
564 | if (eventSourcePerformsWork(inEvent)) { | |
565 | if (eventChain == inEvent) | |
566 | eventChain = inEvent->getNext(); | |
567 | else { | |
568 | IOEventSource *event, *next = 0; | |
569 | ||
570 | event = eventChain; | |
571 | if (event) while ((next = event->getNext()) && (next != inEvent)) | |
572 | event = next; | |
573 | ||
574 | if (!next) { | |
575 | res = kIOReturnBadArgument; | |
576 | break; | |
577 | } | |
578 | event->setNext(inEvent->getNext()); | |
579 | } | |
580 | } | |
581 | else { | |
582 | if (passiveEventChain == inEvent) | |
583 | passiveEventChain = inEvent->getNext(); | |
584 | else { | |
585 | IOEventSource *event, *next = 0; | |
586 | ||
587 | event = passiveEventChain; | |
588 | if (event) while ((next = event->getNext()) && (next != inEvent)) | |
589 | event = next; | |
590 | ||
591 | if (!next) { | |
592 | res = kIOReturnBadArgument; | |
593 | break; | |
594 | } | |
595 | event->setNext(inEvent->getNext()); | |
596 | } | |
597 | } | |
598 | ||
599 | inEvent->setWorkLoop(0); | |
600 | inEvent->setNext(0); | |
601 | inEvent->release(); | |
602 | SETP(&fFlags, kLoopRestart); | |
603 | } | |
604 | break; | |
605 | ||
606 | default: | |
607 | return kIOReturnUnsupported; | |
608 | } | |
609 | ||
610 | return res; | |
611 | } | |
612 | ||
613 | bool | |
614 | IOWorkLoop::eventSourcePerformsWork(IOEventSource *inEventSource) | |
615 | { | |
616 | bool result = true; | |
617 | ||
618 | /* | |
619 | * The idea here is to see if the subclass of IOEventSource has overridden checkForWork(). | |
620 | * The assumption is that if you override checkForWork(), you need to be | |
621 | * active and not passive. | |
622 | * | |
623 | * We picked a known quantity controlG that does not override | |
624 | * IOEventSource::checkForWork(), namely the IOCommandGate associated with | |
625 | * the workloop to which this event source is getting attached. | |
626 | * | |
627 | * We do a pointer comparison on the offset in the vtable for inNewEvent against | |
628 | * the offset in the vtable for inReferenceEvent. This works because | |
629 | * IOCommandGate's slot for checkForWork() has the address of | |
630 | * IOEventSource::checkForWork() in it. | |
631 | * | |
632 | * Think of OSMemberFunctionCast yielding the value at the vtable offset for | |
633 | * checkForWork() here. We're just testing to see if it's the same or not. | |
634 | * | |
635 | */ | |
636 | if (controlG) { | |
637 | void * ptr1; | |
638 | void * ptr2; | |
639 | ||
640 | ptr1 = OSMemberFunctionCast(void*, inEventSource, &IOEventSource::checkForWork); | |
641 | ptr2 = OSMemberFunctionCast(void*, controlG, &IOEventSource::checkForWork); | |
642 | ||
643 | if (ptr1 == ptr2) | |
644 | result = false; | |
645 | } | |
646 | ||
647 | return result; | |
648 | } | |
649 | ||
650 | void | |
651 | IOWorkLoop::lockTime(void) | |
652 | { | |
653 | uint64_t time; | |
654 | time = mach_absolute_time() - reserved->lockTime; | |
655 | if (time > reserved->lockInterval) | |
656 | { | |
657 | absolutetime_to_nanoseconds(time, &time); | |
658 | if (kTimeLockPanics & reserved->options) panic("IOWorkLoop %p lock time %qd us", this, time / 1000ULL); | |
659 | else OSReportWithBacktrace("IOWorkLoop %p lock time %qd us", this, time / 1000ULL); | |
660 | } | |
661 | } | |
662 | ||
663 | void | |
664 | IOWorkLoop::setMaximumLockTime(uint64_t interval, uint32_t options) | |
665 | { | |
666 | IORecursiveLockLock(gateLock); | |
667 | reserved->lockInterval = interval; | |
668 | reserved->options = (reserved->options & ~kTimeLockPanics) | (options & kTimeLockPanics); | |
669 | IORecursiveLockUnlock(gateLock); | |
670 | } |