]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOWorkLoop.cpp
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
26 1998-7-13 Godfrey van der Linden(gvdl)
29 #include <IOKit/IOWorkLoop.h>
30 #include <IOKit/IOEventSource.h>
31 #include <IOKit/IOInterruptEventSource.h>
32 #include <IOKit/IOCommandGate.h>
33 #include <IOKit/IOTimeStamp.h>
35 #define super OSObject
37 OSDefineMetaClassAndStructors(IOWorkLoop
, OSObject
);
39 // Block of unused functions intended for future use
40 OSMetaClassDefineReservedUsed(IOWorkLoop
, 0);
42 OSMetaClassDefineReservedUnused(IOWorkLoop
, 1);
43 OSMetaClassDefineReservedUnused(IOWorkLoop
, 2);
44 OSMetaClassDefineReservedUnused(IOWorkLoop
, 3);
45 OSMetaClassDefineReservedUnused(IOWorkLoop
, 4);
46 OSMetaClassDefineReservedUnused(IOWorkLoop
, 5);
47 OSMetaClassDefineReservedUnused(IOWorkLoop
, 6);
48 OSMetaClassDefineReservedUnused(IOWorkLoop
, 7);
50 enum IOWorkLoopState
{ kLoopRestart
= 0x1, kLoopTerminate
= 0x2 };
51 static inline void SETP(void *addr
, unsigned int flag
)
52 { unsigned int *num
= (unsigned int *) addr
; *num
|= flag
; }
53 static inline void CLRP(void *addr
, unsigned int flag
)
54 { unsigned int *num
= (unsigned int *) addr
; *num
&= ~flag
; }
55 static inline bool ISSETP(void *addr
, unsigned int flag
)
56 { unsigned int *num
= (unsigned int *) addr
; return (*num
& flag
) != 0; }
58 #define fFlags loopRestart
60 void IOWorkLoop::launchThreadMain(void *self
)
62 thread_set_cont_arg((int) self
);
63 threadMainContinuation();
66 bool IOWorkLoop::init()
68 // The super init and gateLock allocation MUST be done first
72 if ( !(gateLock
= IORecursiveLockAlloc()) )
75 if ( !(workToDoLock
= IOSimpleLockAlloc()) )
78 controlG
= IOCommandGate::
79 commandGate(this, (IOCommandGate::Action
) &IOWorkLoop::_maintRequest
);
83 IOSimpleLockInit(workToDoLock
);
86 // Point the controlGate at the workLoop. Usually addEventSource
87 // does this automatically. The problem is in this case addEventSource
88 // uses the control gate and it has to be bootstrapped.
89 controlG
->setWorkLoop(this);
90 if (addEventSource(controlG
) != kIOReturnSuccess
)
93 workThread
= IOCreateThread(launchThreadMain
, (void *) this);
101 IOWorkLoop::workLoop()
103 IOWorkLoop
*me
= new IOWorkLoop
;
105 if (me
&& !me
->init()) {
113 // Free is called twice:
114 // First when the atomic retainCount transitions from 1 -> 0
115 // Secondly when the work loop itself is commiting hari kari
116 // Hence the each leg of the free must be single threaded.
117 void IOWorkLoop::free()
122 // If we are here then we must be trying to shut down this work loop
123 // in this case disable all of the event source, mark the loop for
124 // as terminating and wakeup the work thread itself and return
125 // Note: we hold the gate across the entire operation mainly for the
126 // benefit of our event sources so we can disable them cleanly.
129 disableAllEventSources();
131 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
132 SETP(&fFlags
, kLoopTerminate
);
133 thread_wakeup_one((void *) &workToDo
);
134 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
138 else /* !workThread */ {
139 IOEventSource
*event
, *next
;
141 for (event
= eventChain
; event
; event
= next
) {
142 next
= event
->getNext();
143 event
->setWorkLoop(0);
149 // Either we have a partial initialisation to clean up
150 // or we the workThread itself is performing hari-kari.
151 // either way clean up all of our resources and return.
159 IOSimpleLockFree(workToDoLock
);
164 IORecursiveLockFree(gateLock
);
172 IOReturn
IOWorkLoop::addEventSource(IOEventSource
*newEvent
)
174 return controlG
->runCommand((void *) mAddEvent
, (void *) newEvent
);
177 IOReturn
IOWorkLoop::removeEventSource(IOEventSource
*toRemove
)
179 return controlG
->runCommand((void *) mRemoveEvent
, (void *) toRemove
);
182 void IOWorkLoop::enableAllEventSources() const
184 IOEventSource
*event
;
186 for (event
= eventChain
; event
; event
= event
->getNext())
190 void IOWorkLoop::disableAllEventSources() const
192 IOEventSource
*event
;
194 for (event
= eventChain
; event
; event
= event
->getNext())
195 if (event
!= controlG
) // Don't disable the control gate
199 void IOWorkLoop::enableAllInterrupts() const
201 IOEventSource
*event
;
203 for (event
= eventChain
; event
; event
= event
->getNext())
204 if (OSDynamicCast(IOInterruptEventSource
, event
))
208 void IOWorkLoop::disableAllInterrupts() const
210 IOEventSource
*event
;
212 for (event
= eventChain
; event
; event
= event
->getNext())
213 if (OSDynamicCast(IOInterruptEventSource
, event
))
218 #define IOTimeClientS() \
220 IOTimeStampStart(IODBG_WORKLOOP(IOWL_CLIENT), \
221 (unsigned int) this, (unsigned int) event); \
224 #define IOTimeClientE() \
226 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_CLIENT), \
227 (unsigned int) this, (unsigned int) event); \
230 #define IOTimeWorkS() \
232 IOTimeStampStart(IODBG_WORKLOOP(IOWL_WORK), (unsigned int) this); \
235 #define IOTimeWorkE() \
237 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_WORK),(unsigned int) this); \
242 #define IOTimeClientS()
243 #define IOTimeClientE()
244 #define IOTimeWorkS()
245 #define IOTimeWorkE()
249 void IOWorkLoop::threadMainContinuation()
252 self
= (IOWorkLoop
*) thread_get_cont_arg();
257 void IOWorkLoop::threadMain()
259 CLRP(&fFlags
, kLoopRestart
);
268 if (ISSETP(&fFlags
, kLoopTerminate
))
272 workToDo
= more
= false;
273 for (IOEventSource
*event
= eventChain
; event
; event
= event
->getNext()) {
276 more
|= event
->checkForWork();
279 if (ISSETP(&fFlags
, kLoopTerminate
))
281 else if (fFlags
& kLoopRestart
) {
282 CLRP(&fFlags
, kLoopRestart
);
292 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
293 if ( !ISSETP(&fFlags
, kLoopTerminate
) && !workToDo
) {
294 assert_wait((void *) &workToDo
, false);
295 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
297 thread_set_cont_arg((int) this);
298 thread_block(&threadMainContinuation
);
302 // At this point we either have work to do or we need
303 // to commit suicide. But no matter
304 // Clear the simple lock and retore the interrupt state
305 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
313 workThread
= 0; // Say we don't have a loop and free ourselves
318 IOThread
IOWorkLoop::getThread() const
323 bool IOWorkLoop::onThread() const
325 return (IOThreadSelf() == workThread
);
328 bool IOWorkLoop::inGate() const
330 return IORecursiveLockHaveLock(gateLock
);
333 // Internal APIs used by event sources to control the thread
334 void IOWorkLoop::signalWorkAvailable()
337 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
339 thread_wakeup_one((void *) &workToDo
);
340 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
344 void IOWorkLoop::openGate()
346 IORecursiveLockUnlock(gateLock
);
349 void IOWorkLoop::closeGate()
351 IORecursiveLockLock(gateLock
);
354 bool IOWorkLoop::tryCloseGate()
356 return IORecursiveLockTryLock(gateLock
) != 0;
359 int IOWorkLoop::sleepGate(void *event
, UInt32 interuptibleType
)
361 return IORecursiveLockSleep(gateLock
, event
, interuptibleType
);
364 void IOWorkLoop::wakeupGate(void *event
, bool oneThread
)
366 IORecursiveLockWakeup(gateLock
, event
, oneThread
);
369 IOReturn
IOWorkLoop::runAction(Action inAction
, OSObject
*target
,
370 void *arg0
, void *arg1
,
371 void *arg2
, void *arg3
)
375 // closeGate is recursive so don't worry if we already hold the lock.
377 res
= (*inAction
)(target
, arg0
, arg1
, arg2
, arg3
);
383 IOReturn
IOWorkLoop::_maintRequest(void *inC
, void *inD
, void *, void *)
385 maintCommandEnum command
= (maintCommandEnum
) (vm_address_t
) inC
;
386 IOEventSource
*inEvent
= (IOEventSource
*) inD
;
387 IOReturn res
= kIOReturnSuccess
;
392 if (!inEvent
->getWorkLoop()) {
393 SETP(&fFlags
, kLoopRestart
);
396 inEvent
->setWorkLoop(this);
400 eventChain
= inEvent
;
402 IOEventSource
*event
, *next
;
404 for (event
= eventChain
; (next
= event
->getNext()); event
= next
)
406 event
->setNext(inEvent
);
412 if (inEvent
->getWorkLoop()) {
413 if (eventChain
== inEvent
)
414 eventChain
= inEvent
->getNext();
416 IOEventSource
*event
, *next
;
419 while ((next
= event
->getNext()) && next
!= inEvent
)
423 res
= kIOReturnBadArgument
;
426 event
->setNext(inEvent
->getNext());
429 inEvent
->setWorkLoop(0);
432 SETP(&fFlags
, kLoopRestart
);
437 return kIOReturnUnsupported
;