]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOWorkLoop.cpp
09135d172fb390838b270f640383d76276020ff8
2 * Copyright (c) 1998-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
32 1998-7-13 Godfrey van der Linden(gvdl)
35 #include <IOKit/IOWorkLoop.h>
36 #include <IOKit/IOEventSource.h>
37 #include <IOKit/IOInterruptEventSource.h>
38 #include <IOKit/IOCommandGate.h>
39 #include <IOKit/IOTimeStamp.h>
40 #include <libkern/OSDebug.h>
42 #define super OSObject
44 OSDefineMetaClassAndStructors(IOWorkLoop
, OSObject
);
46 // Block of unused functions intended for future use
47 OSMetaClassDefineReservedUsed(IOWorkLoop
, 0);
48 OSMetaClassDefineReservedUsed(IOWorkLoop
, 1);
50 OSMetaClassDefineReservedUnused(IOWorkLoop
, 2);
51 OSMetaClassDefineReservedUnused(IOWorkLoop
, 3);
52 OSMetaClassDefineReservedUnused(IOWorkLoop
, 4);
53 OSMetaClassDefineReservedUnused(IOWorkLoop
, 5);
54 OSMetaClassDefineReservedUnused(IOWorkLoop
, 6);
55 OSMetaClassDefineReservedUnused(IOWorkLoop
, 7);
57 enum IOWorkLoopState
{ kLoopRestart
= 0x1, kLoopTerminate
= 0x2 };
59 static inline void SETP(void *addr
, unsigned int flag
)
60 { unsigned int *num
= (unsigned int *) addr
; *num
|= flag
; }
61 static inline void CLRP(void *addr
, unsigned int flag
)
62 { unsigned int *num
= (unsigned int *) addr
; *num
&= ~flag
; }
63 static inline bool ISSETP(void *addr
, unsigned int flag
)
64 { unsigned int *num
= (unsigned int *) addr
; return (*num
& flag
) != 0; }
66 static inline void SETP(void *addr
, unsigned int flag
)
67 { unsigned char *num
= (unsigned char *) addr
; *num
|= flag
; }
68 static inline void CLRP(void *addr
, unsigned int flag
)
69 { unsigned char *num
= (unsigned char *) addr
; *num
&= ~flag
; }
70 static inline bool ISSETP(void *addr
, unsigned int flag
)
71 { unsigned char *num
= (unsigned char *) addr
; return (*num
& flag
) != 0; }
74 #define fFlags loopRestart
76 bool IOWorkLoop::init()
78 // The super init and gateLock allocation MUST be done first
82 if ( gateLock
== NULL
) {
83 if ( !( gateLock
= IORecursiveLockAlloc()) )
87 if ( workToDoLock
== NULL
) {
88 if ( !(workToDoLock
= IOSimpleLockAlloc()) )
90 IOSimpleLockInit(workToDoLock
);
94 if ( controlG
== NULL
) {
95 controlG
= IOCommandGate::commandGate(
98 IOCommandGate::Action
,
100 &IOWorkLoop::_maintRequest
));
104 // Point the controlGate at the workLoop. Usually addEventSource
105 // does this automatically. The problem is in this case addEventSource
106 // uses the control gate and it has to be bootstrapped.
107 controlG
->setWorkLoop(this);
108 if (addEventSource(controlG
) != kIOReturnSuccess
)
112 if ( workThread
== NULL
) {
113 IOThreadFunc cptr
= OSMemberFunctionCast(
116 &IOWorkLoop::threadMain
);
117 workThread
= IOCreateThread(cptr
, this);
126 IOWorkLoop::workLoop()
128 return IOWorkLoop::workLoopWithOptions(0);
132 IOWorkLoop::workLoopWithOptions(IOOptionBits options
)
134 IOWorkLoop
*me
= new IOWorkLoop
;
137 me
->reserved
= IONew(ExpansionData
, 1);
142 me
->reserved
->options
= options
;
145 if (me
&& !me
->init()) {
153 // Free is called twice:
154 // First when the atomic retainCount transitions from 1 -> 0
155 // Secondly when the work loop itself is commiting hari kari
156 // Hence the each leg of the free must be single threaded.
157 void IOWorkLoop::free()
162 // If we are here then we must be trying to shut down this work loop
163 // in this case disable all of the event source, mark the loop
164 // as terminating and wakeup the work thread itself and return
165 // Note: we hold the gate across the entire operation mainly for the
166 // benefit of our event sources so we can disable them cleanly.
169 disableAllEventSources();
171 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
172 SETP(&fFlags
, kLoopTerminate
);
173 thread_wakeup_one((void *) &workToDo
);
174 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
178 else /* !workThread */ {
179 IOEventSource
*event
, *next
;
181 for (event
= eventChain
; event
; event
= next
) {
182 next
= event
->getNext();
183 event
->setWorkLoop(0);
189 // Either we have a partial initialization to clean up
190 // or the workThread itself is performing hari-kari.
191 // Either way clean up all of our resources and return.
199 IOSimpleLockFree(workToDoLock
);
204 IORecursiveLockFree(gateLock
);
208 IODelete(reserved
, ExpansionData
, 1);
216 IOReturn
IOWorkLoop::addEventSource(IOEventSource
*newEvent
)
218 return controlG
->runCommand((void *) mAddEvent
, (void *) newEvent
);
221 IOReturn
IOWorkLoop::removeEventSource(IOEventSource
*toRemove
)
223 return controlG
->runCommand((void *) mRemoveEvent
, (void *) toRemove
);
226 void IOWorkLoop::enableAllEventSources() const
228 IOEventSource
*event
;
230 for (event
= eventChain
; event
; event
= event
->getNext())
234 void IOWorkLoop::disableAllEventSources() const
236 IOEventSource
*event
;
238 for (event
= eventChain
; event
; event
= event
->getNext())
239 if (event
!= controlG
) // Don't disable the control gate
243 void IOWorkLoop::enableAllInterrupts() const
245 IOEventSource
*event
;
247 for (event
= eventChain
; event
; event
= event
->getNext())
248 if (OSDynamicCast(IOInterruptEventSource
, event
))
252 void IOWorkLoop::disableAllInterrupts() const
254 IOEventSource
*event
;
256 for (event
= eventChain
; event
; event
= event
->getNext())
257 if (OSDynamicCast(IOInterruptEventSource
, event
))
262 #define IOTimeClientS() \
264 IOTimeStampStart(IODBG_WORKLOOP(IOWL_CLIENT), \
265 (unsigned int) this, (unsigned int) event); \
268 #define IOTimeClientE() \
270 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_CLIENT), \
271 (unsigned int) this, (unsigned int) event); \
274 #define IOTimeWorkS() \
276 IOTimeStampStart(IODBG_WORKLOOP(IOWL_WORK), (unsigned int) this); \
279 #define IOTimeWorkE() \
281 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_WORK),(unsigned int) this); \
286 #define IOTimeClientS()
287 #define IOTimeClientE()
288 #define IOTimeWorkS()
289 #define IOTimeWorkE()
293 /* virtual */ bool IOWorkLoop::runEventSources()
297 if (ISSETP(&fFlags
, kLoopTerminate
))
303 CLRP(&fFlags
, kLoopRestart
);
304 workToDo
= more
= false;
305 for (IOEventSource
*evnt
= eventChain
; evnt
; evnt
= evnt
->getNext()) {
308 more
|= evnt
->checkForWork();
311 if (ISSETP(&fFlags
, kLoopTerminate
))
313 else if (fFlags
& kLoopRestart
) {
328 /* virtual */ void IOWorkLoop::threadMain()
332 if ( !runEventSources() )
335 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
336 if ( !ISSETP(&fFlags
, kLoopTerminate
) && !workToDo
) {
337 assert_wait((void *) &workToDo
, false);
338 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
339 thread_continue_t cptr
= NULL
;
340 if (!reserved
|| !(kPreciousStack
& reserved
->options
))
341 cptr
= OSMemberFunctionCast(
342 thread_continue_t
, this, &IOWorkLoop::threadMain
);
343 thread_block_parameter(cptr
, this);
348 // At this point we either have work to do or we need
349 // to commit suicide. But no matter
350 // Clear the simple lock and retore the interrupt state
351 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
355 workThread
= 0; // Say we don't have a loop and free ourselves
360 IOThread
IOWorkLoop::getThread() const
365 bool IOWorkLoop::onThread() const
367 return (IOThreadSelf() == workThread
);
370 bool IOWorkLoop::inGate() const
372 return IORecursiveLockHaveLock(gateLock
);
375 // Internal APIs used by event sources to control the thread
376 void IOWorkLoop::signalWorkAvailable()
379 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
381 thread_wakeup_one((void *) &workToDo
);
382 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
386 void IOWorkLoop::openGate()
388 IORecursiveLockUnlock(gateLock
);
391 void IOWorkLoop::closeGate()
393 IORecursiveLockLock(gateLock
);
396 bool IOWorkLoop::tryCloseGate()
398 return IORecursiveLockTryLock(gateLock
) != 0;
401 int IOWorkLoop::sleepGate(void *event
, UInt32 interuptibleType
)
403 return IORecursiveLockSleep(gateLock
, event
, interuptibleType
);
406 void IOWorkLoop::wakeupGate(void *event
, bool oneThread
)
408 IORecursiveLockWakeup(gateLock
, event
, oneThread
);
411 IOReturn
IOWorkLoop::runAction(Action inAction
, OSObject
*target
,
412 void *arg0
, void *arg1
,
413 void *arg2
, void *arg3
)
417 // closeGate is recursive so don't worry if we already hold the lock.
419 res
= (*inAction
)(target
, arg0
, arg1
, arg2
, arg3
);
425 IOReturn
IOWorkLoop::_maintRequest(void *inC
, void *inD
, void *, void *)
427 maintCommandEnum command
= (maintCommandEnum
) (vm_address_t
) inC
;
428 IOEventSource
*inEvent
= (IOEventSource
*) inD
;
429 IOReturn res
= kIOReturnSuccess
;
434 if (!inEvent
->getWorkLoop()) {
435 SETP(&fFlags
, kLoopRestart
);
438 inEvent
->setWorkLoop(this);
442 eventChain
= inEvent
;
444 IOEventSource
*event
, *next
;
446 for (event
= eventChain
; (next
= event
->getNext()); event
= next
)
448 event
->setNext(inEvent
);
454 if (inEvent
->getWorkLoop()) {
455 if (eventChain
== inEvent
)
456 eventChain
= inEvent
->getNext();
458 IOEventSource
*event
, *next
;
461 while ((next
= event
->getNext()) && next
!= inEvent
)
465 res
= kIOReturnBadArgument
;
468 event
->setNext(inEvent
->getNext());
471 inEvent
->setWorkLoop(0);
474 SETP(&fFlags
, kLoopRestart
);
479 return kIOReturnUnsupported
;