]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOWorkLoop.cpp
80d7e32d78a5fa4343424c58d7873f3f941fa5f4
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
34 1998-7-13 Godfrey van der Linden(gvdl)
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>
43 #define super OSObject
45 OSDefineMetaClassAndStructors(IOWorkLoop
, OSObject
);
47 // Block of unused functions intended for future use
48 OSMetaClassDefineReservedUsed(IOWorkLoop
, 0);
50 OSMetaClassDefineReservedUnused(IOWorkLoop
, 1);
51 OSMetaClassDefineReservedUnused(IOWorkLoop
, 2);
52 OSMetaClassDefineReservedUnused(IOWorkLoop
, 3);
53 OSMetaClassDefineReservedUnused(IOWorkLoop
, 4);
54 OSMetaClassDefineReservedUnused(IOWorkLoop
, 5);
55 OSMetaClassDefineReservedUnused(IOWorkLoop
, 6);
56 OSMetaClassDefineReservedUnused(IOWorkLoop
, 7);
58 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 #define fFlags loopRestart
68 bool IOWorkLoop::init()
70 // The super init and gateLock allocation MUST be done first
74 if ( !(gateLock
= IORecursiveLockAlloc()) )
77 if ( !(workToDoLock
= IOSimpleLockAlloc()) )
80 controlG
= IOCommandGate::
81 commandGate(this, (IOCommandGate::Action
) &IOWorkLoop::_maintRequest
);
85 IOSimpleLockInit(workToDoLock
);
88 // Point the controlGate at the workLoop. Usually addEventSource
89 // does this automatically. The problem is in this case addEventSource
90 // uses the control gate and it has to be bootstrapped.
91 controlG
->setWorkLoop(this);
92 if (addEventSource(controlG
) != kIOReturnSuccess
)
95 workThread
= IOCreateThread((thread_continue_t
)threadMainContinuation
, this);
103 IOWorkLoop::workLoop()
105 IOWorkLoop
*me
= new IOWorkLoop
;
107 if (me
&& !me
->init()) {
115 // Free is called twice:
116 // First when the atomic retainCount transitions from 1 -> 0
117 // Secondly when the work loop itself is commiting hari kari
118 // Hence the each leg of the free must be single threaded.
119 void IOWorkLoop::free()
124 // If we are here then we must be trying to shut down this work loop
125 // in this case disable all of the event source, mark the loop for
126 // as terminating and wakeup the work thread itself and return
127 // Note: we hold the gate across the entire operation mainly for the
128 // benefit of our event sources so we can disable them cleanly.
131 disableAllEventSources();
133 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
134 SETP(&fFlags
, kLoopTerminate
);
135 thread_wakeup_one((void *) &workToDo
);
136 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
140 else /* !workThread */ {
141 IOEventSource
*event
, *next
;
143 for (event
= eventChain
; event
; event
= next
) {
144 next
= event
->getNext();
145 event
->setWorkLoop(0);
151 // Either we have a partial initialisation to clean up
152 // or we the workThread itself is performing hari-kari.
153 // either way clean up all of our resources and return.
161 IOSimpleLockFree(workToDoLock
);
166 IORecursiveLockFree(gateLock
);
174 IOReturn
IOWorkLoop::addEventSource(IOEventSource
*newEvent
)
176 return controlG
->runCommand((void *) mAddEvent
, (void *) newEvent
);
179 IOReturn
IOWorkLoop::removeEventSource(IOEventSource
*toRemove
)
181 return controlG
->runCommand((void *) mRemoveEvent
, (void *) toRemove
);
184 void IOWorkLoop::enableAllEventSources() const
186 IOEventSource
*event
;
188 for (event
= eventChain
; event
; event
= event
->getNext())
192 void IOWorkLoop::disableAllEventSources() const
194 IOEventSource
*event
;
196 for (event
= eventChain
; event
; event
= event
->getNext())
197 if (event
!= controlG
) // Don't disable the control gate
201 void IOWorkLoop::enableAllInterrupts() const
203 IOEventSource
*event
;
205 for (event
= eventChain
; event
; event
= event
->getNext())
206 if (OSDynamicCast(IOInterruptEventSource
, event
))
210 void IOWorkLoop::disableAllInterrupts() const
212 IOEventSource
*event
;
214 for (event
= eventChain
; event
; event
= event
->getNext())
215 if (OSDynamicCast(IOInterruptEventSource
, event
))
220 #define IOTimeClientS() \
222 IOTimeStampStart(IODBG_WORKLOOP(IOWL_CLIENT), \
223 (unsigned int) this, (unsigned int) event); \
226 #define IOTimeClientE() \
228 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_CLIENT), \
229 (unsigned int) this, (unsigned int) event); \
232 #define IOTimeWorkS() \
234 IOTimeStampStart(IODBG_WORKLOOP(IOWL_WORK), (unsigned int) this); \
237 #define IOTimeWorkE() \
239 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_WORK),(unsigned int) this); \
244 #define IOTimeClientS()
245 #define IOTimeClientE()
246 #define IOTimeWorkS()
247 #define IOTimeWorkE()
251 void IOWorkLoop::threadMainContinuation(IOWorkLoop
*self
)
256 void IOWorkLoop::threadMain()
258 CLRP(&fFlags
, kLoopRestart
);
267 if (ISSETP(&fFlags
, kLoopTerminate
))
271 workToDo
= more
= false;
272 for (IOEventSource
*event
= eventChain
; event
; event
= event
->getNext()) {
275 more
|= event
->checkForWork();
278 if (ISSETP(&fFlags
, kLoopTerminate
))
280 else if (fFlags
& kLoopRestart
) {
281 CLRP(&fFlags
, kLoopRestart
);
291 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
292 if ( !ISSETP(&fFlags
, kLoopTerminate
) && !workToDo
) {
293 assert_wait((void *) &workToDo
, false);
294 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
296 thread_block_parameter((thread_continue_t
)threadMainContinuation
, this);
300 // At this point we either have work to do or we need
301 // to commit suicide. But no matter
302 // Clear the simple lock and retore the interrupt state
303 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
311 workThread
= 0; // Say we don't have a loop and free ourselves
316 IOThread
IOWorkLoop::getThread() const
321 bool IOWorkLoop::onThread() const
323 return (IOThreadSelf() == workThread
);
326 bool IOWorkLoop::inGate() const
328 return IORecursiveLockHaveLock(gateLock
);
331 // Internal APIs used by event sources to control the thread
332 void IOWorkLoop::signalWorkAvailable()
335 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
337 thread_wakeup_one((void *) &workToDo
);
338 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
342 void IOWorkLoop::openGate()
344 IORecursiveLockUnlock(gateLock
);
347 void IOWorkLoop::closeGate()
349 IORecursiveLockLock(gateLock
);
352 bool IOWorkLoop::tryCloseGate()
354 return IORecursiveLockTryLock(gateLock
) != 0;
357 int IOWorkLoop::sleepGate(void *event
, UInt32 interuptibleType
)
359 return IORecursiveLockSleep(gateLock
, event
, interuptibleType
);
362 void IOWorkLoop::wakeupGate(void *event
, bool oneThread
)
364 IORecursiveLockWakeup(gateLock
, event
, oneThread
);
367 IOReturn
IOWorkLoop::runAction(Action inAction
, OSObject
*target
,
368 void *arg0
, void *arg1
,
369 void *arg2
, void *arg3
)
373 // closeGate is recursive so don't worry if we already hold the lock.
375 res
= (*inAction
)(target
, arg0
, arg1
, arg2
, arg3
);
381 IOReturn
IOWorkLoop::_maintRequest(void *inC
, void *inD
, void *, void *)
383 maintCommandEnum command
= (maintCommandEnum
) (vm_address_t
) inC
;
384 IOEventSource
*inEvent
= (IOEventSource
*) inD
;
385 IOReturn res
= kIOReturnSuccess
;
390 if (!inEvent
->getWorkLoop()) {
391 SETP(&fFlags
, kLoopRestart
);
394 inEvent
->setWorkLoop(this);
398 eventChain
= inEvent
;
400 IOEventSource
*event
, *next
;
402 for (event
= eventChain
; (next
= event
->getNext()); event
= next
)
404 event
->setNext(inEvent
);
410 if (inEvent
->getWorkLoop()) {
411 if (eventChain
== inEvent
)
412 eventChain
= inEvent
->getNext();
414 IOEventSource
*event
, *next
;
417 while ((next
= event
->getNext()) && next
!= inEvent
)
421 res
= kIOReturnBadArgument
;
424 event
->setNext(inEvent
->getNext());
427 inEvent
->setWorkLoop(0);
430 SETP(&fFlags
, kLoopRestart
);
435 return kIOReturnUnsupported
;