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