]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOWorkLoop.cpp
1581f38ca02a226847681d898ce06cbac57de65c
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 void IOWorkLoop::launchThreadMain(void *self
)
62 register thread_t mythread
= current_thread();
64 // Make sure that this thread always has a kernel stack
65 stack_privilege(mythread
);
66 thread_set_cont_arg((int) self
);
67 threadMainContinuation();
70 bool IOWorkLoop::init()
72 // The super init and gateLock allocation MUST be done first
76 if ( !(gateLock
= IORecursiveLockAlloc()) )
79 if ( !(workToDoLock
= IOSimpleLockAlloc()) )
82 controlG
= IOCommandGate::
83 commandGate(this, (IOCommandGate::Action
) &IOWorkLoop::_maintRequest
);
87 IOSimpleLockInit(workToDoLock
);
90 // Point the controlGate at the workLoop. Usually addEventSource
91 // does this automatically. The problem is in this case addEventSource
92 // uses the control gate and it has to be bootstrapped.
93 controlG
->setWorkLoop(this);
94 if (addEventSource(controlG
) != kIOReturnSuccess
)
97 workThread
= IOCreateThread(launchThreadMain
, (void *) this);
105 IOWorkLoop::workLoop()
107 IOWorkLoop
*me
= new IOWorkLoop
;
109 if (me
&& !me
->init()) {
117 // Free is called twice:
118 // First when the atomic retainCount transitions from 1 -> 0
119 // Secondly when the work loop itself is commiting hari kari
120 // Hence the each leg of the free must be single threaded.
121 void IOWorkLoop::free()
126 // If we are here then we must be trying to shut down this work loop
127 // in this case disable all of the event source, mark the loop for
128 // as terminating and wakeup the work thread itself and return
129 // Note: we hold the gate across the entire operation mainly for the
130 // benefit of our event sources so we can disable them cleanly.
133 disableAllEventSources();
135 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
136 SETP(&fFlags
, kLoopTerminate
);
137 thread_wakeup_one((void *) &workToDo
);
138 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
142 else /* !workThread */ {
143 IOEventSource
*event
, *next
;
145 for (event
= eventChain
; event
; event
= next
) {
146 next
= event
->getNext();
147 event
->setWorkLoop(0);
153 // Either we have a partial initialisation to clean up
154 // or we the workThread itself is performing hari-kari.
155 // either way clean up all of our resources and return.
163 IOSimpleLockFree(workToDoLock
);
168 IORecursiveLockFree(gateLock
);
176 IOReturn
IOWorkLoop::addEventSource(IOEventSource
*newEvent
)
178 return controlG
->runCommand((void *) mAddEvent
, (void *) newEvent
);
181 IOReturn
IOWorkLoop::removeEventSource(IOEventSource
*toRemove
)
183 return controlG
->runCommand((void *) mRemoveEvent
, (void *) toRemove
);
186 void IOWorkLoop::enableAllEventSources() const
188 IOEventSource
*event
;
190 for (event
= eventChain
; event
; event
= event
->getNext())
194 void IOWorkLoop::disableAllEventSources() const
196 IOEventSource
*event
;
198 for (event
= eventChain
; event
; event
= event
->getNext())
199 if (event
!= controlG
) // Don't disable the control gate
203 void IOWorkLoop::enableAllInterrupts() const
205 IOEventSource
*event
;
207 for (event
= eventChain
; event
; event
= event
->getNext())
208 if (OSDynamicCast(IOInterruptEventSource
, event
))
212 void IOWorkLoop::disableAllInterrupts() const
214 IOEventSource
*event
;
216 for (event
= eventChain
; event
; event
= event
->getNext())
217 if (OSDynamicCast(IOInterruptEventSource
, event
))
222 #define IOTimeClientS() \
224 IOTimeStampStart(IODBG_WORKLOOP(IOWL_CLIENT), \
225 (unsigned int) this, (unsigned int) event); \
228 #define IOTimeClientE() \
230 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_CLIENT), \
231 (unsigned int) this, (unsigned int) event); \
234 #define IOTimeWorkS() \
236 IOTimeStampStart(IODBG_WORKLOOP(IOWL_WORK), (unsigned int) this); \
239 #define IOTimeWorkE() \
241 IOTimeStampEnd(IODBG_WORKLOOP(IOWL_WORK),(unsigned int) this); \
246 #define IOTimeClientS()
247 #define IOTimeClientE()
248 #define IOTimeWorkS()
249 #define IOTimeWorkE()
253 void IOWorkLoop::threadMainContinuation()
256 self
= (IOWorkLoop
*) thread_get_cont_arg();
261 void IOWorkLoop::threadMain()
263 CLRP(&fFlags
, kLoopRestart
);
272 if (ISSETP(&fFlags
, kLoopTerminate
))
276 workToDo
= more
= false;
277 for (IOEventSource
*event
= eventChain
; event
; event
= event
->getNext()) {
280 more
|= event
->checkForWork();
283 if (ISSETP(&fFlags
, kLoopTerminate
))
285 else if (fFlags
& kLoopRestart
) {
286 CLRP(&fFlags
, kLoopRestart
);
296 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
297 if ( !ISSETP(&fFlags
, kLoopTerminate
) && !workToDo
) {
298 assert_wait((void *) &workToDo
, false);
299 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
301 #if defined (__i386__)
305 thread_set_cont_arg((int) this);
306 thread_block(&threadMainContinuation
);
311 // At this point we either have work to do or we need
312 // to commit suicide. But no matter
313 // Clear the simple lock and retore the interrupt state
314 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
322 workThread
= 0; // Say we don't have a loop and free ourselves
327 IOThread
IOWorkLoop::getThread() const
332 bool IOWorkLoop::onThread() const
334 return (IOThreadSelf() == workThread
);
337 bool IOWorkLoop::inGate() const
339 return IORecursiveLockHaveLock(gateLock
);
342 // Internal APIs used by event sources to control the thread
343 void IOWorkLoop::signalWorkAvailable()
346 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
348 thread_wakeup_one((void *) &workToDo
);
349 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
353 void IOWorkLoop::openGate()
355 IORecursiveLockUnlock(gateLock
);
358 void IOWorkLoop::closeGate()
360 IORecursiveLockLock(gateLock
);
363 bool IOWorkLoop::tryCloseGate()
365 return IORecursiveLockTryLock(gateLock
) != 0;
368 int IOWorkLoop::sleepGate(void *event
, UInt32 interuptibleType
)
370 return IORecursiveLockSleep(gateLock
, event
, interuptibleType
);
373 void IOWorkLoop::wakeupGate(void *event
, bool oneThread
)
375 IORecursiveLockWakeup(gateLock
, event
, oneThread
);
378 IOReturn
IOWorkLoop::runAction(Action inAction
, OSObject
*target
,
379 void *arg0
= 0, void *arg1
= 0,
380 void *arg2
= 0, void *arg3
= 0)
384 // closeGate is recursive so don't worry if we already hold the lock.
386 res
= (*inAction
)(target
, arg0
, arg1
, arg2
, arg3
);
392 IOReturn
IOWorkLoop::_maintRequest(void *inC
, void *inD
, void *, void *)
394 maintCommandEnum command
= (maintCommandEnum
) (vm_address_t
) inC
;
395 IOEventSource
*inEvent
= (IOEventSource
*) inD
;
396 IOReturn res
= kIOReturnSuccess
;
401 SETP(&fFlags
, kLoopRestart
);
403 inEvent
->setWorkLoop(this);
407 eventChain
= inEvent
;
409 IOEventSource
*event
, *next
;
411 for (event
= eventChain
; (next
= event
->getNext()); event
= next
)
413 event
->setNext(inEvent
);
418 if (eventChain
== inEvent
)
419 eventChain
= inEvent
->getNext();
421 IOEventSource
*event
, *next
;
424 while ((next
= event
->getNext()) && next
!= inEvent
)
428 res
= kIOReturnBadArgument
;
431 event
->setNext(inEvent
->getNext());
434 inEvent
->setWorkLoop(0);
437 SETP(&fFlags
, kLoopRestart
);
441 return kIOReturnUnsupported
;