xref: /xnu-12377.41.6/iokit/IOKit/IOWorkLoop.h (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions  * Copyright (c) 1998-2019 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions  *
4*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions  *
6*bbb1b6f9SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions  *
15*bbb1b6f9SApple OSS Distributions  * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions  *
18*bbb1b6f9SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions  * limitations under the License.
25*bbb1b6f9SApple OSS Distributions  *
26*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions  */
28*bbb1b6f9SApple OSS Distributions 
29*bbb1b6f9SApple OSS Distributions #ifndef __IOKIT_IOWORKLOOP_H
30*bbb1b6f9SApple OSS Distributions #define __IOKIT_IOWORKLOOP_H
31*bbb1b6f9SApple OSS Distributions 
32*bbb1b6f9SApple OSS Distributions #include <libkern/c++/OSObject.h>
33*bbb1b6f9SApple OSS Distributions #include <IOKit/IOReturn.h>
34*bbb1b6f9SApple OSS Distributions #include <IOKit/IOLib.h>
35*bbb1b6f9SApple OSS Distributions #include <IOKit/IOLocks.h>
36*bbb1b6f9SApple OSS Distributions #include <libkern/c++/OSPtr.h>
37*bbb1b6f9SApple OSS Distributions 
38*bbb1b6f9SApple OSS Distributions #include <IOKit/system.h>
39*bbb1b6f9SApple OSS Distributions 
40*bbb1b6f9SApple OSS Distributions #if IOKITSTATS
41*bbb1b6f9SApple OSS Distributions #include <IOKit/IOStatisticsPrivate.h>
42*bbb1b6f9SApple OSS Distributions #endif
43*bbb1b6f9SApple OSS Distributions 
44*bbb1b6f9SApple OSS Distributions class IOEventSource;
45*bbb1b6f9SApple OSS Distributions class IOTimerEventSource;
46*bbb1b6f9SApple OSS Distributions class IOCommandGate;
47*bbb1b6f9SApple OSS Distributions #if CONFIG_EXCLAVES
48*bbb1b6f9SApple OSS Distributions class IOExclaveWorkLoopAperture;
49*bbb1b6f9SApple OSS Distributions #endif /* CONFIG_EXCLAVES */
50*bbb1b6f9SApple OSS Distributions 
51*bbb1b6f9SApple OSS Distributions /*! @class IOWorkLoop
52*bbb1b6f9SApple OSS Distributions  *   @discussion An IOWorkLoop is a thread of control that is intended to be used to provide single threaded access to hardware.  This class has no knowledge of the nature and type of the events that it marshals and forwards.  When a device driver successfully starts (see IOService::start), it is expected to create the event sources it will need to receive events.  Then a work loop is initialized and the events are added to the work loop for monitoring.  In general this set up will be automated by the family superclass of the specific device.
53*bbb1b6f9SApple OSS Distributions  *  <br><br>
54*bbb1b6f9SApple OSS Distributions  *       The thread main method walks the event source linked list and messages each one requesting a work check.  At this point each event source is expected to notify its registered owner that the event has occurred.  After each event has been walked and each indicates that another loop isn't required (by setting the 'more' flag to false) the thread will go to sleep on a signaling semaphore.
55*bbb1b6f9SApple OSS Distributions  *  <br><br>
56*bbb1b6f9SApple OSS Distributions  *       When an event source is registered with a work loop it is informed of the semaphore to use to wake up the loop.
57*bbb1b6f9SApple OSS Distributions  */
58*bbb1b6f9SApple OSS Distributions class IOWorkLoop : public OSObject
59*bbb1b6f9SApple OSS Distributions {
60*bbb1b6f9SApple OSS Distributions 	OSDeclareDefaultStructors(IOWorkLoop);
61*bbb1b6f9SApple OSS Distributions 
62*bbb1b6f9SApple OSS Distributions public:
63*bbb1b6f9SApple OSS Distributions /*!
64*bbb1b6f9SApple OSS Distributions  *   @typedef Action
65*bbb1b6f9SApple OSS Distributions  *   @discussion Type and arguments of callout C function that is used when
66*bbb1b6f9SApple OSS Distributions  *  a runCommand is executed by a client.  Cast to this type when you want a C++
67*bbb1b6f9SApple OSS Distributions  *  member function to be used.  Note the arg1 - arg3 parameters are straight pass
68*bbb1b6f9SApple OSS Distributions  *  through from the runCommand to the action callout.
69*bbb1b6f9SApple OSS Distributions  *   @param target
70*bbb1b6f9SApple OSS Distributions  *       Target of the function, can be used as a refcon.  Note if a C++ function
71*bbb1b6f9SApple OSS Distributions  *  was specified, this parameter is implicitly the first parameter in the target
72*bbb1b6f9SApple OSS Distributions  *  member function's parameter list.
73*bbb1b6f9SApple OSS Distributions  *   @param arg0 Argument to action from run operation.
74*bbb1b6f9SApple OSS Distributions  *   @param arg1 Argument to action from run operation.
75*bbb1b6f9SApple OSS Distributions  *   @param arg2 Argument to action from run operation.
76*bbb1b6f9SApple OSS Distributions  *   @param arg3 Argument to action from run operation.
77*bbb1b6f9SApple OSS Distributions  */
78*bbb1b6f9SApple OSS Distributions 	typedef IOReturn (*Action)(OSObject *target,
79*bbb1b6f9SApple OSS Distributions 	    void *arg0, void *arg1,
80*bbb1b6f9SApple OSS Distributions 	    void *arg2, void *arg3);
81*bbb1b6f9SApple OSS Distributions 
82*bbb1b6f9SApple OSS Distributions #ifdef __BLOCKS__
83*bbb1b6f9SApple OSS Distributions 	typedef IOReturn (^ActionBlock)();
84*bbb1b6f9SApple OSS Distributions #endif /* __BLOCKS__ */
85*bbb1b6f9SApple OSS Distributions 
86*bbb1b6f9SApple OSS Distributions 	enum {
87*bbb1b6f9SApple OSS Distributions 		kPreciousStack  = 0x00000001,
88*bbb1b6f9SApple OSS Distributions 		kTimeLockPanics = 0x00000002,
89*bbb1b6f9SApple OSS Distributions 	};
90*bbb1b6f9SApple OSS Distributions 
91*bbb1b6f9SApple OSS Distributions private:
92*bbb1b6f9SApple OSS Distributions /*! @function threadMainContinuation
93*bbb1b6f9SApple OSS Distributions  *   @abstract Static function that calls the threadMain function.
94*bbb1b6f9SApple OSS Distributions  */
95*bbb1b6f9SApple OSS Distributions 	static void threadMainContinuation(IOWorkLoop *self);
96*bbb1b6f9SApple OSS Distributions 
97*bbb1b6f9SApple OSS Distributions /*! @function eventSourcePerformsWork
98*bbb1b6f9SApple OSS Distributions  *       @abstract Checks if the event source passed in overrides checkForWork() to perform any work.
99*bbb1b6f9SApple OSS Distributions  *  IOWorkLoop uses this to determine if the event source should be polled in runEventSources() or not.
100*bbb1b6f9SApple OSS Distributions  *       @param inEventSource The event source to check.
101*bbb1b6f9SApple OSS Distributions  */
102*bbb1b6f9SApple OSS Distributions 	bool eventSourcePerformsWork(IOEventSource *inEventSource);
103*bbb1b6f9SApple OSS Distributions 
104*bbb1b6f9SApple OSS Distributions /*! @function releaseEventChain
105*bbb1b6f9SApple OSS Distributions  *   @abstract Static function that releases the events in a chain and sets
106*bbb1b6f9SApple OSS Distributions  *   their work loops to NULL.
107*bbb1b6f9SApple OSS Distributions  */
108*bbb1b6f9SApple OSS Distributions 	static void releaseEventChain(LIBKERN_CONSUMED IOEventSource *eventChain);
109*bbb1b6f9SApple OSS Distributions 
110*bbb1b6f9SApple OSS Distributions protected:
111*bbb1b6f9SApple OSS Distributions 
112*bbb1b6f9SApple OSS Distributions /*! @typedef maintCommandEnum
113*bbb1b6f9SApple OSS Distributions  *   @discussion Enumeration of commands that _maintCommand can deal with.
114*bbb1b6f9SApple OSS Distributions  *   @constant mAddEvent Used to tag a Remove event source command.
115*bbb1b6f9SApple OSS Distributions  *   @constant mRemoveEvent Used to tag a Remove event source command.
116*bbb1b6f9SApple OSS Distributions  */
117*bbb1b6f9SApple OSS Distributions 	typedef enum { mAddEvent, mRemoveEvent } maintCommandEnum;
118*bbb1b6f9SApple OSS Distributions 
119*bbb1b6f9SApple OSS Distributions /*! @var gateLock
120*bbb1b6f9SApple OSS Distributions  *   Mutual exclusion lock that is used by close and open Gate functions.
121*bbb1b6f9SApple OSS Distributions  *   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.
122*bbb1b6f9SApple OSS Distributions  */
123*bbb1b6f9SApple OSS Distributions 	IORecursiveLock *gateLock;
124*bbb1b6f9SApple OSS Distributions 
125*bbb1b6f9SApple OSS Distributions /*! @var eventChain
126*bbb1b6f9SApple OSS Distributions  *   Pointer to first event source in linked list.
127*bbb1b6f9SApple OSS Distributions  */
128*bbb1b6f9SApple OSS Distributions 	IOEventSource *eventChain;
129*bbb1b6f9SApple OSS Distributions 
130*bbb1b6f9SApple OSS Distributions /*! @var controlG
131*bbb1b6f9SApple OSS Distributions  *   Internal control gate to maintain event system.
132*bbb1b6f9SApple OSS Distributions  */
133*bbb1b6f9SApple OSS Distributions 	IOCommandGate *controlG;
134*bbb1b6f9SApple OSS Distributions 
135*bbb1b6f9SApple OSS Distributions /*! @var workToDoLock
136*bbb1b6f9SApple OSS Distributions  *   The spin lock that is used to guard the 'workToDo' variable.
137*bbb1b6f9SApple OSS Distributions  */
138*bbb1b6f9SApple OSS Distributions 	IOSimpleLock *workToDoLock;
139*bbb1b6f9SApple OSS Distributions 
140*bbb1b6f9SApple OSS Distributions /*! @var workThread
141*bbb1b6f9SApple OSS Distributions  *   Work loop thread.
142*bbb1b6f9SApple OSS Distributions  */
143*bbb1b6f9SApple OSS Distributions 	IOThread workThread;
144*bbb1b6f9SApple OSS Distributions 
145*bbb1b6f9SApple OSS Distributions /*! @var workToDo
146*bbb1b6f9SApple OSS Distributions  *   Used to to indicate that an interrupt has fired and needs to be processed.
147*bbb1b6f9SApple OSS Distributions  */
148*bbb1b6f9SApple OSS Distributions 	volatile bool workToDo;
149*bbb1b6f9SApple OSS Distributions 
150*bbb1b6f9SApple OSS Distributions /*! @var loopRestart
151*bbb1b6f9SApple OSS Distributions  *   Set if an event chain has been changed and the system has to be rechecked from start.  (Internal use only)
152*bbb1b6f9SApple OSS Distributions  */
153*bbb1b6f9SApple OSS Distributions 	bool loopRestart;
154*bbb1b6f9SApple OSS Distributions 
155*bbb1b6f9SApple OSS Distributions /*! @struct ExpansionData
156*bbb1b6f9SApple OSS Distributions  *   @discussion This structure will be used to expand the capablilties of the IOWorkLoop in the future.
157*bbb1b6f9SApple OSS Distributions  */
158*bbb1b6f9SApple OSS Distributions 	struct ExpansionData {
159*bbb1b6f9SApple OSS Distributions 		IOOptionBits options;
160*bbb1b6f9SApple OSS Distributions 		IOEventSource *passiveEventChain;
161*bbb1b6f9SApple OSS Distributions #if IOKITSTATS
162*bbb1b6f9SApple OSS Distributions 		struct IOWorkLoopCounter *counter;
163*bbb1b6f9SApple OSS Distributions #else
164*bbb1b6f9SApple OSS Distributions 		void *iokitstatsReserved;
165*bbb1b6f9SApple OSS Distributions #endif
166*bbb1b6f9SApple OSS Distributions 		uint64_t lockInterval;
167*bbb1b6f9SApple OSS Distributions 		uint64_t lockTime;
168*bbb1b6f9SApple OSS Distributions 	};
169*bbb1b6f9SApple OSS Distributions 
170*bbb1b6f9SApple OSS Distributions /*! @var reserved
171*bbb1b6f9SApple OSS Distributions  *   Reserved for future use.  (Internal use only)
172*bbb1b6f9SApple OSS Distributions  */
173*bbb1b6f9SApple OSS Distributions 	ExpansionData *reserved;
174*bbb1b6f9SApple OSS Distributions 
175*bbb1b6f9SApple OSS Distributions /*! @function _maintRequest
176*bbb1b6f9SApple OSS Distributions  *   @abstract Synchronous implementation of addEventSource and removeEventSource functions.
177*bbb1b6f9SApple OSS Distributions  *   @discussion This function implements the commands as defined in the maintCommandEnum.  It can be subclassed but it isn't an external API in the usual sense.  A subclass implementation of _maintRequest would be called synchronously with respect to the work loop and it should be implemented in the usual way that an ioctl would be.
178*bbb1b6f9SApple OSS Distributions  *   @return kIOReturnUnsupported if the command given is not implemented, kIOReturnSuccess otherwise.
179*bbb1b6f9SApple OSS Distributions  */
180*bbb1b6f9SApple OSS Distributions 	virtual IOReturn _maintRequest(void *command, void *data, void *, void *);
181*bbb1b6f9SApple OSS Distributions 
182*bbb1b6f9SApple OSS Distributions /*! @function free
183*bbb1b6f9SApple OSS Distributions  *   @discussion Mandatory free of the object independent of the current retain count.  If the work loop is running, this method will not return until the thread has successfully terminated.  Each event source in the chain will be released and the working semaphore will be destroyed.
184*bbb1b6f9SApple OSS Distributions  *  <br><br>
185*bbb1b6f9SApple OSS Distributions  *       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.
186*bbb1b6f9SApple OSS Distributions  */
187*bbb1b6f9SApple OSS Distributions 	virtual void free() APPLE_KEXT_OVERRIDE;
188*bbb1b6f9SApple OSS Distributions 
189*bbb1b6f9SApple OSS Distributions /*! @function threadMain
190*bbb1b6f9SApple OSS Distributions  *   @discussion Work loop threads main function.  This function consists of 3
191*bbb1b6f9SApple OSS Distributions  *   loops: the outermost loop is the semaphore clear and wait loop, the middle
192*bbb1b6f9SApple OSS Distributions  *   loop terminates when there is no more work, and the inside loop walks the
193*bbb1b6f9SApple OSS Distributions  *   event list calling the checkForWork method in each event source.  If an
194*bbb1b6f9SApple OSS Distributions  *   event source has more work to do, it can set the more flag and the middle
195*bbb1b6f9SApple OSS Distributions  *   loop will repeat.  When no more work is outstanding the outermost will
196*bbb1b6f9SApple OSS Distributions  *   sleep until an event is signalled.
197*bbb1b6f9SApple OSS Distributions  */
198*bbb1b6f9SApple OSS Distributions 	virtual void threadMain();
199*bbb1b6f9SApple OSS Distributions 
200*bbb1b6f9SApple OSS Distributions public:
201*bbb1b6f9SApple OSS Distributions 
202*bbb1b6f9SApple OSS Distributions /*! @function workLoop
203*bbb1b6f9SApple OSS Distributions  *   @abstract Factory member function to construct and intialize a work loop.
204*bbb1b6f9SApple OSS Distributions  *   @result Returns a workLoop instance if constructed successfully, 0 otherwise.
205*bbb1b6f9SApple OSS Distributions  */
206*bbb1b6f9SApple OSS Distributions 	static OSPtr<IOWorkLoop> workLoop();
207*bbb1b6f9SApple OSS Distributions 
208*bbb1b6f9SApple OSS Distributions /*! @function workLoopWithOptions(IOOptionBits options)
209*bbb1b6f9SApple OSS Distributions  *   @abstract Factory member function to constuct and intialize a work loop.
210*bbb1b6f9SApple OSS Distributions  *   @param options Options - kPreciousStack to avoid stack deallocation on paging path.
211*bbb1b6f9SApple OSS Distributions  *   @result Returns a workLoop instance if constructed successfully, 0 otherwise.
212*bbb1b6f9SApple OSS Distributions  */
213*bbb1b6f9SApple OSS Distributions 	static OSPtr<IOWorkLoop> workLoopWithOptions(IOOptionBits options);
214*bbb1b6f9SApple OSS Distributions 
215*bbb1b6f9SApple OSS Distributions /*! @function init
216*bbb1b6f9SApple OSS Distributions  *   @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.
217*bbb1b6f9SApple OSS Distributions  *   @result Returns true if initialized successfully, false otherwise.
218*bbb1b6f9SApple OSS Distributions  */
219*bbb1b6f9SApple OSS Distributions 	virtual bool init() APPLE_KEXT_OVERRIDE;
220*bbb1b6f9SApple OSS Distributions 
221*bbb1b6f9SApple OSS Distributions /*! @function getThread
222*bbb1b6f9SApple OSS Distributions  *   @abstract Gets the workThread.
223*bbb1b6f9SApple OSS Distributions  *   @result Returns workThread.
224*bbb1b6f9SApple OSS Distributions  */
225*bbb1b6f9SApple OSS Distributions 	virtual IOThread getThread() const;
226*bbb1b6f9SApple OSS Distributions 
227*bbb1b6f9SApple OSS Distributions /*! @function onThread
228*bbb1b6f9SApple OSS Distributions  *   @abstract Is the current execution context on the work thread?
229*bbb1b6f9SApple OSS Distributions  *   @result Returns true if IOThreadSelf() == workThread.
230*bbb1b6f9SApple OSS Distributions  */
231*bbb1b6f9SApple OSS Distributions 	virtual bool onThread() const;
232*bbb1b6f9SApple OSS Distributions 
233*bbb1b6f9SApple OSS Distributions /*! @function inGate
234*bbb1b6f9SApple OSS Distributions  *   @abstract Is the current execution context holding the work-loop's gate?
235*bbb1b6f9SApple OSS Distributions  *   @result Returns true if IOThreadSelf() is gate holder.
236*bbb1b6f9SApple OSS Distributions  */
237*bbb1b6f9SApple OSS Distributions 	virtual bool inGate() const;
238*bbb1b6f9SApple OSS Distributions 
239*bbb1b6f9SApple OSS Distributions /*! @function addEventSource
240*bbb1b6f9SApple OSS Distributions  *   @discussion Add an event source to be monitored by the work loop.  This function does not return until the work loop has acknowledged the arrival of the new event source.  When a new event has been added the threadMain will always restart its loop and check all outstanding events.  The event source is retained by the work loop.
241*bbb1b6f9SApple OSS Distributions  *   @param newEvent Pointer to IOEventSource subclass to add.
242*bbb1b6f9SApple OSS Distributions  *   @result Always returns kIOReturnSuccess.
243*bbb1b6f9SApple OSS Distributions  */
244*bbb1b6f9SApple OSS Distributions 	virtual IOReturn addEventSource(IOEventSource *newEvent);
245*bbb1b6f9SApple OSS Distributions 
246*bbb1b6f9SApple OSS Distributions /*! @function removeEventSource
247*bbb1b6f9SApple OSS Distributions  *   @discussion Remove an event source from the work loop.  This function does not return until the work loop has acknowledged the removal of the event source.  When an event has been removed the threadMain will always restart its loop and check all outstanding events.  The event source will be released before return.
248*bbb1b6f9SApple OSS Distributions  *   @param toRemove Pointer to IOEventSource subclass to remove.
249*bbb1b6f9SApple OSS Distributions  *   @result Returns kIOReturnSuccess if successful, kIOReturnBadArgument if toRemove couldn't be found.
250*bbb1b6f9SApple OSS Distributions  */
251*bbb1b6f9SApple OSS Distributions 	virtual IOReturn removeEventSource(IOEventSource *toRemove);
252*bbb1b6f9SApple OSS Distributions 
253*bbb1b6f9SApple OSS Distributions /*! @function enableAllEventSources
254*bbb1b6f9SApple OSS Distributions  *   @abstract Calls enable() in all event sources.
255*bbb1b6f9SApple OSS Distributions  *   @discussion For all event sources in eventChain, call enable() function.  See IOEventSource::enable().
256*bbb1b6f9SApple OSS Distributions  */
257*bbb1b6f9SApple OSS Distributions 	virtual void enableAllEventSources() const;
258*bbb1b6f9SApple OSS Distributions 
259*bbb1b6f9SApple OSS Distributions /*! @function disableAllEventSources
260*bbb1b6f9SApple OSS Distributions  *   @abstract Calls disable() in all event sources.
261*bbb1b6f9SApple OSS Distributions  *   @discussion For all event sources in eventChain, call disable() function.  See IOEventSource::disable().
262*bbb1b6f9SApple OSS Distributions  */
263*bbb1b6f9SApple OSS Distributions 	virtual void disableAllEventSources() const;
264*bbb1b6f9SApple OSS Distributions 
265*bbb1b6f9SApple OSS Distributions /*! @function enableAllInterrupts
266*bbb1b6f9SApple OSS Distributions  *   @abstract Calls enable() in all interrupt event sources.
267*bbb1b6f9SApple OSS Distributions  *   @discussion For all event sources (ES) for which OSDynamicCast(IOInterruptEventSource, ES) is valid, in eventChain call enable() function.  See IOEventSource::enable().
268*bbb1b6f9SApple OSS Distributions  */
269*bbb1b6f9SApple OSS Distributions 	virtual void enableAllInterrupts() const;
270*bbb1b6f9SApple OSS Distributions 
271*bbb1b6f9SApple OSS Distributions /*! @function disableAllInterrupts
272*bbb1b6f9SApple OSS Distributions  *   @abstract Calls disable() in all interrupt event sources.
273*bbb1b6f9SApple OSS Distributions  *   @discussion For all event sources (ES) for which OSDynamicCast(IOInterruptEventSource, ES) is valid,  in eventChain call disable() function.  See IOEventSource::disable().
274*bbb1b6f9SApple OSS Distributions  */
275*bbb1b6f9SApple OSS Distributions 	virtual void disableAllInterrupts() const;
276*bbb1b6f9SApple OSS Distributions 
277*bbb1b6f9SApple OSS Distributions 
278*bbb1b6f9SApple OSS Distributions protected:
279*bbb1b6f9SApple OSS Distributions // Internal APIs used by event sources to control the thread
280*bbb1b6f9SApple OSS Distributions 	friend class IOEventSource;
281*bbb1b6f9SApple OSS Distributions 	friend class IOTimerEventSource;
282*bbb1b6f9SApple OSS Distributions 	friend class IOCommandGate;
283*bbb1b6f9SApple OSS Distributions #if CONFIG_EXCLAVES
284*bbb1b6f9SApple OSS Distributions 	friend class IOExclaveWorkLoopAperture;
285*bbb1b6f9SApple OSS Distributions #endif /* CONFIG_EXCLAVES */
286*bbb1b6f9SApple OSS Distributions #if IOKITSTATS
287*bbb1b6f9SApple OSS Distributions 	friend class IOStatistics;
288*bbb1b6f9SApple OSS Distributions #endif
289*bbb1b6f9SApple OSS Distributions 	virtual void signalWorkAvailable();
290*bbb1b6f9SApple OSS Distributions 	virtual void openGate();
291*bbb1b6f9SApple OSS Distributions 	virtual void closeGate();
292*bbb1b6f9SApple OSS Distributions 	virtual bool tryCloseGate();
293*bbb1b6f9SApple OSS Distributions 	virtual int  sleepGate(void *event, UInt32 interuptibleType);
294*bbb1b6f9SApple OSS Distributions 	virtual void wakeupGate(void *event, bool oneThread);
295*bbb1b6f9SApple OSS Distributions 
296*bbb1b6f9SApple OSS Distributions public:
297*bbb1b6f9SApple OSS Distributions /* methods available in Mac OS X 10.1 or later */
298*bbb1b6f9SApple OSS Distributions 
299*bbb1b6f9SApple OSS Distributions /*! @function runAction
300*bbb1b6f9SApple OSS Distributions  *   @abstract Single thread a call to an action with the work-loop.
301*bbb1b6f9SApple OSS Distributions  *   @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.
302*bbb1b6f9SApple OSS Distributions  *   @param action Pointer to function to be executed in work-loop context.
303*bbb1b6f9SApple OSS Distributions  *   @param arg0 Parameter for action parameter, defaults to 0.
304*bbb1b6f9SApple OSS Distributions  *   @param arg1 Parameter for action parameter, defaults to 0.
305*bbb1b6f9SApple OSS Distributions  *   @param arg2 Parameter for action parameter, defaults to 0.
306*bbb1b6f9SApple OSS Distributions  *   @param arg3 Parameter for action parameter, defaults to 0.
307*bbb1b6f9SApple OSS Distributions  *   @result Returns the value of the Action callout.
308*bbb1b6f9SApple OSS Distributions  */
309*bbb1b6f9SApple OSS Distributions 	virtual IOReturn runAction(Action action, OSObject *target,
310*bbb1b6f9SApple OSS Distributions 	    void *arg0 = NULL, void *arg1 = NULL,
311*bbb1b6f9SApple OSS Distributions 	    void *arg2 = NULL, void *arg3 = NULL);
312*bbb1b6f9SApple OSS Distributions 
313*bbb1b6f9SApple OSS Distributions #ifdef __BLOCKS__
314*bbb1b6f9SApple OSS Distributions /*! @function runAction
315*bbb1b6f9SApple OSS Distributions  *   @abstract Single thread a call to an action with the work-loop.
316*bbb1b6f9SApple OSS Distributions  *   @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.
317*bbb1b6f9SApple OSS Distributions  *   @param action Block to be executed in work-loop context.
318*bbb1b6f9SApple OSS Distributions  *   @result Returns the result of the action block.
319*bbb1b6f9SApple OSS Distributions  */
320*bbb1b6f9SApple OSS Distributions 	IOReturn runActionBlock(ActionBlock action);
321*bbb1b6f9SApple OSS Distributions #endif /* __BLOCKS__ */
322*bbb1b6f9SApple OSS Distributions 
323*bbb1b6f9SApple OSS Distributions /*! @function runEventSources
324*bbb1b6f9SApple OSS Distributions  *   @discussion Consists of the inner 2 loops of the threadMain function(qv).
325*bbb1b6f9SApple OSS Distributions  *   The outer loop terminates when there is no more work, and the inside loop
326*bbb1b6f9SApple OSS Distributions  *   walks the event list calling the checkForWork method in each event source.
327*bbb1b6f9SApple OSS Distributions  *   If an event source has more work to do, it can set the more flag and the
328*bbb1b6f9SApple OSS Distributions  *   outer loop will repeat.
329*bbb1b6f9SApple OSS Distributions  *  <br><br>
330*bbb1b6f9SApple OSS Distributions  *   This function can be used to clear a priority inversion between the normal
331*bbb1b6f9SApple OSS Distributions  *   workloop thread and multimedia's real time threads.  The problem is that
332*bbb1b6f9SApple OSS Distributions  *   the interrupt action routine is often held off by high priority threads.
333*bbb1b6f9SApple OSS Distributions  *   So if they want to get their data now they will have to call us and ask if
334*bbb1b6f9SApple OSS Distributions  *   any data is available.  The multi-media user client will arrange for this
335*bbb1b6f9SApple OSS Distributions  *   function to be called, which causes any pending interrupts to be processed
336*bbb1b6f9SApple OSS Distributions  *   and the completion routines called.  By the time the function returns all
337*bbb1b6f9SApple OSS Distributions  *   outstanding work will have been completed at the real time threads
338*bbb1b6f9SApple OSS Distributions  *   priority.
339*bbb1b6f9SApple OSS Distributions  *
340*bbb1b6f9SApple OSS Distributions  *   @result Return false if the work loop is shutting down, true otherwise.
341*bbb1b6f9SApple OSS Distributions  */
342*bbb1b6f9SApple OSS Distributions 	virtual bool runEventSources();
343*bbb1b6f9SApple OSS Distributions 
344*bbb1b6f9SApple OSS Distributions /*! @function setMaximumLockTime
345*bbb1b6f9SApple OSS Distributions  *   @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.
346*bbb1b6f9SApple OSS Distributions  *   @param interval An absolute time interval, eg. created with clock_interval_to_absolutetime_interval().
347*bbb1b6f9SApple OSS Distributions  *   @param options Pass IOWorkLoop::kTimeLockPanics to panic when the time is exceeded, otherwise a log will be generated with OSReportWithBacktrace().
348*bbb1b6f9SApple OSS Distributions  */
349*bbb1b6f9SApple OSS Distributions 	void setMaximumLockTime(uint64_t interval, uint32_t options);
350*bbb1b6f9SApple OSS Distributions 
351*bbb1b6f9SApple OSS Distributions protected:
352*bbb1b6f9SApple OSS Distributions // Internal APIs used by event sources to control the thread
353*bbb1b6f9SApple OSS Distributions 	virtual int sleepGate(void *event, AbsoluteTime deadline, UInt32 interuptibleType);
354*bbb1b6f9SApple OSS Distributions 
355*bbb1b6f9SApple OSS Distributions #if XNU_KERNEL_PRIVATE
356*bbb1b6f9SApple OSS Distributions 	void lockTime(void);
357*bbb1b6f9SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
358*bbb1b6f9SApple OSS Distributions 
359*bbb1b6f9SApple OSS Distributions protected:
360*bbb1b6f9SApple OSS Distributions #if __LP64__
361*bbb1b6f9SApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOWorkLoop, 0);
362*bbb1b6f9SApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOWorkLoop, 1);
363*bbb1b6f9SApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOWorkLoop, 2);
364*bbb1b6f9SApple OSS Distributions #else
365*bbb1b6f9SApple OSS Distributions 	OSMetaClassDeclareReservedUsedX86(IOWorkLoop, 0);
366*bbb1b6f9SApple OSS Distributions 	OSMetaClassDeclareReservedUsedX86(IOWorkLoop, 1);
367*bbb1b6f9SApple OSS Distributions 	OSMetaClassDeclareReservedUsedX86(IOWorkLoop, 2);
368*bbb1b6f9SApple OSS Distributions #endif
369*bbb1b6f9SApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOWorkLoop, 3);
370*bbb1b6f9SApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOWorkLoop, 4);
371*bbb1b6f9SApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOWorkLoop, 5);
372*bbb1b6f9SApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOWorkLoop, 6);
373*bbb1b6f9SApple OSS Distributions 	OSMetaClassDeclareReservedUnused(IOWorkLoop, 7);
374*bbb1b6f9SApple OSS Distributions };
375*bbb1b6f9SApple OSS Distributions 
376*bbb1b6f9SApple OSS Distributions #endif /* !__IOKIT_IOWORKLOOP_H */
377