NSPR Reference Previous Contents Next |
PRIntervalTime
, for timing intervals of
fewer than approximately 6 hours. This chapter describes PRIntervalTime
and the
functions that allow you to use it for timing purposes:
Interval Time Type and Constants
Interval Functions
PRIntervalTime
. Such parameters are common in NSPR functions
such as those used for I/O operations and operations on condition variables.
NSPR 2.0 provides interval times that are efficient in terms of performance and storage requirements. Conceptually, they are based on free-running counters that increment at a fixed rate without possibility of outside influence (as might be observed if one was using a time-of-day clock that gets reset due to some administrative action). The counters have no fixed epoch and have a finite period. To make use of these counters, the application must declare a point in time, the epoch, and an amount of time elapsed since that epoch, the interval. In almost all cases the epoch is defined as the value of the interval timer at the time it was sampled.
#include <prinrval.h>
typedef PRUint32 PRIntervalTime;
#define PR_INTERVAL_MIN 1000UL
#define PR_INTERVAL_MAX 100000UL
#define PR_INTERVAL_NO_WAIT 0UL
#define PR_INTERVAL_NO_TIMEOUT 0xffffffffUL
PRIntervalTime
are platform-dependent. They are chosen to be
appropriate for the host OS, yet provide sufficient resolution and period to be
useful to clients.
The increasing interval value represented by PRIntervalTime
wraps. It should
therefore never be used for intervals greater than approximately 6 hours. Interval
times are accurate regardless of host processing requirements and are very cheap
to acquire.
The constants PR_INTERVAL_MIN
and PR_INTERVAL_MAX
define a range in ticks per
second. These constants bound both the period and the resolution of a
PRIntervalTime
object.
The reserved constants PR_INTERVAL_NO_WAIT
and PR_INTERVAL_NO_TIMEOUT
have special meaning for NSPR. They indicate that the process should wait no time
(return immediately) or wait forever (never time out), respectively.
if ((PRIntervalTime)(now - epoch) > interval)
<... interval has expired ...>
As long as the interval and the elapsed time (now - epoch
) do not exceed half the
namespace allowed by a PRIntervalTime
(231-1), the expression shown above
provides the expected result even if the signs of now
and epoch
differ.
The resolution of a PRIntervalTime
object is defined by the API. NSPR guarantees
that there will be at least 1000 ticks per second and not more than 100000. At the
maximum resolution of 10000 ticks per second, each tick represents 1/100000 of a
second. At that rate, a 32-bit register will overflow in approximately 28 hours,
making the maximum useful interval approximately 6 hours. Waiting on events
more than half a day in the future must therefore be based on a calendar time.
Getting the Current Interval and Ticks Per Second
Converting Standard Clock Units to Platform-Dependent Intervals
Converting Platform-Dependent Intervals to Standard Clock Units
#include <prinrval.h>
PRIntervalTime PR_IntervalNow(void);
PRIntervalTime
object.
PR_IntervalNow
to establish epochs and to
determine intervals (that is, compute the difference between two times).
PR_IntervalNow
is both very efficient and nonblocking, so it is appropriate to use
(for example) while holding a mutex.
The most common use for PR_IntervalNow
() is to establish an epoch and test for
the expiration of intervals. In this case, you typically call PR_IntervalNow
in a
sequence that looks like this:
PRStatus rv;
PRIntervalTime epoch = PR_IntervalNow();
PR_Lock(data->mutex);
if (!EvaluateData(data))
{
while (PR_TRUE)
{
rv = PR_Wait(data->condition, interval);
if (PR_FAILURE == rv) break; /* likely an interrupt */
if (EvaluateData(data)) break; /* condition is met */
if ((PRIntervalTime)(PR_IntervalNow() - epoch) > interval) break; /* timeout */
}
}
PR_Unlock(data->mutex);
Note the casting of (PR_InternvalNow() - epoch) back to a
PRIntervalTime . In C, the difference between any two fields is
signed. The comparison performed by PR_IntervalNow relies on
unsigned (modulo 2) arithmetic to function properly.
|
PRIntervalTime
.
#include <prinrval.h>
PRUint32 PR_TicksPerSecond(void);
PRIntervalTime
on the current platform. This value is platform
dependent and does not change after NSPR is initialized.
PR_TicksPerSecond
lies between PR_INTERVAL_MIN
and
PR_INTERVAL_MAX
.
The relationship between a PRIntervalTime
tick and standard clock units is
platform-dependent. PR_TicksPerSecond
allows you to discover exactly what that
relationship is. Seconds per tick (the inverse of PR_TicksPerSecond
) is always
between 10 microseconds and 1 millisecond.
PRIntervalTime
.
These functions are not expensive. However, if the value being converted is a constant, it is a good idea to make the conversion once, early, and cache it for later use.
These functions perform mathematical rounding. If that is not a desirable feature,
you should provide your own conversions based on PR_TicksPerSecond
.
#include <prinrval.h>
PRIntervalTime PR_SecondsToInterval(PRUint32 seconds);
seconds
parameter.
#include <prinrval.h>
PRIntervalTime PR_MillisecondsToInterval(PRUint32 milli);
milli
parameter.
#include <prinrval.h>
PRIntervalTime PPR_MicrosecondsToInterval(PRUint32 micro);
micro
parameter.
PRIntervalTime
.
These functions are not expensive. However, if the value being converted is a constant, it is a good idea to make the conversion once, early, and cache it for later use.
These functions perform mathematical rounding. If that is not a desirable feature,
you should provide your own conversions based on PR_TicksPerSecond
.
#include <prinrval.h>
PRUint32 PR_IntervalToSeconds(PRIntervalTime ticks);
ticks
parameter.
#include <prinrval.h>
PRUint32 PR_IntervalToMilliseconds(PRIntervalTime ticks);
ticks
parameter.
#include <prinrval.h>
PRUint32 PR_IntervalToMicroseconds(PRIntervalTime ticks);
Last Updated May 18, 2001