2 * Copyright (c) 1998-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
31 * IOTimerEventSource.h
34 * 2-Feb-1999 Joe Liu (jliu) created.
38 #ifndef _IOTIMEREVENTSOURCE
39 #define _IOTIMEREVENTSOURCE
41 #include <sys/cdefs.h>
44 #include <kern/clock.h>
47 #include <IOKit/IOEventSource.h>
48 #include <IOKit/IOTypes.h>
51 @enum IOTimerEventSource constructor options
52 @abstract Constants defining behavior of the IOTimerEventSource.
53 @constant kIOTimerEventSourceOptionsPriorityHigh Importance above everything but realtime.
54 Thread calls allocated with this priority execute at extremely high priority,
55 above everything but realtime threads. They are generally executed in serial.
56 Though they may execute concurrently under some circumstances, no fan-out is implied.
57 These work items should do very small amounts of work or risk disrupting system
59 @constant kIOTimerEventSourceOptionsPriorityKernelHigh Importance higher than most kernel
61 @constant kIOTimerEventSourceOptionsPriorityKernel Importance similar to that of normal kernel
63 @constant kIOTimerEventSourceOptionsPriorityUser Importance similar to that of normal user threads.
64 @constant kIOTimerEventSourceOptionsPriorityLow Very low importance.
65 @constant kIOTimerEventSourceOptionsPriorityWorkLoop Run the callout on the thread of the IOWorkLoop
66 the event source has been added to.
67 @constant kIOTimerEventSourceOptionsAllowReenter Allow the callout to be rescheduled and potentially
68 re-entered, if the IOWorkLoop lock has been released (eg. with commandSleep) during its invocation.
69 @constant kIOTimerEventSourceOptionsDefault Recommended default options.
73 kIOTimerEventSourceOptionsPriorityMask
= 0x000000ff,
74 kIOTimerEventSourceOptionsPriorityHigh
= 0x00000000,
75 kIOTimerEventSourceOptionsPriorityKernelHigh
= 0x00000001,
76 kIOTimerEventSourceOptionsPriorityKernel
= 0x00000002,
77 kIOTimerEventSourceOptionsPriorityUser
= 0x00000003,
78 kIOTimerEventSourceOptionsPriorityLow
= 0x00000004,
79 kIOTimerEventSourceOptionsPriorityWorkLoop
= 0x000000ff,
81 kIOTimerEventSourceOptionsAllowReenter
= 0x00000100,
83 kIOTimerEventSourceOptionsDefault
= kIOTimerEventSourceOptionsPriorityKernelHigh
86 #define IOTIMEREVENTSOURCEOPTIONS_DEFINED 1
89 @enum IOTimerEventSource setTimeout/wakeAtTime options
90 @abstract Constants defining behavior of a scheduled call from IOTimerEventSource.
91 @constant kIOTimeOptionsWithLeeway Use the leeway parameter to the call.
92 @constant kIOTimeOptionsContinuous Use mach_continuous_time() to generate the callback.
96 kIOTimeOptionsWithLeeway
= 0x00000020,
97 kIOTimeOptionsContinuous
= 0x00000100,
101 @class IOTimerEventSource : public IOEventSource
102 @abstract Time based event source mechanism.
103 @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.
105 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.
107 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.
110 class IOTimerEventSource
: public IOEventSource
112 OSDeclareDefaultStructors(IOTimerEventSource
)
115 /*! @var calloutEntry thread_call entry for preregistered thread callouts */
118 /*! @var abstime time to wake up next, see enable. */
119 AbsoluteTime abstime
;
121 /*! @struct ExpansionData
122 @discussion This structure is private to the IOTimerEventSource implementation.
126 SInt32 calloutGeneration
;
127 SInt32 calloutGenerationSignaled
;
128 IOWorkLoop
* workLoop
;
132 Reserved for future use. (Internal use only) */
133 APPLE_KEXT_WSHADOW_PUSH
;
134 ExpansionData
*reserved
;
135 APPLE_KEXT_WSHADOW_POP
;
137 /*! @function timeout
138 @abstract Function that routes the call from the OS' timeout mechanism into a work-loop context.
139 @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.
140 @param self This argument will be cast to an IOTimerEventSource. */
141 static void timeout(void *self
);
143 /*! @function setTimeoutFunc
144 @abstract Set's timeout as the function of calloutEntry.
145 @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. */
146 virtual void setTimeoutFunc();
149 @abstract Sub-class implementation of free method, frees calloutEntry */
150 virtual void free() APPLE_KEXT_OVERRIDE
;
152 virtual void setWorkLoop(IOWorkLoop
*workLoop
) APPLE_KEXT_OVERRIDE
;
157 @discussion 'C' Function pointer defining the callout routine of this event source.
158 @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.
159 @param sender The object that timed out. */
160 typedef void (*Action
)(OSObject
*owner
, IOTimerEventSource
*sender
);
162 static IOTimerEventSource
*
163 timerEventSource(OSObject
*owner
, Action action
= 0);
165 /*! @function timerEventSource
166 @abstract Allocates and returns an initialized timer instance.
167 @param options Mask of kIOTimerEventSourceOptions* options.
168 @param owner The object that that will be passed to the Action callback.
169 @param action 'C' Function pointer for the callout routine of this event source.
171 static IOTimerEventSource
*
172 timerEventSource(uint32_t options
, OSObject
*owner
, Action action
= 0);
175 @abstract Initializes the timer with an owner, and a handler to call when the timeout expires.
177 virtual bool init(OSObject
*owner
, Action action
= 0);
180 @abstract Enables a call to the action.
181 @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. */
182 virtual void enable() APPLE_KEXT_OVERRIDE
;
184 /*! @function disable
185 @abstract Disable a timed callout.
186 @discussion When disable returns the action will not be called until the next time enable(qv) is called. */
187 virtual void disable() APPLE_KEXT_OVERRIDE
;
189 /*! @function checkForWork
190 @abstract Pure Virtual member function used by IOWorkLoop for issuing a client calls.
191 @discussion This function called when the work-loop is ready to check for any work to do and then to call out the owner/action.
192 @result Return true if this function needs to be called again before all its outstanding events have been processed. */
193 virtual bool checkForWork() APPLE_KEXT_OVERRIDE
;
195 /*! @function setTimeoutTicks
196 @abstract Setup a callback at after the delay in scheduler ticks. See wakeAtTime(AbsoluteTime).
197 @param ticks Delay from now to wake up, in scheduler ticks, whatever that may be.
198 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
199 virtual IOReturn
setTimeoutTicks(UInt32 ticks
);
201 /*! @function setTimeoutMS
202 @abstract Setup a callback at after the delay in milliseconds. See wakeAtTime(AbsoluteTime).
203 @param ms Delay from now to wake up, time in milliseconds.
204 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
205 virtual IOReturn
setTimeoutMS(UInt32 ms
);
207 /*! @function setTimeoutUS
208 @abstract Setup a callback at after the delay in microseconds. See wakeAtTime(AbsoluteTime).
209 @param us Delay from now to wake up, time in microseconds.
210 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
211 virtual IOReturn
setTimeoutUS(UInt32 us
);
213 /*! @function setTimeout
214 @abstract Setup a callback at after the delay in some unit. See wakeAtTime(AbsoluteTime).
215 @param interval Delay from now to wake up in some defined unit.
216 @param scale_factor Define the unit of interval, default to nanoseconds.
217 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
218 virtual IOReturn
setTimeout(UInt32 interval
,
219 UInt32 scale_factor
= kNanosecondScale
);
221 #if !defined(__LP64__)
222 virtual IOReturn
setTimeout(mach_timespec_t interval
)
223 APPLE_KEXT_DEPRECATED
;
226 /*! @function setTimeout
227 @abstract Setup a callback at after the delay in decrementer ticks. See wakeAtTime(AbsoluteTime).
228 @param interval Delay from now to wake up in decrementer ticks.
229 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
230 virtual IOReturn
setTimeout(AbsoluteTime interval
);
232 /*! @function wakeAtTimeTicks
233 @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime).
234 @param ticks Time to wake up in scheduler quantums, whatever that is?
235 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
236 virtual IOReturn
wakeAtTimeTicks(UInt32 ticks
);
238 /*! @function wakeAtTimeMS
239 @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime).
240 @param ms Time to wake up in milliseconds.
241 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
242 virtual IOReturn
wakeAtTimeMS(UInt32 ms
);
244 /*! @function wakeAtTimeUS
245 @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime).
246 @param us Time to wake up in microseconds.
247 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
248 virtual IOReturn
wakeAtTimeUS(UInt32 us
);
250 /*! @function wakeAtTime
251 @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime).
252 @param abstime Time to wake up in some unit.
253 @param scale_factor Define the unit of abstime, default to nanoseconds.
254 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
255 virtual IOReturn
wakeAtTime(UInt32 abstime
,
256 UInt32 scale_factor
= kNanosecondScale
);
258 #if !defined(__LP64__)
259 virtual IOReturn
wakeAtTime(mach_timespec_t abstime
)
260 APPLE_KEXT_DEPRECATED
;
263 /*! @function wakeAtTime
264 @abstract Setup a callback at this absolute time.
265 @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.
266 @param abstime Absolute Time when to wake up, counted in 'decrementer' units and starts at zero when system boots.
267 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared by init or IOEventSource::setAction (qqv). */
268 virtual IOReturn
wakeAtTime(AbsoluteTime abstime
);
270 /*! @function cancelTimeout
271 @abstract Disable any outstanding calls to this event source.
272 @discussion Clear down any oustanding calls. By the time this function completes it is guaranteed that the action will not be called again. */
273 virtual void cancelTimeout();
276 @abstract Initializes the timer with an owner, and a handler to call when the timeout expires.
278 virtual bool init(uint32_t options
, OSObject
*inOwner
, Action inAction
);
280 /*! @function setTimeout
281 @abstract Setup a callback at after the delay in decrementer ticks. See wakeAtTime(AbsoluteTime).
282 @param options see kIOTimeOptionsWithLeeway and kIOTimeOptionsContinuous
283 @param interval Delay from now to wake up in decrementer ticks.
284 @param leeway Allowable leeway to wake time, if the kIOTimeOptionsWithLeeway option is set
285 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
286 virtual IOReturn
setTimeout(uint32_t options
, AbsoluteTime interval
, AbsoluteTime leeway
);
288 /*! @function wakeAtTime
289 @abstract Setup a callback at this absolute time.
290 @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.
291 @param options see kIOTimeOptionsWithLeeway and kIOTimeOptionsContinuous
292 @param abstime Absolute Time when to wake up, counted in 'decrementer' units and starts at zero when system boots.
293 @param leeway Allowable leeway to wake time, if the kIOTimeOptionsWithLeeway option is set
294 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared by init or IOEventSource::setAction (qqv). */
295 virtual IOReturn
wakeAtTime(uint32_t options
, AbsoluteTime abstime
, AbsoluteTime leeway
);
298 static void timeoutAndRelease(void *self
, void *c
);
299 static void timeoutSignaled(void *self
, void *c
);
302 OSMetaClassDeclareReservedUsed(IOTimerEventSource
, 0);
303 OSMetaClassDeclareReservedUsed(IOTimerEventSource
, 1);
304 OSMetaClassDeclareReservedUsed(IOTimerEventSource
, 2);
305 OSMetaClassDeclareReservedUnused(IOTimerEventSource
, 3);
306 OSMetaClassDeclareReservedUnused(IOTimerEventSource
, 4);
307 OSMetaClassDeclareReservedUnused(IOTimerEventSource
, 5);
308 OSMetaClassDeclareReservedUnused(IOTimerEventSource
, 6);
309 OSMetaClassDeclareReservedUnused(IOTimerEventSource
, 7);
312 #endif /* !_IOTIMEREVENTSOURCE */