]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOWorkLoop.h
d2d8222d3ad7e96f39c682a10d4941aa9849fa3e
[apple/xnu.git] / iokit / IOKit / IOWorkLoop.h
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
30 HISTORY
31 1998-7-13 Godfrey van der Linden(gvdl)
32 Created.
33 1998-10-30 Godfrey van der Linden(gvdl)
34 Converted to C++
35 */
36
37 #ifndef __IOKIT_IOWORKLOOP_H
38 #define __IOKIT_IOWORKLOOP_H
39
40 #include <libkern/c++/OSObject.h>
41 #include <IOKit/IOReturn.h>
42 #include <IOKit/IOLib.h>
43 #include <IOKit/IOLocks.h>
44
45 #include <IOKit/system.h>
46
47 class IOEventSource;
48 class IOTimerEventSource;
49 class IOCommandGate;
50
51 /*! @class IOWorkLoop
52 @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 <br><br>
54 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 <br><br>
56 When an event source is registered with a work loop it is informed of the semaphore to use to wake up the loop.
57 */
58 class IOWorkLoop : public OSObject
59 {
60 OSDeclareDefaultStructors(IOWorkLoop)
61
62 public:
63 /*!
64 @typedef Action
65 @discussion Type and arguments of callout C function that is used when
66 a runCommand is executed by a client. Cast to this type when you want a C++
67 member function to be used. Note the arg1 - arg3 parameters are straight pass
68 through from the runCommand to the action callout.
69 @param target
70 Target of the function, can be used as a refcon. Note if a C++ function
71 was specified, this parameter is implicitly the first parameter in the target
72 member function's parameter list.
73 @param arg0 Argument to action from run operation.
74 @param arg1 Argument to action from run operation.
75 @param arg2 Argument to action from run operation.
76 @param arg3 Argument to action from run operation.
77 */
78 typedef IOReturn (*Action)(OSObject *target,
79 void *arg0, void *arg1,
80 void *arg2, void *arg3);
81
82 private:
83 /*! @function threadMainContinuation
84 @abstract Static function that calls the threadMain function.
85 */
86 static void threadMainContinuation(IOWorkLoop *self);
87
88 protected:
89
90 /*! @typedef maintCommandEnum
91 @discussion Enumeration of commands that _maintCommand can deal with.
92 @constant mAddEvent Used to tag a Remove event source command.
93 @constant mRemoveEvent Used to tag a Remove event source command.
94 */
95 typedef enum { mAddEvent, mRemoveEvent } maintCommandEnum;
96
97 /*! @var gateLock
98 Mutual exclusion lock that is used by close and open Gate functions.
99 */
100 IORecursiveLock *gateLock;
101
102 /*! @var eventChain
103 Pointer to first event source in linked list.
104 */
105 IOEventSource *eventChain;
106
107 /*! @var controlG
108 Internal control gate to maintain event system.
109 */
110 IOCommandGate *controlG;
111
112 /*! @var workSpinLock
113 The spin lock that is used to guard the 'workToDo' variable.
114 */
115 IOSimpleLock *workToDoLock;
116
117 /*! @var workThread
118 Work loop thread.
119 */
120 IOThread workThread;
121
122 /*! @var workToDo
123 Used to to indicate that an interrupt has fired and needs to be processed.
124 */
125 volatile bool workToDo;
126
127 /*! @var loopRestart
128 Set if an event chain has been changed and the system has to be rechecked from start. (Internal use only)
129 */
130 bool loopRestart;
131
132 /*! @struct ExpansionData
133 @discussion This structure will be used to expand the capablilties of the IOWorkLoop in the future.
134 */
135 struct ExpansionData { };
136
137 /*! @var reserved
138 Reserved for future use. (Internal use only)
139 */
140 ExpansionData *reserved;
141
142 /*! @function _maintRequest
143 @abstract Synchronous implementation of addEventSource and removeEventSource functions.
144 @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.
145 @return kIOReturnUnsupported if the command given is not implemented, kIOReturnSuccess otherwise.
146 */
147 virtual IOReturn _maintRequest(void *command, void *data, void *, void *);
148
149 /*! @function free
150 @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.
151 <br><br>
152 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.
153 */
154 virtual void free();
155
156 /*! @function threadMain
157 @discussion Work loop threads main function. This function consists of 3
158 loops: the outermost loop is the semaphore clear and wait loop, the middle
159 loop terminates when there is no more work, and the inside loop walks the
160 event list calling the checkForWork method in each event source. If an
161 event source has more work to do, it can set the more flag and the middle
162 loop will repeat. When no more work is outstanding the outermost will
163 sleep until an event is signalled.
164 */
165 virtual void threadMain();
166
167 public:
168
169 /*! @function workLoop
170 @abstract Factory member function to constuct and intialize a work loop.
171 @result Returns a workLoop instance if constructed successfully, 0 otherwise.
172 */
173 static IOWorkLoop *workLoop();
174
175 /*! @function init
176 @discussion Initializes an instance of the workloop. This method creates and initialses the signaling semaphore and forks the thread that will continue executing.
177 @result Returns true if initialized successfully, false otherwise.
178 */
179 virtual bool init();
180
181 /*! @function getThread
182 @abstract Gets the workThread.
183 @result Returns workThread.
184 */
185 virtual IOThread getThread() const;
186
187 /*! @function onThread
188 @abstract Is the current execution context on the work thread?
189 @result Returns true if IOThreadSelf() == workThread.
190 */
191 virtual bool onThread() const;
192
193 /*! @function inGate
194 @abstract Is the current execution context holding the work-loop's gate?
195 @result Returns true if IOThreadSelf() is gate holder.
196 */
197 virtual bool inGate() const;
198
199 /*! @function addEventSource
200 @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.
201 @param newEvent Pointer to IOEventSource subclass to add.
202 @result Always returns kIOReturnSuccess.
203 */
204 virtual IOReturn addEventSource(IOEventSource *newEvent);
205
206 /*! @function removeEventSource
207 @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.
208 @param toRemove Pointer to IOEventSource subclass to remove.
209 @result Returns kIOReturnSuccess if successful, kIOReturnBadArgument if toRemove couldn't be found.
210 */
211 virtual IOReturn removeEventSource(IOEventSource *toRemove);
212
213 /*! @function enableAllEventSources
214 @abstract Calls enable() in all event sources.
215 @discussion For all event sources in eventChain, call enable() function. See IOEventSource::enable().
216 */
217 virtual void enableAllEventSources() const;
218
219 /*! @function disableAllEventSources
220 @abstract Calls disable() in all event sources.
221 @discussion For all event sources in eventChain, call disable() function. See IOEventSource::disable().
222 */
223 virtual void disableAllEventSources() const;
224
225 /*! @function enableAllInterrupts
226 @abstract Calls enable() in all interrupt event sources.
227 @discussion For all event sources (ES) for which IODynamicCast(IOInterruptEventSource, ES) is valid, in eventChain call enable() function. See IOEventSource::enable().
228 */
229 virtual void enableAllInterrupts() const;
230
231 /*! @function disableAllInterrupts
232 @abstract Calls disable() in all interrupt event sources.
233 @discussion For all event sources (ES) for which IODynamicCast(IOInterruptEventSource, ES) is valid, in eventChain call disable() function. See IOEventSource::disable().
234 */
235 virtual void disableAllInterrupts() const;
236
237
238 protected:
239 // Internal APIs used by event sources to control the thread
240 friend class IOEventSource;
241 friend class IOTimerEventSource;
242 virtual void signalWorkAvailable();
243 virtual void openGate();
244 virtual void closeGate();
245 virtual bool tryCloseGate();
246 virtual int sleepGate(void *event, UInt32 interuptibleType);
247 virtual void wakeupGate(void *event, bool oneThread);
248
249 public:
250 /* methods available in Mac OS X 10.1 or later */
251
252 /*! @function runAction
253 @abstract Single thread a call to an action with the work-loop.
254 @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.
255 @param action Pointer to function to be executed in work-loop context.
256 @param arg0 Parameter for action parameter, defaults to 0.
257 @param arg1 Parameter for action parameter, defaults to 0.
258 @param arg2 Parameter for action parameter, defaults to 0.
259 @param arg3 Parameter for action parameter, defaults to 0.
260 @result Returns the value of the Action callout.
261 */
262 OSMetaClassDeclareReservedUsed(IOWorkLoop, 0);
263 virtual IOReturn runAction(Action action, OSObject *target,
264 void *arg0 = 0, void *arg1 = 0,
265 void *arg2 = 0, void *arg3 = 0);
266
267 #if !(defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
268 /*! @function runEventSources
269 @discussion Consists of the inner 2 loops of the threadMain function(qv).
270 The outer loop terminates when there is no more work, and the inside loop
271 walks the event list calling the checkForWork method in each event source.
272 If an event source has more work to do, it can set the more flag and the
273 outer loop will repeat.
274 <br><br>
275 This function can be used to clear a priority inversion between the normal
276 workloop thread and multimedia's real time threads. The problem is that
277 the interrupt action routine is often held off by high priority threads.
278 So if they want to get their data now they will have to call us and ask if
279 any data is available. The multi-media user client will arrange for this
280 function to be called, which causes any pending interrupts to be processed
281 and the completion routines called. By the time the function returns all
282 outstanding work will have been completed at the real time threads
283 priority.
284
285 @result Return false if the work loop is shutting down, true otherwise.
286 */
287 OSMetaClassDeclareReservedUsed(IOWorkLoop, 1);
288 virtual bool runEventSources();
289 #endif
290
291 protected:
292
293 #if (defined(__ppc__) && defined(KPI_10_4_0_PPC_COMPAT))
294 OSMetaClassDeclareReservedUnused(IOWorkLoop, 1);
295 #endif
296 OSMetaClassDeclareReservedUnused(IOWorkLoop, 2);
297 OSMetaClassDeclareReservedUnused(IOWorkLoop, 3);
298 OSMetaClassDeclareReservedUnused(IOWorkLoop, 4);
299 OSMetaClassDeclareReservedUnused(IOWorkLoop, 5);
300 OSMetaClassDeclareReservedUnused(IOWorkLoop, 6);
301 OSMetaClassDeclareReservedUnused(IOWorkLoop, 7);
302 };
303
304 #endif /* !__IOKIT_IOWORKLOOP_H */