]> git.saurik.com Git - apple/xnu.git/blame - iokit/IOKit/IOTimerEventSource.h
xnu-3789.1.32.tar.gz
[apple/xnu.git] / iokit / IOKit / IOTimerEventSource.h
CommitLineData
1c79356b 1/*
39037602 2 * Copyright (c) 1998-2016 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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 @class IOTimerEventSource : public IOEventSource
52 @abstract Time based event source mechanism.
53 @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.
54<br><br>
55 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.
56<br><br>
57 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.
58*/
59class IOTimerEventSource : public IOEventSource
60{
61 OSDeclareDefaultStructors(IOTimerEventSource)
62
63protected:
64/*! @var calloutEntry thread_call entry for preregistered thread callouts */
65 void *calloutEntry;
66
67/*! @var abstime time to wake up next, see enable. */
68 AbsoluteTime abstime;
69
70/*! @struct ExpansionData
91447636 71 @discussion This structure is private to the IOTimerEventSource implementation.
1c79356b 72 */
91447636
A
73 struct ExpansionData
74 {
75 SInt32 calloutGeneration;
76 IOWorkLoop * workLoop;
77 };
1c79356b
A
78
79/*! @var reserved
80 Reserved for future use. (Internal use only) */
81 ExpansionData *reserved;
82
83/*! @function timeout
84 @abstract Function that routes the call from the OS' timeout mechanism into a work-loop context.
85 @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.
86 @param self This argument will be cast to an IOTimerEventSource. */
87 static void timeout(void *self);
88
89/*! @function setTimeoutFunc
90 @abstract Set's timeout as the function of calloutEntry.
91 @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. */
92 virtual void setTimeoutFunc();
93
94/*! @function free
95 @abstract Sub-class implementation of free method, frees calloutEntry */
3e170ce0 96 virtual void free() APPLE_KEXT_OVERRIDE;
1c79356b 97
3e170ce0 98 virtual void setWorkLoop(IOWorkLoop *workLoop) APPLE_KEXT_OVERRIDE;
91447636 99
1c79356b
A
100public:
101
102/*! @typedef Action
103 @discussion 'C' Function pointer defining the callout routine of this event source.
104 @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.
105 @param sender The object that timed out. */
106 typedef void (*Action)(OSObject *owner, IOTimerEventSource *sender);
107
108/*! @function timerEventSource
109 @abstract Allocates and returns an initialized timer instance.
39037602 110 */
1c79356b
A
111 static IOTimerEventSource *
112 timerEventSource(OSObject *owner, Action action = 0);
113
114/*! @function init
115 @abstract Initializes the timer with an owner, and a handler to call when the timeout expires.
39037602 116 */
1c79356b
A
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. */
3e170ce0 122 virtual void enable() APPLE_KEXT_OVERRIDE;
1c79356b
A
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. */
3e170ce0 127 virtual void disable() APPLE_KEXT_OVERRIDE;
1c79356b
A
128
129
130/*! @function setTimeoutTicks
131 @abstract Setup a callback at after the delay in scheduler ticks. See wakeAtTime(AbsoluteTime).
39037602 132 @param ticks Delay from now to wake up, in scheduler ticks, whatever that may be.
1c79356b
A
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).
39037602 138 @param ms Delay from now to wake up, time in milliseconds.
1c79356b
A
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).
39037602 144 @param us Delay from now to wake up, time in microseconds.
1c79356b
A
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
b0d623f7
A
156#if !defined(__LP64__)
157 virtual IOReturn setTimeout(mach_timespec_t interval)
158 APPLE_KEXT_DEPRECATED;
159#endif
1c79356b
A
160
161/*! @function setTimeout
162 @abstract Setup a callback at after the delay in decrementer ticks. See wakeAtTime(AbsoluteTime).
163 @param interval Delay from now to wake up in decrementer ticks.
164 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
165 virtual IOReturn setTimeout(AbsoluteTime interval);
166
167/*! @function wakeAtTimeTicks
168 @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime).
39037602 169 @param ticks Time to wake up in scheduler quantums, whatever that is?
1c79356b
A
170 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
171 virtual IOReturn wakeAtTimeTicks(UInt32 ticks);
172
173/*! @function wakeAtTimeMS
174 @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime).
39037602 175 @param ms Time to wake up in milliseconds.
1c79356b
A
176 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
177 virtual IOReturn wakeAtTimeMS(UInt32 ms);
178
179/*! @function wakeAtTimeUS
180 @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime).
39037602 181 @param us Time to wake up in microseconds.
1c79356b
A
182 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
183 virtual IOReturn wakeAtTimeUS(UInt32 us);
184
185/*! @function wakeAtTime
186 @abstract Setup a callback at this absolute time. See wakeAtTime(AbsoluteTime).
187 @param abstime Time to wake up in some unit.
188 @param scale_factor Define the unit of abstime, default to nanoseconds.
189 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared. */
190 virtual IOReturn wakeAtTime(UInt32 abstime,
191 UInt32 scale_factor = kNanosecondScale);
192
b0d623f7
A
193#if !defined(__LP64__)
194 virtual IOReturn wakeAtTime(mach_timespec_t abstime)
195 APPLE_KEXT_DEPRECATED;
196#endif
1c79356b
A
197
198/*! @function wakeAtTime
199 @abstract Setup a callback at this absolute time.
200 @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.
201 @param abstime Absolute Time when to wake up, counted in 'decrementer' units and starts at zero when system boots.
202 @result kIOReturnSuccess if everything is fine, kIOReturnNoResources if action hasn't been declared by init or IOEventSource::setAction (qqv). */
203 virtual IOReturn wakeAtTime(AbsoluteTime abstime);
204
205/*! @function cancelTimeout
206 @abstract Disable any outstanding calls to this event source.
207 @discussion Clear down any oustanding calls. By the time this function completes it is guaranteed that the action will not be called again. */
208 virtual void cancelTimeout();
209
91447636
A
210private:
211 static void timeoutAndRelease(void *self, void *wl);
212
1c79356b
A
213private:
214 OSMetaClassDeclareReservedUnused(IOTimerEventSource, 0);
215 OSMetaClassDeclareReservedUnused(IOTimerEventSource, 1);
216 OSMetaClassDeclareReservedUnused(IOTimerEventSource, 2);
217 OSMetaClassDeclareReservedUnused(IOTimerEventSource, 3);
218 OSMetaClassDeclareReservedUnused(IOTimerEventSource, 4);
219 OSMetaClassDeclareReservedUnused(IOTimerEventSource, 5);
220 OSMetaClassDeclareReservedUnused(IOTimerEventSource, 6);
221 OSMetaClassDeclareReservedUnused(IOTimerEventSource, 7);
222};
223
224#endif /* !_IOTIMEREVENTSOURCE */