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