VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/freebsd/timer-r0drv-freebsd.c@ 18972

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

IPRT/r0drv/freebsd: properties.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1/* $Id: timer-r0drv-freebsd.c 18972 2009-04-16 23:43:08Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Ring-0 Driver, FreeBSD.
4 */
5
6/*
7 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "the-freebsd-kernel.h"
36
37#include <iprt/timer.h>
38#include <iprt/time.h>
39#include <iprt/spinlock.h>
40#include <iprt/err.h>
41#include <iprt/asm.h>
42#include <iprt/assert.h>
43#include <iprt/alloc.h>
44
45#include "internal/magics.h"
46
47
48/*******************************************************************************
49* Structures and Typedefs *
50*******************************************************************************/
51/**
52 * The internal representation of an FreeBSD timer handle.
53 */
54typedef struct RTTIMER
55{
56 /** Magic.
57 * This is RTTIMER_MAGIC, but changes to something else before the timer
58 * is destroyed to indicate clearly that thread should exit. */
59 uint32_t volatile u32Magic;
60 /** Flag indicating that the timer is suspended. */
61 uint8_t volatile fSuspended;
62 /** Whether the timer must run on a specific CPU or not. */
63 uint8_t fSpecificCpu;
64 /** The CPU it must run on if fSpecificCpu is set. */
65 uint8_t iCpu;
66 /** The FreeBSD callout structure. */
67 struct callout Callout;
68 /** Callback. */
69 PFNRTTIMER pfnTimer;
70 /** User argument. */
71 void *pvUser;
72 /** The timer interval. 0 if one-shot. */
73 uint64_t u64NanoInterval;
74 /** The start of the current run.
75 * This is used to calculate when the timer ought to fire the next time. */
76 uint64_t volatile u64StartTS;
77 /** The start of the current run.
78 * This is used to calculate when the timer ought to fire the next time. */
79 uint64_t volatile u64NextTS;
80 /** The current tick number (since u64StartTS). */
81 uint64_t volatile iTick;
82} RTTIMER;
83
84
85/*******************************************************************************
86* Internal Functions *
87*******************************************************************************/
88static void rtTimerFreeBSDCallback(void *pvTimer);
89
90
91
92RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
93{
94 *ppTimer = NULL;
95
96 /*
97 * Validate flags.
98 */
99 if (!RTTIMER_FLAGS_ARE_VALID(fFlags))
100 return VERR_INVALID_PARAMETER;
101 if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
102 && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL
103 && (fFlags & RTTIMER_FLAGS_CPU_MASK) > mp_maxid)
104 return VERR_INVALID_PARAMETER;
105
106 /*
107 * Allocate and initialize the timer handle.
108 */
109 PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
110 if (!pTimer)
111 return VERR_NO_MEMORY;
112
113 pTimer->u32Magic = RTTIMER_MAGIC;
114 pTimer->fSuspended = true;
115 pTimer->fSpecificCpu = !!(fFlags & RTTIMER_FLAGS_CPU_SPECIFIC);
116 pTimer->iCpu = fFlags & RTTIMER_FLAGS_CPU_MASK;
117 pTimer->pfnTimer = pfnTimer;
118 pTimer->pvUser = pvUser;
119 pTimer->u64NanoInterval = u64NanoInterval;
120 pTimer->u64StartTS = 0;
121 callout_init(&pTimer->Callout, CALLOUT_MPSAFE);
122
123 *ppTimer = pTimer;
124 return VINF_SUCCESS;
125}
126
127
128/**
129 * Validates the timer handle.
130 *
131 * @returns true if valid, false if invalid.
132 * @param pTimer The handle.
133 */
134DECLINLINE(bool) rtTimerIsValid(PRTTIMER pTimer)
135{
136 AssertReturn(VALID_PTR(pTimer), false);
137 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
138 return true;
139}
140
141
142RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
143{
144 /* It's ok to pass NULL pointer. */
145 if (pTimer == /*NIL_RTTIMER*/ NULL)
146 return VINF_SUCCESS;
147 if (!rtTimerIsValid(pTimer))
148 return VERR_INVALID_HANDLE;
149
150 /*
151 * Free the associated resources.
152 */
153 pTimer->u32Magic++;
154 callout_stop(&pTimer->Callout);
155 RTMemFree(pTimer);
156 return VINF_SUCCESS;
157}
158
159
160RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
161{
162 struct timeval tv;
163
164 if (!rtTimerIsValid(pTimer))
165 return VERR_INVALID_HANDLE;
166 if (!pTimer->fSuspended)
167 return VERR_TIMER_ACTIVE;
168
169 /*
170 * Calc when it should start fireing.
171 */
172 u64First += RTTimeNanoTS();
173
174 pTimer->fSuspended = false;
175 pTimer->iTick = 0;
176 pTimer->u64StartTS = u64First;
177 pTimer->u64NextTS = u64First;
178
179 tv.tv_sec = u64First / 1000000000;
180 tv.tv_usec = (u64First % 1000000000) / 1000;
181 callout_reset(&pTimer->Callout, tvtohz(&tv), rtTimerFreeBSDCallback, pTimer);
182
183 return VINF_SUCCESS;
184}
185
186
187RTDECL(int) RTTimerStop(PRTTIMER pTimer)
188{
189 if (!rtTimerIsValid(pTimer))
190 return VERR_INVALID_HANDLE;
191 if (pTimer->fSuspended)
192 return VERR_TIMER_SUSPENDED;
193
194 /*
195 * Suspend the timer.
196 */
197 pTimer->fSuspended = true;
198 callout_stop(&pTimer->Callout);
199
200 return VINF_SUCCESS;
201}
202
203
204/**
205 * smp_rendezvous action callback.
206 *
207 * This will perform the timer callback if we're on the right CPU.
208 *
209 * @param pvTimer The timer.
210 */
211static void rtTimerFreeBSDIpiAction(void *pvTimer)
212{
213 PRTTIMER pTimer = (PRTTIMER)pvTimer;
214 if ( pTimer->iCpu == RTTIMER_FLAGS_CPU_MASK
215 || (u_int)pTimer->iCpu == curcpu)
216 pTimer->pfnTimer(pTimer, pTimer->pvUser, pTimer->iTick);
217}
218
219
220static void rtTimerFreeBSDCallback(void *pvTimer)
221{
222 PRTTIMER pTimer = (PRTTIMER)pvTimer;
223
224 /* calculate and set the next timeout */
225 pTimer->iTick++;
226 if (!pTimer->u64NanoInterval)
227 {
228 pTimer->fSuspended = true;
229 callout_stop(&pTimer->Callout);
230 }
231 else
232 {
233 struct timeval tv;
234 const uint64_t u64NanoTS = RTTimeNanoTS();
235 pTimer->u64NextTS = pTimer->u64StartTS + pTimer->iTick * pTimer->u64NanoInterval;
236 if (pTimer->u64NextTS < u64NanoTS)
237 pTimer->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
238
239 tv.tv_sec = pTimer->u64NextTS / 1000000000;
240 tv.tv_usec = (pTimer->u64NextTS % 1000000000) / 1000;
241 callout_reset(&pTimer->Callout, tvtohz(&tv), rtTimerFreeBSDCallback, pTimer);
242 }
243
244 /* callback */
245 if ( !pTimer->fSpecificCpu
246 || pTimer->iCpu == curcpu)
247 pTimer->pfnTimer(pTimer, pTimer->pvUser, pTimer->iTick);
248 else
249 smp_rendezvous(NULL, rtTimerFreeBSDIpiAction, NULL, pvTimer);
250}
251
252
253RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
254{
255 return 1000000000 / hz; /* ns */
256}
257
258
259RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
260{
261 return VERR_NOT_SUPPORTED;
262}
263
264
265RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
266{
267 return VERR_NOT_SUPPORTED;
268}
269
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