1/*
2 * Copyright (c) 1998-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. 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.
14 *
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
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
30 *
31 * IOTimerEventSource.h
32 *
33 * HISTORY
34 * 2-Feb-1999 Joe Liu (jliu) created.
35 *
36 */
37
38#ifndef _IOTIMEREVENTSOURCE
39#define _IOTIMEREVENTSOURCE
40
41#include <sys/cdefs.h>
42
43__BEGIN_DECLS
44#include <kern/clock.h>
45__END_DECLS
46
47#include <IOKit/IOEventSource.h>
48#include <IOKit/IOTypes.h>
49
50/*!
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
58 responsiveness.
59 @constant kIOTimerEventSourceOptionsPriorityKernelHigh Importance higher than most kernel
60 threads.
61 @constant kIOTimerEventSourceOptionsPriorityKernel Importance similar to that of normal kernel
62 threads.
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.
70 */
71enum
72{
73 kIOTimerEventSourceOptionsPriorityMask = 0x000000ff,
74 kIOTimerEventSourceOptionsPriorityHigh = 0x00000000,
75 kIOTimerEventSourceOptionsPriorityKernelHigh = 0x00000001,
76 kIOTimerEventSourceOptionsPriorityKernel = 0x00000002,
77 kIOTimerEventSourceOptionsPriorityUser = 0x00000003,
78 kIOTimerEventSourceOptionsPriorityLow = 0x00000004,
79 kIOTimerEventSourceOptionsPriorityWorkLoop = 0x000000ff,
80
81 kIOTimerEventSourceOptionsAllowReenter = 0x00000100,
82
83 kIOTimerEventSourceOptionsDefault = kIOTimerEventSourceOptionsPriorityKernelHigh
84};
85
86#define IOTIMEREVENTSOURCEOPTIONS_DEFINED 1
87
88/*!
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.
93*/
94enum
95{
96 kIOTimeOptionsWithLeeway = 0x00000020,
97 kIOTimeOptionsContinuous = 0x00000100,
98};
99
100/*!
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.
104<br><br>
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.
106<br><br>
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.
108*/
109
110class IOTimerEventSource : public IOEventSource
111{
112 OSDeclareDefaultStructors(IOTimerEventSource)
113
114protected:
115/*! @var calloutEntry thread_call entry for preregistered thread callouts */
116 void *calloutEntry;
117
118/*! @var abstime time to wake up next, see enable. */
119 AbsoluteTime abstime;
120
121/*! @struct ExpansionData
122 @discussion This structure is private to the IOTimerEventSource implementation.
123 */
124 struct ExpansionData
125 {
126 SInt32 calloutGeneration;
127 SInt32 calloutGenerationSignaled;
128 IOWorkLoop * workLoop;
129 };
130
131/*! @var reserved
132 Reserved for future use. (Internal use only) */
133 APPLE_KEXT_WSHADOW_PUSH;
134 ExpansionData *reserved;
135 APPLE_KEXT_WSHADOW_POP;
136
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);
142
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();
147
148/*! @function free
149 @abstract Sub-class implementation of free method, frees calloutEntry */
150 virtual void free() APPLE_KEXT_OVERRIDE;
151
152 virtual void setWorkLoop(IOWorkLoop *workLoop) APPLE_KEXT_OVERRIDE;
153
154public:
155
156/*! @typedef Action
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);
161
162#ifdef __BLOCKS__
163 typedef void (^ActionBlock)(IOTimerEventSource *sender);
164#endif /* __BLOCKS__ */
165
166 static IOTimerEventSource *
167 timerEventSource(OSObject *owner, Action action = 0);
168
169/*! @function timerEventSource
170 @abstract Allocates and returns an initialized timer instance.
171 @param options Mask of kIOTimerEventSourceOptions* options.
172 @param owner The object that that will be passed to the Action callback.
173 @param action 'C' Function pointer for the callout routine of this event source.
174 */
175 static IOTimerEventSource *
176 timerEventSource(uint32_t options, OSObject *owner, Action action = 0);
177
178#ifdef __BLOCKS__
179/*! @function timerEventSource
180 @abstract Allocates and returns an initialized timer instance.
181 @param options Mask of kIOTimerEventSourceOptions* options.
182 @param inOwner The object that that will be passed to the Action callback.
183 @param action Block for the callout routine of this event source.
184 */
185 static IOTimerEventSource *
186 timerEventSource(uint32_t options, OSObject *inOwner, ActionBlock action);
187#endif /* __BLOCKS__ */
188
189#if XNU_KERNEL_PRIVATE
190 __inline__ void invokeAction(IOTimerEventSource::Action action, IOTimerEventSource * ts,
191 OSObject * owner, IOWorkLoop * workLoop);
192#endif /* XNU_KERNEL_PRIVATE */
193
194/*! @function init
195 @abstract Initializes the timer with an owner, and a handler to call when the timeout expires.
196 */
197 virtual bool init(OSObject *owner, Action action = 0);
198
199/*! @function enable
200 @abstract Enables a call to the action.
201 @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. */
202 virtual void enable() APPLE_KEXT_OVERRIDE;
203
204/*! @function disable
205 @abstract Disable a timed callout.
206 @discussion When disable returns the action will not be called until the next time enable(qv) is called. */
207 virtual void disable() APPLE_KEXT_OVERRIDE;
208
209/*! @function checkForWork
210 @abstract Pure Virtual member function used by IOWorkLoop for issuing a client calls.
211 @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.
212 @result Return true if this function needs to be called again before all its outstanding events have been processed. */
213 virtual bool checkForWork() APPLE_KEXT_OVERRIDE;
214
215/*! @function setTimeoutTicks
216 @abstract Setup a callback at after the delay in scheduler ticks. See wakeAtTime(AbsoluteTime).
217 @param ticks Delay from now to wake up, in scheduler ticks, whatever that may be.
218 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
219 virtual IOReturn setTimeoutTicks(UInt32 ticks);
220
221/*! @function setTimeoutMS
222 @abstract Setup a callback at after the delay in milliseconds. See wakeAtTime(AbsoluteTime).
223 @param ms Delay from now to wake up, time in milliseconds.
224 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
225 virtual IOReturn setTimeoutMS(UInt32 ms);
226
227/*! @function setTimeoutUS
228 @abstract Setup a callback at after the delay in microseconds. See wakeAtTime(AbsoluteTime).
229 @param us Delay from now to wake up, time in microseconds.
230 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
231 virtual IOReturn setTimeoutUS(UInt32 us);
232
233/*! @function setTimeout
234 @abstract Setup a callback at after the delay in some unit. See wakeAtTime(AbsoluteTime).
235 @param interval Delay from now to wake up in some defined unit.
236 @param scale_factor Define the unit of interval, default to nanoseconds.
237 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
238 virtual IOReturn setTimeout(UInt32 interval,
239 UInt32 scale_factor = kNanosecondScale);
240
241#if !defined(__LP64__)
242 virtual IOReturn setTimeout(mach_timespec_t interval)
243 APPLE_KEXT_DEPRECATED;
244#endif
245
246/*! @function setTimeout
247 @abstract Setup a callback at after the delay in decrementer ticks. See wakeAtTime(AbsoluteTime).
248 @param interval Delay from now to wake up in decrementer ticks.
249 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
250 virtual IOReturn setTimeout(AbsoluteTime interval);
251
252/*! @function wakeAtTimeTicks
253 @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime).
254 @param ticks Time to wake up in scheduler quantums, whatever that is?
255 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
256 virtual IOReturn wakeAtTimeTicks(UInt32 ticks);
257
258/*! @function wakeAtTimeMS
259 @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime).
260 @param ms Time to wake up in milliseconds.
261 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
262 virtual IOReturn wakeAtTimeMS(UInt32 ms);
263
264/*! @function wakeAtTimeUS
265 @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime).
266 @param us Time to wake up in microseconds.
267 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
268 virtual IOReturn wakeAtTimeUS(UInt32 us);
269
270/*! @function wakeAtTime
271 @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime).
272 @param abstime Time to wake up in some unit.
273 @param scale_factor Define the unit of abstime, default to nanoseconds.
274 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
275 virtual IOReturn wakeAtTime(UInt32 abstime,
276 UInt32 scale_factor = kNanosecondScale);
277
278#if !defined(__LP64__)
279 virtual IOReturn wakeAtTime(mach_timespec_t abstime)
280 APPLE_KEXT_DEPRECATED;
281#endif
282
283/*! @function wakeAtTime
284 @abstract Setup a callback at this absolute time.
285 @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.
286 @param abstime Absolute Time when to wake up, counted in 'decrementer' units and starts at zero when system boots.
287 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared by init or IOEventSource::setAction (qqv). */
288 virtual IOReturn wakeAtTime(AbsoluteTime abstime);
289
290/*! @function cancelTimeout
291 @abstract Disable any outstanding calls to this event source.
292 @discussion Clear down any oustanding calls. By the time this function completes it is guaranteed that the action will not be called again. */
293 virtual void cancelTimeout();
294
295/*! @function init
296 @abstract Initializes the timer with an owner, and a handler to call when the timeout expires.
297 */
298 virtual bool init(uint32_t options, OSObject *inOwner, Action inAction);
299
300/*! @function setTimeout
301 @abstract Setup a callback at after the delay in decrementer ticks. See wakeAtTime(AbsoluteTime).
302 @param options see kIOTimeOptionsWithLeeway and kIOTimeOptionsContinuous
303 @param interval Delay from now to wake up in decrementer ticks.
304 @param leeway Allowable leeway to wake time, if the kIOTimeOptionsWithLeeway option is set
305 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
306 virtual IOReturn setTimeout(uint32_t options, AbsoluteTime interval, AbsoluteTime leeway);
307
308/*! @function wakeAtTime
309 @abstract Setup a callback at this absolute time.
310 @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.
311 @param options see kIOTimeOptionsWithLeeway and kIOTimeOptionsContinuous
312 @param abstime Absolute Time when to wake up, counted in 'decrementer' units and starts at zero when system boots.
313 @param leeway Allowable leeway to wake time, if the kIOTimeOptionsWithLeeway option is set
314 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared by init or IOEventSource::setAction (qqv). */
315 virtual IOReturn wakeAtTime(uint32_t options, AbsoluteTime abstime, AbsoluteTime leeway);
316
317private:
318 static void timeoutAndRelease(void *self, void *c);
319 static void timeoutSignaled(void *self, void *c);
320
321private:
322 OSMetaClassDeclareReservedUsed(IOTimerEventSource, 0);
323 OSMetaClassDeclareReservedUsed(IOTimerEventSource, 1);
324 OSMetaClassDeclareReservedUsed(IOTimerEventSource, 2);
325 OSMetaClassDeclareReservedUnused(IOTimerEventSource, 3);
326 OSMetaClassDeclareReservedUnused(IOTimerEventSource, 4);
327 OSMetaClassDeclareReservedUnused(IOTimerEventSource, 5);
328 OSMetaClassDeclareReservedUnused(IOTimerEventSource, 6);
329 OSMetaClassDeclareReservedUnused(IOTimerEventSource, 7);
330};
331
332#endif /* !_IOTIMEREVENTSOURCE */
333