nsprpub/pr/src/md/os2/os2inrval.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/src/md/os2/os2inrval.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,80 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * OS/2 interval timers
    1.11 + *
    1.12 + */
    1.13 +
    1.14 +#include "primpl.h"
    1.15 +
    1.16 +static PRBool useHighResTimer = PR_FALSE;
    1.17 +PRIntervalTime _os2_ticksPerSec = -1;
    1.18 +PRIntn _os2_bitShift = 0;
    1.19 +PRInt32 _os2_highMask = 0;
    1.20 +   
    1.21 +void
    1.22 +_PR_MD_INTERVAL_INIT()
    1.23 +{
    1.24 +    char *envp;
    1.25 +    ULONG timerFreq;
    1.26 +    APIRET rc;
    1.27 +
    1.28 +    if ((envp = getenv("NSPR_OS2_NO_HIRES_TIMER")) != NULL) {
    1.29 +        if (atoi(envp) == 1)
    1.30 +           return;
    1.31 +    }
    1.32 +
    1.33 +    timerFreq = 0; /* OS/2 high-resolution timer frequency in Hz */
    1.34 +    rc = DosTmrQueryFreq(&timerFreq);
    1.35 +    if (NO_ERROR == rc) {
    1.36 +        useHighResTimer = PR_TRUE;
    1.37 +        PR_ASSERT(timerFreq != 0);
    1.38 +        while (timerFreq > PR_INTERVAL_MAX) {
    1.39 +            timerFreq >>= 1;
    1.40 +            _os2_bitShift++;
    1.41 +            _os2_highMask = (_os2_highMask << 1)+1;
    1.42 +        }
    1.43 +
    1.44 +        _os2_ticksPerSec = timerFreq;
    1.45 +        PR_ASSERT(_os2_ticksPerSec > PR_INTERVAL_MIN);
    1.46 +    }
    1.47 +}
    1.48 +
    1.49 +PRIntervalTime
    1.50 +_PR_MD_GET_INTERVAL()
    1.51 +{
    1.52 +    if (useHighResTimer) {
    1.53 +        QWORD timestamp;
    1.54 +        PRInt32 top;
    1.55 +        APIRET rc = DosTmrQueryTime(&timestamp);
    1.56 +        if (NO_ERROR != rc) {
    1.57 +            return -1;
    1.58 +        }
    1.59 +        /* Sadly, nspr requires the interval to range from 1000 ticks per
    1.60 +         * second to only 100000 ticks per second. DosTmrQueryTime is too
    1.61 +         * high resolution...
    1.62 +         */
    1.63 +        top = timestamp.ulHi & _os2_highMask;
    1.64 +        top = top << (32 - _os2_bitShift);
    1.65 +        timestamp.ulLo = timestamp.ulLo >> _os2_bitShift;   
    1.66 +        timestamp.ulLo = timestamp.ulLo + top; 
    1.67 +        return (PRUint32)timestamp.ulLo;
    1.68 +    } else {
    1.69 +        ULONG msCount = -1;
    1.70 +        DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &msCount, sizeof(msCount));
    1.71 +        return msCount;
    1.72 +    }
    1.73 +}
    1.74 +
    1.75 +PRIntervalTime
    1.76 +_PR_MD_INTERVAL_PER_SEC()
    1.77 +{
    1.78 +    if (useHighResTimer) {
    1.79 +        return _os2_ticksPerSec;
    1.80 +    } else {
    1.81 +        return 1000;
    1.82 +    }
    1.83 +}

mercurial