]> git.saurik.com Git - apple/xnu.git/blame - iokit/IOKit/IOWorkLoop.h
xnu-792.6.22.tar.gz
[apple/xnu.git] / iokit / IOKit / IOWorkLoop.h
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
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.
1c79356b 11 *
e5568f75
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
24HISTORY
25 1998-7-13 Godfrey van der Linden(gvdl)
26 Created.
27 1998-10-30 Godfrey van der Linden(gvdl)
28 Converted to C++
29*/
30
31#ifndef __IOKIT_IOWORKLOOP_H
32#define __IOKIT_IOWORKLOOP_H
33
34#include <libkern/c++/OSObject.h>
35#include <IOKit/IOReturn.h>
36#include <IOKit/IOLib.h>
37#include <IOKit/IOLocks.h>
38
39#include <IOKit/system.h>
40
41class IOEventSource;
91447636 42class IOTimerEventSource;
1c79356b
A
43class IOCommandGate;
44
91447636
A
45/*! @class IOWorkLoop
46 @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.
1c79356b 47<br><br>
91447636 48 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.
1c79356b 49<br><br>
91447636
A
50 When an event source is registered with a work loop it is informed of the semaphore to use to wake up the loop.
51*/
1c79356b
A
52class IOWorkLoop : public OSObject
53{
54 OSDeclareDefaultStructors(IOWorkLoop)
55
0b4e3aa0
A
56public:
57/*!
58 @typedef Action
59 @discussion Type and arguments of callout C function that is used when
60a runCommand is executed by a client. Cast to this type when you want a C++
61member function to be used. Note the arg1 - arg3 parameters are straight pass
62through from the runCommand to the action callout.
63 @param target
64 Target of the function, can be used as a refcon. Note if a C++ function
91447636 65was specified, this parameter is implicitly the first parameter in the target
0b4e3aa0
A
66member function's parameter list.
67 @param arg0 Argument to action from run operation.
68 @param arg1 Argument to action from run operation.
69 @param arg2 Argument to action from run operation.
70 @param arg3 Argument to action from run operation.
71*/
72 typedef IOReturn (*Action)(OSObject *target,
73 void *arg0, void *arg1,
74 void *arg2, void *arg3);
75
1c79356b 76private:
1c79356b 77/*! @function threadMainContinuation
91447636
A
78 @abstract Static function that calls the threadMain function.
79*/
80 static void threadMainContinuation(IOWorkLoop *self);
1c79356b
A
81
82protected:
83
84/*! @typedef maintCommandEnum
91447636 85 @discussion Enumeration of commands that _maintCommand can deal with.
1c79356b 86 @constant mAddEvent Used to tag a Remove event source command.
91447636
A
87 @constant mRemoveEvent Used to tag a Remove event source command.
88*/
1c79356b
A
89 typedef enum { mAddEvent, mRemoveEvent } maintCommandEnum;
90
91/*! @var gateLock
91447636
A
92 Mutual exclusion lock that is used by close and open Gate functions.
93*/
1c79356b
A
94 IORecursiveLock *gateLock;
95
91447636
A
96/*! @var eventChain
97 Pointer to first event source in linked list.
98*/
1c79356b
A
99 IOEventSource *eventChain;
100
91447636
A
101/*! @var controlG
102 Internal control gate to maintain event system.
103*/
1c79356b
A
104 IOCommandGate *controlG;
105
106/*! @var workSpinLock
91447636
A
107 The spin lock that is used to guard the 'workToDo' variable.
108*/
1c79356b
A
109 IOSimpleLock *workToDoLock;
110
91447636
A
111/*! @var workThread
112 Work loop thread.
113*/
1c79356b
A
114 IOThread workThread;
115
116/*! @var workToDo
117 Used to to indicate that an interrupt has fired and needs to be processed.
118*/
119 volatile bool workToDo;
120
121/*! @var loopRestart
91447636
A
122 Set if an event chain has been changed and the system has to be rechecked from start. (Internal use only)
123*/
1c79356b
A
124 bool loopRestart;
125
126/*! @struct ExpansionData
127 @discussion This structure will be used to expand the capablilties of the IOWorkLoop in the future.
91447636 128*/
1c79356b
A
129 struct ExpansionData { };
130
131/*! @var reserved
91447636
A
132 Reserved for future use. (Internal use only)
133*/
1c79356b
A
134 ExpansionData *reserved;
135
136/*! @function _maintRequest
91447636
A
137 @abstract Synchronous implementation of addEventSource and removeEventSource functions.
138 @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.
139 @return kIOReturnUnsupported if the command given is not implemented, kIOReturnSuccess otherwise.
140*/
1c79356b
A
141 virtual IOReturn _maintRequest(void *command, void *data, void *, void *);
142
143/*! @function free
91447636 144 @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.
1c79356b 145<br><br>
91447636
A
146 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.
147*/
1c79356b
A
148 virtual void free();
149
150/*! @function threadMain
91447636
A
151 @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.
152*/
1c79356b
A
153 virtual void threadMain();
154
155public:
156
157/*! @function workLoop
91447636
A
158 @abstract Factory member function to constuct and intialize a work loop.
159 @result Returns a workLoop instance if constructed successfully, 0 otherwise.
160*/
1c79356b
A
161 static IOWorkLoop *workLoop();
162
163/*! @function init
91447636
A
164 @discussion Initializes an instance of the workloop. This method creates and initialses the signaling semaphore and forks the thread that will continue executing.
165 @result Returns true if initialized successfully, false otherwise.
166*/
1c79356b
A
167 virtual bool init();
168
169/*! @function getThread
91447636
A
170 @abstract Gets the workThread.
171 @result Returns workThread.
172*/
1c79356b
A
173 virtual IOThread getThread() const;
174
175/*! @function onThread
176 @abstract Is the current execution context on the work thread?
91447636
A
177 @result Returns true if IOThreadSelf() == workThread.
178*/
1c79356b
A
179 virtual bool onThread() const;
180
181/*! @function inGate
182 @abstract Is the current execution context holding the work-loop's gate?
91447636
A
183 @result Returns true if IOThreadSelf() is gate holder.
184*/
1c79356b
A
185 virtual bool inGate() const;
186
187/*! @function addEventSource
91447636
A
188 @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.
189 @param newEvent Pointer to IOEventSource subclass to add.
190 @result Always returns kIOReturnSuccess.
191*/
1c79356b
A
192 virtual IOReturn addEventSource(IOEventSource *newEvent);
193
194/*! @function removeEventSource
91447636
A
195 @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.
196 @param toRemove Pointer to IOEventSource subclass to remove.
197 @result Returns kIOReturnSuccess if successful, kIOReturnBadArgument if toRemove couldn't be found.
198*/
1c79356b
A
199 virtual IOReturn removeEventSource(IOEventSource *toRemove);
200
201/*! @function enableAllEventSources
91447636
A
202 @abstract Calls enable() in all event sources.
203 @discussion For all event sources in eventChain, call enable() function. See IOEventSource::enable().
204*/
1c79356b
A
205 virtual void enableAllEventSources() const;
206
207/*! @function disableAllEventSources
91447636
A
208 @abstract Calls disable() in all event sources.
209 @discussion For all event sources in eventChain, call disable() function. See IOEventSource::disable().
210*/
1c79356b
A
211 virtual void disableAllEventSources() const;
212
213/*! @function enableAllInterrupts
91447636
A
214 @abstract Calls enable() in all interrupt event sources.
215 @discussion For all event sources (ES) for which IODynamicCast(IOInterruptEventSource, ES) is valid, in eventChain call enable() function. See IOEventSource::enable().
216*/
1c79356b
A
217 virtual void enableAllInterrupts() const;
218
219/*! @function disableAllInterrupts
91447636
A
220 @abstract Calls disable() in all interrupt event sources.
221 @discussion For all event sources (ES) for which IODynamicCast(IOInterruptEventSource, ES) is valid, in eventChain call disable() function. See IOEventSource::disable().
222*/
1c79356b
A
223 virtual void disableAllInterrupts() const;
224
0b4e3aa0 225
1c79356b
A
226protected:
227 // Internal APIs used by event sources to control the thread
228 friend class IOEventSource;
91447636 229 friend class IOTimerEventSource;
1c79356b
A
230 virtual void signalWorkAvailable();
231 virtual void openGate();
232 virtual void closeGate();
233 virtual bool tryCloseGate();
234 virtual int sleepGate(void *event, UInt32 interuptibleType);
235 virtual void wakeupGate(void *event, bool oneThread);
236
0b4e3aa0
A
237public:
238 /* methods available in Mac OS X 10.1 or later */
239
240/*! @function runAction
241 @abstract Single thread a call to an action with the work-loop.
242 @discussion Client function that causes the given action to be called in
91447636
A
243a 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
244execution of client actions, the action is single threaded against all other work-loop event sources.
0b4e3aa0
A
245 @param action Pointer to function to be executed in work-loop context.
246 @param arg0 Parameter for action parameter, defaults to 0.
247 @param arg1 Parameter for action parameter, defaults to 0.
248 @param arg2 Parameter for action parameter, defaults to 0.
249 @param arg3 Parameter for action parameter, defaults to 0.
91447636 250 @result Returns the value of the Action callout.
0b4e3aa0
A
251*/
252 virtual IOReturn runAction(Action action, OSObject *target,
253 void *arg0 = 0, void *arg1 = 0,
254 void *arg2 = 0, void *arg3 = 0);
255
256protected:
257 OSMetaClassDeclareReservedUsed(IOWorkLoop, 0);
258
1c79356b
A
259 OSMetaClassDeclareReservedUnused(IOWorkLoop, 1);
260 OSMetaClassDeclareReservedUnused(IOWorkLoop, 2);
261 OSMetaClassDeclareReservedUnused(IOWorkLoop, 3);
262 OSMetaClassDeclareReservedUnused(IOWorkLoop, 4);
263 OSMetaClassDeclareReservedUnused(IOWorkLoop, 5);
264 OSMetaClassDeclareReservedUnused(IOWorkLoop, 6);
265 OSMetaClassDeclareReservedUnused(IOWorkLoop, 7);
266};
267
268#endif /* !__IOKIT_IOWORKLOOP_H */