mirror of
https://gitlab.freedesktop.org/mesa/mesa.git
synced 2025-12-29 16:40:13 +01:00
util: add a timespec helper
Copied from Weston, upon Daniel's suggestion Signed-off-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Suggested-by: Daniel Stone <daniels@collabora.com> Reviewed-by: Daniel Stone <daniels@collabora.com>
This commit is contained in:
parent
ef57fb2350
commit
d2d70c3bb5
5 changed files with 673 additions and 0 deletions
|
|
@ -60,6 +60,7 @@ MESA_UTIL_FILES := \
|
|||
strtod.c \
|
||||
strtod.h \
|
||||
texcompress_rgtc_tmp.h \
|
||||
timespec.h \
|
||||
u_atomic.c \
|
||||
u_atomic.h \
|
||||
u_dynarray.h \
|
||||
|
|
|
|||
|
|
@ -82,6 +82,7 @@ files_mesa_util = files(
|
|||
'strtod.c',
|
||||
'strtod.h',
|
||||
'texcompress_rgtc_tmp.h',
|
||||
'timespec.h',
|
||||
'u_atomic.c',
|
||||
'u_atomic.h',
|
||||
'u_dynarray.h',
|
||||
|
|
@ -218,6 +219,7 @@ if with_tests
|
|||
subdir('tests/fast_urem_by_const')
|
||||
subdir('tests/hash_table')
|
||||
subdir('tests/string_buffer')
|
||||
subdir('tests/timespec')
|
||||
subdir('tests/vma')
|
||||
subdir('tests/set')
|
||||
endif
|
||||
|
|
|
|||
31
src/util/tests/timespec/meson.build
Normal file
31
src/util/tests/timespec/meson.build
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
# Copyright © 2019 Intel Corporation
|
||||
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
test(
|
||||
'timespec',
|
||||
executable(
|
||||
'timespec_test',
|
||||
'timespec_test.cpp',
|
||||
dependencies : [dep_thread, dep_dl, idep_gtest],
|
||||
include_directories : inc_common,
|
||||
link_with : [libmesa_util],
|
||||
),
|
||||
suite : ['util'],
|
||||
)
|
||||
309
src/util/tests/timespec/timespec_test.cpp
Normal file
309
src/util/tests/timespec/timespec_test.cpp
Normal file
|
|
@ -0,0 +1,309 @@
|
|||
/*
|
||||
* Copyright © 2016 Collabora, Ltd.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the
|
||||
* next paragraph) shall be included in all copies or substantial
|
||||
* portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "util/timespec.h"
|
||||
|
||||
TEST(timespec_test, timespec_add)
|
||||
{
|
||||
struct timespec a, b, r;
|
||||
|
||||
a.tv_sec = 1;
|
||||
a.tv_nsec = NSEC_PER_SEC - 1;
|
||||
b.tv_sec = 1;
|
||||
b.tv_nsec = 2;
|
||||
timespec_add(&r, &a, &b);
|
||||
EXPECT_EQ(r.tv_sec, 3);
|
||||
EXPECT_EQ(r.tv_nsec, 1);
|
||||
}
|
||||
|
||||
TEST(timespec_test, timespec_sub)
|
||||
{
|
||||
struct timespec a, b, r;
|
||||
|
||||
a.tv_sec = 1;
|
||||
a.tv_nsec = 1;
|
||||
b.tv_sec = 0;
|
||||
b.tv_nsec = 2;
|
||||
timespec_sub(&r, &a, &b);
|
||||
EXPECT_EQ(r.tv_sec, 0);
|
||||
EXPECT_EQ(r.tv_nsec, NSEC_PER_SEC - 1);
|
||||
}
|
||||
|
||||
TEST(timespec_test, timespec_to_nsec)
|
||||
{
|
||||
struct timespec a;
|
||||
|
||||
a.tv_sec = 4;
|
||||
a.tv_nsec = 4;
|
||||
EXPECT_EQ(timespec_to_nsec(&a), (NSEC_PER_SEC * 4ULL) + 4);
|
||||
}
|
||||
|
||||
TEST(timespec_test, timespec_to_usec)
|
||||
{
|
||||
struct timespec a;
|
||||
|
||||
a.tv_sec = 4;
|
||||
a.tv_nsec = 4000;
|
||||
EXPECT_EQ(timespec_to_usec(&a), (4000000ULL) + 4);
|
||||
}
|
||||
|
||||
TEST(timespec_test, timespec_to_msec)
|
||||
{
|
||||
struct timespec a;
|
||||
|
||||
a.tv_sec = 4;
|
||||
a.tv_nsec = 4000000;
|
||||
EXPECT_EQ(timespec_to_msec(&a), (4000ULL) + 4);
|
||||
}
|
||||
|
||||
TEST(timespec_test, timespec_to_proto)
|
||||
{
|
||||
struct timespec a;
|
||||
uint32_t tv_sec_hi;
|
||||
uint32_t tv_sec_lo;
|
||||
uint32_t tv_nsec;
|
||||
|
||||
a.tv_sec = 0;
|
||||
a.tv_nsec = 0;
|
||||
timespec_to_proto(&a, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
|
||||
EXPECT_EQ(0, tv_sec_hi);
|
||||
EXPECT_EQ(0, tv_sec_lo);
|
||||
EXPECT_EQ(0, tv_nsec);
|
||||
|
||||
a.tv_sec = 1234;
|
||||
a.tv_nsec = NSEC_PER_SEC - 1;
|
||||
timespec_to_proto(&a, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
|
||||
EXPECT_EQ(0, tv_sec_hi);
|
||||
EXPECT_EQ(1234, tv_sec_lo);
|
||||
EXPECT_EQ(NSEC_PER_SEC - 1, tv_nsec);
|
||||
|
||||
a.tv_sec = (time_t)0x7000123470005678LL;
|
||||
a.tv_nsec = 1;
|
||||
timespec_to_proto(&a, &tv_sec_hi, &tv_sec_lo, &tv_nsec);
|
||||
EXPECT_EQ((uint64_t)a.tv_sec >> 32, tv_sec_hi);
|
||||
EXPECT_EQ(0x70005678, tv_sec_lo);
|
||||
EXPECT_EQ(1, tv_nsec);
|
||||
}
|
||||
|
||||
TEST(timespec_test, millihz_to_nsec)
|
||||
{
|
||||
EXPECT_EQ(millihz_to_nsec(60000), 16666666);
|
||||
}
|
||||
|
||||
TEST(timespec_test, timespec_add_nsec)
|
||||
{
|
||||
struct timespec a, r;
|
||||
|
||||
a.tv_sec = 0;
|
||||
a.tv_nsec = NSEC_PER_SEC - 1;
|
||||
timespec_add_nsec(&r, &a, 1);
|
||||
EXPECT_EQ(1, r.tv_sec);
|
||||
EXPECT_EQ(0, r.tv_nsec);
|
||||
|
||||
timespec_add_nsec(&r, &a, 2);
|
||||
EXPECT_EQ(1, r.tv_sec);
|
||||
EXPECT_EQ(1, r.tv_nsec);
|
||||
|
||||
timespec_add_nsec(&r, &a, (NSEC_PER_SEC * 2ULL));
|
||||
EXPECT_EQ(2, r.tv_sec);
|
||||
EXPECT_EQ(NSEC_PER_SEC - 1, r.tv_nsec);
|
||||
|
||||
timespec_add_nsec(&r, &a, (NSEC_PER_SEC * 2ULL) + 2);
|
||||
EXPECT_EQ(r.tv_sec, 3);
|
||||
EXPECT_EQ(r.tv_nsec, 1);
|
||||
|
||||
a.tv_sec = 1;
|
||||
a.tv_nsec = 1;
|
||||
timespec_add_nsec(&r, &a, -2);
|
||||
EXPECT_EQ(r.tv_sec, 0);
|
||||
EXPECT_EQ(r.tv_nsec, NSEC_PER_SEC - 1);
|
||||
|
||||
a.tv_nsec = 0;
|
||||
timespec_add_nsec(&r, &a, -NSEC_PER_SEC);
|
||||
EXPECT_EQ(0, r.tv_sec);
|
||||
EXPECT_EQ(0, r.tv_nsec);
|
||||
|
||||
a.tv_nsec = 0;
|
||||
timespec_add_nsec(&r, &a, -NSEC_PER_SEC + 1);
|
||||
EXPECT_EQ(0, r.tv_sec);
|
||||
EXPECT_EQ(1, r.tv_nsec);
|
||||
|
||||
a.tv_nsec = 50;
|
||||
timespec_add_nsec(&r, &a, (-NSEC_PER_SEC * 10ULL));
|
||||
EXPECT_EQ(-9, r.tv_sec);
|
||||
EXPECT_EQ(50, r.tv_nsec);
|
||||
|
||||
r.tv_sec = 4;
|
||||
r.tv_nsec = 0;
|
||||
timespec_add_nsec(&r, &r, NSEC_PER_SEC + 10ULL);
|
||||
EXPECT_EQ(5, r.tv_sec);
|
||||
EXPECT_EQ(10, r.tv_nsec);
|
||||
|
||||
timespec_add_nsec(&r, &r, (NSEC_PER_SEC * 3ULL) - 9ULL);
|
||||
EXPECT_EQ(8, r.tv_sec);
|
||||
EXPECT_EQ(1, r.tv_nsec);
|
||||
|
||||
timespec_add_nsec(&r, &r, (NSEC_PER_SEC * 7ULL) + (NSEC_PER_SEC - 1ULL));
|
||||
EXPECT_EQ(16, r.tv_sec);
|
||||
EXPECT_EQ(0, r.tv_nsec);
|
||||
}
|
||||
|
||||
TEST(timespec_test, timespec_add_msec)
|
||||
{
|
||||
struct timespec a, r;
|
||||
|
||||
a.tv_sec = 1000;
|
||||
a.tv_nsec = 1;
|
||||
timespec_add_msec(&r, &a, 2002);
|
||||
EXPECT_EQ(1002, r.tv_sec);
|
||||
EXPECT_EQ(2000001, r.tv_nsec);
|
||||
}
|
||||
|
||||
TEST(timespec_test, timespec_sub_to_nsec)
|
||||
{
|
||||
struct timespec a, b;
|
||||
|
||||
a.tv_sec = 1000;
|
||||
a.tv_nsec = 1;
|
||||
b.tv_sec = 1;
|
||||
b.tv_nsec = 2;
|
||||
EXPECT_EQ((999LL * NSEC_PER_SEC) - 1, timespec_sub_to_nsec(&a, &b));
|
||||
}
|
||||
|
||||
TEST(timespec_test, timespec_sub_to_msec)
|
||||
{
|
||||
struct timespec a, b;
|
||||
|
||||
a.tv_sec = 1000;
|
||||
a.tv_nsec = 2000000L;
|
||||
b.tv_sec = 2;
|
||||
b.tv_nsec = 1000000L;
|
||||
EXPECT_EQ((998 * 1000) + 1, timespec_sub_to_msec(&a, &b));
|
||||
}
|
||||
|
||||
TEST(timespec_test, timespec_from_nsec)
|
||||
{
|
||||
struct timespec a;
|
||||
|
||||
timespec_from_nsec(&a, 0);
|
||||
EXPECT_EQ(0, a.tv_sec);
|
||||
EXPECT_EQ(0, a.tv_nsec);
|
||||
|
||||
timespec_from_nsec(&a, NSEC_PER_SEC - 1);
|
||||
EXPECT_EQ(0, a.tv_sec);
|
||||
EXPECT_EQ(NSEC_PER_SEC - 1, a.tv_nsec);
|
||||
|
||||
timespec_from_nsec(&a, NSEC_PER_SEC);
|
||||
EXPECT_EQ(1, a.tv_sec);
|
||||
EXPECT_EQ(0, a.tv_nsec);
|
||||
|
||||
timespec_from_nsec(&a, (5LL * NSEC_PER_SEC) + 1);
|
||||
EXPECT_EQ(5, a.tv_sec);
|
||||
EXPECT_EQ(1, a.tv_nsec);
|
||||
}
|
||||
|
||||
TEST(timespec_test, timespec_from_usec)
|
||||
{
|
||||
struct timespec a;
|
||||
|
||||
timespec_from_usec(&a, 0);
|
||||
EXPECT_EQ(0, a.tv_sec);
|
||||
EXPECT_EQ(0, a.tv_nsec);
|
||||
|
||||
timespec_from_usec(&a, 999999);
|
||||
EXPECT_EQ(0, a.tv_sec);
|
||||
EXPECT_EQ(999999 * 1000, a.tv_nsec);
|
||||
|
||||
timespec_from_usec(&a, 1000000);
|
||||
EXPECT_EQ(1, a.tv_sec);
|
||||
EXPECT_EQ(0, a.tv_nsec);
|
||||
|
||||
timespec_from_usec(&a, 5000001);
|
||||
EXPECT_EQ(5, a.tv_sec);
|
||||
EXPECT_EQ(1000, a.tv_nsec);
|
||||
}
|
||||
|
||||
TEST(timespec_test, timespec_from_msec)
|
||||
{
|
||||
struct timespec a;
|
||||
|
||||
timespec_from_msec(&a, 0);
|
||||
EXPECT_EQ(0, a.tv_sec);
|
||||
EXPECT_EQ(0, a.tv_nsec);
|
||||
|
||||
timespec_from_msec(&a, 999);
|
||||
EXPECT_EQ(0, a.tv_sec);
|
||||
EXPECT_EQ(999 * 1000000, a.tv_nsec);
|
||||
|
||||
timespec_from_msec(&a, 1000);
|
||||
EXPECT_EQ(1, a.tv_sec);
|
||||
EXPECT_EQ(0, a.tv_nsec);
|
||||
|
||||
timespec_from_msec(&a, 5001);
|
||||
EXPECT_EQ(5, a.tv_sec);
|
||||
EXPECT_EQ(1000000, a.tv_nsec);
|
||||
}
|
||||
|
||||
TEST(timespec_test, timespec_from_proto)
|
||||
{
|
||||
struct timespec a;
|
||||
|
||||
timespec_from_proto(&a, 0, 0, 0);
|
||||
EXPECT_EQ(0, a.tv_sec);
|
||||
EXPECT_EQ(0, a.tv_nsec);
|
||||
|
||||
timespec_from_proto(&a, 0, 1234, 9999);
|
||||
EXPECT_EQ(1234, a.tv_sec);
|
||||
EXPECT_EQ(9999, a.tv_nsec);
|
||||
|
||||
timespec_from_proto(&a, 0x1234, 0x5678, 1);
|
||||
EXPECT_EQ((time_t)0x0000123400005678LL, a.tv_sec);
|
||||
EXPECT_EQ(1, a.tv_nsec);
|
||||
}
|
||||
|
||||
TEST(timespec_test, timespec_is_zero)
|
||||
{
|
||||
struct timespec zero = { 0 };
|
||||
struct timespec non_zero_sec = { .tv_sec = 1, .tv_nsec = 0 };
|
||||
struct timespec non_zero_nsec = { .tv_sec = 0, .tv_nsec = 1 };
|
||||
|
||||
EXPECT_TRUE(timespec_is_zero(&zero));
|
||||
EXPECT_FALSE(timespec_is_zero(&non_zero_nsec));
|
||||
EXPECT_FALSE(timespec_is_zero(&non_zero_sec));
|
||||
}
|
||||
|
||||
TEST(timespec_test, timespec_eq)
|
||||
{
|
||||
struct timespec a = { .tv_sec = 2, .tv_nsec = 1 };
|
||||
struct timespec b = { .tv_sec = -1, .tv_nsec = 2 };
|
||||
|
||||
EXPECT_TRUE(timespec_eq(&a, &a));
|
||||
EXPECT_TRUE(timespec_eq(&b, &b));
|
||||
|
||||
EXPECT_FALSE(timespec_eq(&a, &b));
|
||||
EXPECT_FALSE(timespec_eq(&b, &a));
|
||||
}
|
||||
330
src/util/timespec.h
Normal file
330
src/util/timespec.h
Normal file
|
|
@ -0,0 +1,330 @@
|
|||
/*
|
||||
* Copyright © 2014 - 2015 Collabora, Ltd.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the
|
||||
* next paragraph) shall be included in all copies or substantial
|
||||
* portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file timespec.h
|
||||
*
|
||||
* Helpers to deal with timespec structures.
|
||||
*/
|
||||
|
||||
#ifndef TIMESPEC_H
|
||||
#define TIMESPEC_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define NSEC_PER_SEC 1000000000
|
||||
|
||||
/**
|
||||
* Add timespecs
|
||||
*
|
||||
* \param r[out] result: a + b
|
||||
* \param a[in] operand
|
||||
* \param b[in] operand
|
||||
*/
|
||||
static inline void
|
||||
timespec_add(struct timespec *r,
|
||||
const struct timespec *a, const struct timespec *b)
|
||||
{
|
||||
r->tv_sec = a->tv_sec + b->tv_sec;
|
||||
r->tv_nsec = a->tv_nsec + b->tv_nsec;
|
||||
if (r->tv_nsec > NSEC_PER_SEC) {
|
||||
r->tv_sec++;
|
||||
r->tv_nsec -= NSEC_PER_SEC;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract timespecs
|
||||
*
|
||||
* \param r[out] result: a - b
|
||||
* \param a[in] operand
|
||||
* \param b[in] operand
|
||||
*/
|
||||
static inline void
|
||||
timespec_sub(struct timespec *r,
|
||||
const struct timespec *a, const struct timespec *b)
|
||||
{
|
||||
r->tv_sec = a->tv_sec - b->tv_sec;
|
||||
r->tv_nsec = a->tv_nsec - b->tv_nsec;
|
||||
if (r->tv_nsec < 0) {
|
||||
r->tv_sec--;
|
||||
r->tv_nsec += NSEC_PER_SEC;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a nanosecond value to a timespec
|
||||
*
|
||||
* \param r[out] result: a + b
|
||||
* \param a[in] base operand as timespec
|
||||
* \param b[in] operand in nanoseconds
|
||||
*/
|
||||
static inline void
|
||||
timespec_add_nsec(struct timespec *r, const struct timespec *a, int64_t b)
|
||||
{
|
||||
r->tv_sec = a->tv_sec + (b / NSEC_PER_SEC);
|
||||
r->tv_nsec = a->tv_nsec + (b % NSEC_PER_SEC);
|
||||
|
||||
if (r->tv_nsec >= NSEC_PER_SEC) {
|
||||
r->tv_sec++;
|
||||
r->tv_nsec -= NSEC_PER_SEC;
|
||||
} else if (r->tv_nsec < 0) {
|
||||
r->tv_sec--;
|
||||
r->tv_nsec += NSEC_PER_SEC;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a millisecond value to a timespec
|
||||
*
|
||||
* \param r[out] result: a + b
|
||||
* \param a[in] base operand as timespec
|
||||
* \param b[in] operand in milliseconds
|
||||
*/
|
||||
static inline void
|
||||
timespec_add_msec(struct timespec *r, const struct timespec *a, int64_t b)
|
||||
{
|
||||
timespec_add_nsec(r, a, b * 1000000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert timespec to nanoseconds
|
||||
*
|
||||
* \param a timespec
|
||||
* \return nanoseconds
|
||||
*/
|
||||
static inline int64_t
|
||||
timespec_to_nsec(const struct timespec *a)
|
||||
{
|
||||
return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract timespecs and return result in nanoseconds
|
||||
*
|
||||
* \param a[in] operand
|
||||
* \param b[in] operand
|
||||
* \return to_nanoseconds(a - b)
|
||||
*/
|
||||
static inline int64_t
|
||||
timespec_sub_to_nsec(const struct timespec *a, const struct timespec *b)
|
||||
{
|
||||
struct timespec r;
|
||||
timespec_sub(&r, a, b);
|
||||
return timespec_to_nsec(&r);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert timespec to milliseconds
|
||||
*
|
||||
* \param a timespec
|
||||
* \return milliseconds
|
||||
*
|
||||
* Rounding to integer milliseconds happens always down (floor()).
|
||||
*/
|
||||
static inline int64_t
|
||||
timespec_to_msec(const struct timespec *a)
|
||||
{
|
||||
return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract timespecs and return result in milliseconds
|
||||
*
|
||||
* \param a[in] operand
|
||||
* \param b[in] operand
|
||||
* \return to_milliseconds(a - b)
|
||||
*/
|
||||
static inline int64_t
|
||||
timespec_sub_to_msec(const struct timespec *a, const struct timespec *b)
|
||||
{
|
||||
return timespec_sub_to_nsec(a, b) / 1000000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert timespec to microseconds
|
||||
*
|
||||
* \param a timespec
|
||||
* \return microseconds
|
||||
*
|
||||
* Rounding to integer microseconds happens always down (floor()).
|
||||
*/
|
||||
static inline int64_t
|
||||
timespec_to_usec(const struct timespec *a)
|
||||
{
|
||||
return (int64_t)a->tv_sec * 1000000 + a->tv_nsec / 1000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert timespec to protocol data
|
||||
*
|
||||
* \param a timespec
|
||||
* \param tv_sec_hi[out] the high bytes of the seconds part
|
||||
* \param tv_sec_lo[out] the low bytes of the seconds part
|
||||
* \param tv_nsec[out] the nanoseconds part
|
||||
*
|
||||
* The input timespec must be normalized (the nanoseconds part should
|
||||
* be less than 1 second) and non-negative.
|
||||
*/
|
||||
static inline void
|
||||
timespec_to_proto(const struct timespec *a, uint32_t *tv_sec_hi,
|
||||
uint32_t *tv_sec_lo, uint32_t *tv_nsec)
|
||||
{
|
||||
assert(a->tv_sec >= 0);
|
||||
assert(a->tv_nsec >= 0 && a->tv_nsec < NSEC_PER_SEC);
|
||||
|
||||
uint64_t sec64 = a->tv_sec;
|
||||
|
||||
*tv_sec_hi = sec64 >> 32;
|
||||
*tv_sec_lo = sec64 & 0xffffffff;
|
||||
*tv_nsec = a->tv_nsec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert nanoseconds to timespec
|
||||
*
|
||||
* \param a timespec
|
||||
* \param b nanoseconds
|
||||
*/
|
||||
static inline void
|
||||
timespec_from_nsec(struct timespec *a, int64_t b)
|
||||
{
|
||||
a->tv_sec = b / NSEC_PER_SEC;
|
||||
a->tv_nsec = b % NSEC_PER_SEC;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert microseconds to timespec
|
||||
*
|
||||
* \param a timespec
|
||||
* \param b microseconds
|
||||
*/
|
||||
static inline void
|
||||
timespec_from_usec(struct timespec *a, int64_t b)
|
||||
{
|
||||
timespec_from_nsec(a, b * 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert milliseconds to timespec
|
||||
*
|
||||
* \param a timespec
|
||||
* \param b milliseconds
|
||||
*/
|
||||
static inline void
|
||||
timespec_from_msec(struct timespec *a, int64_t b)
|
||||
{
|
||||
timespec_from_nsec(a, b * 1000000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert protocol data to timespec
|
||||
*
|
||||
* \param a[out] timespec
|
||||
* \param tv_sec_hi the high bytes of seconds part
|
||||
* \param tv_sec_lo the low bytes of seconds part
|
||||
* \param tv_nsec the nanoseconds part
|
||||
*/
|
||||
static inline void
|
||||
timespec_from_proto(struct timespec *a, uint32_t tv_sec_hi,
|
||||
uint32_t tv_sec_lo, uint32_t tv_nsec)
|
||||
{
|
||||
a->tv_sec = ((uint64_t)tv_sec_hi << 32) + tv_sec_lo;
|
||||
a->tv_nsec = tv_nsec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a timespec is zero
|
||||
*
|
||||
* \param a timespec
|
||||
* \return whether the timespec is zero
|
||||
*/
|
||||
static inline bool
|
||||
timespec_is_zero(const struct timespec *a)
|
||||
{
|
||||
return a->tv_sec == 0 && a->tv_nsec == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if two timespecs are equal
|
||||
*
|
||||
* \param a[in] timespec to check
|
||||
* \param b[in] timespec to check
|
||||
* \return whether timespecs a and b are equal
|
||||
*/
|
||||
static inline bool
|
||||
timespec_eq(const struct timespec *a, const struct timespec *b)
|
||||
{
|
||||
return a->tv_sec == b->tv_sec &&
|
||||
a->tv_nsec == b->tv_nsec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert milli-Hertz to nanoseconds
|
||||
*
|
||||
* \param mhz frequency in mHz, not zero
|
||||
* \return period in nanoseconds
|
||||
*/
|
||||
static inline int64_t
|
||||
millihz_to_nsec(uint32_t mhz)
|
||||
{
|
||||
assert(mhz > 0);
|
||||
return 1000000000000LL / mhz;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a timespec value is after another
|
||||
*
|
||||
* \param a[in] timespec to compare
|
||||
* \param b[in] timespec to compare
|
||||
* \return whether a is after b
|
||||
*/
|
||||
static inline bool
|
||||
timespec_after(const struct timespec *a, const struct timespec *b)
|
||||
{
|
||||
return (a->tv_sec == b->tv_sec) ?
|
||||
(a->tv_nsec > b->tv_nsec) :
|
||||
(a->tv_sec > b->tv_sec);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a timespec value is after the current time
|
||||
*
|
||||
* \param clock_domain[in] clock in which to do the comparison
|
||||
* \param deadline[in] timespec to compare
|
||||
* \return whether deadline is after the current time
|
||||
*/
|
||||
static inline bool
|
||||
timespec_passed(clockid_t clock_domain, const struct timespec *deadline)
|
||||
{
|
||||
struct timespec current_time;
|
||||
clock_gettime(clock_domain, ¤t_time);
|
||||
return timespec_after(¤t_time, deadline);
|
||||
}
|
||||
|
||||
#endif /* TIMESPEC_H */
|
||||
Loading…
Add table
Reference in a new issue