VirtualBox

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

Last change on this file since 54512 was 54512, checked in by vboxsync, 10 years ago

RTTimerDestroy() is off limits from the timer callback on windows too.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.3 KB
Line 
1/** @file
2 * IPRT - Timer.
3 */
4
5/*
6 * Copyright (C) 2006-2012 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(16)
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(0xffff)
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(17)
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(0x3ffff) : UINT32_C(0x30000))) )
141/** @} */
142
143/**
144 * Stops and destroys a running timer.
145 *
146 * @returns iprt status code.
147 * @retval VERR_WRONG_CONTEXT if executing at the wrong IRQL (windows), PIL
148 * (solaris), or similar. Portable code does not destroy timers with
149 * preemption (or interrupts) disabled.
150 * @param pTimer Timer to stop and destroy. NULL is ok.
151 */
152RTDECL(int) RTTimerDestroy(PRTTIMER pTimer);
153
154/**
155 * Starts a suspended timer.
156 *
157 * @returns IPRT status code.
158 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
159 * @retval VERR_TIMER_ACTIVE if the timer isn't suspended.
160 * @retval VERR_CPU_OFFLINE if the CPU the timer was created to run on is not
161 * online (this include the case where it's not present in the
162 * system).
163 *
164 * @param pTimer The timer to activate.
165 * @param u64First The RTTimeSystemNanoTS() for when the timer should start
166 * firing (relative). If 0 is specified, the timer will
167 * fire ASAP.
168 * @remarks When RTTimerCanDoHighResolution returns true, this API is
169 * callable with preemption disabled in ring-0.
170 * @see RTTimerStop
171 */
172RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First);
173
174/**
175 * Stops an active timer.
176 *
177 * @returns IPRT status code.
178 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
179 * @retval VERR_TIMER_SUSPENDED if the timer isn't active.
180 * @retval VERR_NOT_SUPPORTED if the IPRT implementation doesn't support
181 * stopping a timer.
182 *
183 * @param pTimer The timer to suspend.
184 * @remarks Can be called from the timer callback function to stop it.
185 * @see RTTimerStart
186 */
187RTDECL(int) RTTimerStop(PRTTIMER pTimer);
188
189/**
190 * Changes the interval of a periodic timer.
191 *
192 * If the timer is active, it is implementation dependent whether the change
193 * takes place immediately or after the next tick. To get defined behavior,
194 * stop the timer before calling this API.
195 *
196 * @returns IPRT status code.
197 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
198 * @retval VERR_NOT_SUPPORTED if not supported.
199 * @retval VERR_INVALID_STATE if not a periodic timer.
200 *
201 * @param pTimer The timer to activate.
202 * @param u64NanoInterval The interval between timer ticks specified in
203 * nanoseconds. This is rounded to the fit the
204 * system timer granularity.
205 * @remarks Callable from the timer callback. Callable with preemption
206 * disabled in ring-0.
207 */
208RTDECL(int) RTTimerChangeInterval(PRTTIMER pTimer, uint64_t u64NanoInterval);
209
210/**
211 * Gets the (current) timer granularity of the system.
212 *
213 * @returns The timer granularity of the system in nanoseconds.
214 * @see RTTimerRequestSystemGranularity
215 */
216RTDECL(uint32_t) RTTimerGetSystemGranularity(void);
217
218/**
219 * Requests a specific system timer granularity.
220 *
221 * Successfull calls to this API must be coupled with the exact same number of
222 * calls to RTTimerReleaseSystemGranularity() in order to undo any changes made.
223 *
224 *
225 * @returns IPRT status code.
226 * @retval VERR_NOT_SUPPORTED if the requested value isn't supported by the host platform
227 * or if the host platform doesn't support modifying the system timer granularity.
228 * @retval VERR_PERMISSION_DENIED if the caller doesn't have the necessary privilege to
229 * modify the system timer granularity.
230 *
231 * @param u32Request The requested system timer granularity in nanoseconds.
232 * @param pu32Granted Where to store the granted system granularity. This is the value
233 * that should be passed to RTTimerReleaseSystemGranularity(). It
234 * is what RTTimerGetSystemGranularity() would return immediately
235 * after the change was made.
236 *
237 * The value differ from the request in two ways; rounding and
238 * scale. Meaning if your request is for 10.000.000 you might
239 * be granted 10.000.055 or 1.000.000.
240 * @see RTTimerReleaseSystemGranularity, RTTimerGetSystemGranularity
241 */
242RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted);
243
244/**
245 * Releases a system timer granularity grant acquired by RTTimerRequestSystemGranularity().
246 *
247 * @returns IPRT status code.
248 * @retval VERR_NOT_SUPPORTED if the host platform doesn't have any way of modifying
249 * the system timer granularity.
250 * @retval VERR_WRONG_ORDER if nobody call RTTimerRequestSystemGranularity() with the
251 * given grant value.
252 * @param u32Granted The granted system granularity.
253 * @see RTTimerRequestSystemGranularity
254 */
255RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted);
256
257/**
258 * Checks if the system support high resolution timers.
259 *
260 * The kind of support we are checking for is the kind of dynamically
261 * reprogrammable timers employed by recent Solaris and Linux kernels. It also
262 * implies that we can specify microsecond (or even better maybe) intervals
263 * without getting into trouble.
264 *
265 * @returns true if supported, false it not.
266 *
267 * @remarks Returning true also means RTTimerChangeInterval must be implemented
268 * and RTTimerStart be callable with preemption disabled.
269 */
270RTDECL(bool) RTTimerCanDoHighResolution(void);
271
272
273/**
274 * Timer callback function for low res timers.
275 *
276 * This is identical to FNRTTIMER except for the first parameter, so
277 * see FNRTTIMER for details.
278 *
279 * @param hTimerLR The low resolution timer handle.
280 * @param pvUser User argument.
281 * @param iTick The current timer tick. This is always 1 on the first
282 * callback after the timer was started. Will jump if we've
283 * skipped ticks when lagging behind.
284 */
285typedef DECLCALLBACK(void) FNRTTIMERLR(RTTIMERLR hTimerLR, void *pvUser, uint64_t iTick);
286/** Pointer to FNRTTIMER() function. */
287typedef FNRTTIMERLR *PFNRTTIMERLR;
288
289
290/**
291 * Create a recurring low resolution timer.
292 *
293 * @returns iprt status code.
294 * @param phTimerLR Where to store the timer handle.
295 * @param uMilliesInterval Milliseconds between the timer ticks, at least 100 ms.
296 * If higher resolution is required use the other API.
297 * @param pfnTimer Callback function which shall be scheduled for execution
298 * on every timer tick.
299 * @param pvUser User argument for the callback.
300 * @see RTTimerLRCreateEx, RTTimerLRDestroy, RTTimerLRStop
301 */
302RTDECL(int) RTTimerLRCreate(PRTTIMERLR phTimerLR, uint32_t uMilliesInterval, PFNRTTIMERLR pfnTimer, void *pvUser);
303
304/**
305 * Create a suspended low resolution timer.
306 *
307 * @returns iprt status code.
308 * @retval VERR_NOT_SUPPORTED if an unsupported flag was specfied.
309 *
310 * @param phTimerLR Where to store the timer handle.
311 * @param u64NanoInterval The interval between timer ticks specified in nanoseconds if it's
312 * a recurring timer, the minimum for is 100000000 ns.
313 * For one shot timers, pass 0.
314 * @param fFlags Timer flags. Same as RTTimerCreateEx.
315 * @param pfnTimer Callback function which shall be scheduled for execution
316 * on every timer tick.
317 * @param pvUser User argument for the callback.
318 * @see RTTimerLRStart, RTTimerLRStop, RTTimerLRDestroy
319 */
320RTDECL(int) RTTimerLRCreateEx(PRTTIMERLR phTimerLR, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMERLR pfnTimer, void *pvUser);
321
322/**
323 * Stops and destroys a running low resolution timer.
324 *
325 * @returns iprt status code.
326 * @param hTimerLR The low resolution timer to stop and destroy.
327 * NIL_RTTIMERLR is accepted.
328 */
329RTDECL(int) RTTimerLRDestroy(RTTIMERLR hTimerLR);
330
331/**
332 * Starts a low resolution timer.
333 *
334 * @returns IPRT status code.
335 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
336 * @retval VERR_TIMER_ACTIVE if the timer isn't suspended.
337 *
338 * @param hTimerLR The low resolution timer to activate.
339 * @param u64First The RTTimeSystemNanoTS() for when the timer should start
340 * firing (relative), the minimum is 100000000 ns.
341 * If 0 is specified, the timer will fire ASAP.
342 *
343 * @see RTTimerLRStop
344 */
345RTDECL(int) RTTimerLRStart(RTTIMERLR hTimerLR, uint64_t u64First);
346
347/**
348 * Stops an active low resolution timer.
349 *
350 * @returns IPRT status code.
351 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
352 * @retval VERR_TIMER_SUSPENDED if the timer isn't active.
353 * @retval VERR_NOT_SUPPORTED if the IPRT implementation doesn't support stopping a timer.
354 *
355 * @param hTimerLR The low resolution timer to suspend.
356 *
357 * @see RTTimerLRStart
358 */
359RTDECL(int) RTTimerLRStop(RTTIMERLR hTimerLR);
360
361/**
362 * Changes the interval of a low resolution timer.
363 *
364 * If the timer is active, the next tick will occure immediately just like with
365 * RTTimerLRStart() when u64First parameter is zero.
366 *
367 * @returns IPRT status code.
368 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
369 * @retval VERR_NOT_SUPPORTED if not supported.
370 *
371 * @param hTimerLR The low resolution timer to update.
372 * @param u64NanoInterval The interval between timer ticks specified in
373 * nanoseconds. This is rounded to the fit the
374 * system timer granularity.
375 * @remarks Callable from the timer callback.
376 */
377RTDECL(int) RTTimerLRChangeInterval(RTTIMERLR hTimerLR, uint64_t u64NanoInterval);
378
379/** @} */
380
381RT_C_DECLS_END
382
383#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