]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
6d2010ae | 2 | * Copyright (c) 1998-2010 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b | 27 | */ |
b0d623f7 A |
28 | |
29 | #include <pexpert/pexpert.h> | |
1c79356b A |
30 | #include <IOKit/IOWorkLoop.h> |
31 | #include <IOKit/IOEventSource.h> | |
32 | #include <IOKit/IOInterruptEventSource.h> | |
33 | #include <IOKit/IOCommandGate.h> | |
39037602 | 34 | #include <IOKit/IOCommandPool.h> |
1c79356b | 35 | #include <IOKit/IOTimeStamp.h> |
060df5ea | 36 | #include <IOKit/IOKitDebug.h> |
2d21ac55 | 37 | #include <libkern/OSDebug.h> |
4b17d6b6 | 38 | #include <kern/thread.h> |
1c79356b A |
39 | |
40 | #define super OSObject | |
41 | ||
42 | OSDefineMetaClassAndStructors(IOWorkLoop, OSObject); | |
43 | ||
44 | // Block of unused functions intended for future use | |
b0d623f7 A |
45 | #if __LP64__ |
46 | OSMetaClassDefineReservedUnused(IOWorkLoop, 0); | |
47 | OSMetaClassDefineReservedUnused(IOWorkLoop, 1); | |
48 | OSMetaClassDefineReservedUnused(IOWorkLoop, 2); | |
49 | #else | |
0b4e3aa0 | 50 | OSMetaClassDefineReservedUsed(IOWorkLoop, 0); |
0c530ab8 | 51 | OSMetaClassDefineReservedUsed(IOWorkLoop, 1); |
b0d623f7 A |
52 | OSMetaClassDefineReservedUsed(IOWorkLoop, 2); |
53 | #endif | |
1c79356b A |
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 }; | |
2d21ac55 A |
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; } | |
1c79356b A |
67 | |
68 | #define fFlags loopRestart | |
69 | ||
6d2010ae A |
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); \ | |
39037602 | 88 | if (reserved->lockInterval) lockTime(); \ |
6d2010ae | 89 | } while(0) |
6d2010ae A |
90 | #define IOStatisticsCloseGate() \ |
91 | do { \ | |
39037602 A |
92 | IOStatistics::countWorkLoopCloseGate(reserved->counter); \ |
93 | if (reserved->lockInterval) reserved->lockTime = mach_absolute_time(); \ | |
6d2010ae A |
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 */ | |
b0d623f7 | 116 | |
1c79356b A |
117 | bool IOWorkLoop::init() |
118 | { | |
6d2010ae | 119 | // The super init and gateLock allocation MUST be done first. |
1c79356b A |
120 | if ( !super::init() ) |
121 | return false; | |
2d21ac55 | 122 | |
6d2010ae A |
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 | } | |
d190cdc3 | 132 | |
2d21ac55 A |
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 | } | |
1c79356b | 144 | |
6d2010ae A |
145 | IOStatisticsRegisterCounter(); |
146 | ||
2d21ac55 A |
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 | } | |
1c79356b | 164 | |
2d21ac55 | 165 | if ( workThread == NULL ) { |
b0d623f7 A |
166 | thread_continue_t cptr = OSMemberFunctionCast( |
167 | thread_continue_t, | |
2d21ac55 A |
168 | this, |
169 | &IOWorkLoop::threadMain); | |
b0d623f7 | 170 | if (KERN_SUCCESS != kernel_thread_start(cptr, this, &workThread)) |
2d21ac55 A |
171 | return false; |
172 | } | |
1c79356b | 173 | |
4b17d6b6 | 174 | (void) thread_set_tag(workThread, THREAD_TAG_IOWORKLOOP); |
1c79356b A |
175 | return true; |
176 | } | |
177 | ||
178 | IOWorkLoop * | |
179 | IOWorkLoop::workLoop() | |
2d21ac55 A |
180 | { |
181 | return IOWorkLoop::workLoopWithOptions(0); | |
182 | } | |
183 | ||
184 | IOWorkLoop * | |
185 | IOWorkLoop::workLoopWithOptions(IOOptionBits options) | |
1c79356b | 186 | { |
6d2010ae A |
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; | |
2d21ac55 | 197 | } |
6d2010ae A |
198 | |
199 | if (me && !me->init()) { | |
200 | me->release(); | |
201 | return 0; | |
202 | } | |
203 | ||
204 | return me; | |
1c79356b A |
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 | |
2d21ac55 | 217 | // in this case disable all of the event source, mark the loop |
1c79356b A |
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 | ||
6d2010ae A |
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 | ||
2d21ac55 A |
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. | |
1c79356b A |
254 | |
255 | if (controlG) { | |
3e170ce0 | 256 | controlG->workLoop = 0; |
1c79356b A |
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 | } | |
6d2010ae A |
270 | |
271 | IOStatisticsUnregisterCounter(); | |
272 | ||
2d21ac55 A |
273 | if (reserved) { |
274 | IODelete(reserved, ExpansionData, 1); | |
275 | reserved = 0; | |
276 | } | |
1c79356b A |
277 | |
278 | super::free(); | |
279 | } | |
280 | } | |
281 | ||
282 | IOReturn IOWorkLoop::addEventSource(IOEventSource *newEvent) | |
283 | { | |
39037602 A |
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 | ||
1c79356b A |
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(); | |
6d2010ae A |
305 | |
306 | for (event = passiveEventChain; event; event = event->getNext()) | |
307 | event->enable(); | |
1c79356b A |
308 | } |
309 | ||
310 | void IOWorkLoop::disableAllEventSources() const | |
311 | { | |
312 | IOEventSource *event; | |
313 | ||
314 | for (event = eventChain; event; event = event->getNext()) | |
6d2010ae A |
315 | event->disable(); |
316 | ||
317 | /* NOTE: controlG is in passiveEventChain since it's an IOCommandGate */ | |
318 | for (event = passiveEventChain; event; event = event->getNext()) | |
1c79356b A |
319 | if (event != controlG) // Don't disable the control gate |
320 | event->disable(); | |
321 | } | |
322 | ||
323 | void IOWorkLoop::enableAllInterrupts() const | |
324 | { | |
325 | IOEventSource *event; | |
6d2010ae | 326 | |
1c79356b A |
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; | |
6d2010ae | 335 | |
1c79356b A |
336 | for (event = eventChain; event; event = event->getNext()) |
337 | if (OSDynamicCast(IOInterruptEventSource, event)) | |
338 | event->disable(); | |
339 | } | |
340 | ||
1c79356b | 341 | |
0c530ab8 | 342 | /* virtual */ bool IOWorkLoop::runEventSources() |
1c79356b | 343 | { |
0c530ab8 | 344 | bool res = false; |
060df5ea A |
345 | bool traceWL = (gIOKitTrace & kIOTraceWorkLoops) ? true : false; |
346 | bool traceES = (gIOKitTrace & kIOTraceEventSources) ? true : false; | |
347 | ||
0c530ab8 A |
348 | closeGate(); |
349 | if (ISSETP(&fFlags, kLoopTerminate)) | |
6d2010ae A |
350 | goto abort; |
351 | ||
060df5ea A |
352 | if (traceWL) |
353 | IOTimeStampStartConstant(IODBG_WORKLOOP(IOWL_WORK), (uintptr_t) this); | |
354 | ||
0c530ab8 A |
355 | bool more; |
356 | do { | |
6d2010ae A |
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()) { | |
060df5ea | 364 | |
6d2010ae A |
365 | if (traceES) |
366 | IOTimeStampStartConstant(IODBG_WORKLOOP(IOWL_CLIENT), (uintptr_t) this, (uintptr_t) evnt); | |
060df5ea | 367 | |
6d2010ae A |
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 | } | |
0c530ab8 | 380 | } while (more); |
6d2010ae | 381 | |
0c530ab8 | 382 | res = true; |
060df5ea A |
383 | |
384 | if (traceWL) | |
385 | IOTimeStampEndConstant(IODBG_WORKLOOP(IOWL_WORK), (uintptr_t) this); | |
6d2010ae | 386 | |
0c530ab8 A |
387 | abort: |
388 | openGate(); | |
389 | return res; | |
390 | } | |
391 | ||
392 | /* virtual */ void IOWorkLoop::threadMain() | |
393 | { | |
2d21ac55 | 394 | restartThread: |
0c530ab8 A |
395 | do { |
396 | if ( !runEventSources() ) | |
397 | goto exitThread; | |
6601e61a | 398 | |
0c530ab8 | 399 | IOInterruptState is = IOSimpleLockLockDisableInterrupt(workToDoLock); |
1c79356b A |
400 | if ( !ISSETP(&fFlags, kLoopTerminate) && !workToDo) { |
401 | assert_wait((void *) &workToDo, false); | |
402 | IOSimpleLockUnlockEnableInterrupt(workToDoLock, is); | |
2d21ac55 A |
403 | thread_continue_t cptr = NULL; |
404 | if (!reserved || !(kPreciousStack & reserved->options)) | |
405 | cptr = OSMemberFunctionCast( | |
406 | thread_continue_t, this, &IOWorkLoop::threadMain); | |
0c530ab8 | 407 | thread_block_parameter(cptr, this); |
2d21ac55 | 408 | goto restartThread; |
1c79356b A |
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); | |
0c530ab8 | 416 | } while(workToDo); |
1c79356b A |
417 | |
418 | exitThread: | |
b0d623f7 | 419 | thread_t thread = workThread; |
1c79356b A |
420 | workThread = 0; // Say we don't have a loop and free ourselves |
421 | free(); | |
b0d623f7 A |
422 | |
423 | thread_deallocate(thread); | |
424 | (void) thread_terminate(thread); | |
1c79356b A |
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 | { | |
6d2010ae | 455 | IOStatisticsOpenGate(); |
1c79356b A |
456 | IORecursiveLockUnlock(gateLock); |
457 | } | |
458 | ||
459 | void IOWorkLoop::closeGate() | |
460 | { | |
461 | IORecursiveLockLock(gateLock); | |
6d2010ae | 462 | IOStatisticsCloseGate(); |
1c79356b A |
463 | } |
464 | ||
465 | bool IOWorkLoop::tryCloseGate() | |
466 | { | |
6d2010ae A |
467 | bool res = (IORecursiveLockTryLock(gateLock) != 0); |
468 | if (res) { | |
469 | IOStatisticsCloseGate(); | |
470 | } | |
471 | return res; | |
1c79356b A |
472 | } |
473 | ||
474 | int IOWorkLoop::sleepGate(void *event, UInt32 interuptibleType) | |
475 | { | |
6d2010ae A |
476 | int res; |
477 | IOStatisticsOpenGate(); | |
478 | res = IORecursiveLockSleep(gateLock, event, interuptibleType); | |
479 | IOStatisticsCloseGate(); | |
480 | return res; | |
1c79356b A |
481 | } |
482 | ||
b0d623f7 A |
483 | int IOWorkLoop::sleepGate(void *event, AbsoluteTime deadline, UInt32 interuptibleType) |
484 | { | |
6d2010ae A |
485 | int res; |
486 | IOStatisticsOpenGate(); | |
487 | res = IORecursiveLockSleepDeadline(gateLock, event, deadline, interuptibleType); | |
488 | IOStatisticsCloseGate(); | |
489 | return res; | |
b0d623f7 A |
490 | } |
491 | ||
1c79356b A |
492 | void IOWorkLoop::wakeupGate(void *event, bool oneThread) |
493 | { | |
494 | IORecursiveLockWakeup(gateLock, event, oneThread); | |
495 | } | |
496 | ||
0b4e3aa0 | 497 | IOReturn IOWorkLoop::runAction(Action inAction, OSObject *target, |
55e303ae A |
498 | void *arg0, void *arg1, |
499 | void *arg2, void *arg3) | |
0b4e3aa0 A |
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 | ||
1c79356b A |
511 | IOReturn IOWorkLoop::_maintRequest(void *inC, void *inD, void *, void *) |
512 | { | |
b0d623f7 | 513 | maintCommandEnum command = (maintCommandEnum) (uintptr_t) inC; |
1c79356b A |
514 | IOEventSource *inEvent = (IOEventSource *) inD; |
515 | IOReturn res = kIOReturnSuccess; | |
516 | ||
517 | switch (command) | |
518 | { | |
519 | case mAddEvent: | |
9bccf70c A |
520 | if (!inEvent->getWorkLoop()) { |
521 | SETP(&fFlags, kLoopRestart); | |
522 | ||
523 | inEvent->retain(); | |
524 | inEvent->setWorkLoop(this); | |
525 | inEvent->setNext(0); | |
6d2010ae A |
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; | |
9bccf70c | 534 | |
6d2010ae A |
535 | for (event = eventChain; (next = event->getNext()); event = next) |
536 | ; | |
537 | event->setNext(inEvent); | |
538 | ||
539 | } | |
540 | ||
541 | } | |
9bccf70c | 542 | else { |
6d2010ae A |
543 | |
544 | if (!passiveEventChain) | |
545 | passiveEventChain = inEvent; | |
546 | else { | |
547 | IOEventSource *event, *next; | |
9bccf70c | 548 | |
6d2010ae A |
549 | for (event = passiveEventChain; (next = event->getNext()); event = next) |
550 | ; | |
551 | event->setNext(inEvent); | |
552 | ||
553 | } | |
554 | ||
9bccf70c | 555 | } |
6d2010ae | 556 | IOStatisticsAttachEventSource(); |
1c79356b A |
557 | } |
558 | break; | |
559 | ||
560 | case mRemoveEvent: | |
9bccf70c | 561 | if (inEvent->getWorkLoop()) { |
316670eb A |
562 | IOStatisticsDetachEventSource(); |
563 | ||
6d2010ae A |
564 | if (eventSourcePerformsWork(inEvent)) { |
565 | if (eventChain == inEvent) | |
566 | eventChain = inEvent->getNext(); | |
567 | else { | |
3e170ce0 | 568 | IOEventSource *event, *next = 0; |
6d2010ae A |
569 | |
570 | event = eventChain; | |
3e170ce0 | 571 | if (event) while ((next = event->getNext()) && (next != inEvent)) |
6d2010ae A |
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 { | |
3e170ce0 | 585 | IOEventSource *event, *next = 0; |
6d2010ae A |
586 | |
587 | event = passiveEventChain; | |
3e170ce0 | 588 | if (event) while ((next = event->getNext()) && (next != inEvent)) |
6d2010ae A |
589 | event = next; |
590 | ||
591 | if (!next) { | |
592 | res = kIOReturnBadArgument; | |
593 | break; | |
594 | } | |
595 | event->setNext(inEvent->getNext()); | |
596 | } | |
597 | } | |
598 | ||
9bccf70c A |
599 | inEvent->setWorkLoop(0); |
600 | inEvent->setNext(0); | |
601 | inEvent->release(); | |
602 | SETP(&fFlags, kLoopRestart); | |
1c79356b | 603 | } |
1c79356b A |
604 | break; |
605 | ||
606 | default: | |
607 | return kIOReturnUnsupported; | |
608 | } | |
609 | ||
610 | return res; | |
611 | } | |
6d2010ae A |
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 | } | |
39037602 A |
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 | } |