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