/*
- * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 1998-2009 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
-/*
-Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
-HISTORY
- 1998-7-13 Godfrey van der Linden(gvdl)
- Created.
- 1998-10-30 Godfrey van der Linden(gvdl)
- Converted to C++
-*/
#ifndef __IOKIT_IOWORKLOOP_H
#define __IOKIT_IOWORKLOOP_H
#include <IOKit/system.h>
+#if IOKITSTATS
+#include <IOKit/IOStatisticsPrivate.h>
+#endif
+
class IOEventSource;
class IOTimerEventSource;
class IOCommandGate;
void *arg0, void *arg1,
void *arg2, void *arg3);
+#ifdef __BLOCKS__
+ typedef IOReturn (^ActionBlock)();
+#endif /* __BLOCKS__ */
+
+ enum {
+ kPreciousStack = 0x00000001,
+ kTimeLockPanics = 0x00000002,
+ };
+
private:
/*! @function threadMainContinuation
@abstract Static function that calls the threadMain function.
*/
static void threadMainContinuation(IOWorkLoop *self);
-
+
+/*! @function eventSourcePerformsWork
+ @abstract Checks if the event source passed in overrides checkForWork() to perform any work.
+IOWorkLoop uses this to determine if the event source should be polled in runEventSources() or not.
+ @param inEventSource The event source to check.
+*/
+ bool eventSourcePerformsWork(IOEventSource *inEventSource);
+
protected:
/*! @typedef maintCommandEnum
/*! @var gateLock
Mutual exclusion lock that is used by close and open Gate functions.
+ This is a recursive lock, which allows multiple layers of code to share a single IOWorkLoop without deadlock. This is common in IOKit since threads of execution tend to follow the service plane in the IORegistry, and multiple objects along the call path may acquire the gate for the same (shared) workloop.
*/
IORecursiveLock *gateLock;
*/
IOCommandGate *controlG;
-/*! @var workSpinLock
+/*! @var workToDoLock
The spin lock that is used to guard the 'workToDo' variable.
*/
IOSimpleLock *workToDoLock;
/*! @struct ExpansionData
@discussion This structure will be used to expand the capablilties of the IOWorkLoop in the future.
*/
- struct ExpansionData { };
+ struct ExpansionData {
+ IOOptionBits options;
+ IOEventSource *passiveEventChain;
+#if IOKITSTATS
+ struct IOWorkLoopCounter *counter;
+#else
+ void *iokitstatsReserved;
+#endif
+ uint64_t lockInterval;
+ uint64_t lockTime;
+ };
/*! @var reserved
Reserved for future use. (Internal use only)
<br><br>
If the client has some outstanding requests on an event they will never be informed of completion. If an external thread is blocked on any of the event sources they will be awakened with a KERN_INTERUPTED status.
*/
- virtual void free();
+ virtual void free() APPLE_KEXT_OVERRIDE;
/*! @function threadMain
- @discussion Work loop threads main function. This function consists of 3 loops: the outermost loop is the semaphore clear and wait loop, the middle loop terminates when there is no more work, and the inside loop walks the event list calling the checkForWork method in each event source. If an event source has more work to do, it can set the more flag and the middle loop will repeat. When no more work is outstanding the outermost will sleep until an event is signalled or the least wakeupTime, whichever occurs first. If the event source does not require the semaphore wait to time out, it must set the provided wakeupTime parameter to zero.
+ @discussion Work loop threads main function. This function consists of 3
+ loops: the outermost loop is the semaphore clear and wait loop, the middle
+ loop terminates when there is no more work, and the inside loop walks the
+ event list calling the checkForWork method in each event source. If an
+ event source has more work to do, it can set the more flag and the middle
+ loop will repeat. When no more work is outstanding the outermost will
+ sleep until an event is signalled.
*/
virtual void threadMain();
public:
/*! @function workLoop
- @abstract Factory member function to constuct and intialize a work loop.
+ @abstract Factory member function to construct and intialize a work loop.
@result Returns a workLoop instance if constructed successfully, 0 otherwise.
*/
static IOWorkLoop *workLoop();
+/*! @function workLoopWithOptions(IOOptionBits options)
+ @abstract Factory member function to constuct and intialize a work loop.
+ @param options Options - kPreciousStack to avoid stack deallocation on paging path.
+ @result Returns a workLoop instance if constructed successfully, 0 otherwise.
+*/
+ static IOWorkLoop *workLoopWithOptions(IOOptionBits options);
+
/*! @function init
- @discussion Initializes an instance of the workloop. This method creates and initialses the signaling semaphore and forks the thread that will continue executing.
+ @discussion Initializes an instance of the workloop. This method creates and initializes the signaling semaphore, the controller gate lock, and spawns the thread that will continue executing.
@result Returns true if initialized successfully, false otherwise.
*/
- virtual bool init();
+ virtual bool init() APPLE_KEXT_OVERRIDE;
/*! @function getThread
@abstract Gets the workThread.
/*! @function enableAllInterrupts
@abstract Calls enable() in all interrupt event sources.
- @discussion For all event sources (ES) for which IODynamicCast(IOInterruptEventSource, ES) is valid, in eventChain call enable() function. See IOEventSource::enable().
+ @discussion For all event sources (ES) for which OSDynamicCast(IOInterruptEventSource, ES) is valid, in eventChain call enable() function. See IOEventSource::enable().
*/
virtual void enableAllInterrupts() const;
/*! @function disableAllInterrupts
@abstract Calls disable() in all interrupt event sources.
- @discussion For all event sources (ES) for which IODynamicCast(IOInterruptEventSource, ES) is valid, in eventChain call disable() function. See IOEventSource::disable().
+ @discussion For all event sources (ES) for which OSDynamicCast(IOInterruptEventSource, ES) is valid, in eventChain call disable() function. See IOEventSource::disable().
*/
virtual void disableAllInterrupts() const;
// Internal APIs used by event sources to control the thread
friend class IOEventSource;
friend class IOTimerEventSource;
+ friend class IOCommandGate;
+#if IOKITSTATS
+ friend class IOStatistics;
+#endif
virtual void signalWorkAvailable();
virtual void openGate();
virtual void closeGate();
virtual bool tryCloseGate();
- virtual int sleepGate(void *event, UInt32 interuptibleType);
+ virtual int sleepGate(void *event, UInt32 interuptibleType);
virtual void wakeupGate(void *event, bool oneThread);
public:
/*! @function runAction
@abstract Single thread a call to an action with the work-loop.
- @discussion Client function that causes the given action to be called in
-a single threaded manner. Beware: the work-loop's gate is recursive and runAction can cause direct or indirect re-entrancy. When executing on a client's thread, runAction will sleep until the work-loop's gate opens for
-execution of client actions, the action is single threaded against all other work-loop event sources.
+ @discussion Client function that causes the given action to be called in a single threaded manner. Beware: the work-loop's gate is recursive and runAction can cause direct or indirect re-entrancy. When executing on a client's thread, runAction will sleep until the work-loop's gate opens for execution of client actions, the action is single threaded against all other work-loop event sources.
@param action Pointer to function to be executed in work-loop context.
@param arg0 Parameter for action parameter, defaults to 0.
@param arg1 Parameter for action parameter, defaults to 0.
void *arg0 = 0, void *arg1 = 0,
void *arg2 = 0, void *arg3 = 0);
+#ifdef __BLOCKS__
+/*! @function runAction
+ @abstract Single thread a call to an action with the work-loop.
+ @discussion Client function that causes the given action to be called in a single threaded manner. Beware: the work-loop's gate is recursive and runAction can cause direct or indirect re-entrancy. When executing on a client's thread, runAction will sleep until the work-loop's gate opens for execution of client actions, the action is single threaded against all other work-loop event sources.
+ @param action Block to be executed in work-loop context.
+ @result Returns the result of the action block.
+*/
+ IOReturn runActionBlock(ActionBlock action);
+#endif /* __BLOCKS__ */
+
+/*! @function runEventSources
+ @discussion Consists of the inner 2 loops of the threadMain function(qv).
+ The outer loop terminates when there is no more work, and the inside loop
+ walks the event list calling the checkForWork method in each event source.
+ If an event source has more work to do, it can set the more flag and the
+ outer loop will repeat.
+<br><br>
+ This function can be used to clear a priority inversion between the normal
+ workloop thread and multimedia's real time threads. The problem is that
+ the interrupt action routine is often held off by high priority threads.
+ So if they want to get their data now they will have to call us and ask if
+ any data is available. The multi-media user client will arrange for this
+ function to be called, which causes any pending interrupts to be processed
+ and the completion routines called. By the time the function returns all
+ outstanding work will have been completed at the real time threads
+ priority.
+
+ @result Return false if the work loop is shutting down, true otherwise.
+*/
+ virtual bool runEventSources();
+
+/*! @function setMaximumLockTime
+ @discussion For diagnostics use in DEVELOPMENT kernels, set a time interval which if the work loop lock is held for this time or greater, IOWorkLoop will panic or log a backtrace.
+ @param interval An absolute time interval, eg. created with clock_interval_to_absolutetime_interval().
+ @param options Pass IOWorkLoop::kTimeLockPanics to panic when the time is exceeded, otherwise a log will be generated with OSReportWithBacktrace().
+*/
+ void setMaximumLockTime(uint64_t interval, uint32_t options);
+
protected:
- OSMetaClassDeclareReservedUsed(IOWorkLoop, 0);
+ // Internal APIs used by event sources to control the thread
+ virtual int sleepGate(void *event, AbsoluteTime deadline, UInt32 interuptibleType);
+#if XNU_KERNEL_PRIVATE
+ void lockTime(void);
+#endif /* XNU_KERNEL_PRIVATE */
+
+protected:
+#if __LP64__
+ OSMetaClassDeclareReservedUnused(IOWorkLoop, 0);
OSMetaClassDeclareReservedUnused(IOWorkLoop, 1);
OSMetaClassDeclareReservedUnused(IOWorkLoop, 2);
+#else
+ OSMetaClassDeclareReservedUsed(IOWorkLoop, 0);
+ OSMetaClassDeclareReservedUsed(IOWorkLoop, 1);
+ OSMetaClassDeclareReservedUsed(IOWorkLoop, 2);
+#endif
OSMetaClassDeclareReservedUnused(IOWorkLoop, 3);
OSMetaClassDeclareReservedUnused(IOWorkLoop, 4);
OSMetaClassDeclareReservedUnused(IOWorkLoop, 5);