VirtualBox

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

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

Solaris/r0drv: Merge VBI into IPRT.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1/* $Id: timer-r0drv-solaris.c 24386 2009-11-05 14:17:10Z 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#include "internal/iprt.h"
37#include <iprt/timer.h>
38
39#include <iprt/asm.h>
40#include <iprt/assert.h>
41#include <iprt/err.h>
42#include <iprt/mem.h>
43#include <iprt/mp.h>
44#include <iprt/spinlock.h>
45#include <iprt/time.h>
46#include <iprt/thread.h>
47#include "internal/magics.h"
48
49
50/*******************************************************************************
51* Structures and Typedefs *
52*******************************************************************************/
53/**
54 * The internal representation of a Solaris timer handle.
55 */
56typedef struct RTTIMER
57{
58 /** Magic.
59 * This is RTTIMER_MAGIC, but changes to something else before the timer
60 * is destroyed to indicate clearly that thread should exit. */
61 uint32_t volatile u32Magic;
62 /** Flag indicating that the timer is suspended. */
63 uint8_t volatile fSuspended;
64 /** Run on all CPUs if set */
65 uint8_t fAllCpu;
66 /** Whether the timer must run on a specific CPU or not. */
67 uint8_t fSpecificCpu;
68 /** The CPU it must run on if fSpecificCpu is set. */
69 uint8_t iCpu;
70 /** The nano second interval for repeating timers */
71 uint64_t interval;
72 /** simple Solaris timer handle. */
73 vbi_stimer_t *stimer;
74 /** global Solaris timer handle. */
75 vbi_gtimer_t *gtimer;
76 /** The user callback. */
77 PFNRTTIMER pfnTimer;
78 /** The argument for the user callback. */
79 void *pvUser;
80} RTTIMER;
81
82
83/*******************************************************************************
84* Defined Constants And Macros *
85*******************************************************************************/
86/** Validates that the timer is valid. */
87#define RTTIMER_ASSERT_VALID_RET(pTimer) \
88 do \
89 { \
90 AssertPtrReturn(pTimer, VERR_INVALID_HANDLE); \
91 AssertReturn((pTimer)->u32Magic == RTTIMER_MAGIC, VERR_INVALID_HANDLE); \
92 } while (0)
93
94
95/*
96 * Need a wrapper to get the PRTTIMER passed through
97 */
98static void rtTimerSolarisCallbackWrapper(PRTTIMER pTimer, uint64_t tick)
99{
100 pTimer->pfnTimer(pTimer, pTimer->pvUser, tick);
101}
102
103
104
105
106RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
107{
108 RT_ASSERT_PREEMPTIBLE();
109 *ppTimer = NULL;
110
111 /*
112 * Validate flags.
113 */
114 if (!RTTIMER_FLAGS_ARE_VALID(fFlags))
115 return VERR_INVALID_PARAMETER;
116 if (vbi_revision_level < 2)
117 return VERR_NOT_SUPPORTED;
118
119 if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
120 && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL
121 && !RTMpIsCpuPossible((fFlags & RTTIMER_FLAGS_CPU_MASK)))
122 return VERR_CPU_NOT_FOUND;
123
124 if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL && u64NanoInterval == 0)
125 return VERR_NOT_SUPPORTED;
126
127 /*
128 * Allocate and initialize the timer handle.
129 */
130 PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
131 if (!pTimer)
132 return VERR_NO_MEMORY;
133
134 pTimer->u32Magic = RTTIMER_MAGIC;
135 pTimer->fSuspended = true;
136 if ((fFlags & RTTIMER_FLAGS_CPU_ALL) == RTTIMER_FLAGS_CPU_ALL)
137 {
138 pTimer->fAllCpu = true;
139 pTimer->fSpecificCpu = false;
140 pTimer->iCpu = 255;
141 }
142 else if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
143 {
144 pTimer->fAllCpu = false;
145 pTimer->fSpecificCpu = true;
146 pTimer->iCpu = fFlags & RTTIMER_FLAGS_CPU_MASK;
147 }
148 else
149 {
150 pTimer->fAllCpu = false;
151 pTimer->fSpecificCpu = false;
152 pTimer->iCpu = 255;
153 }
154 pTimer->interval = u64NanoInterval;
155 pTimer->pfnTimer = pfnTimer;
156 pTimer->pvUser = pvUser;
157 pTimer->stimer = NULL;
158 pTimer->gtimer = NULL;
159
160 *ppTimer = pTimer;
161 return VINF_SUCCESS;
162}
163
164
165RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
166{
167 if (pTimer == NULL)
168 return VINF_SUCCESS;
169 RTTIMER_ASSERT_VALID_RET(pTimer);
170 RT_ASSERT_INTS_ON();
171
172 /*
173 * Free the associated resources.
174 */
175 RTTimerStop(pTimer);
176 ASMAtomicWriteU32(pTimer, ~RTTIMER_MAGIC);
177 RTMemFree(pTimer);
178 return VINF_SUCCESS;
179}
180
181
182RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
183{
184 RTTIMER_ASSERT_VALID_RET(pTimer);
185 RT_ASSERT_INTS_ON();
186
187 if (!pTimer->fSuspended)
188 return VERR_TIMER_ACTIVE;
189
190 pTimer->fSuspended = false;
191 if (pTimer->fAllCpu)
192 {
193 pTimer->gtimer = vbi_gtimer_begin(rtTimerSolarisCallbackWrapper, pTimer, u64First, pTimer->interval);
194 if (pTimer->gtimer == NULL)
195 return VERR_INVALID_PARAMETER;
196 }
197 else
198 {
199 int cpu = VBI_ANY_CPU;
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 RTTIMER_ASSERT_VALID_RET(pTimer);
218 RT_ASSERT_INTS_ON();
219
220 if (pTimer->fSuspended)
221 return VERR_TIMER_SUSPENDED;
222
223 pTimer->fSuspended = true;
224 if (pTimer->stimer)
225 {
226 vbi_stimer_end(pTimer->stimer);
227 pTimer->stimer = NULL;
228 }
229 else if (pTimer->gtimer)
230 {
231 vbi_gtimer_end(pTimer->gtimer);
232 pTimer->gtimer = NULL;
233 }
234
235 return VINF_SUCCESS;
236}
237
238
239
240RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
241{
242 return vbi_timer_granularity();
243}
244
245
246RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
247{
248 return VERR_NOT_SUPPORTED;
249}
250
251
252RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
253{
254 return VERR_NOT_SUPPORTED;
255}
256
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