VirtualBox

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

Last change on this file since 8006 was 5999, checked in by vboxsync, 16 years ago

The Giant CDDL Dual-License Header Change.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1/** @file
2 * innotek Portable Runtime - Timer.
3 */
4
5/*
6 * Copyright (C) 2006-2007 innotek GmbH
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
34__BEGIN_DECLS
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 */
77typedef DECLCALLBACK(void) FNRTTIMER(PRTTIMER pTimer, void *pvUser);
78/** Pointer to FNRTTIMER() function. */
79typedef FNRTTIMER *PFNRTTIMER;
80
81
82/**
83 * Create a recurring timer.
84 *
85 * @returns iprt status code.
86 * @param ppTimer Where to store the timer handle.
87 * @param uMilliesInterval Milliseconds between the timer ticks.
88 * This is rounded up to the system granularity.
89 * @param pfnTimer Callback function which shall be scheduled for execution
90 * on every timer tick.
91 * @param pvUser User argument for the callback.
92 * @see RTTimerDestroy, RTTimerStop
93 */
94RTDECL(int) RTTimerCreate(PRTTIMER *ppTimer, unsigned uMilliesInterval, PFNRTTIMER pfnTimer, void *pvUser);
95
96/**
97 * Create a suspended timer.
98 *
99 * @returns iprt status code.
100 * @param ppTimer Where to store the timer handle.
101 * @param u64NanoInterval The interval between timer ticks specified in nanoseconds if it's
102 * a recurring timer. This is rounded to the fit the system timer granularity.
103 * For one shot timers, pass 0.
104 * @param fFlags Timer flags.
105 * @param pfnTimer Callback function which shall be scheduled for execution
106 * on every timer tick.
107 * @param pvUser User argument for the callback.
108 * @see RTTimerStart, RTTimerStop, RTTimerDestroy, RTTimerGetSystemGranularity
109 */
110RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser);
111
112/** @name RTTimerCreateEx flags
113 * @{ */
114/** Any CPU is fine. (Must be 0.) */
115#define RTTIMER_FLAGS_CPU_ANY 0
116/** One specific CPU */
117#define RTTIMER_FLAGS_CPU_SPECIFIC RT_BIT(8)
118/** All online CPUs. */
119#define RTTIMER_FLAGS_CPU_ALL ( RTTIMER_FLAGS_CPU_MASK | RTTIMER_FLAGS_CPU_SPECIFIC )
120/** CPU mask. */
121#define RTTIMER_FLAGS_CPU_MASK 0xff
122/** Convert a CPU number (0-based) to RTTimerCreateEx flags.
123 * This will automatically OR in the RTTIMER_FLAG_CPU_SPECIFIC flag. */
124#define RTTIMER_FLAGS_CPU(iCpu) ( (iCpu) | RTTIMER_FLAG_CPU_SPECIFIC )
125/** Macro that validates the flags. */
126#define RTTIMER_FLAGS_IS_VALID(fFlags) ( !((fFlags) & ((fFlags) & RTTIMER_FLAGS_CPU_SPECIFIC ? 0x1ff : 0x100)) )
127/** @} */
128
129/**
130 * Stops and destroys a running timer.
131 *
132 * @returns iprt status code.
133 * @param pTimer Timer to stop and destroy. NULL is ok.
134 */
135RTDECL(int) RTTimerDestroy(PRTTIMER pTimer);
136
137/**
138 * Stops an active timer.
139 *
140 * @returns IPRT status code.
141 * @retval VERR_INVALID_HANDLE if pTimer isn't valid.
142 * @retval VERR_TIMER_ACTIVE if the timer isn't suspended.
143 *
144 * @param pTimer The timer to activate.
145 * @param u64First The RTTimeSystemNanoTS() for when the timer should start firing.
146 * If 0 is specified, the timer will fire ASAP.
147 * @see RTTimerStop
148 */
149RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First);
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_SUSPENDED if the timer isn't active.
157 * @retval VERR_NOT_SUPPORTED if the IPRT implementation doesn't support stopping a timer.
158 *
159 * @param pTimer The timer to suspend.
160 * @see RTTimerStart
161 */
162RTDECL(int) RTTimerStop(PRTTIMER pTimer);
163
164
165/**
166 * Gets the (current) timer granularity of the system.
167 *
168 * @returns The timer granularity of the system in nanoseconds.
169 * @see RTTimerRequestSystemGranularity
170 */
171RTDECL(uint32_t) RTTimerGetSystemGranularity(void);
172
173/**
174 * Requests a specific system timer granularity.
175 *
176 * Successfull calls to this API must be coupled with the exact same number of
177 * calls to RTTimerReleaseSystemGranularity() in order to undo any changes made.
178 *
179 *
180 * @returns IPRT status code.
181 * @retval VERR_NOT_SUPPORTED if the requested value isn't supported by the host platform
182 * or if the host platform doesn't support modifying the system timer granularity.
183 * @retval VERR_PERMISSION_DENIED if the caller doesn't have the necessary privilege to
184 * modify the system timer granularity.
185 *
186 * @param u32Request The requested system timer granularity in nanoseconds.
187 * @param pu32Granted Where to store the granted system granularity. This is the value
188 * that should be passed to RTTimerReleaseSystemGranularity(). It
189 * is what RTTimerGetSystemGranularity() would return immediately
190 * after the change was made.
191 *
192 * The value differ from the request in two ways; rounding and
193 * scale. Meaning if your request is for 10.000.000 you might
194 * be granted 10.000.055 or 1.000.000.
195 * @see RTTimerReleaseSystemGranularity, RTTimerGetSystemGranularity
196 */
197RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted);
198
199/**
200 * Releases a system timer granularity grant acquired by RTTimerRequestSystemGranularity().
201 *
202 * @returns IPRT status code.
203 * @retval VERR_NOT_SUPPORTED if the host platform doesn't have any way of modifying
204 * the system timer granularity.
205 * @retval VERR_WRONG_ORDER if nobody call RTTimerRequestSystemGranularity() with the
206 * given grant value.
207 * @param u32Granted The granted system granularity.
208 * @see RTTimerRequestSystemGranularity
209 */
210RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted);
211
212/** @} */
213
214__END_DECLS
215
216#endif
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use