VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/os2/timer-r0drv-os2.cpp@ 98187

Last change on this file since 98187 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.7 KB
Line 
1/* $Id: timer-r0drv-os2.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Memory Allocation, Ring-0 Driver, OS/2.
4 */
5
6/*
7 * Contributed by knut st. osmundsen.
8 *
9 * Copyright (C) 2007-2023 Oracle and/or its affiliates.
10 *
11 * This file is part of VirtualBox base platform packages, as
12 * available from https://www.virtualbox.org.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation, in version 3 of the
17 * License.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see <https://www.gnu.org/licenses>.
26 *
27 * The contents of this file may alternatively be used under the terms
28 * of the Common Development and Distribution License Version 1.0
29 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
30 * in the VirtualBox distribution, in which case the provisions of the
31 * CDDL are applicable instead of those of the GPL.
32 *
33 * You may elect to license modified versions of this file under the
34 * terms and conditions of either the GPL or the CDDL or both.
35 *
36 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
37 * --------------------------------------------------------------------
38 *
39 * This code is based on:
40 *
41 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
42 *
43 * Permission is hereby granted, free of charge, to any person
44 * obtaining a copy of this software and associated documentation
45 * files (the "Software"), to deal in the Software without
46 * restriction, including without limitation the rights to use,
47 * copy, modify, merge, publish, distribute, sublicense, and/or sell
48 * copies of the Software, and to permit persons to whom the
49 * Software is furnished to do so, subject to the following
50 * conditions:
51 *
52 * The above copyright notice and this permission notice shall be
53 * included in all copies or substantial portions of the Software.
54 *
55 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
56 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
57 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
58 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
59 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
60 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
61 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
62 * OTHER DEALINGS IN THE SOFTWARE.
63 */
64
65
66/*********************************************************************************************************************************
67* Header Files *
68*********************************************************************************************************************************/
69#include "the-os2-kernel.h"
70
71#include <iprt/timer.h>
72#include <iprt/time.h>
73#include <iprt/spinlock.h>
74#include <iprt/err.h>
75#include <iprt/asm.h>
76#include <iprt/assert.h>
77#include <iprt/alloc.h>
78
79#include "internal/magics.h"
80
81
82/*********************************************************************************************************************************
83* Structures and Typedefs *
84*********************************************************************************************************************************/
85/**
86 * The internal representation of an OS/2 timer handle.
87 */
88typedef struct RTTIMER
89{
90 /** Magic.
91 * This is RTTIMER_MAGIC, but changes to something else before the timer
92 * is destroyed to indicate clearly that thread should exit. */
93 uint32_t volatile u32Magic;
94 /** The next timer in the timer list. */
95 PRTTIMER pNext;
96 /** Flag indicating the timer is suspended. */
97 uint8_t volatile fSuspended;
98 /** Cleared at the start of timer processing, set when calling pfnTimer.
99 * If any timer changes occurs while doing the callback this will be used to resume the cycle. */
100 bool fDone;
101 /** Callback. */
102 PFNRTTIMER pfnTimer;
103 /** User argument. */
104 void *pvUser;
105 /** The timer interval. 0 if one-shot. */
106 uint64_t u64NanoInterval;
107 /** The start of the current run.
108 * This is used to calculate when the timer ought to fire the next time. */
109 uint64_t volatile u64StartTS;
110 /** The start of the current run.
111 * This is used to calculate when the timer ought to fire the next time. */
112 uint64_t volatile u64NextTS;
113 /** The current tick number (since u64StartTS). */
114 uint64_t volatile iTick;
115} RTTIMER;
116
117
118/*********************************************************************************************************************************
119* Global Variables *
120*********************************************************************************************************************************/
121/** Spinlock protecting the timers. */
122static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
123/** The timer head. */
124static PRTTIMER volatile g_pTimerHead = NULL;
125/** The number of active timers. */
126static uint32_t volatile g_cActiveTimers = 0;
127/** The number of active timers. */
128static uint32_t volatile g_cTimers = 0;
129/** The change number.
130 * This is used to detect list changes during the timer callback loop. */
131static uint32_t volatile g_u32ChangeNo;
132
133
134/*********************************************************************************************************************************
135* Internal Functions *
136*********************************************************************************************************************************/
137RT_C_DECLS_BEGIN
138DECLASM(void) rtTimerOs2Tick(void);
139DECLASM(int) rtTimerOs2Arm(void);
140DECLASM(int) rtTimerOs2Dearm(void);
141RT_C_DECLS_END
142
143
144
145RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
146{
147 *ppTimer = NULL;
148
149 /*
150 * We don't support the fancy MP features.
151 */
152 if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
153 return VERR_NOT_SUPPORTED;
154
155 /*
156 * Lazy initialize the spinlock.
157 */
158 if (g_Spinlock == NIL_RTSPINLOCK)
159 {
160 RTSPINLOCK Spinlock;
161 int rc = RTSpinlockCreate(&Spinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "RTTimerOS2");
162 AssertRCReturn(rc, rc);
163 //bool fRc;
164 //ASMAtomicCmpXchgSize(&g_Spinlock, Spinlock, NIL_RTSPINLOCK, fRc);
165 //if (!fRc)
166 if (!ASMAtomicCmpXchgPtr((void * volatile *)&g_Spinlock, Spinlock, NIL_RTSPINLOCK))
167 RTSpinlockDestroy(Spinlock);
168 }
169
170 /*
171 * Allocate and initialize the timer handle.
172 */
173 PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
174 if (!pTimer)
175 return VERR_NO_MEMORY;
176
177 pTimer->u32Magic = RTTIMER_MAGIC;
178 pTimer->pNext = NULL;
179 pTimer->fSuspended = true;
180 pTimer->pfnTimer = pfnTimer;
181 pTimer->pvUser = pvUser;
182 pTimer->u64NanoInterval = u64NanoInterval;
183 pTimer->u64StartTS = 0;
184
185 /*
186 * Insert the timer into the list (LIFO atm).
187 */
188 RTSpinlockAcquire(g_Spinlock);
189 g_u32ChangeNo++;
190 pTimer->pNext = g_pTimerHead;
191 g_pTimerHead = pTimer;
192 g_cTimers++;
193 RTSpinlockRelease(g_Spinlock);
194
195 *ppTimer = pTimer;
196 return VINF_SUCCESS;
197}
198
199
200/**
201 * Validates the timer handle.
202 *
203 * @returns true if valid, false if invalid.
204 * @param pTimer The handle.
205 */
206DECLINLINE(bool) rtTimerIsValid(PRTTIMER pTimer)
207{
208 AssertPtrReturn(pTimer, false);
209 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
210 return true;
211}
212
213
214RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
215{
216 /* It's ok to pass NULL pointer. */
217 if (pTimer == /*NIL_RTTIMER*/ NULL)
218 return VINF_SUCCESS;
219 if (!rtTimerIsValid(pTimer))
220 return VERR_INVALID_HANDLE;
221
222 /*
223 * Remove it from the list.
224 */
225 RTSpinlockAcquire(g_Spinlock);
226 g_u32ChangeNo++;
227 if (g_pTimerHead == pTimer)
228 g_pTimerHead = pTimer->pNext;
229 else
230 {
231 PRTTIMER pPrev = g_pTimerHead;
232 while (pPrev->pNext != pTimer)
233 {
234 pPrev = pPrev->pNext;
235 if (RT_UNLIKELY(!pPrev))
236 {
237 RTSpinlockRelease(g_Spinlock);
238 return VERR_INVALID_HANDLE;
239 }
240 }
241 pPrev->pNext = pTimer->pNext;
242 }
243 Assert(g_cTimers > 0);
244 g_cTimers--;
245 if (!pTimer->fSuspended)
246 {
247 Assert(g_cActiveTimers > 0);
248 g_cActiveTimers--;
249 if (!g_cActiveTimers)
250 rtTimerOs2Dearm();
251 }
252 RTSpinlockRelease(g_Spinlock);
253
254 /*
255 * Free the associated resources.
256 */
257 pTimer->u32Magic++;
258 RTMemFree(pTimer);
259 return VINF_SUCCESS;
260}
261
262
263RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
264{
265 if (!rtTimerIsValid(pTimer))
266 return VERR_INVALID_HANDLE;
267 if (!pTimer->fSuspended)
268 return VERR_TIMER_ACTIVE;
269
270 /*
271 * Calc when it should start firing and give the thread a kick so it get going.
272 */
273 u64First += RTTimeNanoTS();
274
275 RTSpinlockAcquire(g_Spinlock);
276 g_u32ChangeNo++;
277 if (!g_cActiveTimers)
278 {
279 int rc = rtTimerOs2Arm();
280 if (RT_FAILURE(rc))
281 {
282 RTSpinlockRelease(g_Spinlock);
283 return rc;
284 }
285 }
286 g_cActiveTimers++;
287 pTimer->fSuspended = false;
288 pTimer->fDone = true; /* next tick, not current! */
289 pTimer->iTick = 0;
290 pTimer->u64StartTS = u64First;
291 pTimer->u64NextTS = u64First;
292 RTSpinlockRelease(g_Spinlock);
293
294 return VINF_SUCCESS;
295}
296
297
298RTDECL(int) RTTimerStop(PRTTIMER pTimer)
299{
300 if (!rtTimerIsValid(pTimer))
301 return VERR_INVALID_HANDLE;
302 if (pTimer->fSuspended)
303 return VERR_TIMER_SUSPENDED;
304
305 /*
306 * Suspend the timer.
307 */
308 RTSpinlockAcquire(g_Spinlock);
309 g_u32ChangeNo++;
310 pTimer->fSuspended = true;
311 Assert(g_cActiveTimers > 0);
312 g_cActiveTimers--;
313 if (!g_cActiveTimers)
314 rtTimerOs2Dearm();
315 RTSpinlockRelease(g_Spinlock);
316
317 return VINF_SUCCESS;
318}
319
320
321RTDECL(int) RTTimerChangeInterval(PRTTIMER pTimer, uint64_t u64NanoInterval)
322{
323 if (!rtTimerIsValid(pTimer))
324 return VERR_INVALID_HANDLE;
325 RT_NOREF(u64NanoInterval);
326 return VERR_NOT_SUPPORTED;
327}
328
329
330DECLASM(void) rtTimerOs2Tick(void)
331{
332 /*
333 * Query the current time and then take the lock.
334 */
335 const uint64_t u64NanoTS = RTTimeNanoTS();
336
337 RTSpinlockAcquire(g_Spinlock);
338
339 /*
340 * Clear the fDone flag.
341 */
342 PRTTIMER pTimer;
343 for (pTimer = g_pTimerHead; pTimer; pTimer = pTimer->pNext)
344 pTimer->fDone = false;
345
346 /*
347 * Walk the timer list and do the callbacks for any active timer.
348 */
349 uint32_t u32CurChangeNo = g_u32ChangeNo;
350 pTimer = g_pTimerHead;
351 while (pTimer)
352 {
353 PRTTIMER pNext = pTimer->pNext;
354 if ( !pTimer->fSuspended
355 && !pTimer->fDone
356 && pTimer->u64NextTS <= u64NanoTS)
357 {
358 pTimer->fDone = true;
359 pTimer->iTick++;
360
361 /* calculate the next timeout */
362 if (!pTimer->u64NanoInterval)
363 pTimer->fSuspended = true;
364 else
365 {
366 pTimer->u64NextTS = pTimer->u64StartTS + pTimer->iTick * pTimer->u64NanoInterval;
367 if (pTimer->u64NextTS < u64NanoTS)
368 pTimer->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
369 }
370
371 /* do the callout */
372 PFNRTTIMER pfnTimer = pTimer->pfnTimer;
373 void *pvUser = pTimer->pvUser;
374 RTSpinlockRelease(g_Spinlock);
375 pfnTimer(pTimer, pvUser, pTimer->iTick);
376
377 RTSpinlockAcquire(g_Spinlock);
378
379 /* check if anything changed. */
380 if (u32CurChangeNo != g_u32ChangeNo)
381 {
382 u32CurChangeNo = g_u32ChangeNo;
383 pNext = g_pTimerHead;
384 }
385 }
386
387 /* next */
388 pTimer = pNext;
389 }
390
391 RTSpinlockRelease(g_Spinlock);
392}
393
394
395RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
396{
397 return 32000000; /* 32ms */
398}
399
400
401RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
402{
403 RT_NOREF(u32Request, pu32Granted);
404 return VERR_NOT_SUPPORTED;
405}
406
407
408RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
409{
410 RT_NOREF(u32Granted);
411 return VERR_NOT_SUPPORTED;
412}
413
414
415RTDECL(bool) RTTimerCanDoHighResolution(void)
416{
417 return false;
418}
419
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