1/*
2 * Copyright (c) 1998-2009 Apple 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#ifndef __IOKIT_IOWORKLOOP_H
30#define __IOKIT_IOWORKLOOP_H
31
32#include <libkern/c++/OSObject.h>
33#include <IOKit/IOReturn.h>
34#include <IOKit/IOLib.h>
35#include <IOKit/IOLocks.h>
36
37#include <IOKit/system.h>
38
39#if IOKITSTATS
40#include <IOKit/IOStatisticsPrivate.h>
41#endif
42
43class IOEventSource;
44class IOTimerEventSource;
45class IOCommandGate;
46
47/*! @class IOWorkLoop
48 @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.
49<br><br>
50 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.
51<br><br>
52 When an event source is registered with a work loop it is informed of the semaphore to use to wake up the loop.
53*/
54class IOWorkLoop : public OSObject
55{
56 OSDeclareDefaultStructors(IOWorkLoop)
57
58public:
59/*!
60 @typedef Action
61 @discussion Type and arguments of callout C function that is used when
62a runCommand is executed by a client. Cast to this type when you want a C++
63member function to be used. Note the arg1 - arg3 parameters are straight pass
64through from the runCommand to the action callout.
65 @param target
66 Target of the function, can be used as a refcon. Note if a C++ function
67was specified, this parameter is implicitly the first parameter in the target
68member function's parameter list.
69 @param arg0 Argument to action from run operation.
70 @param arg1 Argument to action from run operation.
71 @param arg2 Argument to action from run operation.
72 @param arg3 Argument to action from run operation.
73*/
74 typedef IOReturn (*Action)(OSObject *target,
75 void *arg0, void *arg1,
76 void *arg2, void *arg3);
77
78#ifdef __BLOCKS__
79 typedef IOReturn (^ActionBlock)();
80#endif /* __BLOCKS__ */
81
82 enum {
83 kPreciousStack = 0x00000001,
84 kTimeLockPanics = 0x00000002,
85 };
86
87private:
88/*! @function threadMainContinuation
89 @abstract Static function that calls the threadMain function.
90*/
91 static void threadMainContinuation(IOWorkLoop *self);
92
93/*! @function eventSourcePerformsWork
94 @abstract Checks if the event source passed in overrides checkForWork() to perform any work.
95IOWorkLoop uses this to determine if the event source should be polled in runEventSources() or not.
96 @param inEventSource The event source to check.
97*/
98 bool eventSourcePerformsWork(IOEventSource *inEventSource);
99
100protected:
101
102/*! @typedef maintCommandEnum
103 @discussion Enumeration of commands that _maintCommand can deal with.
104 @constant mAddEvent Used to tag a Remove event source command.
105 @constant mRemoveEvent Used to tag a Remove event source command.
106*/
107 typedef enum { mAddEvent, mRemoveEvent } maintCommandEnum;
108
109/*! @var gateLock
110 Mutual exclusion lock that is used by close and open Gate functions.
111 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.
112*/
113 IORecursiveLock *gateLock;
114
115/*! @var eventChain
116 Pointer to first event source in linked list.
117*/
118 IOEventSource *eventChain;
119
120/*! @var controlG
121 Internal control gate to maintain event system.
122*/
123 IOCommandGate *controlG;
124
125/*! @var workToDoLock
126 The spin lock that is used to guard the 'workToDo' variable.
127*/
128 IOSimpleLock *workToDoLock;
129
130/*! @var workThread
131 Work loop thread.
132*/
133 IOThread workThread;
134
135/*! @var workToDo
136 Used to to indicate that an interrupt has fired and needs to be processed.
137*/
138 volatile bool workToDo;
139
140/*! @var loopRestart
141 Set if an event chain has been changed and the system has to be rechecked from start. (Internal use only)
142*/
143 bool loopRestart;
144
145/*! @struct ExpansionData
146 @discussion This structure will be used to expand the capablilties of the IOWorkLoop in the future.
147*/
148 struct ExpansionData {
149 IOOptionBits options;
150 IOEventSource *passiveEventChain;
151#if IOKITSTATS
152 struct IOWorkLoopCounter *counter;
153#else
154 void *iokitstatsReserved;
155#endif
156 uint64_t lockInterval;
157 uint64_t lockTime;
158 };
159
160/*! @var reserved
161 Reserved for future use. (Internal use only)
162*/
163 ExpansionData *reserved;
164
165/*! @function _maintRequest
166 @abstract Synchronous implementation of addEventSource and removeEventSource functions.
167 @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.
168 @return kIOReturnUnsupported if the command given is not implemented, kIOReturnSuccess otherwise.
169*/
170 virtual IOReturn _maintRequest(void *command, void *data, void *, void *);
171
172/*! @function free
173 @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.
174<br><br>
175 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.
176*/
177 virtual void free() APPLE_KEXT_OVERRIDE;
178
179/*! @function threadMain
180 @discussion Work loop threads main function. This function consists of 3
181 loops: the outermost loop is the semaphore clear and wait loop, the middle
182 loop terminates when there is no more work, and the inside loop walks the
183 event list calling the checkForWork method in each event source. If an
184 event source has more work to do, it can set the more flag and the middle
185 loop will repeat. When no more work is outstanding the outermost will
186 sleep until an event is signalled.
187*/
188 virtual void threadMain();
189
190public:
191
192/*! @function workLoop
193 @abstract Factory member function to construct and intialize a work loop.
194 @result Returns a workLoop instance if constructed successfully, 0 otherwise.
195*/
196 static IOWorkLoop *workLoop();
197
198/*! @function workLoopWithOptions(IOOptionBits options)
199 @abstract Factory member function to constuct and intialize a work loop.
200 @param options Options - kPreciousStack to avoid stack deallocation on paging path.
201 @result Returns a workLoop instance if constructed successfully, 0 otherwise.
202*/
203 static IOWorkLoop *workLoopWithOptions(IOOptionBits options);
204
205/*! @function init
206 @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.
207 @result Returns true if initialized successfully, false otherwise.
208*/
209 virtual bool init() APPLE_KEXT_OVERRIDE;
210
211/*! @function getThread
212 @abstract Gets the workThread.
213 @result Returns workThread.
214*/
215 virtual IOThread getThread() const;
216
217/*! @function onThread
218 @abstract Is the current execution context on the work thread?
219 @result Returns true if IOThreadSelf() == workThread.
220*/
221 virtual bool onThread() const;
222
223/*! @function inGate
224 @abstract Is the current execution context holding the work-loop's gate?
225 @result Returns true if IOThreadSelf() is gate holder.
226*/
227 virtual bool inGate() const;
228
229/*! @function addEventSource
230 @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.
231 @param newEvent Pointer to IOEventSource subclass to add.
232 @result Always returns kIOReturnSuccess.
233*/
234 virtual IOReturn addEventSource(IOEventSource *newEvent);
235
236/*! @function removeEventSource
237 @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.
238 @param toRemove Pointer to IOEventSource subclass to remove.
239 @result Returns kIOReturnSuccess if successful, kIOReturnBadArgument if toRemove couldn't be found.
240*/
241 virtual IOReturn removeEventSource(IOEventSource *toRemove);
242
243/*! @function enableAllEventSources
244 @abstract Calls enable() in all event sources.
245 @discussion For all event sources in eventChain, call enable() function. See IOEventSource::enable().
246*/
247 virtual void enableAllEventSources() const;
248
249/*! @function disableAllEventSources
250 @abstract Calls disable() in all event sources.
251 @discussion For all event sources in eventChain, call disable() function. See IOEventSource::disable().
252*/
253 virtual void disableAllEventSources() const;
254
255/*! @function enableAllInterrupts
256 @abstract Calls enable() in all interrupt event sources.
257 @discussion For all event sources (ES) for which OSDynamicCast(IOInterruptEventSource, ES) is valid, in eventChain call enable() function. See IOEventSource::enable().
258*/
259 virtual void enableAllInterrupts() const;
260
261/*! @function disableAllInterrupts
262 @abstract Calls disable() in all interrupt event sources.
263 @discussion For all event sources (ES) for which OSDynamicCast(IOInterruptEventSource, ES) is valid, in eventChain call disable() function. See IOEventSource::disable().
264*/
265 virtual void disableAllInterrupts() const;
266
267
268protected:
269 // Internal APIs used by event sources to control the thread
270 friend class IOEventSource;
271 friend class IOTimerEventSource;
272 friend class IOCommandGate;
273#if IOKITSTATS
274 friend class IOStatistics;
275#endif
276 virtual void signalWorkAvailable();
277 virtual void openGate();
278 virtual void closeGate();
279 virtual bool tryCloseGate();
280 virtual int sleepGate(void *event, UInt32 interuptibleType);
281 virtual void wakeupGate(void *event, bool oneThread);
282
283public:
284 /* methods available in Mac OS X 10.1 or later */
285
286/*! @function runAction
287 @abstract Single thread a call to an action with the work-loop.
288 @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.
289 @param action Pointer to function to be executed in work-loop context.
290 @param arg0 Parameter for action parameter, defaults to 0.
291 @param arg1 Parameter for action parameter, defaults to 0.
292 @param arg2 Parameter for action parameter, defaults to 0.
293 @param arg3 Parameter for action parameter, defaults to 0.
294 @result Returns the value of the Action callout.
295*/
296 virtual IOReturn runAction(Action action, OSObject *target,
297 void *arg0 = 0, void *arg1 = 0,
298 void *arg2 = 0, void *arg3 = 0);
299
300#ifdef __BLOCKS__
301/*! @function runAction
302 @abstract Single thread a call to an action with the work-loop.
303 @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.
304 @param action Block to be executed in work-loop context.
305 @result Returns the result of the action block.
306*/
307 IOReturn runActionBlock(ActionBlock action);
308#endif /* __BLOCKS__ */
309
310/*! @function runEventSources
311 @discussion Consists of the inner 2 loops of the threadMain function(qv).
312 The outer loop terminates when there is no more work, and the inside loop
313 walks the event list calling the checkForWork method in each event source.
314 If an event source has more work to do, it can set the more flag and the
315 outer loop will repeat.
316<br><br>
317 This function can be used to clear a priority inversion between the normal
318 workloop thread and multimedia's real time threads. The problem is that
319 the interrupt action routine is often held off by high priority threads.
320 So if they want to get their data now they will have to call us and ask if
321 any data is available. The multi-media user client will arrange for this
322 function to be called, which causes any pending interrupts to be processed
323 and the completion routines called. By the time the function returns all
324 outstanding work will have been completed at the real time threads
325 priority.
326
327 @result Return false if the work loop is shutting down, true otherwise.
328*/
329 virtual bool runEventSources();
330
331/*! @function setMaximumLockTime
332 @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.
333 @param interval An absolute time interval, eg. created with clock_interval_to_absolutetime_interval().
334 @param options Pass IOWorkLoop::kTimeLockPanics to panic when the time is exceeded, otherwise a log will be generated with OSReportWithBacktrace().
335*/
336 void setMaximumLockTime(uint64_t interval, uint32_t options);
337
338protected:
339 // Internal APIs used by event sources to control the thread
340 virtual int sleepGate(void *event, AbsoluteTime deadline, UInt32 interuptibleType);
341
342#if XNU_KERNEL_PRIVATE
343 void lockTime(void);
344#endif /* XNU_KERNEL_PRIVATE */
345
346protected:
347#if __LP64__
348 OSMetaClassDeclareReservedUnused(IOWorkLoop, 0);
349 OSMetaClassDeclareReservedUnused(IOWorkLoop, 1);
350 OSMetaClassDeclareReservedUnused(IOWorkLoop, 2);
351#else
352 OSMetaClassDeclareReservedUsed(IOWorkLoop, 0);
353 OSMetaClassDeclareReservedUsed(IOWorkLoop, 1);
354 OSMetaClassDeclareReservedUsed(IOWorkLoop, 2);
355#endif
356 OSMetaClassDeclareReservedUnused(IOWorkLoop, 3);
357 OSMetaClassDeclareReservedUnused(IOWorkLoop, 4);
358 OSMetaClassDeclareReservedUnused(IOWorkLoop, 5);
359 OSMetaClassDeclareReservedUnused(IOWorkLoop, 6);
360 OSMetaClassDeclareReservedUnused(IOWorkLoop, 7);
361};
362
363#endif /* !__IOKIT_IOWORKLOOP_H */
364