]> git.saurik.com Git - apple/xnu.git/blame - iokit/IOKit/IOWorkLoop.h
xnu-517.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 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
43866e37
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
1c79356b
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/*
26Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
27HISTORY
28 1998-7-13 Godfrey van der Linden(gvdl)
29 Created.
30 1998-10-30 Godfrey van der Linden(gvdl)
31 Converted to C++
32*/
33
34#ifndef __IOKIT_IOWORKLOOP_H
35#define __IOKIT_IOWORKLOOP_H
36
37#include <libkern/c++/OSObject.h>
38#include <IOKit/IOReturn.h>
39#include <IOKit/IOLib.h>
40#include <IOKit/IOLocks.h>
41
42#include <IOKit/system.h>
43
44class IOEventSource;
45class IOCommandGate;
46
47/*! @class IOWorkLoop : public OSObject
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 an device driver sucessfully starts, See $link IOService::start it is expected to create the event sources it will need to receive events from. Then a work loop is initialised 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 their registered owner that the event has occured. After each event has been walked and they indicate that another loop isn't required by the 'more' flag being 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.*/
53class IOWorkLoop : public OSObject
54{
55 OSDeclareDefaultStructors(IOWorkLoop)
56
0b4e3aa0
A
57public:
58/*!
59 @typedef Action
60 @discussion Type and arguments of callout C function that is used when
61a runCommand is executed by a client. Cast to this type when you want a C++
62member function to be used. Note the arg1 - arg3 parameters are straight pass
63through from the runCommand to the action callout.
64 @param target
65 Target of the function, can be used as a refcon. Note if a C++ function
66was specified this parameter is implicitly the first paramter in the target
67member function's parameter list.
68 @param arg0 Argument to action from run operation.
69 @param arg1 Argument to action from run operation.
70 @param arg2 Argument to action from run operation.
71 @param arg3 Argument to action from run operation.
72*/
73 typedef IOReturn (*Action)(OSObject *target,
74 void *arg0, void *arg1,
75 void *arg2, void *arg3);
76
1c79356b
A
77private:
78/*! @function launchThreadMain
79 @abstract Static function that setup thread state and calls the continuation function, $link threadMainContinuation */
80 static void launchThreadMain(void *self);
81
82/*! @function threadMainContinuation
83 @abstract Static function that calls the $link threadMain function. */
84 static void threadMainContinuation();
85
86protected:
87
88/*! @typedef maintCommandEnum
89 @discussion Enumeration of commands that $link _maintCommand can deal with.
90 @enum
91 @constant mAddEvent Used to tag a Remove event source command.
92 @constant mRemoveEvent Used to tag a Remove event source command. */
93 typedef enum { mAddEvent, mRemoveEvent } maintCommandEnum;
94
95/*! @var gateLock
96 Mutual exlusion lock that used by close and open Gate functions. */
97 IORecursiveLock *gateLock;
98
99/*! @var eventChain Pointer to first Event Source in linked list. */
100 IOEventSource *eventChain;
101
102/*! @var controlG Internal control gate to maintain event system. */
103 IOCommandGate *controlG;
104
105/*! @var workSpinLock
106 The spin lock that is used to guard the 'workToDo' variable. */
107 IOSimpleLock *workToDoLock;
108
109/*! @var workThread Work loop thread. */
110 IOThread workThread;
111
112/*! @var workToDo
113 Used to to indicate that an interrupt has fired and needs to be processed.
114*/
115 volatile bool workToDo;
116
117/*! @var loopRestart
118 If event chain has been changed and the system has to be rechecked from start this flag is set. (Internal use only) */
119 bool loopRestart;
120
121/*! @struct ExpansionData
122 @discussion This structure will be used to expand the capablilties of the IOWorkLoop in the future.
123 */
124 struct ExpansionData { };
125
126/*! @var reserved
127 Reserved for future use. (Internal use only) */
128 ExpansionData *reserved;
129
130/*! @function _maintRequest
131 @abstract Synchrounous implementation of $link addEventSource & $link removeEventSource functions. */
132 virtual IOReturn _maintRequest(void *command, void *data, void *, void *);
133
134/*! @function free
135 @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 succefully terminated. Each event source in the chain will be released and the working semaphore will be destroyed.
136<br><br>
137 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 awoken with a KERN_INTERUPTED status. */
138 virtual void free();
139
140/*! @function threadMain
141 @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 $link checkForWork method in each event source. If an event source has more work to do then it can set the more flag and the middle loop will repeat. When no more work is outstanding the outermost will sleep until and event is signaled 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. */
142 virtual void threadMain();
143
144public:
145
146/*! @function workLoop
147 @abstract Factory member function to constuct and intialise a work loop.
148 @result workLoop instance if constructed successfully, 0 otherwise. */
149 static IOWorkLoop *workLoop();
150
151/*! @function init
152 @description
153 Initialises an instance of the workloop. This method creates and initialses the signaling semaphore and forks the thread that will continue executing.
154 @result true if initialised successfully, false otherwise. */
155 virtual bool init();
156
157/*! @function getThread
158 @abstract Get'ter for $link workThread.
159 @result Returns workThread */
160 virtual IOThread getThread() const;
161
162/*! @function onThread
163 @abstract Is the current execution context on the work thread?
164 @result Returns true if IOThreadSelf() == workThread. */
165 virtual bool onThread() const;
166
167/*! @function inGate
168 @abstract Is the current execution context holding the work-loop's gate?
169 @result Returns true if IOThreadSelf() is gate holder. */
170 virtual bool inGate() const;
171
172/*! @function addEventSource
173 @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 it's loop and check all outstanding events. The event source is retained by the work loop
174 @param newEvent Pointer to $link IOEventSource subclass to add.
175 @result Always returns kIOReturnSuccess. */
176 virtual IOReturn addEventSource(IOEventSource *newEvent);
177
178/*! @function removeEventSource
179 @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 it's loop and check all outstanding events. The event source will be released before return.
180 @param toRemove Pointer to $link IOEventSource subclass to remove.
181 @result kIOReturnSuccess if successful, kIOReturnBadArgument if toRemove couldn't be found. */
182 virtual IOReturn removeEventSource(IOEventSource *toRemove);
183
184/*! @function enableAllEventSources
185 @abstract Call enable() in all event sources
186 @discussion For all event sources in $link eventChain call enable() function. See $link IOEventSource::enable() */
187 virtual void enableAllEventSources() const;
188
189/*! @function disableAllEventSources
190 @abstract Call disable() in all event sources
191 @discussion For all event sources in $link eventChain call disable() function. See $link IOEventSource::disable() */
192 virtual void disableAllEventSources() const;
193
194/*! @function enableAllInterrupts
195 @abstract Call enable() in all interrupt event sources
196 @discussion For all event sources, ES, for which IODynamicCast(IOInterruptEventSource, ES) is valid, in $link eventChain call enable() function. See $link IOEventSource::enable() */
197 virtual void enableAllInterrupts() const;
198
199/*! @function disableAllInterrupts
200 @abstract Call disable() in all interrupt event sources
201 @discussion For all event sources, ES, for which IODynamicCast(IOInterruptEventSource, ES) is valid, in $link eventChain call disable() function. See $link IOEventSource::disable() */
202 virtual void disableAllInterrupts() const;
203
0b4e3aa0 204
1c79356b
A
205protected:
206 // Internal APIs used by event sources to control the thread
207 friend class IOEventSource;
208 virtual void signalWorkAvailable();
209 virtual void openGate();
210 virtual void closeGate();
211 virtual bool tryCloseGate();
212 virtual int sleepGate(void *event, UInt32 interuptibleType);
213 virtual void wakeupGate(void *event, bool oneThread);
214
0b4e3aa0
A
215public:
216 /* methods available in Mac OS X 10.1 or later */
217
218/*! @function runAction
219 @abstract Single thread a call to an action with the work-loop.
220 @discussion Client function that causes the given action to be called in
221a single threaded manner. Beware the work-loop's gate is recursive and runAction
222 can cause direct or indirect re-entrancy. When the executing on a
223client's thread runAction will sleep until the work-loop's gate opens for
224execution of client actions, the action is single threaded against all other
225work-loop event sources.
226 @param action Pointer to function to be executed in work-loop context.
227 @param arg0 Parameter for action parameter, defaults to 0.
228 @param arg1 Parameter for action parameter, defaults to 0.
229 @param arg2 Parameter for action parameter, defaults to 0.
230 @param arg3 Parameter for action parameter, defaults to 0.
231 @result return value of the Action callout.
232*/
233 virtual IOReturn runAction(Action action, OSObject *target,
234 void *arg0 = 0, void *arg1 = 0,
235 void *arg2 = 0, void *arg3 = 0);
236
237protected:
238 OSMetaClassDeclareReservedUsed(IOWorkLoop, 0);
239
1c79356b
A
240 OSMetaClassDeclareReservedUnused(IOWorkLoop, 1);
241 OSMetaClassDeclareReservedUnused(IOWorkLoop, 2);
242 OSMetaClassDeclareReservedUnused(IOWorkLoop, 3);
243 OSMetaClassDeclareReservedUnused(IOWorkLoop, 4);
244 OSMetaClassDeclareReservedUnused(IOWorkLoop, 5);
245 OSMetaClassDeclareReservedUnused(IOWorkLoop, 6);
246 OSMetaClassDeclareReservedUnused(IOWorkLoop, 7);
247};
248
249#endif /* !__IOKIT_IOWORKLOOP_H */