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