]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
6d2010ae | 2 | * Copyright (c) 1998-2000, 2009 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
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 | #ifndef _IOKIT_IOEVENTSOURCE_H | |
37 | #define _IOKIT_IOEVENTSOURCE_H | |
38 | ||
39 | #include <sys/cdefs.h> | |
40 | ||
41 | #include <libkern/c++/OSObject.h> | |
42 | ||
43 | #include <IOKit/IOLib.h> | |
44 | #include <IOKit/system.h> | |
45 | #include <IOKit/IOWorkLoop.h> | |
46 | ||
6d2010ae A |
47 | #if IOKITSTATS |
48 | #include <IOKit/IOStatisticsPrivate.h> | |
49 | #endif | |
1c79356b A |
50 | |
51 | __BEGIN_DECLS | |
52 | #include <mach/clock_types.h> | |
53 | #include <kern/clock.h> | |
54 | __END_DECLS | |
55 | ||
56 | /*! | |
57 | @class IOEventSource : public OSObject | |
58 | @abstract Abstract class for all work-loop event sources. | |
59 | @discussion The IOEventSource declares the abstract super class that all | |
60 | event sources must inherit from if an IOWorkLoop is to receive events from them. | |
61 | <br><br> | |
62 | An event source can represent any event that should cause the work-loop of a | |
63 | device to wake up and perform work. Two examples of event sources are the | |
64 | IOInterruptEventSource which delivers interrupt notifications and IOCommandGate | |
65 | which delivers command requests. | |
66 | <br><br> | |
67 | A kernel module can always use the work-loop model for serialising access to | |
68 | anything at all. The IOEventSource is used for communicating events to the | |
69 | work-loop, and the chain of event sources should be used to walk the possible | |
70 | event sources and demultipex them. Note a particular instance of an event | |
71 | source may only be a member of 1 linked list chain. If you need to move it | |
72 | between chains than make sure it is removed from the original chain before | |
73 | attempting to move it. | |
74 | <br><br> | |
6d2010ae | 75 | The IOEventSource makes no attempt to maintain the consistency of its internal data across multi-threading. It is assumed that the user of these basic tools will protect the data that these objects represent in some sort of device wide instance lock. For example the IOWorkLoop maintains the event chain by using an IOCommandGate and thus single threading access to its state. |
1c79356b | 76 | <br><br> |
6d2010ae | 77 | All subclasses of IOEventSource that wish to perform work on the work-loop thread are expected to implement the checkForWork() member function. As of Mac OS X, 10.7 (Darwin 11), checkForWork is no longer pure virtual, and should not be overridden if there is no work to be done. |
1c79356b A |
78 | |
79 | <br><br> | |
6d2010ae | 80 | checkForWork() is the key method in this class. It is called by some work-loop when convienient and is expected to evaluate its internal state and determine if an event has occurred since the last call. In the case of an event having occurred then the instance defined target(owner)/action will be called. The action is stored as an ordinary C function pointer but the first parameter is always the owner. This means that a C++ member function can be used as an action function though this depends on the ABI. |
1c79356b A |
81 | <br><br> |
82 | Although the eventChainNext variable contains a reference to the next event source in the chain this reference is not retained. The list 'owner' i.e. the client that creates the event, not the work-loop, is expected to retain the source. | |
83 | */ | |
84 | class IOEventSource : public OSObject | |
85 | { | |
86 | OSDeclareAbstractStructors(IOEventSource) | |
87 | friend class IOWorkLoop; | |
6d2010ae A |
88 | #if IOKITSTATS |
89 | friend class IOStatistics; | |
90 | #endif | |
1c79356b A |
91 | |
92 | public: | |
93 | /*! | |
94 | @typedef Action | |
95 | @discussion Placeholder type for C++ function overloading discrimination. | |
96 | As the all event sources require an action and it has to be stored somewhere | |
97 | and be of some type, this is that type. | |
98 | @param owner | |
99 | Target of the function, can be used as a refcon. The owner is set | |
100 | during initialisation. Note if a C++ function was specified this parameter | |
101 | is implicitly the first paramter in the target member function's parameter list. | |
102 | */ | |
103 | typedef void (*Action)(OSObject *owner, ...); | |
104 | ||
105 | /*! @defined IOEventSourceAction | |
106 | @discussion Backward compatibilty define for the old non-class scoped type definition. See $link IOEventSource::Action */ | |
107 | #define IOEventSourceAction IOEventSource::Action | |
108 | ||
109 | protected: | |
110 | /*! @var eventChainNext | |
111 | The next event source in the event chain. nil at end of chain. */ | |
112 | IOEventSource *eventChainNext; | |
113 | ||
114 | /*! @var owner The owner object called when an event has been delivered. */ | |
115 | OSObject *owner; | |
116 | ||
117 | /*! @var action | |
118 | The action method called when an event has been delivered */ | |
119 | Action action; | |
120 | ||
121 | /*! @var enabled | |
122 | Is this event source enabled to deliver requests to the work-loop. */ | |
123 | bool enabled; | |
124 | ||
5ba3f43e A |
125 | #if XNU_KERNEL_PRIVATE |
126 | ||
127 | enum | |
128 | { | |
129 | kPassive = 0x0001, | |
130 | kActive = 0x0002, | |
131 | }; | |
132 | uint8_t eventSourceReserved1[1]; | |
133 | uint16_t flags; | |
134 | #if __LP64__ | |
135 | uint8_t eventSourceReserved2[4]; | |
136 | #endif /* __LP64__ */ | |
137 | ||
138 | #endif /* XNU_KERNEL_PRIVATE */ | |
139 | ||
1c79356b A |
140 | /*! @var workLoop What is the work-loop for this event source. */ |
141 | IOWorkLoop *workLoop; | |
142 | ||
143 | /*! @var refcon What ever the client wants to do, see $link setRefcon. */ | |
144 | void *refcon; | |
145 | ||
146 | /*! @struct ExpansionData | |
147 | @discussion This structure will be used to expand the capablilties of the IOEventSource in the future. | |
148 | */ | |
6d2010ae A |
149 | struct ExpansionData { |
150 | #if IOKITSTATS | |
151 | struct IOEventSourceCounter *counter; | |
152 | #else | |
153 | void *iokitstatsReserved; | |
154 | #endif | |
155 | }; | |
1c79356b A |
156 | |
157 | /*! @var reserved | |
158 | Reserved for future use. (Internal use only) */ | |
159 | ExpansionData *reserved; | |
160 | ||
161 | /*! @function init | |
162 | @abstract Primary initialiser for the IOEventSource class. | |
163 | @param owner | |
164 | Owner of this instance of an event source. Used as the first parameter | |
5ba3f43e | 165 | of the action callout. Owner must be an OSObject. |
1c79356b A |
166 | @param action |
167 | Pointer to C call out function. Action is a pointer to a C function | |
168 | that gets called when this event source has outstanding work. It will usually | |
169 | be called by the checkForWork member function. The first parameter of the | |
170 | action call out will always be the owner, this allows C++ member functions to | |
171 | be used as actions. Defaults to 0. | |
172 | @result true if the inherited classes and this instance initialise | |
173 | successfully. | |
174 | */ | |
175 | virtual bool init(OSObject *owner, IOEventSource::Action action = 0); | |
176 | ||
3e170ce0 | 177 | virtual void free( void ) APPLE_KEXT_OVERRIDE; |
6d2010ae | 178 | |
1c79356b | 179 | /*! @function checkForWork |
6d2010ae | 180 | @abstract Virtual member function used by IOWorkLoop for work |
1c79356b A |
181 | scheduling. |
182 | @discussion This function will be called to request a subclass to check | |
6d2010ae A |
183 | its internal state for any work to do and then to call out the owner/action. |
184 | If this event source never performs any work (e.g. IOCommandGate), this | |
185 | method should not be overridden. NOTE: This method is no longer declared pure | |
186 | virtual. A default implementation is provided in IOEventSource. | |
1c79356b A |
187 | @result Return true if this function needs to be called again before all its outstanding events have been processed. |
188 | */ | |
6d2010ae | 189 | virtual bool checkForWork(); |
1c79356b A |
190 | |
191 | /*! @function setWorkLoop | |
192 | @abstract Set'ter for $link workLoop variable. | |
193 | @param workLoop | |
194 | Target work-loop of this event source instance. A subclass of | |
195 | IOWorkLoop that at least reacts to signalWorkAvailable() and onThread functions. | |
196 | */ | |
197 | virtual void setWorkLoop(IOWorkLoop *workLoop); | |
198 | ||
199 | /*! @function setNext | |
200 | @abstract Set'ter for $link eventChainNext variable. | |
201 | @param next | |
202 | Pointer to another IOEventSource instance. | |
203 | */ | |
204 | virtual void setNext(IOEventSource *next); | |
205 | ||
206 | /*! @function getNext | |
207 | @abstract Get'ter for $link eventChainNext variable. | |
208 | @result value of eventChainNext. | |
209 | */ | |
210 | virtual IOEventSource *getNext() const; | |
211 | ||
212 | ||
213 | protected: | |
214 | // Methods to access the IOWorkLoop exported fields | |
b0d623f7 A |
215 | void signalWorkAvailable(); |
216 | void openGate(); | |
217 | void closeGate(); | |
218 | bool tryCloseGate(); | |
219 | int sleepGate(void *event, UInt32 type); | |
220 | int sleepGate(void *event, AbsoluteTime deadline, UInt32 type); | |
221 | void wakeupGate(void *event, bool oneThread); | |
1c79356b A |
222 | |
223 | public: | |
224 | /*! @function setAction | |
225 | @abstract Set'ter for $link action variable. | |
226 | @param action Pointer to a C function of type IOEventSource::Action. */ | |
227 | virtual void setAction(IOEventSource::Action action); | |
228 | ||
229 | /*! @function getAction | |
230 | @abstract Get'ter for $link action variable. | |
231 | @result value of action. */ | |
232 | virtual IOEventSource::Action getAction() const; | |
233 | ||
234 | /*! @function enable | |
235 | @abstract Enable event source. | |
236 | @discussion A subclass implementation is expected to respect the enabled | |
237 | state when checkForWork is called. Calling this function will cause the | |
238 | work-loop to be signalled so that a checkForWork is performed. */ | |
239 | virtual void enable(); | |
240 | ||
241 | /*! @function disable | |
242 | @abstract Disable event source. | |
243 | @discussion A subclass implementation is expected to respect the enabled | |
244 | state when checkForWork is called. */ | |
245 | virtual void disable(); | |
246 | ||
247 | /*! @function isEnabled | |
248 | @abstract Get'ter for $link enable variable. | |
249 | @result true if enabled. */ | |
250 | virtual bool isEnabled() const; | |
251 | ||
252 | /*! @function getWorkLoop | |
253 | @abstract Get'ter for $link workLoop variable. | |
254 | @result value of workLoop. */ | |
255 | virtual IOWorkLoop *getWorkLoop() const; | |
256 | ||
257 | /*! @function onThread | |
258 | @abstract Convenience function for workLoop->onThread. | |
259 | @result true if called on the work-loop thread. | |
260 | */ | |
261 | virtual bool onThread() const; | |
262 | ||
263 | private: | |
264 | OSMetaClassDeclareReservedUnused(IOEventSource, 0); | |
265 | OSMetaClassDeclareReservedUnused(IOEventSource, 1); | |
266 | OSMetaClassDeclareReservedUnused(IOEventSource, 2); | |
267 | OSMetaClassDeclareReservedUnused(IOEventSource, 3); | |
268 | OSMetaClassDeclareReservedUnused(IOEventSource, 4); | |
269 | OSMetaClassDeclareReservedUnused(IOEventSource, 5); | |
270 | OSMetaClassDeclareReservedUnused(IOEventSource, 6); | |
271 | OSMetaClassDeclareReservedUnused(IOEventSource, 7); | |
272 | }; | |
273 | ||
274 | #endif /* !_IOKIT_IOEVENTSOURCE_H */ |