]>
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);
41 OSMetaClassDefineReservedUsed(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 bool IOWorkLoop::init()
62 // The super init and gateLock allocation MUST be done first
66 if ( !(gateLock
= IORecursiveLockAlloc()) )
69 if ( !(workToDoLock
= IOSimpleLockAlloc()) )
72 controlG
= IOCommandGate::
73 commandGate(this, OSMemberFunctionCast(IOCommandGate::Action
,
74 this, &IOWorkLoop::_maintRequest
));
78 IOSimpleLockInit(workToDoLock
);
81 // Point the controlGate at the workLoop. Usually addEventSource
82 // does this automatically. The problem is in this case addEventSource
83 // uses the control gate and it has to be bootstrapped.
84 controlG
->setWorkLoop(this);
85 if (addEventSource(controlG
) != kIOReturnSuccess
)
89 OSMemberFunctionCast(IOThreadFunc
, this, &IOWorkLoop::threadMain
);
90 workThread
= IOCreateThread(cptr
, this);
98 IOWorkLoop::workLoop()
100 IOWorkLoop
*me
= new IOWorkLoop
;
102 if (me
&& !me
->init()) {
110 // Free is called twice:
111 // First when the atomic retainCount transitions from 1 -> 0
112 // Secondly when the work loop itself is commiting hari kari
113 // Hence the each leg of the free must be single threaded.
114 void IOWorkLoop::free()
119 // If we are here then we must be trying to shut down this work loop
120 // in this case disable all of the event source, mark the loop for
121 // as terminating and wakeup the work thread itself and return
122 // Note: we hold the gate across the entire operation mainly for the
123 // benefit of our event sources so we can disable them cleanly.
126 disableAllEventSources();
128 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
129 SETP(&fFlags
, kLoopTerminate
);
130 thread_wakeup_one((void *) &workToDo
);
131 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
135 else /* !workThread */ {
136 IOEventSource
*event
, *next
;
138 for (event
= eventChain
; event
; event
= next
) {
139 next
= event
->getNext();
140 event
->setWorkLoop(0);
146 // Either we have a partial initialisation to clean up
147 // or we the workThread itself is performing hari-kari.
148 // either way clean up all of our resources and return.
156 IOSimpleLockFree(workToDoLock
);
161 IORecursiveLockFree(gateLock
);
169 IOReturn
IOWorkLoop::addEventSource(IOEventSource
*newEvent
)
171 return controlG
->runCommand((void *) mAddEvent
, (void *) newEvent
);
174 IOReturn
IOWorkLoop::removeEventSource(IOEventSource
*toRemove
)
176 return controlG
->runCommand((void *) mRemoveEvent
, (void *) toRemove
);
179 void IOWorkLoop::enableAllEventSources() const
181 IOEventSource
*event
;
183 for (event
= eventChain
; event
; event
= event
->getNext())
187 void IOWorkLoop::disableAllEventSources() const
189 IOEventSource
*event
;
191 for (event
= eventChain
; event
; event
= event
->getNext())
192 if (event
!= controlG
) // Don't disable the control gate
196 void IOWorkLoop::enableAllInterrupts() const
198 IOEventSource
*event
;
200 for (event
= eventChain
; event
; event
= event
->getNext())
201 if (OSDynamicCast(IOInterruptEventSource
, event
))
205 void IOWorkLoop::disableAllInterrupts() const
207 IOEventSource
*event
;
209 for (event
= eventChain
; event
; event
= event
->getNext())
210 if (OSDynamicCast(IOInterruptEventSource
, event
))
215 #define IOTimeClientS() \
217 IOTimeStampStart(IODBG_WORKLOOP(IOWL_CLIENT), \
218 (unsigned int) this, (unsigned int) event); \
221 #define IOTimeClientE() \
223 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_CLIENT), \
224 (unsigned int) this, (unsigned int) event); \
227 #define IOTimeWorkS() \
229 IOTimeStampStart(IODBG_WORKLOOP(IOWL_WORK), (unsigned int) this); \
232 #define IOTimeWorkE() \
234 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_WORK),(unsigned int) this); \
239 #define IOTimeClientS()
240 #define IOTimeClientE()
241 #define IOTimeWorkS()
242 #define IOTimeWorkE()
246 /* virtual */ bool IOWorkLoop::runEventSources()
250 if (ISSETP(&fFlags
, kLoopTerminate
))
256 CLRP(&fFlags
, kLoopRestart
);
257 workToDo
= more
= false;
258 for (IOEventSource
*evnt
= eventChain
; evnt
; evnt
= evnt
->getNext()) {
261 more
|= evnt
->checkForWork();
264 if (ISSETP(&fFlags
, kLoopTerminate
))
266 else if (fFlags
& kLoopRestart
) {
281 /* virtual */ void IOWorkLoop::threadMain()
284 if ( !runEventSources() )
287 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
288 if ( !ISSETP(&fFlags
, kLoopTerminate
) && !workToDo
) {
289 assert_wait((void *) &workToDo
, false);
290 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
292 thread_continue_t cptr
= OSMemberFunctionCast(
293 thread_continue_t
, this, &IOWorkLoop::threadMain
);
294 thread_block_parameter(cptr
, this);
298 // At this point we either have work to do or we need
299 // to commit suicide. But no matter
300 // Clear the simple lock and retore the interrupt state
301 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
305 workThread
= 0; // Say we don't have a loop and free ourselves
310 IOThread
IOWorkLoop::getThread() const
315 bool IOWorkLoop::onThread() const
317 return (IOThreadSelf() == workThread
);
320 bool IOWorkLoop::inGate() const
322 return IORecursiveLockHaveLock(gateLock
);
325 // Internal APIs used by event sources to control the thread
326 void IOWorkLoop::signalWorkAvailable()
329 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
331 thread_wakeup_one((void *) &workToDo
);
332 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
336 void IOWorkLoop::openGate()
338 IORecursiveLockUnlock(gateLock
);
341 void IOWorkLoop::closeGate()
343 IORecursiveLockLock(gateLock
);
346 bool IOWorkLoop::tryCloseGate()
348 return IORecursiveLockTryLock(gateLock
) != 0;
351 int IOWorkLoop::sleepGate(void *event
, UInt32 interuptibleType
)
353 return IORecursiveLockSleep(gateLock
, event
, interuptibleType
);
356 void IOWorkLoop::wakeupGate(void *event
, bool oneThread
)
358 IORecursiveLockWakeup(gateLock
, event
, oneThread
);
361 IOReturn
IOWorkLoop::runAction(Action inAction
, OSObject
*target
,
362 void *arg0
, void *arg1
,
363 void *arg2
, void *arg3
)
367 // closeGate is recursive so don't worry if we already hold the lock.
369 res
= (*inAction
)(target
, arg0
, arg1
, arg2
, arg3
);
375 IOReturn
IOWorkLoop::_maintRequest(void *inC
, void *inD
, void *, void *)
377 maintCommandEnum command
= (maintCommandEnum
) (vm_address_t
) inC
;
378 IOEventSource
*inEvent
= (IOEventSource
*) inD
;
379 IOReturn res
= kIOReturnSuccess
;
384 if (!inEvent
->getWorkLoop()) {
385 SETP(&fFlags
, kLoopRestart
);
388 inEvent
->setWorkLoop(this);
392 eventChain
= inEvent
;
394 IOEventSource
*event
, *next
;
396 for (event
= eventChain
; (next
= event
->getNext()); event
= next
)
398 event
->setNext(inEvent
);
404 if (inEvent
->getWorkLoop()) {
405 if (eventChain
== inEvent
)
406 eventChain
= inEvent
->getNext();
408 IOEventSource
*event
, *next
;
411 while ((next
= event
->getNext()) && next
!= inEvent
)
415 res
= kIOReturnBadArgument
;
418 event
->setNext(inEvent
->getNext());
421 inEvent
->setWorkLoop(0);
424 SETP(&fFlags
, kLoopRestart
);
429 return kIOReturnUnsupported
;