Skip to content

Commit

Permalink
Merge pull request ros2#140 from ros2/move_time
Browse files Browse the repository at this point in the history
move time files to rcutils
  • Loading branch information
dirk-thomas authored Jun 2, 2017
2 parents e86f7a6 + e84b178 commit 14a4966
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 332 deletions.
7 changes: 0 additions & 7 deletions rcl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ if(NOT WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra")
endif()

if(WIN32)
set(time_impl_c src/rcl/time_win32.c)
else()
set(time_impl_c src/rcl/time_unix.c)
endif()

set(${PROJECT_NAME}_sources
src/rcl/client.c
src/rcl/common.c
Expand All @@ -36,7 +30,6 @@ set(${PROJECT_NAME}_sources
src/rcl/service.c
src/rcl/subscription.c
src/rcl/time.c
${time_impl_c}
src/rcl/timer.c
src/rcl/validate_topic_name.c
src/rcl/wait.c
Expand Down
79 changes: 9 additions & 70 deletions rcl/include/rcl/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,26 @@ extern "C"
#include "rcl/macros.h"
#include "rcl/types.h"
#include "rcl/visibility_control.h"
#include "rcutils/time.h"

/// Convenience macro to convert seconds to nanoseconds.
#define RCL_S_TO_NS(seconds) (seconds * (1000 * 1000 * 1000))
#define RCL_S_TO_NS RCUTILS_S_TO_NS
/// Convenience macro to convert milliseconds to nanoseconds.
#define RCL_MS_TO_NS(milliseconds) (milliseconds * (1000 * 1000))
#define RCL_MS_TO_NS RCUTILS_MS_TO_NS
/// Convenience macro to convert microseconds to nanoseconds.
#define RCL_US_TO_NS(microseconds) (microseconds * 1000)
#define RCL_US_TO_NS RCUTILS_US_TO_NS

/// Convenience macro to convert nanoseconds to seconds.
#define RCL_NS_TO_S(nanoseconds) (nanoseconds / (1000 * 1000 * 1000))
#define RCL_NS_TO_S RCUTILS_NS_TO_S
/// Convenience macro to convert nanoseconds to milliseconds.
#define RCL_NS_TO_MS(nanoseconds) (nanoseconds / (1000 * 1000))
#define RCL_NS_TO_MS RCUTILS_NS_TO_MS
/// Convenience macro to convert nanoseconds to microseconds.
#define RCL_NS_TO_US(nanoseconds) (nanoseconds / 1000)
#define RCL_NS_TO_US RCUTILS_NS_TO_US

/// A single point in time, measured in nanoseconds since the Unix epoch.
typedef uint64_t rcl_time_point_value_t;
typedef rcutils_time_point_value_t rcl_time_point_value_t;
/// A duration of time, measured in nanoseconds.
typedef int64_t rcl_duration_value_t;
typedef rcutils_duration_value_t rcl_duration_value_t;

/// Time source type, used to indicate the source of a time measurement.
enum rcl_time_source_type_t
Expand Down Expand Up @@ -487,68 +488,6 @@ rcl_ret_t
rcl_set_ros_time_override(rcl_time_source_t * time_source,
rcl_time_point_value_t time_value);

/// Retrieve the current time as a rcl_time_point_value_t.
/**
* This function returns the time from a system clock.
* The closest equivalent would be to std::chrono::system_clock::now();
*
* The resolution (e.g. nanoseconds vs microseconds) is not guaranteed.
*
* The now argument must point to an allocated rcl_system_time_point_t struct,
* as the result is copied into this variable.
*
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | No
* Thread-Safe | Yes
* Uses Atomics | No
* Lock-Free | Yes [1]
* <i>[1] if `atomic_is_lock_free()` returns true for `atomic_int_least64_t`</i>
*
* \todo TODO(tfoote): consider moving this to rmw for more reuse
*
* \param[out] now a datafield in which the current time is stored
* \return `RCL_RET_OK` if the current time was successfully obtained, or
* \return `RCL_RET_INVALID_ARGUMENT` if any arguments are invalid, or
* \return `RCL_RET_ERROR` an unspecified error occur.
*/
RCL_PUBLIC
RCL_WARN_UNUSED
rcl_ret_t
rcl_system_time_now(rcl_time_point_value_t * now);

/// Retrieve the current time as a rcl_time_point_value_t object.
/**
* This function returns the time from a monotonically increasing clock.
* The closest equivalent would be to std::chrono::steady_clock::now();
*
* The resolution (e.g. nanoseconds vs microseconds) is not guaranteed.
*
* The now argument must point to an allocated rcl_time_point_value_t object,
* as the result is copied into this variable.
*
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | No
* Thread-Safe | Yes
* Uses Atomics | No
* Lock-Free | Yes [1]
* <i>[1] if `atomic_is_lock_free()` returns true for `atomic_int_least64_t`</i>
*
* \todo TODO(tfoote): consider moving this to rmw for more reuse
*
* \param[out] now a struct in which the current time is stored
* \return `RCL_RET_OK` if the current time was successfully obtained, or
* \return `RCL_RET_INVALID_ARGUMENT` if any arguments are invalid, or
* \return `RCL_RET_ERROR` an unspecified error occur.
*/
RCL_PUBLIC
RCL_WARN_UNUSED
rcl_ret_t
rcl_steady_time_now(rcl_time_point_value_t * now);

#if __cplusplus
}
#endif
Expand Down
5 changes: 3 additions & 2 deletions rcl/src/rcl/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "./stdatomic_helper.h"
#include "rcl/allocator.h"
#include "rcl/error_handling.h"
#include "rcutils/time.h"

// Process default ROS time sources
static rcl_time_source_t * rcl_default_ros_time_source;
Expand All @@ -40,15 +41,15 @@ rcl_ret_t
rcl_get_steady_time(void * data, rcl_time_point_value_t * current_time)
{
(void)data; // unused
return rcl_steady_time_now(current_time);
return rcutils_steady_time_now(current_time);
}

// Implementation only
rcl_ret_t
rcl_get_system_time(void * data, rcl_time_point_value_t * current_time)
{
(void)data; // unused
return rcl_system_time_now(current_time);
return rcutils_system_time_now(current_time);
}

// Internal method for zeroing values on init, assumes time_source is valid
Expand Down
107 changes: 0 additions & 107 deletions rcl/src/rcl/time_unix.c

This file was deleted.

70 changes: 0 additions & 70 deletions rcl/src/rcl/time_win32.c

This file was deleted.

11 changes: 6 additions & 5 deletions rcl/src/rcl/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern "C"

#include "./common.h"
#include "./stdatomic_helper.h"
#include "rcutils/time.h"

typedef struct rcl_timer_impl_t
{
Expand Down Expand Up @@ -62,7 +63,7 @@ rcl_timer_init(
return RCL_RET_ALREADY_INIT;
}
rcl_time_point_value_t now_steady;
rcl_ret_t now_ret = rcl_steady_time_now(&now_steady);
rcl_ret_t now_ret = rcutils_steady_time_now(&now_steady);
if (now_ret != RCL_RET_OK) {
return now_ret; // rcl error state should already be set.
}
Expand Down Expand Up @@ -105,7 +106,7 @@ rcl_timer_call(rcl_timer_t * timer)
return RCL_RET_TIMER_CANCELED;
}
rcl_time_point_value_t now_steady;
rcl_ret_t now_ret = rcl_steady_time_now(&now_steady);
rcl_ret_t now_ret = rcutils_steady_time_now(&now_steady);
if (now_ret != RCL_RET_OK) {
return now_ret; // rcl error state should already be set.
}
Expand Down Expand Up @@ -149,7 +150,7 @@ rcl_timer_get_time_until_next_call(const rcl_timer_t * timer, int64_t * time_unt
}
RCL_CHECK_ARGUMENT_FOR_NULL(time_until_next_call, RCL_RET_INVALID_ARGUMENT, *allocator);
rcl_time_point_value_t now;
rcl_ret_t ret = rcl_steady_time_now(&now);
rcl_ret_t ret = rcutils_steady_time_now(&now);
if (ret != RCL_RET_OK) {
return ret; // rcl error state should already be set.
}
Expand All @@ -171,7 +172,7 @@ rcl_timer_get_time_since_last_call(
}
RCL_CHECK_ARGUMENT_FOR_NULL(time_since_last_call, RCL_RET_INVALID_ARGUMENT, *allocator);
rcl_time_point_value_t now;
rcl_ret_t ret = rcl_steady_time_now(&now);
rcl_ret_t ret = rcutils_steady_time_now(&now);
if (ret != RCL_RET_OK) {
return ret; // rcl error state should already be set.
}
Expand Down Expand Up @@ -255,7 +256,7 @@ rcl_timer_reset(rcl_timer_t * timer)
RCL_CHECK_FOR_NULL_WITH_MSG(
timer->impl, "timer is invalid", return RCL_RET_TIMER_INVALID, rcl_get_default_allocator());
rcl_time_point_value_t now;
rcl_ret_t now_ret = rcl_steady_time_now(&now);
rcl_ret_t now_ret = rcutils_steady_time_now(&now);
if (now_ret != RCL_RET_OK) {
return now_ret; // rcl error state should already be set.
}
Expand Down
Loading

0 comments on commit 14a4966

Please sign in to comment.