VirtualBox

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

Last change on this file since 4612 was 4474, checked in by vboxsync, 17 years ago

Solaris.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1/** $Id: timer-r0drv-solaris.c 4474 2007-08-31 19:23:49Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Timer, Ring-0 Driver, Solaris.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include "the-solaris-kernel.h"
23
24#include <iprt/timer.h>
25#include <iprt/time.h>
26#include <iprt/spinlock.h>
27#include <iprt/err.h>
28#include <iprt/asm.h>
29#include <iprt/assert.h>
30#include <iprt/alloc.h>
31
32#include "internal/magics.h"
33
34
35/*******************************************************************************
36* Structures and Typedefs *
37*******************************************************************************/
38/**
39 * The internal representation of a Solaris timer handle.
40 */
41typedef struct RTTIMER
42{
43 /** Magic.
44 * This is RTTIMER_MAGIC, but changes to something else before the timer
45 * is destroyed to indicate clearly that thread should exit. */
46 uint32_t volatile u32Magic;
47 /** Flag indicating the the timer is suspended. */
48 uint8_t volatile fSuspended;
49 /** Whether the timer must run on a specific CPU or not. */
50 uint8_t fSpecificCpu;
51 /** The CPU it must run on if fSpecificCpu is set. */
52 uint8_t iCpu;
53 /** The Solaris cyclic structure. */
54 cyc_handler_t CyclicInfo;
55 /** The Solaris cyclic handle. */
56 cyclic_id_t CyclicID;
57 /** Callback. */
58 PFNRTTIMER pfnTimer;
59 /** User argument. */
60 void *pvUser;
61 /** The timer interval. 0 for one-shot timer */
62 uint64_t u64NanoInterval;
63} RTTIMER;
64
65
66/*******************************************************************************
67* Internal Functions *
68*******************************************************************************/
69static void rtTimerSolarisCallback(void *pvTimer);
70static void rtTimerSolarisStop(PRTTIMER pTimer);
71
72
73RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
74{
75 *ppTimer = NULL;
76
77 /*
78 * Validate flags.
79 */
80 if (!RTTIMER_FLAGS_IS_VALID(fFlags))
81 return VERR_INVALID_PARAMETER;
82 if ( (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
83 /** @todo implement && (fFlags & RTTIMER_FLAGS_CPU_ALL) != RTTIMER_FLAGS_CPU_ALL*/)
84 return VERR_NOT_SUPPORTED;
85
86 /*
87 * Allocate and initialize the timer handle.
88 */
89 PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
90 if (!pTimer)
91 return VERR_NO_MEMORY;
92
93 pTimer->u32Magic = RTTIMER_MAGIC;
94 pTimer->fSuspended = true;
95 pTimer->fSpecificCpu = !!(fFlags & RTTIMER_FLAGS_CPU_SPECIFIC);
96 pTimer->iCpu = fFlags & RTTIMER_FLAGS_CPU_MASK;
97 pTimer->CyclicInfo.cyh_func = rtTimerSolarisCallback;
98 pTimer->CyclicInfo.cyh_level = CY_LOCK_LEVEL;
99 pTimer->CyclicInfo.cyh_arg = pTimer;
100 pTimer->CyclicID = CYCLIC_NONE;
101 pTimer->u64NanoInterval = u64NanoInterval;
102 pTimer->pfnTimer = pfnTimer;
103 pTimer->pvUser = pvUser;
104
105 *ppTimer = pTimer;
106 return VINF_SUCCESS;
107}
108
109
110/**
111 * Validates the timer handle.
112 *
113 * @returns true if valid, false if invalid.
114 * @param pTimer The handle.
115 */
116DECLINLINE(bool) rtTimerIsValid(PRTTIMER pTimer)
117{
118 AssertReturn(VALID_PTR(pTimer), false);
119 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
120 return true;
121}
122
123
124RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
125{
126 if (pTimer == NULL)
127 return VINF_SUCCESS;
128 if (!rtTimerIsValid(pTimer))
129 return VERR_INVALID_HANDLE;
130
131 /*
132 * Free the associated resources.
133 */
134 pTimer->u32Magic++;
135 rtTimerSolarisStop(pTimer);
136 RTMemFree(pTimer);
137 return VINF_SUCCESS;
138}
139
140
141RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
142{
143 cyc_time_t timerSpec;
144
145 if (!rtTimerIsValid(pTimer))
146 return VERR_INVALID_HANDLE;
147 if (!pTimer->fSuspended)
148 return VERR_TIMER_ACTIVE;
149
150 /*
151 * Calc when it should start fireing.
152 */
153 u64First += RTTimeNanoTS();
154
155 pTimer->fSuspended = false;
156 timerSpec.cyt_when = u64First;
157 timerSpec.cyt_interval = pTimer->u64NanoInterval == 0 ? u64First : pTimer->u64NanoInterval;
158
159 rtTimerSolarisStop(pTimer); /** @todo r=bird: can't happen. */
160 pTimer->CyclicID = cyclic_add(&pTimer->CyclicInfo, &timerSpec);
161 return VINF_SUCCESS;
162}
163
164
165RTDECL(int) RTTimerStop(PRTTIMER pTimer)
166{
167 if (!rtTimerIsValid(pTimer))
168 return VERR_INVALID_HANDLE;
169 if (pTimer->fSuspended)
170 return VERR_TIMER_SUSPENDED;
171
172 /*
173 * Suspend the timer.
174 */
175 pTimer->fSuspended = true;
176 rtTimerSolarisStop(pTimer);
177
178 return VINF_SUCCESS;
179}
180
181
182static void rtTimerSolarisCallback(void *pvTimer)
183{
184 PRTTIMER pTimer = (PRTTIMER)pvTimer;
185
186 /* If this is a one shot timer, call pfnTimer and suspend
187 * as Solaris does not support 0 interval timers implictly
188 */
189 if (!pTimer->u64NanoInterval)
190 {
191 pTimer->fSuspended = true;
192 rtTimerSolarisStop(pTimer);
193 }
194
195 /* Callback user defined callback function */
196 pTimer->pfnTimer(pTimer, pTimer->pvUser);
197}
198
199
200static void rtTimerSolarisStop(PRTTIMER pTimer)
201{
202 /* Important we check for invalid cyclic object */
203 if (pTimer->CyclicID != CYCLIC_NONE)
204 {
205 cyclic_remove(pTimer->CyclicID);
206 pTimer->CyclicID = CYCLIC_NONE;
207 }
208}
209
210
211RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
212{
213 return nsec_per_tick;
214}
215
216
217RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
218{
219 return VERR_NOT_SUPPORTED;
220}
221
222
223RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
224{
225 return VERR_NOT_SUPPORTED;
226}
227
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