]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOWorkLoop.cpp
54eaa0e3c7a42183a75fca994dc29b91ad66c35f
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
);
305 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
307 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
308 for (IOEventSource
*evnt
= eventChain
; evnt
; evnt
= evnt
->getNext()) {
311 more
|= evnt
->checkForWork();
314 if (ISSETP(&fFlags
, kLoopTerminate
))
316 else if (fFlags
& kLoopRestart
) {
331 /* virtual */ void IOWorkLoop::threadMain()
335 if ( !runEventSources() )
338 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
339 if ( !ISSETP(&fFlags
, kLoopTerminate
) && !workToDo
) {
340 assert_wait((void *) &workToDo
, false);
341 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
342 thread_continue_t cptr
= NULL
;
343 if (!reserved
|| !(kPreciousStack
& reserved
->options
))
344 cptr
= OSMemberFunctionCast(
345 thread_continue_t
, this, &IOWorkLoop::threadMain
);
346 thread_block_parameter(cptr
, this);
351 // At this point we either have work to do or we need
352 // to commit suicide. But no matter
353 // Clear the simple lock and retore the interrupt state
354 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
358 workThread
= 0; // Say we don't have a loop and free ourselves
363 IOThread
IOWorkLoop::getThread() const
368 bool IOWorkLoop::onThread() const
370 return (IOThreadSelf() == workThread
);
373 bool IOWorkLoop::inGate() const
375 return IORecursiveLockHaveLock(gateLock
);
378 // Internal APIs used by event sources to control the thread
379 void IOWorkLoop::signalWorkAvailable()
382 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
384 thread_wakeup_one((void *) &workToDo
);
385 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
389 void IOWorkLoop::openGate()
391 IORecursiveLockUnlock(gateLock
);
394 void IOWorkLoop::closeGate()
396 IORecursiveLockLock(gateLock
);
399 bool IOWorkLoop::tryCloseGate()
401 return IORecursiveLockTryLock(gateLock
) != 0;
404 int IOWorkLoop::sleepGate(void *event
, UInt32 interuptibleType
)
406 return IORecursiveLockSleep(gateLock
, event
, interuptibleType
);
409 void IOWorkLoop::wakeupGate(void *event
, bool oneThread
)
411 IORecursiveLockWakeup(gateLock
, event
, oneThread
);
414 IOReturn
IOWorkLoop::runAction(Action inAction
, OSObject
*target
,
415 void *arg0
, void *arg1
,
416 void *arg2
, void *arg3
)
420 // closeGate is recursive so don't worry if we already hold the lock.
422 res
= (*inAction
)(target
, arg0
, arg1
, arg2
, arg3
);
428 IOReturn
IOWorkLoop::_maintRequest(void *inC
, void *inD
, void *, void *)
430 maintCommandEnum command
= (maintCommandEnum
) (vm_address_t
) inC
;
431 IOEventSource
*inEvent
= (IOEventSource
*) inD
;
432 IOReturn res
= kIOReturnSuccess
;
437 if (!inEvent
->getWorkLoop()) {
438 SETP(&fFlags
, kLoopRestart
);
441 inEvent
->setWorkLoop(this);
445 eventChain
= inEvent
;
447 IOEventSource
*event
, *next
;
449 for (event
= eventChain
; (next
= event
->getNext()); event
= next
)
451 event
->setNext(inEvent
);
457 if (inEvent
->getWorkLoop()) {
458 if (eventChain
== inEvent
)
459 eventChain
= inEvent
->getNext();
461 IOEventSource
*event
, *next
;
464 while ((next
= event
->getNext()) && next
!= inEvent
)
468 res
= kIOReturnBadArgument
;
471 event
->setNext(inEvent
->getNext());
474 inEvent
->setWorkLoop(0);
477 SETP(&fFlags
, kLoopRestart
);
482 return kIOReturnUnsupported
;