mirror of
https://github.com/wjwwood/serial.git
synced 2026-01-22 19:54:57 +08:00
style changes to new/old timespec functions
This commit is contained in:
parent
9f63f4d2b5
commit
282b79efc6
@ -54,13 +54,13 @@ using serial::IOException;
|
||||
|
||||
/* Timespec related functions provided by @mikepurvis of Clearpath Robotics */
|
||||
|
||||
/*! Smooth over platform variances in getting an accurate timespec
|
||||
/* Smooth over platform variances in getting an accurate timespec
|
||||
* representing the present moment. */
|
||||
static inline struct timespec
|
||||
timespec_now ()
|
||||
{
|
||||
struct timespec ts;
|
||||
# ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
|
||||
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
|
||||
clock_serv_t cclock;
|
||||
mach_timespec_t mts;
|
||||
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
||||
@ -68,78 +68,91 @@ timespec_now ()
|
||||
mach_port_deallocate(mach_task_self(), cclock);
|
||||
ts.tv_sec = mts.tv_sec;
|
||||
ts.tv_nsec = mts.tv_nsec;
|
||||
# else
|
||||
#else
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
# endif
|
||||
#endif
|
||||
return ts;
|
||||
}
|
||||
|
||||
/*! Simple function to normalize the tv_nsec field to [0..1e9), carrying
|
||||
/* Simple function to normalize the tv_nsec field to [0..1e9), carrying
|
||||
* the remainder into the tv_sec field. This will not protect against the
|
||||
* possibility of an overflow in the nsec field--proceed with caution. */
|
||||
static inline void
|
||||
normalize(struct timespec* ts) {
|
||||
while (ts->tv_nsec < 0) {
|
||||
normalize (struct timespec* ts)
|
||||
{
|
||||
while (ts->tv_nsec < 0)
|
||||
{
|
||||
ts->tv_nsec += 1e9;
|
||||
ts->tv_sec -= 1;
|
||||
}
|
||||
while (ts->tv_nsec >= 1e9) {
|
||||
while (ts->tv_nsec >= 1e9)
|
||||
{
|
||||
ts->tv_nsec -= 1e9;
|
||||
ts->tv_sec += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*! Return a timespec which is the sum of two other timespecs. This
|
||||
/* Return a timespec which is the sum of two other timespecs. This
|
||||
* operator only makes logical sense when one or both of the arguments
|
||||
* represents a duration. */
|
||||
static inline timespec
|
||||
operator+ (const struct timespec &a, const struct timespec &b) {
|
||||
struct timespec result = { a.tv_sec + b.tv_sec,
|
||||
a.tv_nsec + b.tv_nsec };
|
||||
operator+ (const struct timespec &a, const struct timespec &b)
|
||||
{
|
||||
struct timespec result = {
|
||||
a.tv_sec + b.tv_sec,
|
||||
a.tv_nsec + b.tv_nsec
|
||||
};
|
||||
normalize(&result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/*! Return a timespec which is the difference of two other timespecs.
|
||||
/* Return a timespec which is the difference of two other timespecs.
|
||||
* This operator only makes logical sense when one or both of the arguments
|
||||
* represents a duration. */
|
||||
static inline timespec
|
||||
operator- (const struct timespec &a, const struct timespec &b) {
|
||||
struct timespec result = { a.tv_sec - b.tv_sec,
|
||||
a.tv_nsec - b.tv_nsec };
|
||||
operator- (const struct timespec &a, const struct timespec &b)
|
||||
{
|
||||
struct timespec result = {
|
||||
a.tv_sec - b.tv_sec,
|
||||
a.tv_nsec - b.tv_nsec
|
||||
};
|
||||
normalize(&result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/*! Return a timespec which is a multiplication of a timespec and a positive
|
||||
* integer. No overflow protection-- not suitable for multiplications with
|
||||
* large carries, eg a <1s timespec multiplied by a large enough integer
|
||||
/* Return a timespec which is a multiplication of a timespec and a positive
|
||||
* integer. No overflow protection-- not suitable for multiplications with
|
||||
* large carries, eg a <1s timespec multiplied by a large enough integer
|
||||
* that the result is muliple seconds. Only makes sense when the timespec
|
||||
* argument is a duration. */
|
||||
static inline timespec
|
||||
operator* (const struct timespec &ts, const size_t mul) {
|
||||
struct timespec result = { ts.tv_sec * mul,
|
||||
ts.tv_nsec * mul };
|
||||
operator* (const struct timespec &ts, const size_t mul)
|
||||
{
|
||||
struct timespec result = {
|
||||
ts.tv_sec * mul,
|
||||
ts.tv_nsec * mul
|
||||
};
|
||||
normalize(&result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/*! Return whichever of two timespec durations represents the shortest or most
|
||||
/* Return whichever of two timespec durations represents the shortest or most
|
||||
* negative period. */
|
||||
static inline struct timespec
|
||||
min (const struct timespec &a, const struct timespec &b) {
|
||||
if (a.tv_sec < b.tv_sec
|
||||
|| (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec)) {
|
||||
min (const struct timespec &a, const struct timespec &b)
|
||||
{
|
||||
if (a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec))
|
||||
{
|
||||
return a;
|
||||
} else {
|
||||
return b;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
/*! Return a timespec duration set from a provided number of milliseconds. */
|
||||
/* Return a timespec duration set from a provided number of milliseconds. */
|
||||
static struct timespec
|
||||
timespec_from_millis(const size_t millis) {
|
||||
struct timespec result = { 0, millis * 1000000 };
|
||||
timespec_from_millis (const size_t millis)
|
||||
{
|
||||
struct timespec result = {0, millis * 1000000};
|
||||
normalize(&result);
|
||||
return result;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user