Skip to content

Commit

Permalink
Decoupled the additional feature from rcl to rcutils, reflecting on t…
Browse files Browse the repository at this point in the history
…he pointing out below.

ros2/rclcpp#2205 (comment)

Signed-off-by: Shoji Morita <[email protected]>
  • Loading branch information
smorita-esol committed Jun 20, 2023
1 parent 73718ca commit e25cf2b
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 271 deletions.
5 changes: 3 additions & 2 deletions rcl/include/rcl/arguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "rcl/types.h"
#include "rcl/visibility_control.h"
#include "rcl_yaml_param_parser/types.h"
#include "rcutils/thread_attr.h"

#ifdef __cplusplus
extern "C"
Expand Down Expand Up @@ -86,7 +87,7 @@ typedef struct rcl_arguments_s
/// The ROS flag that precedes the ROS thread attribute file path.
#define RCL_THREAD_ATTRS_FILE_FLAG "--thread-attrs-file"

/// The ROS flag that precedes the ROS logging thread attribute.
/// The ROS flag that precedes the ROS thread attribute.
#define RCL_THREAD_ATTRS_VALUE_FLAG "--thread-attrs-value"

/// Return a rcl_arguments_t struct with members initialized to `NULL`.
Expand Down Expand Up @@ -469,7 +470,7 @@ RCL_WARN_UNUSED
rcl_ret_t
rcl_arguments_get_thread_attrs(
const rcl_arguments_t * arguments,
rcl_thread_attrs_t ** thread_attrs);
rcutils_thread_attrs_t ** thread_attrs);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion rcl/include/rcl/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ RCL_WARN_UNUSED
rcl_ret_t
rcl_context_get_thread_attrs(
const rcl_context_t * context,
rcl_thread_attrs_t ** thread_attrs);
rcutils_thread_attrs_t ** thread_attrs);

#ifdef __cplusplus
}
Expand Down
6 changes: 3 additions & 3 deletions rcl/include/rcl/thread_attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extern "C"
#include "rcl/macros.h"
#include "rcl/types.h"
#include "rcl/visibility_control.h"
#include "rcl_yaml_param_parser/types.h"
#include "rcutils/thread_attr.h"

extern const char * const RCL_THREAD_ATTR_VALUE_ENV_VAR;
extern const char * const RCL_THREAD_ATTR_FILE_ENV_VAR;
Expand All @@ -43,7 +43,7 @@ extern const char * const RCL_THREAD_ATTR_FILE_ENV_VAR;
RCL_PUBLIC
rcl_ret_t
rcl_get_default_thread_attrs_from_value(
rcl_thread_attrs_t * thread_attrs,
rcutils_thread_attrs_t * thread_attrs,
rcl_allocator_t allocator);

/// Determine the default thread attribute from file path, based on the environment.
Expand All @@ -56,7 +56,7 @@ rcl_get_default_thread_attrs_from_value(
RCL_PUBLIC
rcl_ret_t
rcl_get_default_thread_attrs_from_file(
rcl_thread_attrs_t * thread_attrs,
rcutils_thread_attrs_t * thread_attrs,
rcl_allocator_t allocator);

#ifdef __cplusplus
Expand Down
13 changes: 7 additions & 6 deletions rcl/src/rcl/arguments.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "rcutils/logging.h"
#include "rcutils/logging_macros.h"
#include "rcutils/strdup.h"
#include "rcutils/thread_attr.h"
#include "rmw/validate_namespace.h"
#include "rmw/validate_node_name.h"

Expand Down Expand Up @@ -287,8 +288,8 @@ rcl_parse_arguments(
goto fail;
}

args_impl->thread_attrs = rcl_get_zero_initialized_thread_attrs();
ret = rcl_thread_attrs_init(&args_impl->thread_attrs, allocator);
args_impl->thread_attrs = rcutils_get_zero_initialized_thread_attrs();
ret = rcutils_thread_attrs_init(&args_impl->thread_attrs, allocator);
if (RCL_RET_OK != ret) {
goto fail;
}
Expand Down Expand Up @@ -1062,8 +1063,8 @@ rcl_arguments_fini(
args->impl->external_log_config_file = NULL;
}

rcl_ret_t thread_ret = rcl_thread_attrs_fini(&args->impl->thread_attrs);
if (thread_ret != RCL_RET_OK) {
rcl_ret_t thread_ret = rcutils_thread_attrs_fini(&args->impl->thread_attrs);
if (RCL_RET_OK != thread_ret) {
ret = thread_ret;
RCUTILS_LOG_ERROR_NAMED(
ROS_PACKAGE_NAME,
Expand Down Expand Up @@ -2149,7 +2150,7 @@ _rcl_allocate_initialized_arguments_impl(rcl_arguments_t * args, rcl_allocator_t
args_impl->log_rosout_disabled = false;
args_impl->log_ext_lib_disabled = false;
args_impl->enclave = NULL;
args_impl->thread_attrs = rcl_get_zero_initialized_thread_attrs();
args_impl->thread_attrs = rcutils_get_zero_initialized_thread_attrs();
args_impl->allocator = *allocator;

return RCL_RET_OK;
Expand All @@ -2158,7 +2159,7 @@ _rcl_allocate_initialized_arguments_impl(rcl_arguments_t * args, rcl_allocator_t
rcl_ret_t
rcl_arguments_get_thread_attrs(
const rcl_arguments_t * arguments,
rcl_thread_attrs_t ** thread_attrs)
rcutils_thread_attrs_t ** thread_attrs)
{
RCL_CHECK_ARGUMENT_FOR_NULL(arguments, RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_ARGUMENT_FOR_NULL(arguments->impl, RCL_RET_INVALID_ARGUMENT);
Expand Down
2 changes: 1 addition & 1 deletion rcl/src/rcl/arguments_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct rcl_arguments_impl_s
int num_remap_rules;

/// thread attribute.
rcl_thread_attrs_t thread_attrs;
rcutils_thread_attrs_t thread_attrs;

/// Log levels parsed from arguments.
rcl_log_levels_t log_levels;
Expand Down
5 changes: 3 additions & 2 deletions rcl/src/rcl/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ rcl_context_get_rmw_context(rcl_context_t * context)
rcl_ret_t
rcl_context_get_thread_attrs(
const rcl_context_t * context,
rcl_thread_attrs_t ** thread_attrs)
rcutils_thread_attrs_t ** thread_attrs)
{
RCL_CHECK_ARGUMENT_FOR_NULL(context, RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_ARGUMENT_FOR_NULL(context->impl, RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_ARGUMENT_FOR_NULL(thread_attrs, RCL_RET_INVALID_ARGUMENT);

if (0 < context->impl->thread_attrs.num_attributes) {
*thread_attrs = &context->impl->thread_attrs;
Expand Down Expand Up @@ -165,7 +166,7 @@ __cleanup_context(rcl_context_t * context)

// clean up thread_attrs_context
rcl_ret_t thread_attrs_context_fini_ret =
rcl_thread_attrs_fini(&(context->impl->thread_attrs));
rcutils_thread_attrs_fini(&(context->impl->thread_attrs));
if (RCL_RET_OK != thread_attrs_context_fini_ret) {
if (RCL_RET_OK == ret) {
ret = thread_attrs_context_fini_ret;
Expand Down
2 changes: 1 addition & 1 deletion rcl/src/rcl/context_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct rcl_context_impl_s
/// rmw context.
rmw_context_t rmw_context;
/// thread attributes.
rcl_thread_attrs_t thread_attrs;
rcutils_thread_attrs_t thread_attrs;
};

RCL_LOCAL
Expand Down
4 changes: 2 additions & 2 deletions rcl/src/rcl/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ rcl_init(
context->impl->rmw_context = rmw_get_zero_initialized_context();

// Zero initialize thread attribute context first so its validity can by checked in cleanup.
context->impl->thread_attrs = rcl_get_zero_initialized_thread_attrs();
context->impl->thread_attrs = rcutils_get_zero_initialized_thread_attrs();

// Store the allocator.
context->impl->allocator = allocator;
Expand Down Expand Up @@ -261,7 +261,7 @@ rcl_init(
"\t%s", discovery_options->static_peers[ii].peer_address);
}

ret = rcl_thread_attrs_init(&(context->impl->thread_attrs), allocator);
ret = rcutils_thread_attrs_init(&(context->impl->thread_attrs), allocator);
if (RCL_RET_OK != ret) {
fail_ret = ret;
goto fail;
Expand Down
6 changes: 3 additions & 3 deletions rcl/src/rcl/thread_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
#include "rcl/error_handling.h"
#include "rcl/macros.h"
#include "rcl_yaml_param_parser/parser_thread_attr.h"
#include "rcl_yaml_param_parser/types.h"
#include "rcutils/env.h"
#include "rcutils/strdup.h"
#include "rcutils/thread_attr.h"

const char * const RCL_THREAD_ATTRS_FILE_ENV_VAR = "ROS_THREAD_ATTRS_FILE";
const char * const RCL_THREAD_ATTRS_VALUE_ENV_VAR = "ROS_THREAD_ATTRS_VALUE";

rcl_ret_t
rcl_get_default_thread_attrs_from_value(
rcl_thread_attrs_t * thread_attrs,
rcutils_thread_attrs_t * thread_attrs,
rcl_allocator_t allocator)
{
RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(RCL_RET_INVALID_ARGUMENT);
Expand Down Expand Up @@ -59,7 +59,7 @@ rcl_get_default_thread_attrs_from_value(

rcl_ret_t
rcl_get_default_thread_attrs_from_file(
rcl_thread_attrs_t * thread_attrs,
rcutils_thread_attrs_t * thread_attrs,
rcl_allocator_t allocator)
{
RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(RCL_RET_INVALID_ARGUMENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,78 +18,36 @@
#include <stddef.h>


#include "rcl_yaml_param_parser/types.h"
#include "rcl_yaml_param_parser/visibility_control.h"

#include "rcutils/allocator.h"
#include "rcutils/macros.h"
#include "rcutils/thread_attr.h"
#include "rcutils/types/rcutils_ret.h"

#ifdef __cplusplus
extern "C"
{
#endif

/// \brief Return a rcl_thread_attrs_t struct with members initialized to zero value.
/// \return a rcl_thread_attrs_t struct with members initialized to zero value.
RCL_YAML_PARAM_PARSER_PUBLIC
rcl_thread_attrs_t
rcl_get_zero_initialized_thread_attrs(void);

/// \brief Initialize list of thread attributes
/// \param[out] thread_attrs the list of thread attributes to be initialized
/// \param[in] allocator memory allocator to be used
/// \return #RCUTILS_RET_OK if the structure was initialized succeessfully, or
/// \return #RCUTILS_RET_INVALID_ARGUMENT if any function arguments are invalid, or
/// \return #RCUTILS_RET_BAD_ALLOC if allocating memory failed, or
/// \return #RCUTILS_RET_ERROR an unspecified error occur.
RCL_YAML_PARAM_PARSER_PUBLIC
rcutils_ret_t
rcl_thread_attrs_init(
rcl_thread_attrs_t * thread_attrs,
rcutils_allocator_t allocator);

/// \brief Initialize list of thread attributes with a capacity
/// \param[out] thread_attrs the list of thread attributes to be initialized
/// \param[in] allocator memory allocator to be used
/// \return #RCUTILS_RET_OK if the structure was initialized succeessfully, or
/// \return #RCUTILS_RET_INVALID_ARGUMENT if any function arguments are invalid, or
/// \return #RCUTILS_RET_BAD_ALLOC if allocating memory failed, or
/// \return #RCUTILS_RET_ERROR an unspecified error occur.
RCL_YAML_PARAM_PARSER_PUBLIC
rcutils_ret_t
rcl_thread_attrs_init_with_capacity(
rcl_thread_attrs_t * thread_attrs,
rcutils_allocator_t allocator,
size_t capacity);

/// \brief Free list of thread attributes
/// \param[in] thread_attrs The structure to be deallocated.
/// \return #RCUTILS_RET_OK if the memory was successfully freed, or
/// \return #RCUTILS_RET_INVALID_ARGUMENT if any function arguments are invalid
RCL_YAML_PARAM_PARSER_PUBLIC
rcutils_ret_t
rcl_thread_attrs_fini(
rcl_thread_attrs_t * thread_attrs);

/// \brief Parse the YAML file and populate \p thread_attrs
/// \pre Given \p thread_attrs must be a valid thread attribute struct
/// \param[in] file_path is the path to the YAML file
/// \param[inout] thread_attrs points to the struct to be populated
/// \param[in,out] thread_attrs points to the struct to be populated
/// \return true on success and false on failure
RCL_YAML_PARAM_PARSER_PUBLIC
rcutils_ret_t rcl_parse_yaml_thread_attrs_file(
const char * file_path,
rcl_thread_attrs_t * thread_attrs);
rcutils_thread_attrs_t * thread_attrs);

/// \brief Parse a thread attribute value as a YAML string, updating thread_attrs accordingly
/// \param[in] yaml_value is the thread attribute value as a YAML string to be parsed
/// \param[inout] thread_attrs points to the thread attribute struct
/// \param[in,out] thread_attrs points to the thread attribute struct
/// \return true on success and false on failure
RCL_YAML_PARAM_PARSER_PUBLIC
rcutils_ret_t rcl_parse_yaml_thread_attrs_value(
const char * yaml_value,
rcl_thread_attrs_t * thread_attrs);
rcutils_thread_attrs_t * thread_attrs);

#ifdef __cplusplus
}
Expand Down
37 changes: 0 additions & 37 deletions rcl_yaml_param_parser/include/rcl_yaml_param_parser/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,41 +108,4 @@ typedef struct rcl_params_s
rcutils_allocator_t allocator; ///< Allocator used
} rcl_params_t;

typedef enum rcl_thread_scheduling_policy_type_e
{
RCL_THREAD_SCHEDULING_POLICY_UNKNOWN = 0,
RCL_THREAD_SCHEDULING_POLICY_FIFO = 1,
RCL_THREAD_SCHEDULING_POLICY_RR = 2,
RCL_THREAD_SCHEDULING_POLICY_SPORADIC = 3,
RCL_THREAD_SCHEDULING_POLICY_OTHER = 4,
RCL_THREAD_SCHEDULING_POLICY_IDLE = 5,
RCL_THREAD_SCHEDULING_POLICY_BATCH = 6,
RCL_THREAD_SCHEDULING_POLICY_DEADLINE = 7
} rcl_thread_scheduling_policy_type_t;

typedef struct rcl_thread_attr_s
{
/// Thread core affinity
int core_affinity;
/// Thread scheduling policy.
rcl_thread_scheduling_policy_type_t scheduling_policy;
/// Thread priority.
int priority;
/// Thread name
char * name;
} rcl_thread_attr_t;

/// Hold thread attribute rules.
typedef struct rcl_thread_attrs_s
{
/// Private implementation array.
rcl_thread_attr_t * attributes;
/// Number of threads attribute
size_t num_attributes;
/// Number of threads attribute capacity
size_t capacity_attributes;
/// Allocator used to allocate objects in this struct
rcutils_allocator_t allocator;
} rcl_thread_attrs_t;

#endif // RCL_YAML_PARAM_PARSER__TYPES_H_
24 changes: 0 additions & 24 deletions rcl_yaml_param_parser/src/impl/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,30 +90,6 @@ rcutils_ret_t find_parameter(
rcl_params_t * param_st,
size_t * parameter_idx);

RCL_YAML_PARAM_PARSER_LOCAL
RCUTILS_WARN_UNUSED
rcutils_ret_t parse_thread_attr_key(
const char * value,
thread_attr_key_type_t * key_type);

RCL_YAML_PARAM_PARSER_LOCAL
RCUTILS_WARN_UNUSED
rcl_thread_scheduling_policy_type_t parse_thread_attr_scheduling_policy(
const char * value);

RCL_YAML_PARAM_PARSER_LOCAL
RCUTILS_WARN_UNUSED
rcutils_ret_t parse_thread_attr(
yaml_parser_t * parser,
rcl_thread_attr_t * attr,
rcutils_allocator_t allocator);

RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t parse_thread_attr_events(
yaml_parser_t * parser,
rcl_thread_attrs_t * thread_attrs);

#ifdef __cplusplus
}
#endif
Expand Down
9 changes: 4 additions & 5 deletions rcl_yaml_param_parser/src/impl/parse_thread_attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

#include "rcutils/allocator.h"
#include "rcutils/macros.h"
#include "rcutils/thread_attr.h"
#include "rcutils/types/rcutils_ret.h"

#include "./types.h"
#include "rcl_yaml_param_parser/types.h"
#include "rcl_yaml_param_parser/visibility_control.h"

#ifdef __cplusplus
Expand All @@ -38,21 +38,20 @@ rcutils_ret_t parse_thread_attr_key(

RCL_YAML_PARAM_PARSER_LOCAL
RCUTILS_WARN_UNUSED
rcl_thread_scheduling_policy_type_t parse_thread_attr_scheduling_policy(
rcutils_thread_scheduling_policy_t parse_thread_attr_scheduling_policy(
const char * value);

RCL_YAML_PARAM_PARSER_LOCAL
RCUTILS_WARN_UNUSED
rcutils_ret_t parse_thread_attr(
yaml_parser_t * parser,
rcl_thread_attr_t * attr,
rcutils_allocator_t allocator);
rcutils_thread_attrs_t * attrs);

RCL_YAML_PARAM_PARSER_PUBLIC
RCUTILS_WARN_UNUSED
rcutils_ret_t parse_thread_attr_events(
yaml_parser_t * parser,
rcl_thread_attrs_t * thread_attrs);
rcutils_thread_attrs_t * thread_attrs);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit e25cf2b

Please sign in to comment.