Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup time_unix.c #389

Merged
merged 1 commit into from
Oct 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 24 additions & 29 deletions src/time_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
# error time_unix.c is not intended to be used with win32 based systems
#endif // defined(_WIN32)

#ifdef __cplusplus
extern "C"
{
#endif

#include "rcutils/time.h"

#if defined(__MACH__) && defined(__APPLE__)
Expand All @@ -40,36 +35,38 @@ extern "C"
#include <time.h>
#endif // defined(__ZEPHYR__)

#include <errno.h>
#include <unistd.h>
#include "./common.h"

#include "rcutils/allocator.h"
#include "rcutils/error_handling.h"

#if !defined(__MACH__) && !defined(__APPLE__) // Assume clock_get_time is available on OS X.
// This id an appropriate check for clock_gettime() according to:
// This is an appropriate check for clock_gettime() according to:
// http://man7.org/linux/man-pages/man2/clock_gettime.2.html
# if !defined(_POSIX_TIMERS) || !_POSIX_TIMERS
# error no monotonic clock function available
# endif // !defined(_POSIX_TIMERS) || !_POSIX_TIMERS
#endif // !defined(__MACH__) && !defined(__APPLE__)

#define __WOULD_BE_NEGATIVE(seconds, subseconds) (seconds < 0 || (subseconds < 0 && seconds == 0))
static inline bool would_be_negative(const struct timespec * const now)
{
return now->tv_sec < 0 || (now->tv_nsec < 0 && now->tv_sec == 0);
}

rcutils_ret_t
rcutils_system_time_now(rcutils_time_point_value_t * now)
{
RCUTILS_CHECK_ARGUMENT_FOR_NULL(now, RCUTILS_RET_INVALID_ARGUMENT);
struct timespec timespec_now;
#if defined(__MACH__) && defined(__APPLE__)
// On macOS, use clock_gettime(CLOCK_REALTIME), which matches
// the clang implementation
// Using clock_gettime(CLOCK_REALTIME) matches what both Linux and macOS use.
// For macOS, see the clang implementation at
// (https://github.com/llvm/llvm-project/blob/baebe12ad0d6f514cd33e418d6504075d3e79c0a/libcxx/src/chrono.cpp)
clock_gettime(CLOCK_REALTIME, &timespec_now);
#else // defined(__MACH__) && defined(__APPLE__)
// Otherwise use clock_gettime.
clock_gettime(CLOCK_REALTIME, &timespec_now);
#endif // defined(__MACH__) && defined(__APPLE__)
if (__WOULD_BE_NEGATIVE(timespec_now.tv_sec, timespec_now.tv_nsec)) {
if (clock_gettime(CLOCK_REALTIME, &timespec_now) < 0) {
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING("Failed to get system time: %d", errno);
return RCUTILS_RET_ERROR;
}
if (would_be_negative(&timespec_now)) {
RCUTILS_SET_ERROR_MSG("unexpected negative time");
return RCUTILS_RET_ERROR;
}
Expand All @@ -81,25 +78,23 @@ rcutils_ret_t
rcutils_steady_time_now(rcutils_time_point_value_t * now)
{
RCUTILS_CHECK_ARGUMENT_FOR_NULL(now, RCUTILS_RET_INVALID_ARGUMENT);
// If clock_gettime is available or on OS X, use a timespec.
struct timespec timespec_now;
clockid_t monotonic_clock = CLOCK_MONOTONIC;

#if defined(__MACH__) && defined(__APPLE__)
// On macOS, use clock_gettime(CLOCK_MONOTONIC_RAW), which matches
// the clang implementation
// On macOS, use CLOCK_MONOTONIC_RAW, which matches the clang implementation
// (https://github.com/llvm/llvm-project/blob/baebe12ad0d6f514cd33e418d6504075d3e79c0a/libcxx/src/chrono.cpp)
clock_gettime(CLOCK_MONOTONIC_RAW, &timespec_now);
#else // defined(__MACH__) && defined(__APPLE__)
// Otherwise use clock_gettime.
clock_gettime(CLOCK_MONOTONIC, &timespec_now);
monotonic_clock = CLOCK_MONOTONIC_RAW;
#endif // defined(__MACH__) && defined(__APPLE__)
if (__WOULD_BE_NEGATIVE(timespec_now.tv_sec, timespec_now.tv_nsec)) {

if (clock_gettime(monotonic_clock, &timespec_now) < 0) {
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING("Failed to get steady time: %d", errno);
return RCUTILS_RET_ERROR;
}
if (would_be_negative(&timespec_now)) {
RCUTILS_SET_ERROR_MSG("unexpected negative time");
return RCUTILS_RET_ERROR;
}
*now = RCUTILS_S_TO_NS((int64_t)timespec_now.tv_sec) + timespec_now.tv_nsec;
return RCUTILS_RET_OK;
}

#ifdef __cplusplus
}
#endif