]>
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 OSMetaClassDefineReservedUnused(IOWorkLoop
, 0);
41 OSMetaClassDefineReservedUnused(IOWorkLoop
, 1);
42 OSMetaClassDefineReservedUnused(IOWorkLoop
, 2);
43 OSMetaClassDefineReservedUnused(IOWorkLoop
, 3);
44 OSMetaClassDefineReservedUnused(IOWorkLoop
, 4);
45 OSMetaClassDefineReservedUnused(IOWorkLoop
, 5);
46 OSMetaClassDefineReservedUnused(IOWorkLoop
, 6);
47 OSMetaClassDefineReservedUnused(IOWorkLoop
, 7);
49 enum IOWorkLoopState
{ kLoopRestart
= 0x1, kLoopTerminate
= 0x2 };
50 static inline void SETP(void *addr
, unsigned int flag
)
51 { unsigned int *num
= (unsigned int *) addr
; *num
|= flag
; }
52 static inline void CLRP(void *addr
, unsigned int flag
)
53 { unsigned int *num
= (unsigned int *) addr
; *num
&= ~flag
; }
54 static inline bool ISSETP(void *addr
, unsigned int flag
)
55 { unsigned int *num
= (unsigned int *) addr
; return (*num
& flag
) != 0; }
57 #define fFlags loopRestart
59 void IOWorkLoop::launchThreadMain(void *self
)
61 register thread_t mythread
= current_thread();
63 // Make sure that this thread always has a kernel stack
64 stack_privilege(mythread
);
65 thread_set_cont_arg((int) self
);
66 threadMainContinuation();
69 bool IOWorkLoop::init()
71 // The super init and gateLock allocation MUST be done first
75 if ( !(gateLock
= IORecursiveLockAlloc()) )
78 if ( !(workToDoLock
= IOSimpleLockAlloc()) )
81 controlG
= IOCommandGate::
82 commandGate(this, (IOCommandGate::Action
) &IOWorkLoop::_maintRequest
);
86 IOSimpleLockInit(workToDoLock
);
89 // Point the controlGate at the workLoop. Usually addEventSource
90 // does this automatically. The problem is in this case addEventSource
91 // uses the control gate and it has to be bootstrapped.
92 controlG
->setWorkLoop(this);
93 if (addEventSource(controlG
) != kIOReturnSuccess
)
96 workThread
= IOCreateThread(launchThreadMain
, (void *) 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 void IOWorkLoop::threadMainContinuation()
255 self
= (IOWorkLoop
*) thread_get_cont_arg();
260 void IOWorkLoop::threadMain()
262 CLRP(&fFlags
, kLoopRestart
);
271 if (ISSETP(&fFlags
, kLoopTerminate
))
275 workToDo
= more
= false;
276 for (IOEventSource
*event
= eventChain
; event
; event
= event
->getNext()) {
279 more
|= event
->checkForWork();
282 if (ISSETP(&fFlags
, kLoopTerminate
))
284 else if (fFlags
& kLoopRestart
) {
285 CLRP(&fFlags
, kLoopRestart
);
295 is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
296 if ( !ISSETP(&fFlags
, kLoopTerminate
) && !workToDo
) {
297 assert_wait((void *) &workToDo
, false);
298 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
300 #if defined (__i386__)
304 thread_set_cont_arg((int) this);
305 thread_block(&threadMainContinuation
);
310 // At this point we either have work to do or we need
311 // to commit suicide. But no matter
312 // Clear the simple lock and retore the interrupt state
313 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
321 workThread
= 0; // Say we don't have a loop and free ourselves
326 IOThread
IOWorkLoop::getThread() const
331 bool IOWorkLoop::onThread() const
333 return (IOThreadSelf() == workThread
);
336 bool IOWorkLoop::inGate() const
338 return IORecursiveLockHaveLock(gateLock
);
341 // Internal APIs used by event sources to control the thread
342 void IOWorkLoop::signalWorkAvailable()
345 IOInterruptState is
= IOSimpleLockLockDisableInterrupt(workToDoLock
);
347 thread_wakeup_one((void *) &workToDo
);
348 IOSimpleLockUnlockEnableInterrupt(workToDoLock
, is
);
352 void IOWorkLoop::openGate()
354 IORecursiveLockUnlock(gateLock
);
357 void IOWorkLoop::closeGate()
359 IORecursiveLockLock(gateLock
);
362 bool IOWorkLoop::tryCloseGate()
364 return IORecursiveLockTryLock(gateLock
) != 0;
367 int IOWorkLoop::sleepGate(void *event
, UInt32 interuptibleType
)
369 return IORecursiveLockSleep(gateLock
, event
, interuptibleType
);
372 void IOWorkLoop::wakeupGate(void *event
, bool oneThread
)
374 IORecursiveLockWakeup(gateLock
, event
, oneThread
);
377 IOReturn
IOWorkLoop::_maintRequest(void *inC
, void *inD
, void *, void *)
379 maintCommandEnum command
= (maintCommandEnum
) (vm_address_t
) inC
;
380 IOEventSource
*inEvent
= (IOEventSource
*) inD
;
381 IOReturn res
= kIOReturnSuccess
;
386 SETP(&fFlags
, kLoopRestart
);
388 inEvent
->setWorkLoop(this);
392 eventChain
= inEvent
;
394 IOEventSource
*event
, *next
;
396 for (event
= eventChain
; (next
= event
->getNext()); event
= next
)
398 event
->setNext(inEvent
);
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
);
426 return kIOReturnUnsupported
;