]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOWorkLoop.cpp
f2c11d9486da297fb715d8dddab212b631a771ef
2 * Copyright (c) 1998-2000 Apple Computer, 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>
41 #define super OSObject
43 OSDefineMetaClassAndStructors(IOWorkLoop
, OSObject
);
45 // Block of unused functions intended for future use
46 OSMetaClassDefineReservedUsed(IOWorkLoop
, 0);
48 OSMetaClassDefineReservedUnused(IOWorkLoop
, 1);
49 OSMetaClassDefineReservedUnused(IOWorkLoop
, 2);
50 OSMetaClassDefineReservedUnused(IOWorkLoop
, 3);
51 OSMetaClassDefineReservedUnused(IOWorkLoop
, 4);
52 OSMetaClassDefineReservedUnused(IOWorkLoop
, 5);
53 OSMetaClassDefineReservedUnused(IOWorkLoop
, 6);
54 OSMetaClassDefineReservedUnused(IOWorkLoop
, 7);
56 enum IOWorkLoopState
{ kLoopRestart
= 0x1, kLoopTerminate
= 0x2 };
57 static inline void SETP(void *addr
, unsigned int flag
)
58 { unsigned int *num
= (unsigned int *) addr
; *num
|= flag
; }
59 static inline void CLRP(void *addr
, unsigned int flag
)
60 { unsigned int *num
= (unsigned int *) addr
; *num
&= ~flag
; }
61 static inline bool ISSETP(void *addr
, unsigned int flag
)
62 { unsigned int *num
= (unsigned int *) addr
; return (*num
& flag
) != 0; }
64 #define fFlags loopRestart
66 bool IOWorkLoop::init()
68 // The super init and gateLock allocation MUST be done first
72 if ( !(gateLock
= IORecursiveLockAlloc()) )
75 if ( !(workToDoLock
= IOSimpleLockAlloc()) )
78 controlG
= IOCommandGate::
79 commandGate(this, (IOCommandGate::Action
) &IOWorkLoop::_maintRequest
);
83 IOSimpleLockInit(workToDoLock
);
86 // Point the controlGate at the workLoop. Usually addEventSource
87 // does this automatically. The problem is in this case addEventSource
88 // uses the control gate and it has to be bootstrapped.
89 controlG
->setWorkLoop(this);
90 if (addEventSource(controlG
) != kIOReturnSuccess
)
93 workThread
= IOCreateThread((thread_continue_t
)threadMainContinuation
, this);
101 IOWorkLoop::workLoop()
103 IOWorkLoop
*me
= new IOWorkLoop
;
105 if (me
&& !me
->init()) {
113 // Free is called twice:
114 // First when the atomic retainCount transitions from 1 -> 0
115 // Secondly when the work loop itself is commiting hari kari
116 // Hence the each leg of the free must be single threaded.
117 void IOWorkLoop::free()
122 // If we are here then we must be trying to shut down this work loop
123 // in this case disable all of the event source, mark the loop for
124 // as terminating and wakeup the work thread itself and return
125 // Note: we hold the gate across the entire operation mainly for the
126 // benefit of our event sources so we can disable them cleanly.
129 disableAllEventSources();
131 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
132 SETP(&fFlags
, kLoopTerminate
);
133 thread_wakeup_one((void *) &workToDo
);
134 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
138 else /* !workThread */ {
139 IOEventSource
*event
, *next
;
141 for (event
= eventChain
; event
; event
= next
) {
142 next
= event
->getNext();
143 event
->setWorkLoop(0);
149 // Either we have a partial initialisation to clean up
150 // or we the workThread itself is performing hari-kari.
151 // either way clean up all of our resources and return.
159 IOSimpleLockFree(workToDoLock
);
164 IORecursiveLockFree(gateLock
);
172 IOReturn
IOWorkLoop::addEventSource(IOEventSource
*newEvent
)
174 return controlG
->runCommand((void *) mAddEvent
, (void *) newEvent
);
177 IOReturn
IOWorkLoop::removeEventSource(IOEventSource
*toRemove
)
179 return controlG
->runCommand((void *) mRemoveEvent
, (void *) toRemove
);
182 void IOWorkLoop::enableAllEventSources() const
184 IOEventSource
*event
;
186 for (event
= eventChain
; event
; event
= event
->getNext())
190 void IOWorkLoop::disableAllEventSources() const
192 IOEventSource
*event
;
194 for (event
= eventChain
; event
; event
= event
->getNext())
195 if (event
!= controlG
) // Don't disable the control gate
199 void IOWorkLoop::enableAllInterrupts() const
201 IOEventSource
*event
;
203 for (event
= eventChain
; event
; event
= event
->getNext())
204 if (OSDynamicCast(IOInterruptEventSource
, event
))
208 void IOWorkLoop::disableAllInterrupts() const
210 IOEventSource
*event
;
212 for (event
= eventChain
; event
; event
= event
->getNext())
213 if (OSDynamicCast(IOInterruptEventSource
, event
))
218 #define IOTimeClientS() \
220 IOTimeStampStart(IODBG_WORKLOOP(IOWL_CLIENT), \
221 (unsigned int) this, (unsigned int) event); \
224 #define IOTimeClientE() \
226 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_CLIENT), \
227 (unsigned int) this, (unsigned int) event); \
230 #define IOTimeWorkS() \
232 IOTimeStampStart(IODBG_WORKLOOP(IOWL_WORK), (unsigned int) this); \
235 #define IOTimeWorkE() \
237 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_WORK),(unsigned int) this); \
242 #define IOTimeClientS()
243 #define IOTimeClientE()
244 #define IOTimeWorkS()
245 #define IOTimeWorkE()
249 void IOWorkLoop::threadMainContinuation(IOWorkLoop
*self
)
254 void IOWorkLoop::threadMain()
256 CLRP(&fFlags
, kLoopRestart
);
265 if (ISSETP(&fFlags
, kLoopTerminate
))
269 workToDo
= more
= false;
270 for (IOEventSource
*event
= eventChain
; event
; event
= event
->getNext()) {
273 more
|= event
->checkForWork();
276 if (ISSETP(&fFlags
, kLoopTerminate
))
278 else if (fFlags
& kLoopRestart
) {
279 CLRP(&fFlags
, kLoopRestart
);
289 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
290 if ( !ISSETP(&fFlags
, kLoopTerminate
) && !workToDo
) {
291 assert_wait((void *) &workToDo
, false);
292 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
294 thread_block_parameter((thread_continue_t
)threadMainContinuation
, 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
);
309 workThread
= 0; // Say we don't have a loop and free ourselves
314 IOThread
IOWorkLoop::getThread() const
319 bool IOWorkLoop::onThread() const
321 return (IOThreadSelf() == workThread
);
324 bool IOWorkLoop::inGate() const
326 return IORecursiveLockHaveLock(gateLock
);
329 // Internal APIs used by event sources to control the thread
330 void IOWorkLoop::signalWorkAvailable()
333 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
335 thread_wakeup_one((void *) &workToDo
);
336 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
340 void IOWorkLoop::openGate()
342 IORecursiveLockUnlock(gateLock
);
345 void IOWorkLoop::closeGate()
347 IORecursiveLockLock(gateLock
);
350 bool IOWorkLoop::tryCloseGate()
352 return IORecursiveLockTryLock(gateLock
) != 0;
355 int IOWorkLoop::sleepGate(void *event
, UInt32 interuptibleType
)
357 return IORecursiveLockSleep(gateLock
, event
, interuptibleType
);
360 void IOWorkLoop::wakeupGate(void *event
, bool oneThread
)
362 IORecursiveLockWakeup(gateLock
, event
, oneThread
);
365 IOReturn
IOWorkLoop::runAction(Action inAction
, OSObject
*target
,
366 void *arg0
, void *arg1
,
367 void *arg2
, void *arg3
)
371 // closeGate is recursive so don't worry if we already hold the lock.
373 res
= (*inAction
)(target
, arg0
, arg1
, arg2
, arg3
);
379 IOReturn
IOWorkLoop::_maintRequest(void *inC
, void *inD
, void *, void *)
381 maintCommandEnum command
= (maintCommandEnum
) (vm_address_t
) inC
;
382 IOEventSource
*inEvent
= (IOEventSource
*) inD
;
383 IOReturn res
= kIOReturnSuccess
;
388 if (!inEvent
->getWorkLoop()) {
389 SETP(&fFlags
, kLoopRestart
);
392 inEvent
->setWorkLoop(this);
396 eventChain
= inEvent
;
398 IOEventSource
*event
, *next
;
400 for (event
= eventChain
; (next
= event
->getNext()); event
= next
)
402 event
->setNext(inEvent
);
408 if (inEvent
->getWorkLoop()) {
409 if (eventChain
== inEvent
)
410 eventChain
= inEvent
->getNext();
412 IOEventSource
*event
, *next
;
415 while ((next
= event
->getNext()) && next
!= inEvent
)
419 res
= kIOReturnBadArgument
;
422 event
->setNext(inEvent
->getNext());
425 inEvent
->setWorkLoop(0);
428 SETP(&fFlags
, kLoopRestart
);
433 return kIOReturnUnsupported
;