VirtualBox

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

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

SUPDrv,IPRT,VMM,DevAPIC: Added RTTimerCanDoHighResolution and exposed the RTTimer* API to the ring-0 modules. Fixed two regression from r65858 (TM + APIC).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.5 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.215389.xyz. 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 RTTimerDestroy, RTTimerStop
96 */
97RTDECL(int) RTTimerCreate(PRTTIMER *ppTimer, unsigned uMilliesInterval, PFNRTTIMER pfnTimer, void *pvUser);
98
99/**
100 * Create a suspended timer.
101 *
102 * @returns iprt status code.
103 * @retval VERR_NOT_SUPPORTED if an unsupported flag was specfied.
104 *
105 * @param ppTimer Where to store the timer handle.
106 * @param u64NanoInterval The interval between timer ticks specified in nanoseconds if it's
107 * a recurring timer. This is rounded to the fit the system timer granularity.
108 * For one shot timers, pass 0.
109 * @param fFlags Timer flags.
110 * @param pfnTimer Callback function which shall be scheduled for execution
111 * on every timer tick.
112 * @param pvUser User argument for the callback.
113 * @see RTTimerStart, RTTimerStop, RTTimerDestroy, RTTimerGetSystemGranularity
114 */
115RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser);
116
117/** @name RTTimerCreateEx flags
118 * @{ */
119/** Any CPU is fine. (Must be 0.) */
120#define RTTIMER_FLAGS_CPU_ANY 0
121/** One specific CPU */
122#define RTTIMER_FLAGS_CPU_SPECIFIC RT_BIT(8)
123/** Omni timer, run on all online CPUs.
124 * @remarks The timer callback isn't necessarily running at the time same time on each CPU. */
125#define RTTIMER_FLAGS_CPU_ALL ( RTTIMER_FLAGS_CPU_MASK | RTTIMER_FLAGS_CPU_SPECIFIC )
126/** CPU mask. */
127#define RTTIMER_FLAGS_CPU_MASK 0xff
128/** Convert a CPU number (0-based) to RTTimerCreateEx flags.
129 * This will automatically OR in the RTTIMER_FLAG_CPU_SPECIFIC flag. */
130#define RTTIMER_FLAGS_CPU(iCpu) ( (iCpu) | RTTIMER_FLAG_CPU_SPECIFIC )
131/** Macro that validates the flags. */
132#define RTTIMER_FLAGS_ARE_VALID(fFlags) ( !((fFlags) & ~((fFlags) & RTTIMER_FLAGS_CPU_SPECIFIC ? 0x1ffU : 0x100U)) )
133/** @} */
134
135/**
136 * Stops and destroys a running timer.
137 *
138 * @returns iprt status code.
139 * @param pTimer Timer to stop and destroy. NULL is ok.
140 */
141RTDECL(int) RTTimerDestroy(PRTTIMER pTimer);
142
143/**
144 * Stops an active timer.
145 *
146 * @returns IPRT status code.
147 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
148 * @retval VERR_TIMER_ACTIVE if the timer isn't suspended.
149 *
150 * @param pTimer The timer to activate.
151 * @param u64First The RTTimeSystemNanoTS() for when the timer should start
152 * firing (relative). If 0 is specified, the timer will fire ASAP.
153 * @see RTTimerStop
154 */
155RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First);
156
157/**
158 * Stops an active timer.
159 *
160 * @returns IPRT status code.
161 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
162 * @retval VERR_TIMER_SUSPENDED if the timer isn't active.
163 * @retval VERR_NOT_SUPPORTED if the IPRT implementation doesn't support stopping a timer.
164 *
165 * @param pTimer The timer to suspend.
166 * @see RTTimerStart
167 */
168RTDECL(int) RTTimerStop(PRTTIMER pTimer);
169
170
171/**
172 * Gets the (current) timer granularity of the system.
173 *
174 * @returns The timer granularity of the system in nanoseconds.
175 * @see RTTimerRequestSystemGranularity
176 */
177RTDECL(uint32_t) RTTimerGetSystemGranularity(void);
178
179/**
180 * Requests a specific system timer granularity.
181 *
182 * Successfull calls to this API must be coupled with the exact same number of
183 * calls to RTTimerReleaseSystemGranularity() in order to undo any changes made.
184 *
185 *
186 * @returns IPRT status code.
187 * @retval VERR_NOT_SUPPORTED if the requested value isn't supported by the host platform
188 * or if the host platform doesn't support modifying the system timer granularity.
189 * @retval VERR_PERMISSION_DENIED if the caller doesn't have the necessary privilege to
190 * modify the system timer granularity.
191 *
192 * @param u32Request The requested system timer granularity in nanoseconds.
193 * @param pu32Granted Where to store the granted system granularity. This is the value
194 * that should be passed to RTTimerReleaseSystemGranularity(). It
195 * is what RTTimerGetSystemGranularity() would return immediately
196 * after the change was made.
197 *
198 * The value differ from the request in two ways; rounding and
199 * scale. Meaning if your request is for 10.000.000 you might
200 * be granted 10.000.055 or 1.000.000.
201 * @see RTTimerReleaseSystemGranularity, RTTimerGetSystemGranularity
202 */
203RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted);
204
205/**
206 * Releases a system timer granularity grant acquired by RTTimerRequestSystemGranularity().
207 *
208 * @returns IPRT status code.
209 * @retval VERR_NOT_SUPPORTED if the host platform doesn't have any way of modifying
210 * the system timer granularity.
211 * @retval VERR_WRONG_ORDER if nobody call RTTimerRequestSystemGranularity() with the
212 * given grant value.
213 * @param u32Granted The granted system granularity.
214 * @see RTTimerRequestSystemGranularity
215 */
216RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted);
217
218/**
219 * Checks if the system support high resolution timers.
220 *
221 * The kind of support we are checking for is the kind of dynamically
222 * reprogrammable timers employed by recent Solaris and Linux kernels. It also
223 * implies that we can specify microsecond (or even better maybe) intervals
224 * without getting into trouble.
225 *
226 * @returns true if supported, false it not.
227 */
228RTDECL(bool) RTTimerCanDoHighResolution(void);
229
230
231/**
232 * Timer callback function for low res timers.
233 *
234 * This is identfical to FNRTTIMER except for the first parameter, so
235 * see FNRTTIMER for details.
236 *
237 * @param hTimerLR The low resolution timer handle.
238 * @param pvUser User argument.
239 * @param iTick The current timer tick. This is always 1 on the first
240 * callback after the timer was started. Will jump if we've
241 * skipped ticks when lagging behind.
242 */
243typedef DECLCALLBACK(void) FNRTTIMERLR(RTTIMERLR hTimerLR, void *pvUser, uint64_t iTick);
244/** Pointer to FNRTTIMER() function. */
245typedef FNRTTIMERLR *PFNRTTIMERLR;
246
247
248/**
249 * Create a recurring low resolution timer.
250 *
251 * @returns iprt status code.
252 * @param phTimerLR Where to store the timer handle.
253 * @param uMilliesInterval Milliseconds between the timer ticks, at least 100 ms.
254 * If higher resolution is required use the other API.
255 * @param pfnTimer Callback function which shall be scheduled for execution
256 * on every timer tick.
257 * @param pvUser User argument for the callback.
258 * @see RTTimerLRCreateEx, RTTimerLRDestroy, RTTimerLRStop
259 */
260RTDECL(int) RTTimerLRCreate(PRTTIMERLR phTimerLR, uint32_t uMilliesInterval, PFNRTTIMERLR pfnTimer, void *pvUser);
261
262/**
263 * Create a suspended low resolution timer.
264 *
265 * @returns iprt status code.
266 * @retval VERR_NOT_SUPPORTED if an unsupported flag was specfied.
267 *
268 * @param phTimerLR Where to store the timer handle.
269 * @param u64NanoInterval The interval between timer ticks specified in nanoseconds if it's
270 * a recurring timer, the minimum for is 100000000 ns.
271 * For one shot timers, pass 0.
272 * @param fFlags Timer flags. Same as RTTimerCreateEx.
273 * @param pfnTimer Callback function which shall be scheduled for execution
274 * on every timer tick.
275 * @param pvUser User argument for the callback.
276 * @see RTTimerLRStart, RTTimerLRStop, RTTimerLRDestroy
277 */
278RTDECL(int) RTTimerLRCreateEx(PRTTIMERLR phTimerLR, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMERLR pfnTimer, void *pvUser);
279
280/**
281 * Stops and destroys a running low resolution timer.
282 *
283 * @returns iprt status code.
284 * @param hTimerLR The low resolution timer to stop and destroy.
285 * NIL_RTTIMERLR is accepted.
286 */
287RTDECL(int) RTTimerLRDestroy(RTTIMERLR hTimerLR);
288
289/**
290 * Starts a low resolution timer.
291 *
292 * @returns IPRT status code.
293 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
294 * @retval VERR_TIMER_ACTIVE if the timer isn't suspended.
295 *
296 * @param hTimerLR The low resolution timer to activate.
297 * @param u64First The RTTimeSystemNanoTS() for when the timer should start
298 * firing (relative), the minimum is 100000000 ns.
299 * If 0 is specified, the timer will fire ASAP.
300 *
301 * @see RTTimerLRStop
302 */
303RTDECL(int) RTTimerLRStart(RTTIMERLR hTimerLR, uint64_t u64First);
304
305/**
306 * Stops an active low resolution timer.
307 *
308 * @returns IPRT status code.
309 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
310 * @retval VERR_TIMER_SUSPENDED if the timer isn't active.
311 * @retval VERR_NOT_SUPPORTED if the IPRT implementation doesn't support stopping a timer.
312 *
313 * @param hTimerLR The low resolution timer to suspend.
314 *
315 * @see RTTimerLRStart
316 */
317RTDECL(int) RTTimerLRStop(RTTIMERLR hTimerLR);
318
319/** @} */
320
321RT_C_DECLS_END
322
323#endif
Note: See TracBrowser for help on using the repository browser.

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