VirtualBox

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

Last change on this file since 22650 was 21399, checked in by vboxsync, 15 years ago

timerlr-generic.cpp,iprt/timer.h: Skip ticks if we're more than 60 ticks behind. See #3611.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
1/** @file
2 * IPRT - Timer.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___iprt_timer_h
31#define ___iprt_timer_h
32
33
34#include <iprt/cdefs.h>
35#include <iprt/types.h>
36
37
38RT_C_DECLS_BEGIN
39
40/** @defgroup grp_rt_timer RTTimer - Timer
41 *
42 * The IPRT timer API provides a simple abstraction of recurring and one-shot callback timers.
43 *
44 * Because of the great variation in the native APIs and the quality of
45 * the service delivered by those native APIs, the timers are operated
46 * on at best effort basis.
47 *
48 * All the ring-3 implementations are naturally at the mercy of the scheduler,
49 * which means that the callback rate might vary quite a bit and we might skip
50 * ticks. Many systems have a restriction that a process can only have one
51 * timer. IPRT currently makes no efforts at multiplexing timers in those kind
52 * of situations and will simply fail if you try to create more than one timer.
53 *
54 * Things are generally better in ring-0. The implementations will use interrupt
55 * time callbacks wherever available, and if not, resort to a high priority
56 * kernel thread.
57 *
58 * @ingroup grp_rt
59 * @{
60 */
61
62
63/** Timer handle. */
64typedef struct RTTIMER *PRTTIMER;
65
66/**
67 * Timer callback function.
68 *
69 * The context this call is made in varies with different platforms and
70 * kernel / user mode IPRT.
71 *
72 * In kernel mode a timer callback should not waste time, it shouldn't
73 * waste stack and it should be prepared that some APIs might not work
74 * correctly because of weird OS restrictions in this context that we
75 * haven't discovered and avoided yet. Please fix those APIs so they
76 * at least avoid panics and weird behaviour.
77 *
78 * @param pTimer Timer handle.
79 * @param pvUser User argument.
80 * @param iTick The current timer tick. This is always 1 on the first
81 * callback after the timer was started. For omni timers
82 * this will be 1 when a cpu comes back online.
83 */
84typedef DECLCALLBACK(void) FNRTTIMER(PRTTIMER pTimer, void *pvUser, uint64_t iTick);
85/** Pointer to FNRTTIMER() function. */
86typedef FNRTTIMER *PFNRTTIMER;
87
88
89/**
90 * Create a recurring timer.
91 *
92 * @returns iprt status code.
93 * @param ppTimer Where to store the timer handle.
94 * @param uMilliesInterval Milliseconds between the timer ticks.
95 * This is rounded up to the system granularity.
96 * @param pfnTimer Callback function which shall be scheduled for execution
97 * on every timer tick.
98 * @param pvUser User argument for the callback.
99 * @see RTTimerDestroy, RTTimerStop
100 */
101RTDECL(int) RTTimerCreate(PRTTIMER *ppTimer, unsigned uMilliesInterval, PFNRTTIMER pfnTimer, void *pvUser);
102
103/**
104 * Create a suspended timer.
105 *
106 * @returns iprt status code.
107 * @retval VERR_NOT_SUPPORTED if an unsupported flag was specfied.
108 *
109 * @param ppTimer Where to store the timer handle.
110 * @param u64NanoInterval The interval between timer ticks specified in nanoseconds if it's
111 * a recurring timer. This is rounded to the fit the system timer granularity.
112 * For one shot timers, pass 0.
113 * @param fFlags Timer flags.
114 * @param pfnTimer Callback function which shall be scheduled for execution
115 * on every timer tick.
116 * @param pvUser User argument for the callback.
117 * @see RTTimerStart, RTTimerStop, RTTimerDestroy, RTTimerGetSystemGranularity
118 */
119RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser);
120
121/** @name RTTimerCreateEx flags
122 * @{ */
123/** Any CPU is fine. (Must be 0.) */
124#define RTTIMER_FLAGS_CPU_ANY 0
125/** One specific CPU */
126#define RTTIMER_FLAGS_CPU_SPECIFIC RT_BIT(8)
127/** Omni timer, run on all online CPUs.
128 * @remarks The timer callback isn't necessarily running at the time same time on each CPU. */
129#define RTTIMER_FLAGS_CPU_ALL ( RTTIMER_FLAGS_CPU_MASK | RTTIMER_FLAGS_CPU_SPECIFIC )
130/** CPU mask. */
131#define RTTIMER_FLAGS_CPU_MASK 0xff
132/** Convert a CPU number (0-based) to RTTimerCreateEx flags.
133 * This will automatically OR in the RTTIMER_FLAG_CPU_SPECIFIC flag. */
134#define RTTIMER_FLAGS_CPU(iCpu) ( (iCpu) | RTTIMER_FLAG_CPU_SPECIFIC )
135/** Macro that validates the flags. */
136#define RTTIMER_FLAGS_ARE_VALID(fFlags) ( !((fFlags) & ~((fFlags) & RTTIMER_FLAGS_CPU_SPECIFIC ? 0x1ffU : 0x100U)) )
137/** @} */
138
139/**
140 * Stops and destroys a running timer.
141 *
142 * @returns iprt status code.
143 * @param pTimer Timer to stop and destroy. NULL is ok.
144 */
145RTDECL(int) RTTimerDestroy(PRTTIMER pTimer);
146
147/**
148 * Stops an active timer.
149 *
150 * @returns IPRT status code.
151 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
152 * @retval VERR_TIMER_ACTIVE if the timer isn't suspended.
153 *
154 * @param pTimer The timer to activate.
155 * @param u64First The RTTimeSystemNanoTS() for when the timer should start
156 * firing (relative). If 0 is specified, the timer will fire ASAP.
157 * @see RTTimerStop
158 */
159RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First);
160
161/**
162 * Stops an active timer.
163 *
164 * @returns IPRT status code.
165 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
166 * @retval VERR_TIMER_SUSPENDED if the timer isn't active.
167 * @retval VERR_NOT_SUPPORTED if the IPRT implementation doesn't support stopping a timer.
168 *
169 * @param pTimer The timer to suspend.
170 * @see RTTimerStart
171 */
172RTDECL(int) RTTimerStop(PRTTIMER pTimer);
173
174
175/**
176 * Gets the (current) timer granularity of the system.
177 *
178 * @returns The timer granularity of the system in nanoseconds.
179 * @see RTTimerRequestSystemGranularity
180 */
181RTDECL(uint32_t) RTTimerGetSystemGranularity(void);
182
183/**
184 * Requests a specific system timer granularity.
185 *
186 * Successfull calls to this API must be coupled with the exact same number of
187 * calls to RTTimerReleaseSystemGranularity() in order to undo any changes made.
188 *
189 *
190 * @returns IPRT status code.
191 * @retval VERR_NOT_SUPPORTED if the requested value isn't supported by the host platform
192 * or if the host platform doesn't support modifying the system timer granularity.
193 * @retval VERR_PERMISSION_DENIED if the caller doesn't have the necessary privilege to
194 * modify the system timer granularity.
195 *
196 * @param u32Request The requested system timer granularity in nanoseconds.
197 * @param pu32Granted Where to store the granted system granularity. This is the value
198 * that should be passed to RTTimerReleaseSystemGranularity(). It
199 * is what RTTimerGetSystemGranularity() would return immediately
200 * after the change was made.
201 *
202 * The value differ from the request in two ways; rounding and
203 * scale. Meaning if your request is for 10.000.000 you might
204 * be granted 10.000.055 or 1.000.000.
205 * @see RTTimerReleaseSystemGranularity, RTTimerGetSystemGranularity
206 */
207RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted);
208
209/**
210 * Releases a system timer granularity grant acquired by RTTimerRequestSystemGranularity().
211 *
212 * @returns IPRT status code.
213 * @retval VERR_NOT_SUPPORTED if the host platform doesn't have any way of modifying
214 * the system timer granularity.
215 * @retval VERR_WRONG_ORDER if nobody call RTTimerRequestSystemGranularity() with the
216 * given grant value.
217 * @param u32Granted The granted system granularity.
218 * @see RTTimerRequestSystemGranularity
219 */
220RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted);
221
222
223
224/**
225 * Timer callback function for low res timers.
226 *
227 * This is identfical to FNRTTIMER except for the first parameter, so
228 * see FNRTTIMER for details.
229 *
230 * @param hTimerLR The low resolution timer handle.
231 * @param pvUser User argument.
232 * @param iTick The current timer tick. This is always 1 on the first
233 * callback after the timer was started. Will jump if we've
234 * skipped ticks when lagging behind.
235 */
236typedef DECLCALLBACK(void) FNRTTIMERLR(RTTIMERLR hTimerLR, void *pvUser, uint64_t iTick);
237/** Pointer to FNRTTIMER() function. */
238typedef FNRTTIMERLR *PFNRTTIMERLR;
239
240
241/**
242 * Create a recurring low resolution timer.
243 *
244 * @returns iprt status code.
245 * @param phTimerLR Where to store the timer handle.
246 * @param uMilliesInterval Milliseconds between the timer ticks, at least 100 ms.
247 * If higher resolution is required use the other API.
248 * @param pfnTimer Callback function which shall be scheduled for execution
249 * on every timer tick.
250 * @param pvUser User argument for the callback.
251 * @see RTTimerLRCreateEx, RTTimerLRDestroy, RTTimerLRStop
252 */
253RTDECL(int) RTTimerLRCreate(PRTTIMERLR phTimerLR, uint32_t uMilliesInterval, PFNRTTIMERLR pfnTimer, void *pvUser);
254
255/**
256 * Create a suspended low resolution timer.
257 *
258 * @returns iprt status code.
259 * @retval VERR_NOT_SUPPORTED if an unsupported flag was specfied.
260 *
261 * @param phTimerLR Where to store the timer handle.
262 * @param u64NanoInterval The interval between timer ticks specified in nanoseconds if it's
263 * a recurring timer, the minimum for is 100000000 ns.
264 * For one shot timers, pass 0.
265 * @param fFlags Timer flags. Same as RTTimerCreateEx.
266 * @param pfnTimer Callback function which shall be scheduled for execution
267 * on every timer tick.
268 * @param pvUser User argument for the callback.
269 * @see RTTimerLRStart, RTTimerLRStop, RTTimerLRDestroy
270 */
271RTDECL(int) RTTimerLRCreateEx(PRTTIMERLR phTimerLR, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMERLR pfnTimer, void *pvUser);
272
273/**
274 * Stops and destroys a running low resolution timer.
275 *
276 * @returns iprt status code.
277 * @param hTimerLR The low resolution timer to stop and destroy.
278 * NIL_RTTIMERLR is accepted.
279 */
280RTDECL(int) RTTimerLRDestroy(RTTIMERLR hTimerLR);
281
282/**
283 * Stops an active low resolution timer.
284 *
285 * @returns IPRT status code.
286 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
287 * @retval VERR_TIMER_ACTIVE if the timer isn't suspended.
288 *
289 * @param hTimerLR The low resolution timer to activate.
290 * @param u64First The RTTimeSystemNanoTS() for when the timer should start
291 * firing (relative), the minimum is 100000000 ns.
292 * If 0 is specified, the timer will fire ASAP.
293 *
294 * @see RTTimerLRStop
295 */
296RTDECL(int) RTTimerLRStart(RTTIMERLR hTimerLR, uint64_t u64First);
297
298/**
299 * Stops an active low resolution timer.
300 *
301 * @returns IPRT status code.
302 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
303 * @retval VERR_TIMER_SUSPENDED if the timer isn't active.
304 * @retval VERR_NOT_SUPPORTED if the IPRT implementation doesn't support stopping a timer.
305 *
306 * @param hTimerLR The low resolution timer to suspend.
307 *
308 * @see RTTimerLRStart
309 */
310RTDECL(int) RTTimerLRStop(RTTIMERLR hTimerLR);
311
312/** @} */
313
314RT_C_DECLS_END
315
316#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