VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/solaris/vbi/timer-r0drv-solaris.c@ 20554

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

Fix a couple of words doubled in comments. No code changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1/** $Id: timer-r0drv-solaris.c 14318 2008-11-18 16:56:53Z vboxsync $ */
2/** @file
3 * IPRT - Timer, Ring-0 Driver, Solaris.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "the-solaris-kernel.h"
36
37#include <iprt/timer.h>
38#include <iprt/time.h>
39#include <iprt/mp.h>
40#include <iprt/spinlock.h>
41#include <iprt/err.h>
42#include <iprt/asm.h>
43#include <iprt/assert.h>
44#include <iprt/alloc.h>
45
46#include "internal/magics.h"
47
48
49/*******************************************************************************
50* Structures and Typedefs *
51*******************************************************************************/
52/**
53 * The internal representation of a Solaris timer handle.
54 */
55typedef struct RTTIMER
56{
57 /** Magic.
58 * This is RTTIMER_MAGIC, but changes to something else before the timer
59 * is destroyed to indicate clearly that thread should exit. */
60 uint32_t volatile u32Magic;
61 /** Flag indicating that the timer is suspended. */
62 uint8_t volatile fSuspended;
63 /** Run on all CPUs if set */
64 uint8_t fAllCpu;
65 /** Whether the timer must run on a specific CPU or not. */
66 uint8_t fSpecificCpu;
67 /** The CPU it must run on if fSpecificCpu is set. */
68 uint8_t iCpu;
69 /** The nano second interval for repeating timers */
70 uint64_t interval;
71 /** simple Solaris timer handle. */
72 vbi_stimer_t *stimer;
73 /** global Solaris timer handle. */
74 vbi_gtimer_t *gtimer;
75 /** The user callback. */
76 PFNRTTIMER pfnTimer;
77 /** The argument for the user callback. */
78 void *pvUser;
79} RTTIMER;
80
81
82/*
83 * Need a wrapper to get the PRTTIMER passed through
84 */
85static void rtTimerSolarisCallbackWrapper(PRTTIMER pTimer, uint64_t tick)
86{
87 pTimer->pfnTimer(pTimer, pTimer->pvUser, tick);
88}
89
90
91
92
93RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
94{
95 *ppTimer = NULL;
96
97 /*
98 * Validate flags.
99 */
100 if (!RTTIMER_FLAGS_ARE_VALID(fFlags))
101 return VERR_INVALID_PARAMETER;
102 if (vbi_revision_level < 2)
103 return VERR_NOT_SUPPORTED;
104
105 if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
106 && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL
107 && !RTMpIsCpuPossible((fFlags & RTTIMER_FLAGS_CPU_MASK)))
108 return VERR_CPU_NOT_FOUND;
109
110 if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL && u64NanoInterval == 0)
111 return VERR_NOT_SUPPORTED;
112
113 /*
114 * Allocate and initialize the timer handle.
115 */
116 PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
117 if (!pTimer)
118 return VERR_NO_MEMORY;
119
120 pTimer->u32Magic = RTTIMER_MAGIC;
121 pTimer->fSuspended = true;
122 if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL)
123 {
124 pTimer->fAllCpu = true;
125 pTimer->fSpecificCpu = false;
126 pTimer->iCpu = 255;
127 }
128 else if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
129 {
130 pTimer->fAllCpu = false;
131 pTimer->fSpecificCpu = true;
132 pTimer->iCpu = fFlags & RTTIMER_FLAGS_CPU_MASK;
133 }
134 else
135 {
136 pTimer->fAllCpu = false;
137 pTimer->fSpecificCpu = false;
138 pTimer->iCpu = 255;
139 }
140 pTimer->interval = u64NanoInterval;
141 pTimer->pfnTimer = pfnTimer;
142 pTimer->pvUser = pvUser;
143 pTimer->stimer = NULL;
144 pTimer->gtimer = NULL;
145
146 *ppTimer = pTimer;
147 return VINF_SUCCESS;
148}
149
150
151/**
152 * Validates the timer handle.
153 *
154 * @returns true if valid, false if invalid.
155 * @param pTimer The handle.
156 */
157DECLINLINE(bool) rtTimerIsValid(PRTTIMER pTimer)
158{
159 AssertReturn(VALID_PTR(pTimer), false);
160 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
161 return true;
162}
163
164
165RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
166{
167 if (pTimer == NULL)
168 return VINF_SUCCESS;
169 if (!rtTimerIsValid(pTimer))
170 return VERR_INVALID_HANDLE;
171
172 /*
173 * Free the associated resources.
174 */
175 RTTimerStop(pTimer);
176 pTimer->u32Magic++;
177 RTMemFree(pTimer);
178 return VINF_SUCCESS;
179}
180
181
182RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
183{
184 int cpu = VBI_ANY_CPU;
185
186 if (!rtTimerIsValid(pTimer))
187 return VERR_INVALID_HANDLE;
188 if (!pTimer->fSuspended)
189 return VERR_TIMER_ACTIVE;
190
191 pTimer->fSuspended = false;
192 if (pTimer->fAllCpu)
193 {
194 pTimer->gtimer = vbi_gtimer_begin(rtTimerSolarisCallbackWrapper, pTimer, u64First, pTimer->interval);
195 if (pTimer->gtimer == NULL)
196 return VERR_INVALID_PARAMETER;
197 }
198 else
199 {
200 if (pTimer->fSpecificCpu)
201 cpu = pTimer->iCpu;
202 pTimer->stimer = vbi_stimer_begin(rtTimerSolarisCallbackWrapper, pTimer, u64First, pTimer->interval, cpu);
203 if (pTimer->stimer == NULL)
204 {
205 if (cpu != VBI_ANY_CPU)
206 return VERR_CPU_OFFLINE;
207 return VERR_INVALID_PARAMETER;
208 }
209 }
210
211 return VINF_SUCCESS;
212}
213
214
215RTDECL(int) RTTimerStop(PRTTIMER pTimer)
216{
217 if (!rtTimerIsValid(pTimer))
218 return VERR_INVALID_HANDLE;
219 if (pTimer->fSuspended)
220 return VERR_TIMER_SUSPENDED;
221
222 pTimer->fSuspended = true;
223 if (pTimer->stimer)
224 {
225 vbi_stimer_end(pTimer->stimer);
226 pTimer->stimer = NULL;
227 }
228 else if (pTimer->gtimer)
229 {
230 vbi_gtimer_end(pTimer->gtimer);
231 pTimer->gtimer = NULL;
232 }
233
234 return VINF_SUCCESS;
235}
236
237
238
239RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
240{
241 return vbi_timer_granularity();
242}
243
244
245RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
246{
247 return VERR_NOT_SUPPORTED;
248}
249
250
251RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
252{
253 return VERR_NOT_SUPPORTED;
254}
255
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