]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOEventSource.h
c03019283c2b47882e8fc489815bd646878e75a1
[apple/xnu.git] / iokit / IOKit / IOEventSource.h
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
25 HISTORY
26 1998-7-13 Godfrey van der Linden(gvdl)
27 Created.
28 1998-10-30 Godfrey van der Linden(gvdl)
29 Converted to C++
30 */
31 #ifndef _IOKIT_IOEVENTSOURCE_H
32 #define _IOKIT_IOEVENTSOURCE_H
33
34 #include <sys/cdefs.h>
35
36 #include <libkern/c++/OSObject.h>
37
38 #include <IOKit/IOLib.h>
39 #include <IOKit/system.h>
40 #include <IOKit/IOWorkLoop.h>
41
42
43 __BEGIN_DECLS
44 #include <mach/clock_types.h>
45 #include <kern/clock.h>
46 __END_DECLS
47
48 /*!
49 @class IOEventSource : public OSObject
50 @abstract Abstract class for all work-loop event sources.
51 @discussion The IOEventSource declares the abstract super class that all
52 event sources must inherit from if an IOWorkLoop is to receive events from them.
53 <br><br>
54 An event source can represent any event that should cause the work-loop of a
55 device to wake up and perform work. Two examples of event sources are the
56 IOInterruptEventSource which delivers interrupt notifications and IOCommandGate
57 which delivers command requests.
58 <br><br>
59 A kernel module can always use the work-loop model for serialising access to
60 anything at all. The IOEventSource is used for communicating events to the
61 work-loop, and the chain of event sources should be used to walk the possible
62 event sources and demultipex them. Note a particular instance of an event
63 source may only be a member of 1 linked list chain. If you need to move it
64 between chains than make sure it is removed from the original chain before
65 attempting to move it.
66 <br><br>
67 The IOEventSource makes no attempt to maintain the consitency of it's 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 handing off change request to its own thread and thus single threading access to its state.
68 <br><br>
69 All subclasses of the IOEventSource are expected to implement the checkForWork() member function.
70
71 <br><br>
72 checkForWork() is the key method in this class. It is called by some work-loop when convienient and is expected to evaluate it's 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.
73 <br><br>
74 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.
75 */
76 class IOEventSource : public OSObject
77 {
78 OSDeclareAbstractStructors(IOEventSource)
79 friend class IOWorkLoop;
80
81 public:
82 /*!
83 @typedef Action
84 @discussion Placeholder type for C++ function overloading discrimination.
85 As the all event sources require an action and it has to be stored somewhere
86 and be of some type, this is that type.
87 @param owner
88 Target of the function, can be used as a refcon. The owner is set
89 during initialisation. Note if a C++ function was specified this parameter
90 is implicitly the first paramter in the target member function's parameter list.
91 */
92 typedef void (*Action)(OSObject *owner, ...);
93
94 /*! @defined IOEventSourceAction
95 @discussion Backward compatibilty define for the old non-class scoped type definition. See $link IOEventSource::Action */
96 #define IOEventSourceAction IOEventSource::Action
97
98 protected:
99 /*! @var eventChainNext
100 The next event source in the event chain. nil at end of chain. */
101 IOEventSource *eventChainNext;
102
103 /*! @var owner The owner object called when an event has been delivered. */
104 OSObject *owner;
105
106 /*! @var action
107 The action method called when an event has been delivered */
108 Action action;
109
110 /*! @var enabled
111 Is this event source enabled to deliver requests to the work-loop. */
112 bool enabled;
113
114 /*! @var workLoop What is the work-loop for this event source. */
115 IOWorkLoop *workLoop;
116
117 /*! @var refcon What ever the client wants to do, see $link setRefcon. */
118 void *refcon;
119
120 /*! @struct ExpansionData
121 @discussion This structure will be used to expand the capablilties of the IOEventSource in the future.
122 */
123 struct ExpansionData { };
124
125 /*! @var reserved
126 Reserved for future use. (Internal use only) */
127 ExpansionData *reserved;
128
129 /*! @function init
130 @abstract Primary initialiser for the IOEventSource class.
131 @param owner
132 Owner of this instance of an event source. Used as the first parameter
133 of the action callout. Owner will generally be an OSObject it doesn't have to
134 be as no member functions will be called directly in it. It can just be a
135 refcon for a client routine.
136 @param action
137 Pointer to C call out function. Action is a pointer to a C function
138 that gets called when this event source has outstanding work. It will usually
139 be called by the checkForWork member function. The first parameter of the
140 action call out will always be the owner, this allows C++ member functions to
141 be used as actions. Defaults to 0.
142 @result true if the inherited classes and this instance initialise
143 successfully.
144 */
145 virtual bool init(OSObject *owner, IOEventSource::Action action = 0);
146
147 /*! @function checkForWork
148 @abstract Pure Virtual member function used by IOWorkLoop for work
149 scheduling.
150 @discussion This function will be called to request a subclass to check
151 it's internal state for any work to do and then to call out the owner/action.
152 @result Return true if this function needs to be called again before all its outstanding events have been processed.
153 */
154 virtual bool checkForWork() = 0;
155
156 /*! @function setWorkLoop
157 @abstract Set'ter for $link workLoop variable.
158 @param workLoop
159 Target work-loop of this event source instance. A subclass of
160 IOWorkLoop that at least reacts to signalWorkAvailable() and onThread functions.
161 */
162 virtual void setWorkLoop(IOWorkLoop *workLoop);
163
164 /*! @function setNext
165 @abstract Set'ter for $link eventChainNext variable.
166 @param next
167 Pointer to another IOEventSource instance.
168 */
169 virtual void setNext(IOEventSource *next);
170
171 /*! @function getNext
172 @abstract Get'ter for $link eventChainNext variable.
173 @result value of eventChainNext.
174 */
175 virtual IOEventSource *getNext() const;
176
177
178 protected:
179 // Methods to access the IOWorkLoop exported fields
180 /* inline */ void signalWorkAvailable();
181 /* { workLoop->signalWorkAvailable(); }; */
182 /* inline */ void openGate();
183 /* { workLoop->openGate(); }; */
184 /* inline */ void closeGate();
185 /* { workLoop->closeGate(); }; */
186 /* inline */ bool tryCloseGate();
187 /* { return workLoop->tryCloseGate(); }; */
188 /* inline */ int sleepGate(void *event, UInt32 type);
189 /* { return workLoop->sleepGate(event, type); }; */
190 /* inline */ void wakeupGate(void *event, bool oneThread);
191 /* { workLoop->wakeupGate(event, oneThread); }; */
192
193 public:
194 /*! @function setAction
195 @abstract Set'ter for $link action variable.
196 @param action Pointer to a C function of type IOEventSource::Action. */
197 virtual void setAction(IOEventSource::Action action);
198
199 /*! @function getAction
200 @abstract Get'ter for $link action variable.
201 @result value of action. */
202 virtual IOEventSource::Action getAction() const;
203
204 /*! @function enable
205 @abstract Enable event source.
206 @discussion A subclass implementation is expected to respect the enabled
207 state when checkForWork is called. Calling this function will cause the
208 work-loop to be signalled so that a checkForWork is performed. */
209 virtual void enable();
210
211 /*! @function disable
212 @abstract Disable event source.
213 @discussion A subclass implementation is expected to respect the enabled
214 state when checkForWork is called. */
215 virtual void disable();
216
217 /*! @function isEnabled
218 @abstract Get'ter for $link enable variable.
219 @result true if enabled. */
220 virtual bool isEnabled() const;
221
222 /*! @function getWorkLoop
223 @abstract Get'ter for $link workLoop variable.
224 @result value of workLoop. */
225 virtual IOWorkLoop *getWorkLoop() const;
226
227 /*! @function onThread
228 @abstract Convenience function for workLoop->onThread.
229 @result true if called on the work-loop thread.
230 */
231 virtual bool onThread() const;
232
233 private:
234 OSMetaClassDeclareReservedUnused(IOEventSource, 0);
235 OSMetaClassDeclareReservedUnused(IOEventSource, 1);
236 OSMetaClassDeclareReservedUnused(IOEventSource, 2);
237 OSMetaClassDeclareReservedUnused(IOEventSource, 3);
238 OSMetaClassDeclareReservedUnused(IOEventSource, 4);
239 OSMetaClassDeclareReservedUnused(IOEventSource, 5);
240 OSMetaClassDeclareReservedUnused(IOEventSource, 6);
241 OSMetaClassDeclareReservedUnused(IOEventSource, 7);
242 };
243
244 #endif /* !_IOKIT_IOEVENTSOURCE_H */