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