]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOWorkLoop.cpp
79b96d20262e30f237e0139ab05e37e5ec022984
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
29 1998-7-13 Godfrey van der Linden(gvdl)
32 #include <IOKit/IOWorkLoop.h>
33 #include <IOKit/IOEventSource.h>
34 #include <IOKit/IOInterruptEventSource.h>
35 #include <IOKit/IOCommandGate.h>
36 #include <IOKit/IOTimeStamp.h>
38 #define super OSObject
40 OSDefineMetaClassAndStructors(IOWorkLoop
, OSObject
);
42 // Block of unused functions intended for future use
43 OSMetaClassDefineReservedUsed(IOWorkLoop
, 0);
45 OSMetaClassDefineReservedUnused(IOWorkLoop
, 1);
46 OSMetaClassDefineReservedUnused(IOWorkLoop
, 2);
47 OSMetaClassDefineReservedUnused(IOWorkLoop
, 3);
48 OSMetaClassDefineReservedUnused(IOWorkLoop
, 4);
49 OSMetaClassDefineReservedUnused(IOWorkLoop
, 5);
50 OSMetaClassDefineReservedUnused(IOWorkLoop
, 6);
51 OSMetaClassDefineReservedUnused(IOWorkLoop
, 7);
53 enum IOWorkLoopState
{ kLoopRestart
= 0x1, kLoopTerminate
= 0x2 };
54 static inline void SETP(void *addr
, unsigned int flag
)
55 { unsigned int *num
= (unsigned int *) addr
; *num
|= flag
; }
56 static inline void CLRP(void *addr
, unsigned int flag
)
57 { unsigned int *num
= (unsigned int *) addr
; *num
&= ~flag
; }
58 static inline bool ISSETP(void *addr
, unsigned int flag
)
59 { unsigned int *num
= (unsigned int *) addr
; return (*num
& flag
) != 0; }
61 #define fFlags loopRestart
63 void IOWorkLoop::launchThreadMain(void *self
)
65 thread_set_cont_arg((int) self
);
66 threadMainContinuation();
69 bool IOWorkLoop::init()
71 // The super init and gateLock allocation MUST be done first
75 if ( !(gateLock
= IORecursiveLockAlloc()) )
78 if ( !(workToDoLock
= IOSimpleLockAlloc()) )
81 controlG
= IOCommandGate::
82 commandGate(this, (IOCommandGate::Action
) &IOWorkLoop::_maintRequest
);
86 IOSimpleLockInit(workToDoLock
);
89 // Point the controlGate at the workLoop. Usually addEventSource
90 // does this automatically. The problem is in this case addEventSource
91 // uses the control gate and it has to be bootstrapped.
92 controlG
->setWorkLoop(this);
93 if (addEventSource(controlG
) != kIOReturnSuccess
)
96 workThread
= IOCreateThread(launchThreadMain
, (void *) this);
104 IOWorkLoop::workLoop()
106 IOWorkLoop
*me
= new IOWorkLoop
;
108 if (me
&& !me
->init()) {
116 // Free is called twice:
117 // First when the atomic retainCount transitions from 1 -> 0
118 // Secondly when the work loop itself is commiting hari kari
119 // Hence the each leg of the free must be single threaded.
120 void IOWorkLoop::free()
125 // If we are here then we must be trying to shut down this work loop
126 // in this case disable all of the event source, mark the loop for
127 // as terminating and wakeup the work thread itself and return
128 // Note: we hold the gate across the entire operation mainly for the
129 // benefit of our event sources so we can disable them cleanly.
132 disableAllEventSources();
134 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
135 SETP(&fFlags
, kLoopTerminate
);
136 thread_wakeup_one((void *) &workToDo
);
137 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
141 else /* !workThread */ {
142 IOEventSource
*event
, *next
;
144 for (event
= eventChain
; event
; event
= next
) {
145 next
= event
->getNext();
146 event
->setWorkLoop(0);
152 // Either we have a partial initialisation to clean up
153 // or we the workThread itself is performing hari-kari.
154 // either way clean up all of our resources and return.
162 IOSimpleLockFree(workToDoLock
);
167 IORecursiveLockFree(gateLock
);
175 IOReturn
IOWorkLoop::addEventSource(IOEventSource
*newEvent
)
177 return controlG
->runCommand((void *) mAddEvent
, (void *) newEvent
);
180 IOReturn
IOWorkLoop::removeEventSource(IOEventSource
*toRemove
)
182 return controlG
->runCommand((void *) mRemoveEvent
, (void *) toRemove
);
185 void IOWorkLoop::enableAllEventSources() const
187 IOEventSource
*event
;
189 for (event
= eventChain
; event
; event
= event
->getNext())
193 void IOWorkLoop::disableAllEventSources() const
195 IOEventSource
*event
;
197 for (event
= eventChain
; event
; event
= event
->getNext())
198 if (event
!= controlG
) // Don't disable the control gate
202 void IOWorkLoop::enableAllInterrupts() const
204 IOEventSource
*event
;
206 for (event
= eventChain
; event
; event
= event
->getNext())
207 if (OSDynamicCast(IOInterruptEventSource
, event
))
211 void IOWorkLoop::disableAllInterrupts() const
213 IOEventSource
*event
;
215 for (event
= eventChain
; event
; event
= event
->getNext())
216 if (OSDynamicCast(IOInterruptEventSource
, event
))
221 #define IOTimeClientS() \
223 IOTimeStampStart(IODBG_WORKLOOP(IOWL_CLIENT), \
224 (unsigned int) this, (unsigned int) event); \
227 #define IOTimeClientE() \
229 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_CLIENT), \
230 (unsigned int) this, (unsigned int) event); \
233 #define IOTimeWorkS() \
235 IOTimeStampStart(IODBG_WORKLOOP(IOWL_WORK), (unsigned int) this); \
238 #define IOTimeWorkE() \
240 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_WORK),(unsigned int) this); \
245 #define IOTimeClientS()
246 #define IOTimeClientE()
247 #define IOTimeWorkS()
248 #define IOTimeWorkE()
252 void IOWorkLoop::threadMainContinuation()
255 self
= (IOWorkLoop
*) thread_get_cont_arg();
260 void IOWorkLoop::threadMain()
262 CLRP(&fFlags
, kLoopRestart
);
271 if (ISSETP(&fFlags
, kLoopTerminate
))
275 workToDo
= more
= false;
276 for (IOEventSource
*event
= eventChain
; event
; event
= event
->getNext()) {
279 more
|= event
->checkForWork();
282 if (ISSETP(&fFlags
, kLoopTerminate
))
284 else if (fFlags
& kLoopRestart
) {
285 CLRP(&fFlags
, kLoopRestart
);
295 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
296 if ( !ISSETP(&fFlags
, kLoopTerminate
) && !workToDo
) {
297 assert_wait((void *) &workToDo
, false);
298 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
300 thread_set_cont_arg((int) this);
301 thread_block(&threadMainContinuation
);
305 // At this point we either have work to do or we need
306 // to commit suicide. But no matter
307 // Clear the simple lock and retore the interrupt state
308 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
316 workThread
= 0; // Say we don't have a loop and free ourselves
321 IOThread
IOWorkLoop::getThread() const
326 bool IOWorkLoop::onThread() const
328 return (IOThreadSelf() == workThread
);
331 bool IOWorkLoop::inGate() const
333 return IORecursiveLockHaveLock(gateLock
);
336 // Internal APIs used by event sources to control the thread
337 void IOWorkLoop::signalWorkAvailable()
340 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
342 thread_wakeup_one((void *) &workToDo
);
343 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
347 void IOWorkLoop::openGate()
349 IORecursiveLockUnlock(gateLock
);
352 void IOWorkLoop::closeGate()
354 IORecursiveLockLock(gateLock
);
357 bool IOWorkLoop::tryCloseGate()
359 return IORecursiveLockTryLock(gateLock
) != 0;
362 int IOWorkLoop::sleepGate(void *event
, UInt32 interuptibleType
)
364 return IORecursiveLockSleep(gateLock
, event
, interuptibleType
);
367 void IOWorkLoop::wakeupGate(void *event
, bool oneThread
)
369 IORecursiveLockWakeup(gateLock
, event
, oneThread
);
372 IOReturn
IOWorkLoop::runAction(Action inAction
, OSObject
*target
,
373 void *arg0
, void *arg1
,
374 void *arg2
, void *arg3
)
378 // closeGate is recursive so don't worry if we already hold the lock.
380 res
= (*inAction
)(target
, arg0
, arg1
, arg2
, arg3
);
386 IOReturn
IOWorkLoop::_maintRequest(void *inC
, void *inD
, void *, void *)
388 maintCommandEnum command
= (maintCommandEnum
) (vm_address_t
) inC
;
389 IOEventSource
*inEvent
= (IOEventSource
*) inD
;
390 IOReturn res
= kIOReturnSuccess
;
395 if (!inEvent
->getWorkLoop()) {
396 SETP(&fFlags
, kLoopRestart
);
399 inEvent
->setWorkLoop(this);
403 eventChain
= inEvent
;
405 IOEventSource
*event
, *next
;
407 for (event
= eventChain
; (next
= event
->getNext()); event
= next
)
409 event
->setNext(inEvent
);
415 if (inEvent
->getWorkLoop()) {
416 if (eventChain
== inEvent
)
417 eventChain
= inEvent
->getNext();
419 IOEventSource
*event
, *next
;
422 while ((next
= event
->getNext()) && next
!= inEvent
)
426 res
= kIOReturnBadArgument
;
429 event
->setNext(inEvent
->getNext());
432 inEvent
->setWorkLoop(0);
435 SETP(&fFlags
, kLoopRestart
);
440 return kIOReturnUnsupported
;