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