]>
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 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, (IOCommandGate::Action
) &IOWorkLoop::_maintRequest
);
77 IOSimpleLockInit(workToDoLock
);
80 // Point the controlGate at the workLoop. Usually addEventSource
81 // does this automatically. The problem is in this case addEventSource
82 // uses the control gate and it has to be bootstrapped.
83 controlG
->setWorkLoop(this);
84 if (addEventSource(controlG
) != kIOReturnSuccess
)
87 workThread
= IOCreateThread((thread_continue_t
)threadMainContinuation
, this);
95 IOWorkLoop::workLoop()
97 IOWorkLoop
*me
= new IOWorkLoop
;
99 if (me
&& !me
->init()) {
107 // Free is called twice:
108 // First when the atomic retainCount transitions from 1 -> 0
109 // Secondly when the work loop itself is commiting hari kari
110 // Hence the each leg of the free must be single threaded.
111 void IOWorkLoop::free()
116 // If we are here then we must be trying to shut down this work loop
117 // in this case disable all of the event source, mark the loop for
118 // as terminating and wakeup the work thread itself and return
119 // Note: we hold the gate across the entire operation mainly for the
120 // benefit of our event sources so we can disable them cleanly.
123 disableAllEventSources();
125 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
126 SETP(&fFlags
, kLoopTerminate
);
127 thread_wakeup_one((void *) &workToDo
);
128 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
132 else /* !workThread */ {
133 IOEventSource
*event
, *next
;
135 for (event
= eventChain
; event
; event
= next
) {
136 next
= event
->getNext();
137 event
->setWorkLoop(0);
143 // Either we have a partial initialisation to clean up
144 // or we the workThread itself is performing hari-kari.
145 // either way clean up all of our resources and return.
153 IOSimpleLockFree(workToDoLock
);
158 IORecursiveLockFree(gateLock
);
166 IOReturn
IOWorkLoop::addEventSource(IOEventSource
*newEvent
)
168 return controlG
->runCommand((void *) mAddEvent
, (void *) newEvent
);
171 IOReturn
IOWorkLoop::removeEventSource(IOEventSource
*toRemove
)
173 return controlG
->runCommand((void *) mRemoveEvent
, (void *) toRemove
);
176 void IOWorkLoop::enableAllEventSources() const
178 IOEventSource
*event
;
180 for (event
= eventChain
; event
; event
= event
->getNext())
184 void IOWorkLoop::disableAllEventSources() const
186 IOEventSource
*event
;
188 for (event
= eventChain
; event
; event
= event
->getNext())
189 if (event
!= controlG
) // Don't disable the control gate
193 void IOWorkLoop::enableAllInterrupts() const
195 IOEventSource
*event
;
197 for (event
= eventChain
; event
; event
= event
->getNext())
198 if (OSDynamicCast(IOInterruptEventSource
, event
))
202 void IOWorkLoop::disableAllInterrupts() const
204 IOEventSource
*event
;
206 for (event
= eventChain
; event
; event
= event
->getNext())
207 if (OSDynamicCast(IOInterruptEventSource
, event
))
212 #define IOTimeClientS() \
214 IOTimeStampStart(IODBG_WORKLOOP(IOWL_CLIENT), \
215 (unsigned int) this, (unsigned int) event); \
218 #define IOTimeClientE() \
220 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_CLIENT), \
221 (unsigned int) this, (unsigned int) event); \
224 #define IOTimeWorkS() \
226 IOTimeStampStart(IODBG_WORKLOOP(IOWL_WORK), (unsigned int) this); \
229 #define IOTimeWorkE() \
231 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_WORK),(unsigned int) this); \
236 #define IOTimeClientS()
237 #define IOTimeClientE()
238 #define IOTimeWorkS()
239 #define IOTimeWorkE()
243 void IOWorkLoop::threadMainContinuation(IOWorkLoop
*self
)
248 void IOWorkLoop::threadMain()
250 CLRP(&fFlags
, kLoopRestart
);
259 if (ISSETP(&fFlags
, kLoopTerminate
))
263 workToDo
= more
= false;
264 for (IOEventSource
*event
= eventChain
; event
; event
= event
->getNext()) {
267 more
|= event
->checkForWork();
270 if (ISSETP(&fFlags
, kLoopTerminate
))
272 else if (fFlags
& kLoopRestart
) {
273 CLRP(&fFlags
, kLoopRestart
);
283 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
284 if ( !ISSETP(&fFlags
, kLoopTerminate
) && !workToDo
) {
285 assert_wait((void *) &workToDo
, false);
286 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
288 thread_block_parameter((thread_continue_t
)threadMainContinuation
, this);
292 // At this point we either have work to do or we need
293 // to commit suicide. But no matter
294 // Clear the simple lock and retore the interrupt state
295 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
303 workThread
= 0; // Say we don't have a loop and free ourselves
308 IOThread
IOWorkLoop::getThread() const
313 bool IOWorkLoop::onThread() const
315 return (IOThreadSelf() == workThread
);
318 bool IOWorkLoop::inGate() const
320 return IORecursiveLockHaveLock(gateLock
);
323 // Internal APIs used by event sources to control the thread
324 void IOWorkLoop::signalWorkAvailable()
327 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
329 thread_wakeup_one((void *) &workToDo
);
330 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
334 void IOWorkLoop::openGate()
336 IORecursiveLockUnlock(gateLock
);
339 void IOWorkLoop::closeGate()
341 IORecursiveLockLock(gateLock
);
344 bool IOWorkLoop::tryCloseGate()
346 return IORecursiveLockTryLock(gateLock
) != 0;
349 int IOWorkLoop::sleepGate(void *event
, UInt32 interuptibleType
)
351 return IORecursiveLockSleep(gateLock
, event
, interuptibleType
);
354 void IOWorkLoop::wakeupGate(void *event
, bool oneThread
)
356 IORecursiveLockWakeup(gateLock
, event
, oneThread
);
359 IOReturn
IOWorkLoop::runAction(Action inAction
, OSObject
*target
,
360 void *arg0
, void *arg1
,
361 void *arg2
, void *arg3
)
365 // closeGate is recursive so don't worry if we already hold the lock.
367 res
= (*inAction
)(target
, arg0
, arg1
, arg2
, arg3
);
373 IOReturn
IOWorkLoop::_maintRequest(void *inC
, void *inD
, void *, void *)
375 maintCommandEnum command
= (maintCommandEnum
) (vm_address_t
) inC
;
376 IOEventSource
*inEvent
= (IOEventSource
*) inD
;
377 IOReturn res
= kIOReturnSuccess
;
382 if (!inEvent
->getWorkLoop()) {
383 SETP(&fFlags
, kLoopRestart
);
386 inEvent
->setWorkLoop(this);
390 eventChain
= inEvent
;
392 IOEventSource
*event
, *next
;
394 for (event
= eventChain
; (next
= event
->getNext()); event
= next
)
396 event
->setNext(inEvent
);
402 if (inEvent
->getWorkLoop()) {
403 if (eventChain
== inEvent
)
404 eventChain
= inEvent
->getNext();
406 IOEventSource
*event
, *next
;
409 while ((next
= event
->getNext()) && next
!= inEvent
)
413 res
= kIOReturnBadArgument
;
416 event
->setNext(inEvent
->getNext());
419 inEvent
->setWorkLoop(0);
422 SETP(&fFlags
, kLoopRestart
);
427 return kIOReturnUnsupported
;