michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * OS/2 interval timers michael@0: * michael@0: */ michael@0: michael@0: #include "primpl.h" michael@0: michael@0: static PRBool useHighResTimer = PR_FALSE; michael@0: PRIntervalTime _os2_ticksPerSec = -1; michael@0: PRIntn _os2_bitShift = 0; michael@0: PRInt32 _os2_highMask = 0; michael@0: michael@0: void michael@0: _PR_MD_INTERVAL_INIT() michael@0: { michael@0: char *envp; michael@0: ULONG timerFreq; michael@0: APIRET rc; michael@0: michael@0: if ((envp = getenv("NSPR_OS2_NO_HIRES_TIMER")) != NULL) { michael@0: if (atoi(envp) == 1) michael@0: return; michael@0: } michael@0: michael@0: timerFreq = 0; /* OS/2 high-resolution timer frequency in Hz */ michael@0: rc = DosTmrQueryFreq(&timerFreq); michael@0: if (NO_ERROR == rc) { michael@0: useHighResTimer = PR_TRUE; michael@0: PR_ASSERT(timerFreq != 0); michael@0: while (timerFreq > PR_INTERVAL_MAX) { michael@0: timerFreq >>= 1; michael@0: _os2_bitShift++; michael@0: _os2_highMask = (_os2_highMask << 1)+1; michael@0: } michael@0: michael@0: _os2_ticksPerSec = timerFreq; michael@0: PR_ASSERT(_os2_ticksPerSec > PR_INTERVAL_MIN); michael@0: } michael@0: } michael@0: michael@0: PRIntervalTime michael@0: _PR_MD_GET_INTERVAL() michael@0: { michael@0: if (useHighResTimer) { michael@0: QWORD timestamp; michael@0: PRInt32 top; michael@0: APIRET rc = DosTmrQueryTime(×tamp); michael@0: if (NO_ERROR != rc) { michael@0: return -1; michael@0: } michael@0: /* Sadly, nspr requires the interval to range from 1000 ticks per michael@0: * second to only 100000 ticks per second. DosTmrQueryTime is too michael@0: * high resolution... michael@0: */ michael@0: top = timestamp.ulHi & _os2_highMask; michael@0: top = top << (32 - _os2_bitShift); michael@0: timestamp.ulLo = timestamp.ulLo >> _os2_bitShift; michael@0: timestamp.ulLo = timestamp.ulLo + top; michael@0: return (PRUint32)timestamp.ulLo; michael@0: } else { michael@0: ULONG msCount = -1; michael@0: DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &msCount, sizeof(msCount)); michael@0: return msCount; michael@0: } michael@0: } michael@0: michael@0: PRIntervalTime michael@0: _PR_MD_INTERVAL_PER_SEC() michael@0: { michael@0: if (useHighResTimer) { michael@0: return _os2_ticksPerSec; michael@0: } else { michael@0: return 1000; michael@0: } michael@0: }