]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOWorkLoop.cpp
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)
36 #include <pexpert/pexpert.h>
37 #include <IOKit/IOWorkLoop.h>
38 #include <IOKit/IOEventSource.h>
39 #include <IOKit/IOInterruptEventSource.h>
40 #include <IOKit/IOCommandGate.h>
41 #include <IOKit/IOTimeStamp.h>
42 #include <libkern/OSDebug.h>
44 #define super OSObject
46 OSDefineMetaClassAndStructors(IOWorkLoop
, OSObject
);
48 // Block of unused functions intended for future use
50 OSMetaClassDefineReservedUnused(IOWorkLoop
, 0);
51 OSMetaClassDefineReservedUnused(IOWorkLoop
, 1);
52 OSMetaClassDefineReservedUnused(IOWorkLoop
, 2);
54 OSMetaClassDefineReservedUsed(IOWorkLoop
, 0);
55 OSMetaClassDefineReservedUsed(IOWorkLoop
, 1);
56 OSMetaClassDefineReservedUsed(IOWorkLoop
, 2);
58 OSMetaClassDefineReservedUnused(IOWorkLoop
, 3);
59 OSMetaClassDefineReservedUnused(IOWorkLoop
, 4);
60 OSMetaClassDefineReservedUnused(IOWorkLoop
, 5);
61 OSMetaClassDefineReservedUnused(IOWorkLoop
, 6);
62 OSMetaClassDefineReservedUnused(IOWorkLoop
, 7);
64 enum IOWorkLoopState
{ kLoopRestart
= 0x1, kLoopTerminate
= 0x2 };
66 static inline void SETP(void *addr
, unsigned int flag
)
67 { unsigned int *num
= (unsigned int *) addr
; *num
|= flag
; }
68 static inline void CLRP(void *addr
, unsigned int flag
)
69 { unsigned int *num
= (unsigned int *) addr
; *num
&= ~flag
; }
70 static inline bool ISSETP(void *addr
, unsigned int flag
)
71 { unsigned int *num
= (unsigned int *) addr
; return (*num
& flag
) != 0; }
73 static inline void SETP(void *addr
, unsigned int flag
)
74 { unsigned char *num
= (unsigned char *) addr
; *num
|= flag
; }
75 static inline void CLRP(void *addr
, unsigned int flag
)
76 { unsigned char *num
= (unsigned char *) addr
; *num
&= ~flag
; }
77 static inline bool ISSETP(void *addr
, unsigned int flag
)
78 { unsigned char *num
= (unsigned char *) addr
; return (*num
& flag
) != 0; }
81 #define fFlags loopRestart
84 bool IOWorkLoop::init()
86 // The super init and gateLock allocation MUST be done first
90 if ( gateLock
== NULL
) {
91 if ( !( gateLock
= IORecursiveLockAlloc()) )
95 if ( workToDoLock
== NULL
) {
96 if ( !(workToDoLock
= IOSimpleLockAlloc()) )
98 IOSimpleLockInit(workToDoLock
);
102 if ( controlG
== NULL
) {
103 controlG
= IOCommandGate::commandGate(
105 OSMemberFunctionCast(
106 IOCommandGate::Action
,
108 &IOWorkLoop::_maintRequest
));
112 // Point the controlGate at the workLoop. Usually addEventSource
113 // does this automatically. The problem is in this case addEventSource
114 // uses the control gate and it has to be bootstrapped.
115 controlG
->setWorkLoop(this);
116 if (addEventSource(controlG
) != kIOReturnSuccess
)
120 if ( workThread
== NULL
) {
121 thread_continue_t cptr
= OSMemberFunctionCast(
124 &IOWorkLoop::threadMain
);
125 if (KERN_SUCCESS
!= kernel_thread_start(cptr
, this, &workThread
))
133 IOWorkLoop::workLoop()
135 return IOWorkLoop::workLoopWithOptions(0);
139 IOWorkLoop::workLoopWithOptions(IOOptionBits options
)
141 IOWorkLoop
*me
= new IOWorkLoop
;
144 me
->reserved
= IONew(ExpansionData
, 1);
149 me
->reserved
->options
= options
;
152 if (me
&& !me
->init()) {
160 // Free is called twice:
161 // First when the atomic retainCount transitions from 1 -> 0
162 // Secondly when the work loop itself is commiting hari kari
163 // Hence the each leg of the free must be single threaded.
164 void IOWorkLoop::free()
169 // If we are here then we must be trying to shut down this work loop
170 // in this case disable all of the event source, mark the loop
171 // as terminating and wakeup the work thread itself and return
172 // Note: we hold the gate across the entire operation mainly for the
173 // benefit of our event sources so we can disable them cleanly.
176 disableAllEventSources();
178 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
179 SETP(&fFlags
, kLoopTerminate
);
180 thread_wakeup_one((void *) &workToDo
);
181 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
185 else /* !workThread */ {
186 IOEventSource
*event
, *next
;
188 for (event
= eventChain
; event
; event
= next
) {
189 next
= event
->getNext();
190 event
->setWorkLoop(0);
196 // Either we have a partial initialization to clean up
197 // or the workThread itself is performing hari-kari.
198 // Either way clean up all of our resources and return.
206 IOSimpleLockFree(workToDoLock
);
211 IORecursiveLockFree(gateLock
);
215 IODelete(reserved
, ExpansionData
, 1);
223 IOReturn
IOWorkLoop::addEventSource(IOEventSource
*newEvent
)
225 return controlG
->runCommand((void *) mAddEvent
, (void *) newEvent
);
228 IOReturn
IOWorkLoop::removeEventSource(IOEventSource
*toRemove
)
230 return controlG
->runCommand((void *) mRemoveEvent
, (void *) toRemove
);
233 void IOWorkLoop::enableAllEventSources() const
235 IOEventSource
*event
;
237 for (event
= eventChain
; event
; event
= event
->getNext())
241 void IOWorkLoop::disableAllEventSources() const
243 IOEventSource
*event
;
245 for (event
= eventChain
; event
; event
= event
->getNext())
246 if (event
!= controlG
) // Don't disable the control gate
250 void IOWorkLoop::enableAllInterrupts() const
252 IOEventSource
*event
;
254 for (event
= eventChain
; event
; event
= event
->getNext())
255 if (OSDynamicCast(IOInterruptEventSource
, event
))
259 void IOWorkLoop::disableAllInterrupts() const
261 IOEventSource
*event
;
263 for (event
= eventChain
; event
; event
= event
->getNext())
264 if (OSDynamicCast(IOInterruptEventSource
, event
))
269 #define IOTimeClientS() \
271 IOTimeStampStart(IODBG_WORKLOOP(IOWL_CLIENT), \
272 (unsigned int) this, (unsigned int) event); \
275 #define IOTimeClientE() \
277 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_CLIENT), \
278 (unsigned int) this, (unsigned int) event); \
281 #define IOTimeWorkS() \
283 IOTimeStampStart(IODBG_WORKLOOP(IOWL_WORK), (unsigned int) this); \
286 #define IOTimeWorkE() \
288 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_WORK),(unsigned int) this); \
293 #define IOTimeClientS()
294 #define IOTimeClientE()
295 #define IOTimeWorkS()
296 #define IOTimeWorkE()
300 /* virtual */ bool IOWorkLoop::runEventSources()
304 if (ISSETP(&fFlags
, kLoopTerminate
))
310 CLRP(&fFlags
, kLoopRestart
);
312 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
314 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
315 for (IOEventSource
*evnt
= eventChain
; evnt
; evnt
= evnt
->getNext()) {
318 more
|= evnt
->checkForWork();
321 if (ISSETP(&fFlags
, kLoopTerminate
))
323 else if (fFlags
& kLoopRestart
) {
338 /* virtual */ void IOWorkLoop::threadMain()
342 if ( !runEventSources() )
345 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
346 if ( !ISSETP(&fFlags
, kLoopTerminate
) && !workToDo
) {
347 assert_wait((void *) &workToDo
, false);
348 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
349 thread_continue_t cptr
= NULL
;
350 if (!reserved
|| !(kPreciousStack
& reserved
->options
))
351 cptr
= OSMemberFunctionCast(
352 thread_continue_t
, this, &IOWorkLoop::threadMain
);
353 thread_block_parameter(cptr
, this);
358 // At this point we either have work to do or we need
359 // to commit suicide. But no matter
360 // Clear the simple lock and retore the interrupt state
361 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
365 thread_t thread
= workThread
;
366 workThread
= 0; // Say we don't have a loop and free ourselves
369 thread_deallocate(thread
);
370 (void) thread_terminate(thread
);
373 IOThread
IOWorkLoop::getThread() const
378 bool IOWorkLoop::onThread() const
380 return (IOThreadSelf() == workThread
);
383 bool IOWorkLoop::inGate() const
385 return IORecursiveLockHaveLock(gateLock
);
388 // Internal APIs used by event sources to control the thread
389 void IOWorkLoop::signalWorkAvailable()
392 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
394 thread_wakeup_one((void *) &workToDo
);
395 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
399 void IOWorkLoop::openGate()
401 IORecursiveLockUnlock(gateLock
);
404 void IOWorkLoop::closeGate()
406 IORecursiveLockLock(gateLock
);
409 bool IOWorkLoop::tryCloseGate()
411 return IORecursiveLockTryLock(gateLock
) != 0;
414 int IOWorkLoop::sleepGate(void *event
, UInt32 interuptibleType
)
416 return IORecursiveLockSleep(gateLock
, event
, interuptibleType
);
419 int IOWorkLoop::sleepGate(void *event
, AbsoluteTime deadline
, UInt32 interuptibleType
)
421 return IORecursiveLockSleepDeadline(gateLock
, event
, deadline
, interuptibleType
);
424 void IOWorkLoop::wakeupGate(void *event
, bool oneThread
)
426 IORecursiveLockWakeup(gateLock
, event
, oneThread
);
429 IOReturn
IOWorkLoop::runAction(Action inAction
, OSObject
*target
,
430 void *arg0
, void *arg1
,
431 void *arg2
, void *arg3
)
435 // closeGate is recursive so don't worry if we already hold the lock.
437 res
= (*inAction
)(target
, arg0
, arg1
, arg2
, arg3
);
443 IOReturn
IOWorkLoop::_maintRequest(void *inC
, void *inD
, void *, void *)
445 maintCommandEnum command
= (maintCommandEnum
) (uintptr_t) inC
;
446 IOEventSource
*inEvent
= (IOEventSource
*) inD
;
447 IOReturn res
= kIOReturnSuccess
;
452 if (!inEvent
->getWorkLoop()) {
453 SETP(&fFlags
, kLoopRestart
);
456 inEvent
->setWorkLoop(this);
460 eventChain
= inEvent
;
462 IOEventSource
*event
, *next
;
464 for (event
= eventChain
; (next
= event
->getNext()); event
= next
)
466 event
->setNext(inEvent
);
472 if (inEvent
->getWorkLoop()) {
473 if (eventChain
== inEvent
)
474 eventChain
= inEvent
->getNext();
476 IOEventSource
*event
, *next
;
479 while ((next
= event
->getNext()) && next
!= inEvent
)
483 res
= kIOReturnBadArgument
;
486 event
->setNext(inEvent
->getNext());
489 inEvent
->setWorkLoop(0);
492 SETP(&fFlags
, kLoopRestart
);
497 return kIOReturnUnsupported
;