VirtualBox

source: vbox/trunk/include/iprt/timer.h@ 34435

Last change on this file since 34435 was 32572, checked in by vboxsync, 14 years ago

VMM,SUPDrv,IPRT: More changes for related to the priodic preemption timer. (still disabled)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.4 KB
Line 
1/** @file
2 * IPRT - Timer.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_timer_h
27#define ___iprt_timer_h
28
29
30#include <iprt/cdefs.h>
31#include <iprt/types.h>
32
33
34RT_C_DECLS_BEGIN
35
36/** @defgroup grp_rt_timer RTTimer - Timer
37 *
38 * The IPRT timer API provides a simple abstraction of recurring and one-shot callback timers.
39 *
40 * Because of the great variation in the native APIs and the quality of
41 * the service delivered by those native APIs, the timers are operated
42 * on at best effort basis.
43 *
44 * All the ring-3 implementations are naturally at the mercy of the scheduler,
45 * which means that the callback rate might vary quite a bit and we might skip
46 * ticks. Many systems have a restriction that a process can only have one
47 * timer. IPRT currently makes no efforts at multiplexing timers in those kind
48 * of situations and will simply fail if you try to create more than one timer.
49 *
50 * Things are generally better in ring-0. The implementations will use interrupt
51 * time callbacks wherever available, and if not, resort to a high priority
52 * kernel thread.
53 *
54 * @ingroup grp_rt
55 * @{
56 */
57
58
59/** Timer handle. */
60typedef struct RTTIMER *PRTTIMER;
61
62/**
63 * Timer callback function.
64 *
65 * The context this call is made in varies with different platforms and
66 * kernel / user mode IPRT.
67 *
68 * In kernel mode a timer callback should not waste time, it shouldn't
69 * waste stack and it should be prepared that some APIs might not work
70 * correctly because of weird OS restrictions in this context that we
71 * haven't discovered and avoided yet. Please fix those APIs so they
72 * at least avoid panics and weird behaviour.
73 *
74 * @param pTimer Timer handle.
75 * @param pvUser User argument.
76 * @param iTick The current timer tick. This is always 1 on the first
77 * callback after the timer was started. For omni timers
78 * this will be 1 when a cpu comes back online.
79 */
80typedef DECLCALLBACK(void) FNRTTIMER(PRTTIMER pTimer, void *pvUser, uint64_t iTick);
81/** Pointer to FNRTTIMER() function. */
82typedef FNRTTIMER *PFNRTTIMER;
83
84
85/**
86 * Create a recurring timer.
87 *
88 * @returns iprt status code.
89 * @param ppTimer Where to store the timer handle.
90 * @param uMilliesInterval Milliseconds between the timer ticks.
91 * This is rounded up to the system granularity.
92 * @param pfnTimer Callback function which shall be scheduled for execution
93 * on every timer tick.
94 * @param pvUser User argument for the callback.
95 * @see RTTimerCreateEx, RTTimerStart, RTTimerStop, RTTimerChangeInterval,
96 * RTTimerDestroy, RTTimerGetSystemGranularity
97 */
98RTDECL(int) RTTimerCreate(PRTTIMER *ppTimer, unsigned uMilliesInterval, PFNRTTIMER pfnTimer, void *pvUser);
99
100/**
101 * Create a suspended timer.
102 *
103 * @returns iprt status code.
104 * @retval VERR_NOT_SUPPORTED if an unsupported flag was specfied.
105 * @retval VERR_CPU_NOT_FOUND if the specified CPU
106 *
107 * @param ppTimer Where to store the timer handle.
108 * @param u64NanoInterval The interval between timer ticks specified in nanoseconds if it's
109 * a recurring timer. This is rounded to the fit the system timer granularity.
110 * For one shot timers, pass 0.
111 * @param fFlags Timer flags.
112 * @param pfnTimer Callback function which shall be scheduled for execution
113 * on every timer tick.
114 * @param pvUser User argument for the callback.
115 * @see RTTimerStart, RTTimerStop, RTTimerChangeInterval, RTTimerDestroy,
116 * RTTimerGetSystemGranularity, RTTimerCanDoHighResolution
117 */
118RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser);
119
120/** @name RTTimerCreateEx flags
121 * @{ */
122/** Any CPU is fine. (Must be 0.) */
123#define RTTIMER_FLAGS_CPU_ANY UINT32_C(0)
124/** One specific CPU */
125#define RTTIMER_FLAGS_CPU_SPECIFIC RT_BIT(8)
126/** Omni timer, run on all online CPUs.
127 * @remarks The timer callback isn't necessarily running at the time same time on each CPU. */
128#define RTTIMER_FLAGS_CPU_ALL ( RTTIMER_FLAGS_CPU_MASK | RTTIMER_FLAGS_CPU_SPECIFIC )
129/** CPU mask. */
130#define RTTIMER_FLAGS_CPU_MASK UINT32_C(0xff)
131/** Desire a high resolution timer that works with RTTimerChangeInterval and
132 * isn't subject to RTTimerGetSystemGranularity rounding.
133 * @remarks This is quietly ignored if the feature isn't supported. */
134#define RTTIMER_FLAGS_HIGH_RES RT_BIT(9)
135/** Convert a CPU set index (0-based) to RTTimerCreateEx flags.
136 * This will automatically OR in the RTTIMER_FLAGS_CPU_SPECIFIC flag. */
137#define RTTIMER_FLAGS_CPU(iCpu) ( (iCpu) | RTTIMER_FLAGS_CPU_SPECIFIC )
138/** Macro that validates the flags. */
139#define RTTIMER_FLAGS_ARE_VALID(fFlags) \
140 ( !((fFlags) & ~((fFlags) & RTTIMER_FLAGS_CPU_SPECIFIC ? UINT32_C(0x3ff) : UINT32_C(0x300))) )
141/** @} */
142
143/**
144 * Stops and destroys a running timer.
145 *
146 * @returns iprt status code.
147 * @param pTimer Timer to stop and destroy. NULL is ok.
148 */
149RTDECL(int) RTTimerDestroy(PRTTIMER pTimer);
150
151/**
152 * Stops an active timer.
153 *
154 * @returns IPRT status code.
155 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
156 * @retval VERR_TIMER_ACTIVE if the timer isn't suspended.
157 * @retval VERR_CPU_OFFLINE if the CPU the timer was created to run on is not
158 * online (this include the case where it's not present in the
159 * system).
160 *
161 * @param pTimer The timer to activate.
162 * @param u64First The RTTimeSystemNanoTS() for when the timer should start
163 * firing (relative). If 0 is specified, the timer will
164 * fire ASAP.
165 * @remarks When RTTimerCanDoHighResolution returns true, this API is
166 * callable with preemption disabled in ring-0.
167 * @see RTTimerStop
168 */
169RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First);
170
171/**
172 * Stops an active timer.
173 *
174 * @returns IPRT status code.
175 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
176 * @retval VERR_TIMER_SUSPENDED if the timer isn't active.
177 * @retval VERR_NOT_SUPPORTED if the IPRT implementation doesn't support
178 * stopping a timer.
179 *
180 * @param pTimer The timer to suspend.
181 * @remarks Can be called from the timer callback function to stop it.
182 * @see RTTimerStart
183 */
184RTDECL(int) RTTimerStop(PRTTIMER pTimer);
185
186/**
187 * Changes the interval of a periodic timer.
188 *
189 * If the timer is active, it is implementation dependent whether the change
190 * takes place immediately or after the next tick. To get defined behavior,
191 * stop the timer before calling this API.
192 *
193 * @returns IPRT status code.
194 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
195 * @retval VERR_NOT_SUPPORTED if not supported.
196 *
197 * @param pTimer The timer to activate.
198 * @param u64NanoInterval The interval between timer ticks specified in
199 * nanoseconds. This is rounded to the fit the
200 * system timer granularity.
201 * @remarks Callable from the timer callback. Callable with preemption
202 * disabled in ring-0.
203 */
204RTDECL(int) RTTimerChangeInterval(PRTTIMER pTimer, uint64_t u64NanoInterval);
205
206/**
207 * Gets the (current) timer granularity of the system.
208 *
209 * @returns The timer granularity of the system in nanoseconds.
210 * @see RTTimerRequestSystemGranularity
211 */
212RTDECL(uint32_t) RTTimerGetSystemGranularity(void);
213
214/**
215 * Requests a specific system timer granularity.
216 *
217 * Successfull calls to this API must be coupled with the exact same number of
218 * calls to RTTimerReleaseSystemGranularity() in order to undo any changes made.
219 *
220 *
221 * @returns IPRT status code.
222 * @retval VERR_NOT_SUPPORTED if the requested value isn't supported by the host platform
223 * or if the host platform doesn't support modifying the system timer granularity.
224 * @retval VERR_PERMISSION_DENIED if the caller doesn't have the necessary privilege to
225 * modify the system timer granularity.
226 *
227 * @param u32Request The requested system timer granularity in nanoseconds.
228 * @param pu32Granted Where to store the granted system granularity. This is the value
229 * that should be passed to RTTimerReleaseSystemGranularity(). It
230 * is what RTTimerGetSystemGranularity() would return immediately
231 * after the change was made.
232 *
233 * The value differ from the request in two ways; rounding and
234 * scale. Meaning if your request is for 10.000.000 you might
235 * be granted 10.000.055 or 1.000.000.
236 * @see RTTimerReleaseSystemGranularity, RTTimerGetSystemGranularity
237 */
238RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted);
239
240/**
241 * Releases a system timer granularity grant acquired by RTTimerRequestSystemGranularity().
242 *
243 * @returns IPRT status code.
244 * @retval VERR_NOT_SUPPORTED if the host platform doesn't have any way of modifying
245 * the system timer granularity.
246 * @retval VERR_WRONG_ORDER if nobody call RTTimerRequestSystemGranularity() with the
247 * given grant value.
248 * @param u32Granted The granted system granularity.
249 * @see RTTimerRequestSystemGranularity
250 */
251RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted);
252
253/**
254 * Checks if the system support high resolution timers.
255 *
256 * The kind of support we are checking for is the kind of dynamically
257 * reprogrammable timers employed by recent Solaris and Linux kernels. It also
258 * implies that we can specify microsecond (or even better maybe) intervals
259 * without getting into trouble.
260 *
261 * @returns true if supported, false it not.
262 *
263 * @remarks Returning true also means RTTimerChangeInterval must be implemented
264 * and RTTimerStart be callable with preemption disabled.
265 */
266RTDECL(bool) RTTimerCanDoHighResolution(void);
267
268
269/**
270 * Timer callback function for low res timers.
271 *
272 * This is identfical to FNRTTIMER except for the first parameter, so
273 * see FNRTTIMER for details.
274 *
275 * @param hTimerLR The low resolution timer handle.
276 * @param pvUser User argument.
277 * @param iTick The current timer tick. This is always 1 on the first
278 * callback after the timer was started. Will jump if we've
279 * skipped ticks when lagging behind.
280 */
281typedef DECLCALLBACK(void) FNRTTIMERLR(RTTIMERLR hTimerLR, void *pvUser, uint64_t iTick);
282/** Pointer to FNRTTIMER() function. */
283typedef FNRTTIMERLR *PFNRTTIMERLR;
284
285
286/**
287 * Create a recurring low resolution timer.
288 *
289 * @returns iprt status code.
290 * @param phTimerLR Where to store the timer handle.
291 * @param uMilliesInterval Milliseconds between the timer ticks, at least 100 ms.
292 * If higher resolution is required use the other API.
293 * @param pfnTimer Callback function which shall be scheduled for execution
294 * on every timer tick.
295 * @param pvUser User argument for the callback.
296 * @see RTTimerLRCreateEx, RTTimerLRDestroy, RTTimerLRStop
297 */
298RTDECL(int) RTTimerLRCreate(PRTTIMERLR phTimerLR, uint32_t uMilliesInterval, PFNRTTIMERLR pfnTimer, void *pvUser);
299
300/**
301 * Create a suspended low resolution timer.
302 *
303 * @returns iprt status code.
304 * @retval VERR_NOT_SUPPORTED if an unsupported flag was specfied.
305 *
306 * @param phTimerLR Where to store the timer handle.
307 * @param u64NanoInterval The interval between timer ticks specified in nanoseconds if it's
308 * a recurring timer, the minimum for is 100000000 ns.
309 * For one shot timers, pass 0.
310 * @param fFlags Timer flags. Same as RTTimerCreateEx.
311 * @param pfnTimer Callback function which shall be scheduled for execution
312 * on every timer tick.
313 * @param pvUser User argument for the callback.
314 * @see RTTimerLRStart, RTTimerLRStop, RTTimerLRDestroy
315 */
316RTDECL(int) RTTimerLRCreateEx(PRTTIMERLR phTimerLR, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMERLR pfnTimer, void *pvUser);
317
318/**
319 * Stops and destroys a running low resolution timer.
320 *
321 * @returns iprt status code.
322 * @param hTimerLR The low resolution timer to stop and destroy.
323 * NIL_RTTIMERLR is accepted.
324 */
325RTDECL(int) RTTimerLRDestroy(RTTIMERLR hTimerLR);
326
327/**
328 * Starts a low resolution timer.
329 *
330 * @returns IPRT status code.
331 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
332 * @retval VERR_TIMER_ACTIVE if the timer isn't suspended.
333 *
334 * @param hTimerLR The low resolution timer to activate.
335 * @param u64First The RTTimeSystemNanoTS() for when the timer should start
336 * firing (relative), the minimum is 100000000 ns.
337 * If 0 is specified, the timer will fire ASAP.
338 *
339 * @see RTTimerLRStop
340 */
341RTDECL(int) RTTimerLRStart(RTTIMERLR hTimerLR, uint64_t u64First);
342
343/**
344 * Stops an active low resolution timer.
345 *
346 * @returns IPRT status code.
347 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
348 * @retval VERR_TIMER_SUSPENDED if the timer isn't active.
349 * @retval VERR_NOT_SUPPORTED if the IPRT implementation doesn't support stopping a timer.
350 *
351 * @param hTimerLR The low resolution timer to suspend.
352 *
353 * @see RTTimerLRStart
354 */
355RTDECL(int) RTTimerLRStop(RTTIMERLR hTimerLR);
356
357/** @} */
358
359RT_C_DECLS_END
360
361#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette