~ubuntu-branches/ubuntu/precise/virtualbox/precise-updates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/* $Id: timer-win.cpp $ */
/** @file
 * IPRT - Timer.
 */

/*
 * Copyright (C) 2006-2007 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
 * VirtualBox OSE distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 */


/* Which code to use is determined here...
 *
 * The default is to use wait on NT timers directly with no APC since this
 * is supposed to give the shortest kernel code paths.
 *
 * The USE_APC variation will do as above except that an APC routine is
 * handling the callback action.
 *
 * The USE_WINMM version will use the NT timer wrappers in WinMM which may
 * result in some 0.1% better correctness in number of delivered ticks. However,
 * this codepath have more overhead (it uses APC among other things), and I'm not
 * quite sure if it's actually any more correct.
 *
 * The USE_CATCH_UP will play catch up when the timer lags behind. However this
 * requires a monotonous time source.
 *
 * The default mode which we are using is using relative periods of time and thus
 * will never suffer from errors in the time source. Neither will it try catch up
 * missed ticks. This suits our current purposes best I'd say.
 */
#undef USE_APC
#undef USE_WINMM
#undef USE_CATCH_UP


/*******************************************************************************
*   Header Files                                                               *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_TIMER
#define _WIN32_WINNT 0x0500
#include <Windows.h>

#include <iprt/timer.h>
#ifdef USE_CATCH_UP
# include <iprt/time.h>
#endif
#include <iprt/alloc.h>
#include <iprt/assert.h>
#include <iprt/thread.h>
#include <iprt/log.h>
#include <iprt/asm.h>
#include <iprt/semaphore.h>
#include <iprt/err.h>
#include "internal/magics.h"

RT_C_DECLS_BEGIN
/* from sysinternals. */
NTSYSAPI LONG NTAPI NtSetTimerResolution(IN ULONG DesiredResolution, IN BOOLEAN SetResolution, OUT PULONG CurrentResolution);
NTSYSAPI LONG NTAPI NtQueryTimerResolution(OUT PULONG MaximumResolution, OUT PULONG MinimumResolution, OUT PULONG CurrentResolution);
RT_C_DECLS_END


/*******************************************************************************
*   Structures and Typedefs                                                    *
*******************************************************************************/
/**
 * The internal representation of a timer handle.
 */
typedef struct RTTIMER
{
    /** Magic.
     * This is RTTIMER_MAGIC, but changes to something else before the timer
     * is destroyed to indicate clearly that thread should exit. */
    volatile uint32_t       u32Magic;
    /** User argument. */
    void                   *pvUser;
    /** Callback. */
    PFNRTTIMER              pfnTimer;
    /** The current tick. */
    uint64_t                iTick;
    /** The interval. */
    unsigned                uMilliesInterval;
#ifdef USE_WINMM
    /** Win32 timer id. */
    UINT                    TimerId;
#else
    /** Time handle. */
    HANDLE                  hTimer;
# ifdef USE_APC
    /** Handle to wait on. */
    HANDLE                  hevWait;
# endif
    /** USE_CATCH_UP: ns time of the next tick.
     * !USE_CATCH_UP: -uMilliesInterval * 10000 */
    LARGE_INTEGER           llNext;
    /** The thread handle of the timer thread. */
    RTTHREAD                Thread;
    /** The error/status of the timer.
     * Initially -1, set to 0 when the timer have been successfully started, and
     * to errno on failure in starting the timer. */
    volatile int            iError;
#endif
} RTTIMER;



#ifdef USE_WINMM
/**
 * Win32 callback wrapper.
 */
static void CALLBACK rttimerCallback(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
{
    PRTTIMER pTimer = (PRTTIMER)(void *)dwUser;
    Assert(pTimer->TimerId == uTimerID);
    pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pTimer->iTick);
    NOREF(uMsg); NOREF(dw1); NOREF(dw2); NOREF(uTimerID);
}
#else /* !USE_WINMM */

#ifdef USE_APC
/**
 * Async callback.
 *
 * @param   lpArgToCompletionRoutine    Pointer to our timer structure.
 */
VOID CALLBACK rttimerAPCProc(LPVOID lpArgToCompletionRoutine, DWORD dwTimerLowValue, DWORD dwTimerHighValue)
{
    PRTTIMER pTimer = (PRTTIMER)lpArgToCompletionRoutine;

    /*
     * Check if we're begin destroyed.
     */
    if (pTimer->u32Magic != RTTIMER_MAGIC)
        return;

    /*
     * Callback the handler.
     */
    pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pTimer->iTick);

    /*
     * Rearm the timer handler.
     */
#ifdef USE_CATCH_UP
    pTimer->llNext.QuadPart += (int64_t)pTimer->uMilliesInterval * 10000;
    LARGE_INTEGER ll;
    ll.QuadPart = RTTimeNanoTS() - pTimer->llNext.QuadPart;
    if (ll.QuadPart < -500000)
        ll.QuadPart = ll.QuadPart / 100;
    else
        ll.QuadPart = -500000 / 100; /* need to catch up, do a minimum wait of 0.5ms. */
#else
    LARGE_INTEGER ll = pTimer->llNext;
#endif
    BOOL frc = SetWaitableTimer(pTimer->hTimer, &ll, 0, rttimerAPCProc, pTimer, FALSE);
    AssertMsg(frc || pTimer->u32Magic != RTTIMER_MAGIC, ("last error %d\n", GetLastError()));
}
#endif /* USE_APC */

/**
 * Timer thread.
 */
static DECLCALLBACK(int) rttimerCallback(RTTHREAD Thread, void *pvArg)
{
    PRTTIMER pTimer = (PRTTIMER)(void *)pvArg;
    Assert(pTimer->u32Magic == RTTIMER_MAGIC);

    /*
     * Bounce our priority up quite a bit.
     */
    if (    !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL)
        /*&&  !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST)*/)
    {
        int rc = GetLastError();
        AssertMsgFailed(("Failed to set priority class lasterror %d.\n", rc));
        pTimer->iError = RTErrConvertFromWin32(rc);
        return rc;
    }

    /*
     * Start the waitable timer.
     */

#ifdef USE_CATCH_UP
    const int64_t NSInterval = (int64_t)pTimer->uMilliesInterval * 1000000;
    pTimer->llNext.QuadPart = RTTimeNanoTS() + NSInterval;
#else
    pTimer->llNext.QuadPart = -(int64_t)pTimer->uMilliesInterval * 10000;
#endif
    LARGE_INTEGER ll;
    ll.QuadPart = -(int64_t)pTimer->uMilliesInterval * 10000;
#ifdef USE_APC
    if (!SetWaitableTimer(pTimer->hTimer, &ll, 0, rttimerAPCProc, pTimer, FALSE))
#else
    if (!SetWaitableTimer(pTimer->hTimer, &ll, 0, NULL, NULL, FALSE))
#endif
    {
        int rc = GetLastError();
        AssertMsgFailed(("Failed to set timer, lasterr %d.\n", rc));
        pTimer->iError = RTErrConvertFromWin32(rc);
        RTThreadUserSignal(Thread);
        return rc;
    }

    /*
     * Wait for the semaphore to be posted.
     */
    RTThreadUserSignal(Thread);
    for (;pTimer->u32Magic == RTTIMER_MAGIC;)
    {
#ifdef USE_APC
        int rc = WaitForSingleObjectEx(pTimer->hevWait, INFINITE, TRUE);
        if (rc != WAIT_OBJECT_0 && rc != WAIT_IO_COMPLETION)
#else
        int rc = WaitForSingleObjectEx(pTimer->hTimer, INFINITE, FALSE);
        if (pTimer->u32Magic != RTTIMER_MAGIC)
            break;
        if (rc == WAIT_OBJECT_0)
        {
            /*
             * Callback the handler.
             */
            pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pTimer->iTick);

            /*
             * Rearm the timer handler.
             */
#ifdef USE_CATCH_UP
            pTimer->llNext.QuadPart += NSInterval;
            LARGE_INTEGER ll;
            ll.QuadPart = RTTimeNanoTS() - pTimer->llNext.QuadPart;
            if (ll.QuadPart < -500000)
                ll.QuadPart = ll.QuadPart / 100;
            else
                ll.QuadPart = -500000 / 100; /* need to catch up, do a minimum wait of 0.5ms. */
#else
            LARGE_INTEGER ll = pTimer->llNext;
#endif
            BOOL frc = SetWaitableTimer(pTimer->hTimer, &ll, 0, NULL, NULL, FALSE);
            AssertMsg(frc || pTimer->u32Magic != RTTIMER_MAGIC, ("last error %d\n", GetLastError()));
        }
        else
#endif
        {
            /*
             * We failed during wait, so just signal the destructor and exit.
             */
            int rc2 = GetLastError();
            RTThreadUserSignal(Thread);
            AssertMsgFailed(("Wait on hTimer failed, rc=%d lasterr=%d\n", rc, rc2));
            return -1;
        }
    }

    /*
     * Exit.
     */
    RTThreadUserSignal(Thread);
    return 0;
}
#endif /* !USE_WINMM */


RTDECL(int) RTTimerCreate(PRTTIMER *ppTimer, unsigned uMilliesInterval, PFNRTTIMER pfnTimer, void *pvUser)
{
#ifndef USE_WINMM
    /*
     * On windows we'll have to set the timer resolution before
     * we start the timer.
     */
    ULONG ulMax = ~0;
    ULONG ulMin = ~0;
    ULONG ulCur = ~0;
    NtQueryTimerResolution(&ulMax, &ulMin, &ulCur);
    Log(("NtQueryTimerResolution -> ulMax=%lu00ns ulMin=%lu00ns ulCur=%lu00ns\n", ulMax, ulMin, ulCur));
    if (ulCur > ulMin && ulCur > 10000 /* = 1ms */)
    {
        if (NtSetTimerResolution(10000, TRUE, &ulCur) >= 0)
            Log(("Changed timer resolution to 1ms.\n"));
        else if (NtSetTimerResolution(20000, TRUE, &ulCur) >= 0)
            Log(("Changed timer resolution to 2ms.\n"));
        else if (NtSetTimerResolution(40000, TRUE, &ulCur) >= 0)
            Log(("Changed timer resolution to 4ms.\n"));
        else if (ulMin <= 50000 && NtSetTimerResolution(ulMin, TRUE, &ulCur) >= 0)
            Log(("Changed timer resolution to %lu *100ns.\n", ulMin));
        else
        {
            AssertMsgFailed(("Failed to configure timer resolution!\n"));
            return VERR_INTERNAL_ERROR;
        }
    }
#endif /* !USE_WINN */

    /*
     * Create new timer.
     */
    int rc;
    PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
    if (pTimer)
    {
        pTimer->u32Magic    = RTTIMER_MAGIC;
        pTimer->pvUser      = pvUser;
        pTimer->pfnTimer    = pfnTimer;
        pTimer->iTick       = 0;
        pTimer->uMilliesInterval = uMilliesInterval;
#ifdef USE_WINMM
        /* sync kill doesn't work. */
        pTimer->TimerId     = timeSetEvent(uMilliesInterval, 0, rttimerCallback, (DWORD_PTR)pTimer, TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
        if (pTimer->TimerId)
        {
            ULONG ulMax = ~0;
            ULONG ulMin = ~0;
            ULONG ulCur = ~0;
            NtQueryTimerResolution(&ulMax, &ulMin, &ulCur);
            Log(("NtQueryTimerResolution -> ulMax=%lu00ns ulMin=%lu00ns ulCur=%lu00ns\n", ulMax, ulMin, ulCur));

            *ppTimer = pTimer;
            return VINF_SUCCESS;
        }
        rc = VERR_INVALID_PARAMETER;

#else /* !USE_WINMM */

        /*
         * Create Win32 event semaphore.
         */
        pTimer->iError      = 0;
        pTimer->hTimer = CreateWaitableTimer(NULL, TRUE, NULL);
        if (pTimer->hTimer)
        {
#ifdef USE_APC
            /*
             * Create wait semaphore.
             */
            pTimer->hevWait = CreateEvent(NULL, FALSE, FALSE, NULL);
            if (pTimer->hevWait)
#endif
            {
                /*
                 * Kick off the timer thread.
                 */
                rc = RTThreadCreate(&pTimer->Thread, rttimerCallback, pTimer, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "Timer");
                if (RT_SUCCESS(rc))
                {
                    /*
                     * Wait for the timer to successfully create the timer
                     * If we don't get a response in 10 secs, then we assume we're screwed.
                     */
                    rc = RTThreadUserWait(pTimer->Thread, 10000);
                    if (RT_SUCCESS(rc))
                    {
                        rc = pTimer->iError;
                        if (RT_SUCCESS(rc))
                        {
                            *ppTimer = pTimer;
                            return VINF_SUCCESS;
                        }
                    }
                    ASMAtomicXchgU32(&pTimer->u32Magic, RTTIMER_MAGIC + 1);
                    RTThreadWait(pTimer->Thread, 250, NULL);
                    CancelWaitableTimer(pTimer->hTimer);
                }
#ifdef USE_APC
                CloseHandle(pTimer->hevWait);
#endif
            }
            CloseHandle(pTimer->hTimer);
        }
#endif /* !USE_WINMM */

        AssertMsgFailed(("Failed to create timer uMilliesInterval=%d. rc=%d\n", uMilliesInterval, rc));
        RTMemFree(pTimer);
    }
    else
        rc = VERR_NO_MEMORY;
    return rc;
}


RTR3DECL(int)     RTTimerDestroy(PRTTIMER pTimer)
{
    /* NULL is ok. */
    if (!pTimer)
        return VINF_SUCCESS;

    /*
     * Validate handle first.
     */
    int rc;
    if (    VALID_PTR(pTimer)
        &&  pTimer->u32Magic == RTTIMER_MAGIC)
    {
#ifdef USE_WINMM
        /*
         * Kill the timer and exit.
         */
        rc = timeKillEvent(pTimer->TimerId);
        AssertMsg(rc == TIMERR_NOERROR, ("timeKillEvent -> %d\n", rc));
        ASMAtomicXchgU32(&pTimer->u32Magic, RTTIMER_MAGIC + 1);
        RTThreadSleep(1);

#else /* !USE_WINMM */

        /*
         * Signal that we want the thread to exit.
         */
        ASMAtomicXchgU32(&pTimer->u32Magic, RTTIMER_MAGIC + 1);
#ifdef USE_APC
        SetEvent(pTimer->hevWait);
        CloseHandle(pTimer->hevWait);
        rc = CancelWaitableTimer(pTimer->hTimer);
        AssertMsg(rc, ("CancelWaitableTimer lasterr=%d\n", GetLastError()));
#else
        LARGE_INTEGER ll = {0};
        ll.LowPart = 100;
        rc = SetWaitableTimer(pTimer->hTimer, &ll, 0, NULL, NULL, FALSE);
        AssertMsg(rc, ("CancelWaitableTimer lasterr=%d\n", GetLastError()));
#endif

        /*
         * Wait for the thread to exit.
         * And if it don't wanna exit, we'll get kill it.
         */
        rc = RTThreadWait(pTimer->Thread, 1000, NULL);
        if (RT_FAILURE(rc))
            TerminateThread((HANDLE)RTThreadGetNative(pTimer->Thread), -1);

        /*
         * Free resource.
         */
        rc = CloseHandle(pTimer->hTimer);
        AssertMsg(rc, ("CloseHandle lasterr=%d\n", GetLastError()));

#endif /* !USE_WINMM */
        RTMemFree(pTimer);
        return rc;
    }

    rc = VERR_INVALID_HANDLE;
    AssertMsgFailed(("Failed to destroy timer %p. rc=%d\n", pTimer, rc));
    return rc;
}