]>
Commit | Line | Data |
---|---|---|
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 | /* | |
23 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
24 | * | |
25 | * IOTimerEventSource.h | |
26 | * | |
27 | * HISTORY | |
28 | * 2-Feb-1999 Joe Liu (jliu) created. | |
29 | * | |
30 | */ | |
31 | ||
32 | #ifndef _IOTIMEREVENTSOURCE | |
33 | #define _IOTIMEREVENTSOURCE | |
34 | ||
35 | #include <sys/cdefs.h> | |
36 | ||
37 | __BEGIN_DECLS | |
38 | #include <kern/clock.h> | |
39 | __END_DECLS | |
40 | ||
41 | #include <IOKit/IOEventSource.h> | |
42 | #include <IOKit/IOTypes.h> | |
43 | ||
44 | /*! | |
45 | @class IOTimerEventSource : public IOEventSource | |
46 | @abstract Time based event source mechanism. | |
47 | @discussion An event source that implements a simple timer. A timeout handler is called once the timeout period expires. This timeout handler will be called by the work-loop that this event source is attached to. | |
48 | <br><br> | |
49 | Usually a timer event source will be used to implement a timeout. In general when a driver makes a request it will need to setup a call to keep track of when the I/O doesn't complete. This class is designed to make that somewhat easier. | |
50 | <br><br> | |
51 | Remember the system doesn't guarantee the accuracy of the callout. It is possible that a higher priority thread is running which will delay the execution of the action routine. In fact the thread will be made runable at the exact requested time, within the accuracy of the CPU's decrementer based interrupt, but the scheduler will then control execution. | |
52 | */ | |
53 | class IOTimerEventSource : public IOEventSource | |
54 | { | |
55 | OSDeclareDefaultStructors(IOTimerEventSource) | |
56 | ||
57 | protected: | |
58 | /*! @var calloutEntry thread_call entry for preregistered thread callouts */ | |
59 | void *calloutEntry; | |
60 | ||
61 | /*! @var abstime time to wake up next, see enable. */ | |
62 | AbsoluteTime abstime; | |
63 | ||
64 | /*! @struct ExpansionData | |
91447636 | 65 | @discussion This structure is private to the IOTimerEventSource implementation. |
1c79356b | 66 | */ |
91447636 A |
67 | struct ExpansionData |
68 | { | |
69 | SInt32 calloutGeneration; | |
70 | IOWorkLoop * workLoop; | |
71 | }; | |
1c79356b A |
72 | |
73 | /*! @var reserved | |
74 | Reserved for future use. (Internal use only) */ | |
75 | ExpansionData *reserved; | |
76 | ||
77 | /*! @function timeout | |
78 | @abstract Function that routes the call from the OS' timeout mechanism into a work-loop context. | |
79 | @discussion timeout will normally not be called nor overridden by a subclass. If the event source is enabled then close the work-loop's gate and call the action routine. | |
80 | @param self This argument will be cast to an IOTimerEventSource. */ | |
81 | static void timeout(void *self); | |
82 | ||
83 | /*! @function setTimeoutFunc | |
84 | @abstract Set's timeout as the function of calloutEntry. | |
85 | @discussion IOTimerEventSource is based upon the kern/thread_call.h APIs currently. This function allocates the calloutEntry member variable by using thread_call_allocate(timeout, this). If you need to write your own subclass of IOTimerEventSource you probably should override this method to allocate an entry that points to your own timeout routine. */ | |
86 | virtual void setTimeoutFunc(); | |
87 | ||
88 | /*! @function free | |
89 | @abstract Sub-class implementation of free method, frees calloutEntry */ | |
90 | virtual void free(); | |
91 | ||
92 | /*! @function checkForWork | |
93 | @abstract Have to implement it is mandatory in $link IOEventSource, but IOTimerEventSources don't actually use this work-loop mechanism. */ | |
94 | virtual bool checkForWork(); | |
95 | ||
91447636 A |
96 | virtual void setWorkLoop(IOWorkLoop *workLoop); |
97 | ||
1c79356b A |
98 | public: |
99 | ||
100 | /*! @typedef Action | |
101 | @discussion 'C' Function pointer defining the callout routine of this event source. | |
102 | @param owner Owning target object. Note by a startling coincidence the first parameter in a C callout is currently used to define the target of a C++ member function. | |
103 | @param sender The object that timed out. */ | |
104 | typedef void (*Action)(OSObject *owner, IOTimerEventSource *sender); | |
105 | ||
106 | /*! @function timerEventSource | |
107 | @abstract Allocates and returns an initialized timer instance. | |
108 | @param owner | |
109 | @param action */ | |
110 | static IOTimerEventSource * | |
111 | timerEventSource(OSObject *owner, Action action = 0); | |
112 | ||
113 | /*! @function init | |
114 | @abstract Initializes the timer with an owner, and a handler to call when the timeout expires. | |
115 | @param owner | |
116 | @param action */ | |
117 | virtual bool init(OSObject *owner, Action action = 0); | |
118 | ||
119 | /*! @function enable | |
120 | @abstract Enables a call to the action. | |
121 | @discussion Allows the action function to be called. If the timer event source was disabled while a call was outstanding and the call wasn't cancelled then it will be rescheduled. So a disable/enable pair will disable calls from this event source. */ | |
122 | virtual void enable(); | |
123 | ||
124 | /*! @function disable | |
125 | @abstract Disable a timed callout. | |
126 | @discussion When disable returns the action will not be called until the next time enable(qv) is called. */ | |
127 | virtual void disable(); | |
128 | ||
129 | ||
130 | /*! @function setTimeoutTicks | |
131 | @abstract Setup a callback at after the delay in scheduler ticks. See wakeAtTime(AbsoluteTime). | |
132 | @param interval Delay from now to wake up, in scheduler ticks, whatever that may be. | |
133 | @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */ | |
134 | virtual IOReturn setTimeoutTicks(UInt32 ticks); | |
135 | ||
136 | /*! @function setTimeoutMS | |
137 | @abstract Setup a callback at after the delay in milliseconds. See wakeAtTime(AbsoluteTime). | |
138 | @param interval Delay from now to wake up, time in milliseconds. | |
139 | @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */ | |
140 | virtual IOReturn setTimeoutMS(UInt32 ms); | |
141 | ||
142 | /*! @function setTimeoutUS | |
143 | @abstract Setup a callback at after the delay in microseconds. See wakeAtTime(AbsoluteTime). | |
144 | @param interval Delay from now to wake up, time in microseconds. | |
145 | @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */ | |
146 | virtual IOReturn setTimeoutUS(UInt32 us); | |
147 | ||
148 | /*! @function setTimeout | |
149 | @abstract Setup a callback at after the delay in some unit. See wakeAtTime(AbsoluteTime). | |
150 | @param interval Delay from now to wake up in some defined unit. | |
151 | @param scale_factor Define the unit of interval, default to nanoseconds. | |
152 | @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */ | |
153 | virtual IOReturn setTimeout(UInt32 interval, | |
154 | UInt32 scale_factor = kNanosecondScale); | |
155 | ||
156 | /*! @function setTimeout | |
157 | @abstract Setup a callback at after the delay in decrementer ticks. See wakeAtTime(AbsoluteTime). | |
158 | @param interval Delay from now to wake up. | |
159 | @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */ | |
160 | virtual IOReturn setTimeout(mach_timespec_t interval); | |
161 | ||
162 | /*! @function setTimeout | |
163 | @abstract Setup a callback at after the delay in decrementer ticks. See wakeAtTime(AbsoluteTime). | |
164 | @param interval Delay from now to wake up in decrementer ticks. | |
165 | @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */ | |
166 | virtual IOReturn setTimeout(AbsoluteTime interval); | |
167 | ||
168 | /*! @function wakeAtTimeTicks | |
169 | @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime). | |
170 | @param abstime Time to wake up in scheduler quantums, whatever that is? | |
171 | @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */ | |
172 | virtual IOReturn wakeAtTimeTicks(UInt32 ticks); | |
173 | ||
174 | /*! @function wakeAtTimeMS | |
175 | @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime). | |
176 | @param abstime Time to wake up in milliseconds. | |
177 | @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */ | |
178 | virtual IOReturn wakeAtTimeMS(UInt32 ms); | |
179 | ||
180 | /*! @function wakeAtTimeUS | |
181 | @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime). | |
182 | @param abstime Time to wake up in microseconds. | |
183 | @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */ | |
184 | virtual IOReturn wakeAtTimeUS(UInt32 us); | |
185 | ||
186 | /*! @function wakeAtTime | |
187 | @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime). | |
188 | @param abstime Time to wake up in some unit. | |
189 | @param scale_factor Define the unit of abstime, default to nanoseconds. | |
190 | @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */ | |
191 | virtual IOReturn wakeAtTime(UInt32 abstime, | |
192 | UInt32 scale_factor = kNanosecondScale); | |
193 | ||
194 | /*! @function wakeAtTime | |
195 | @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime). | |
196 | @param abstime mach_timespec_t of the desired callout time. | |
197 | @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */ | |
198 | virtual IOReturn wakeAtTime(mach_timespec_t abstime); | |
199 | ||
200 | /*! @function wakeAtTime | |
201 | @abstract Setup a callback at this absolute time. | |
202 | @discussion Starts the timer, which will expire at abstime. After it expires, the timer will call the 'action' registered in the init() function. This timer is not periodic, a further call is needed to reset and restart the timer after it expires. | |
203 | @param abstime Absolute Time when to wake up, counted in 'decrementer' units and starts at zero when system boots. | |
204 | @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared by init or IOEventSource::setAction (qqv). */ | |
205 | virtual IOReturn wakeAtTime(AbsoluteTime abstime); | |
206 | ||
207 | /*! @function cancelTimeout | |
208 | @abstract Disable any outstanding calls to this event source. | |
209 | @discussion Clear down any oustanding calls. By the time this function completes it is guaranteed that the action will not be called again. */ | |
210 | virtual void cancelTimeout(); | |
211 | ||
91447636 A |
212 | private: |
213 | static void timeoutAndRelease(void *self, void *wl); | |
214 | ||
1c79356b A |
215 | private: |
216 | OSMetaClassDeclareReservedUnused(IOTimerEventSource, 0); | |
217 | OSMetaClassDeclareReservedUnused(IOTimerEventSource, 1); | |
218 | OSMetaClassDeclareReservedUnused(IOTimerEventSource, 2); | |
219 | OSMetaClassDeclareReservedUnused(IOTimerEventSource, 3); | |
220 | OSMetaClassDeclareReservedUnused(IOTimerEventSource, 4); | |
221 | OSMetaClassDeclareReservedUnused(IOTimerEventSource, 5); | |
222 | OSMetaClassDeclareReservedUnused(IOTimerEventSource, 6); | |
223 | OSMetaClassDeclareReservedUnused(IOTimerEventSource, 7); | |
224 | }; | |
225 | ||
226 | #endif /* !_IOTIMEREVENTSOURCE */ |