]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOWorkLoop.cpp
c5f757160e3d1bdc7c8981807f7e3151ad2bdcc0
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);
47 OSMetaClassDefineReservedUsed(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, OSMemberFunctionCast(IOCommandGate::Action
,
80 this, &IOWorkLoop::_maintRequest
));
84 IOSimpleLockInit(workToDoLock
);
87 // Point the controlGate at the workLoop. Usually addEventSource
88 // does this automatically. The problem is in this case addEventSource
89 // uses the control gate and it has to be bootstrapped.
90 controlG
->setWorkLoop(this);
91 if (addEventSource(controlG
) != kIOReturnSuccess
)
95 OSMemberFunctionCast(IOThreadFunc
, this, &IOWorkLoop::threadMain
);
96 workThread
= IOCreateThread(cptr
, this);
104 IOWorkLoop::workLoop()
106 IOWorkLoop
*me
= new IOWorkLoop
;
108 if (me
&& !me
->init()) {
116 // Free is called twice:
117 // First when the atomic retainCount transitions from 1 -> 0
118 // Secondly when the work loop itself is commiting hari kari
119 // Hence the each leg of the free must be single threaded.
120 void IOWorkLoop::free()
125 // If we are here then we must be trying to shut down this work loop
126 // in this case disable all of the event source, mark the loop for
127 // as terminating and wakeup the work thread itself and return
128 // Note: we hold the gate across the entire operation mainly for the
129 // benefit of our event sources so we can disable them cleanly.
132 disableAllEventSources();
134 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
135 SETP(&fFlags
, kLoopTerminate
);
136 thread_wakeup_one((void *) &workToDo
);
137 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
141 else /* !workThread */ {
142 IOEventSource
*event
, *next
;
144 for (event
= eventChain
; event
; event
= next
) {
145 next
= event
->getNext();
146 event
->setWorkLoop(0);
152 // Either we have a partial initialisation to clean up
153 // or we the workThread itself is performing hari-kari.
154 // either way clean up all of our resources and return.
162 IOSimpleLockFree(workToDoLock
);
167 IORecursiveLockFree(gateLock
);
175 IOReturn
IOWorkLoop::addEventSource(IOEventSource
*newEvent
)
177 return controlG
->runCommand((void *) mAddEvent
, (void *) newEvent
);
180 IOReturn
IOWorkLoop::removeEventSource(IOEventSource
*toRemove
)
182 return controlG
->runCommand((void *) mRemoveEvent
, (void *) toRemove
);
185 void IOWorkLoop::enableAllEventSources() const
187 IOEventSource
*event
;
189 for (event
= eventChain
; event
; event
= event
->getNext())
193 void IOWorkLoop::disableAllEventSources() const
195 IOEventSource
*event
;
197 for (event
= eventChain
; event
; event
= event
->getNext())
198 if (event
!= controlG
) // Don't disable the control gate
202 void IOWorkLoop::enableAllInterrupts() const
204 IOEventSource
*event
;
206 for (event
= eventChain
; event
; event
= event
->getNext())
207 if (OSDynamicCast(IOInterruptEventSource
, event
))
211 void IOWorkLoop::disableAllInterrupts() const
213 IOEventSource
*event
;
215 for (event
= eventChain
; event
; event
= event
->getNext())
216 if (OSDynamicCast(IOInterruptEventSource
, event
))
221 #define IOTimeClientS() \
223 IOTimeStampStart(IODBG_WORKLOOP(IOWL_CLIENT), \
224 (unsigned int) this, (unsigned int) event); \
227 #define IOTimeClientE() \
229 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_CLIENT), \
230 (unsigned int) this, (unsigned int) event); \
233 #define IOTimeWorkS() \
235 IOTimeStampStart(IODBG_WORKLOOP(IOWL_WORK), (unsigned int) this); \
238 #define IOTimeWorkE() \
240 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_WORK),(unsigned int) this); \
245 #define IOTimeClientS()
246 #define IOTimeClientE()
247 #define IOTimeWorkS()
248 #define IOTimeWorkE()
252 /* virtual */ bool IOWorkLoop::runEventSources()
256 if (ISSETP(&fFlags
, kLoopTerminate
))
262 CLRP(&fFlags
, kLoopRestart
);
263 workToDo
= more
= false;
264 for (IOEventSource
*evnt
= eventChain
; evnt
; evnt
= evnt
->getNext()) {
267 more
|= evnt
->checkForWork();
270 if (ISSETP(&fFlags
, kLoopTerminate
))
272 else if (fFlags
& kLoopRestart
) {
287 /* virtual */ void IOWorkLoop::threadMain()
290 if ( !runEventSources() )
293 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
294 if ( !ISSETP(&fFlags
, kLoopTerminate
) && !workToDo
) {
295 assert_wait((void *) &workToDo
, false);
296 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
298 thread_continue_t cptr
= OSMemberFunctionCast(
299 thread_continue_t
, this, &IOWorkLoop::threadMain
);
300 thread_block_parameter(cptr
, this);
304 // At this point we either have work to do or we need
305 // to commit suicide. But no matter
306 // Clear the simple lock and retore the interrupt state
307 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
;