diff --git a/rcl/include/rcl/arguments.h b/rcl/include/rcl/arguments.h index ee4a0cfcb..ee830bfe1 100644 --- a/rcl/include/rcl/arguments.h +++ b/rcl/include/rcl/arguments.h @@ -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" @@ -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`. @@ -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 } diff --git a/rcl/include/rcl/context.h b/rcl/include/rcl/context.h index 67a57b05d..805b5b39c 100644 --- a/rcl/include/rcl/context.h +++ b/rcl/include/rcl/context.h @@ -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 } diff --git a/rcl/include/rcl/thread_attr.h b/rcl/include/rcl/thread_attr.h index 787a69837..cdf107b1d 100644 --- a/rcl/include/rcl/thread_attr.h +++ b/rcl/include/rcl/thread_attr.h @@ -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; @@ -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. @@ -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 diff --git a/rcl/src/rcl/arguments.c b/rcl/src/rcl/arguments.c index 725c43412..64627388f 100644 --- a/rcl/src/rcl/arguments.c +++ b/rcl/src/rcl/arguments.c @@ -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" @@ -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; } @@ -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, @@ -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; @@ -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); diff --git a/rcl/src/rcl/arguments_impl.h b/rcl/src/rcl/arguments_impl.h index 79a0a274f..880775993 100644 --- a/rcl/src/rcl/arguments_impl.h +++ b/rcl/src/rcl/arguments_impl.h @@ -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; diff --git a/rcl/src/rcl/context.c b/rcl/src/rcl/context.c index 2de0175b3..c30497857 100644 --- a/rcl/src/rcl/context.c +++ b/rcl/src/rcl/context.c @@ -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; @@ -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; diff --git a/rcl/src/rcl/context_impl.h b/rcl/src/rcl/context_impl.h index eff1d0a66..87c6908bf 100644 --- a/rcl/src/rcl/context_impl.h +++ b/rcl/src/rcl/context_impl.h @@ -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 diff --git a/rcl/src/rcl/init.c b/rcl/src/rcl/init.c index ecaa018d4..6923ce612 100644 --- a/rcl/src/rcl/init.c +++ b/rcl/src/rcl/init.c @@ -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; @@ -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; diff --git a/rcl/src/rcl/thread_attr.c b/rcl/src/rcl/thread_attr.c index e5c2781e6..4933046a9 100644 --- a/rcl/src/rcl/thread_attr.c +++ b/rcl/src/rcl/thread_attr.c @@ -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); @@ -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); diff --git a/rcl_yaml_param_parser/include/rcl_yaml_param_parser/parser_thread_attr.h b/rcl_yaml_param_parser/include/rcl_yaml_param_parser/parser_thread_attr.h index 0ac148d86..bbaf21ca5 100644 --- a/rcl_yaml_param_parser/include/rcl_yaml_param_parser/parser_thread_attr.h +++ b/rcl_yaml_param_parser/include/rcl_yaml_param_parser/parser_thread_attr.h @@ -18,11 +18,11 @@ #include -#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 @@ -30,66 +30,24 @@ 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 } diff --git a/rcl_yaml_param_parser/include/rcl_yaml_param_parser/types.h b/rcl_yaml_param_parser/include/rcl_yaml_param_parser/types.h index b7bad0b46..e0e4b5f1c 100644 --- a/rcl_yaml_param_parser/include/rcl_yaml_param_parser/types.h +++ b/rcl_yaml_param_parser/include/rcl_yaml_param_parser/types.h @@ -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_ diff --git a/rcl_yaml_param_parser/src/impl/parse.h b/rcl_yaml_param_parser/src/impl/parse.h index 87724f9ec..99f5d28e7 100644 --- a/rcl_yaml_param_parser/src/impl/parse.h +++ b/rcl_yaml_param_parser/src/impl/parse.h @@ -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 diff --git a/rcl_yaml_param_parser/src/impl/parse_thread_attr.h b/rcl_yaml_param_parser/src/impl/parse_thread_attr.h index e039853a1..1be1e6883 100644 --- a/rcl_yaml_param_parser/src/impl/parse_thread_attr.h +++ b/rcl_yaml_param_parser/src/impl/parse_thread_attr.h @@ -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 @@ -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 } diff --git a/rcl_yaml_param_parser/src/parse_thread_attr.c b/rcl_yaml_param_parser/src/parse_thread_attr.c index ff9f18431..2be2c94ee 100644 --- a/rcl_yaml_param_parser/src/parse_thread_attr.c +++ b/rcl_yaml_param_parser/src/parse_thread_attr.c @@ -23,7 +23,7 @@ #include "rcutils/error_handling.h" #include "rcutils/format_string.h" #include "rcutils/strdup.h" -#include "rcutils/types/rcutils_ret.h" +#include "rcutils/thread_attr.h" #include "./impl/parse.h" // to use get_value #include "./impl/parse_thread_attr.h" @@ -91,26 +91,26 @@ parse_thread_attr_key( /// /// Parse the value of the scheduling policy of a thread attribute /// -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_thread_scheduling_policy_type_t ret; + rcutils_thread_scheduling_policy_t ret; if (strcmp(value, "FIFO") == 0) { - ret = RCL_THREAD_SCHEDULING_POLICY_FIFO; + ret = RCUTILS_THREAD_SCHEDULING_POLICY_FIFO; } else if (strcmp(value, "RR") == 0) { - ret = RCL_THREAD_SCHEDULING_POLICY_RR; + ret = RCUTILS_THREAD_SCHEDULING_POLICY_RR; } else if (strcmp(value, "SPORADIC") == 0) { - ret = RCL_THREAD_SCHEDULING_POLICY_SPORADIC; + ret = RCUTILS_THREAD_SCHEDULING_POLICY_SPORADIC; } else if (strcmp(value, "OTHER") == 0) { - ret = RCL_THREAD_SCHEDULING_POLICY_OTHER; + ret = RCUTILS_THREAD_SCHEDULING_POLICY_OTHER; } else if (strcmp(value, "IDLE") == 0) { - ret = RCL_THREAD_SCHEDULING_POLICY_IDLE; + ret = RCUTILS_THREAD_SCHEDULING_POLICY_IDLE; } else if (strcmp(value, "BATCH") == 0) { - ret = RCL_THREAD_SCHEDULING_POLICY_BATCH; + ret = RCUTILS_THREAD_SCHEDULING_POLICY_BATCH; } else if (strcmp(value, "DEADLINE") == 0) { - ret = RCL_THREAD_SCHEDULING_POLICY_DEADLINE; + ret = RCUTILS_THREAD_SCHEDULING_POLICY_DEADLINE; } else { - ret = RCL_THREAD_SCHEDULING_POLICY_UNKNOWN; + ret = RCUTILS_THREAD_SCHEDULING_POLICY_UNKNOWN; } return ret; } @@ -120,12 +120,16 @@ rcl_thread_scheduling_policy_type_t parse_thread_attr_scheduling_policy( /// rcutils_ret_t parse_thread_attr( yaml_parser_t * parser, - rcl_thread_attr_t * attr, - rcutils_allocator_t allocator) + rcutils_thread_attrs_t * attrs) { rcutils_ret_t ret; yaml_event_t event; thread_attr_key_bits_t key_bits = THREAD_ATTR_KEY_BITS_NONE; + rcutils_thread_scheduling_policy_t sched_policy; + int core_affinity; + int priority; + char const * name = NULL; + rcutils_allocator_t allocator = attrs->allocator; while (1) { PARSE_WITH_CHECK_ERROR(&event); @@ -168,7 +172,7 @@ rcutils_ret_t parse_thread_attr( ret = RCUTILS_RET_ERROR; goto error; } - attr->core_affinity = ((int)*(int64_t *)(ret_val)); + core_affinity = ((int)*(int64_t *)(ret_val)); break; case THREAD_ATTR_KEY_PRIORITY: ret_val = get_value(value, style, tag, &val_type, allocator); @@ -178,10 +182,10 @@ rcutils_ret_t parse_thread_attr( ret = RCUTILS_RET_ERROR; goto error; } - attr->priority = ((int)*(int64_t *)(ret_val)); + priority = ((int)*(int64_t *)(ret_val)); break; case THREAD_ATTR_KEY_SCHEDULING_POLICY: - attr->scheduling_policy = parse_thread_attr_scheduling_policy(value); + sched_policy = parse_thread_attr_scheduling_policy(value); break; case THREAD_ATTR_KEY_NAME: if (*value == '\0') { @@ -189,8 +193,8 @@ rcutils_ret_t parse_thread_attr( ret = RCUTILS_RET_ERROR; goto error; } - attr->name = rcutils_strdup(value, allocator); - if (NULL == attr->name) { + name = rcutils_strdup(value, allocator); + if (NULL == name) { ret = RCUTILS_RET_BAD_ALLOC; goto error; } @@ -210,44 +214,28 @@ rcutils_ret_t parse_thread_attr( goto error; } + ret = rcutils_thread_attrs_add_attr(attrs, sched_policy, core_affinity, priority, name); + if (RCUTILS_RET_OK != ret) { + goto error; + } + + allocator.deallocate((char *)name, allocator.state); + return RCUTILS_RET_OK; error: - if (NULL != attr->name) { - allocator.deallocate(attr->name, allocator.state); + if (NULL != name) { + allocator.deallocate((char *)name, allocator.state); } return ret; } -static inline rcutils_ret_t extend_thread_attrs_capacity( - rcl_thread_attrs_t * attrs, - size_t new_cap) -{ - size_t cap = attrs->capacity_attributes; - size_t size = cap * sizeof(rcl_thread_attr_t); - size_t new_size = new_cap * sizeof(rcl_thread_attr_t); - rcl_thread_attr_t * new_attrs = attrs->allocator.reallocate( - attrs->attributes, new_size, attrs->allocator.state); - - if (NULL == new_attrs) { - RCUTILS_SET_ERROR_MSG("Failed to allocate memory for thread attributes"); - return RCUTILS_RET_BAD_ALLOC; - } - - memset(new_attrs + cap, 0, new_size - size); - - attrs->capacity_attributes = new_cap; - attrs->attributes = new_attrs; - - return RCUTILS_RET_OK; -} - /// /// Get events from parsing thread attributes YAML value string and process them /// rcutils_ret_t parse_thread_attr_events( yaml_parser_t * parser, - rcl_thread_attrs_t * thread_attrs) + rcutils_thread_attrs_t * thread_attrs) { yaml_event_t event; rcutils_ret_t ret; @@ -270,22 +258,7 @@ rcutils_ret_t parse_thread_attr_events( goto error; } - if (thread_attrs->num_attributes == thread_attrs->capacity_attributes) { - size_t new_cap = 0; - if (0 == thread_attrs->capacity_attributes) { - new_cap = 1; - } else { - new_cap = thread_attrs->capacity_attributes * 2; - } - // Extend the capacity - ret = extend_thread_attrs_capacity(thread_attrs, new_cap); - if (RCUTILS_RET_OK != ret) { - goto error; - } - } - - rcl_thread_attr_t * attr = thread_attrs->attributes + thread_attrs->num_attributes; - ret = parse_thread_attr(parser, attr, thread_attrs->allocator); + ret = parse_thread_attr(parser, thread_attrs); if (RCUTILS_RET_OK != ret) { goto error; } @@ -305,7 +278,9 @@ rcutils_ret_t parse_thread_attr_events( error: if (0 < thread_attrs->capacity_attributes) { - rcl_thread_attrs_fini(thread_attrs); + rcutils_ret_t thread_attrs_ret = rcutils_thread_attrs_fini(thread_attrs); + (void)thread_attrs_ret; + // Since an error has already occurred, ignore this result. } return ret; } diff --git a/rcl_yaml_param_parser/src/parser_thread_attr.c b/rcl_yaml_param_parser/src/parser_thread_attr.c index 6281ce183..abddba9b5 100644 --- a/rcl_yaml_param_parser/src/parser_thread_attr.c +++ b/rcl_yaml_param_parser/src/parser_thread_attr.c @@ -24,6 +24,7 @@ #include "rcutils/allocator.h" #include "rcutils/error_handling.h" +#include "rcutils/thread_attr.h" #include "rcutils/types/rcutils_ret.h" #include "./impl/types.h" @@ -32,84 +33,18 @@ #include "./impl/node_params.h" #include "./impl/yaml_variant.h" -#define INIT_NUM_THREAD_ATTRIBUTE 0U - -rcl_thread_attrs_t -rcl_get_zero_initialized_thread_attrs(void) -{ - rcl_thread_attrs_t ret = { - NULL, - }; - return ret; -} - -rcutils_ret_t -rcl_thread_attrs_init( - rcl_thread_attrs_t * thread_attrs, - rcutils_allocator_t allocator) -{ - return rcl_thread_attrs_init_with_capacity( - thread_attrs, allocator, INIT_NUM_THREAD_ATTRIBUTE); -} - -rcutils_ret_t -rcl_thread_attrs_init_with_capacity( - rcl_thread_attrs_t * thread_attrs, - rcutils_allocator_t allocator, - size_t capacity) -{ - RCUTILS_CHECK_ARGUMENT_FOR_NULL(thread_attrs, RCUTILS_RET_INVALID_ARGUMENT); - RCUTILS_CHECK_ALLOCATOR_WITH_MSG( - &allocator, "invalid allocator", return RCUTILS_RET_INVALID_ARGUMENT); - - thread_attrs->allocator = allocator; - thread_attrs->num_attributes = 0U; - thread_attrs->capacity_attributes = capacity; - if (capacity > 0) { - thread_attrs->attributes = - allocator.zero_allocate(capacity, sizeof(rcl_thread_attr_t), allocator.state); - if (NULL == thread_attrs->attributes) { - *thread_attrs = rcl_get_zero_initialized_thread_attrs(); - RCUTILS_SET_ERROR_MSG("Failed to allocate memory for thread attributes"); - return RCUTILS_RET_BAD_ALLOC; - } - } - return RCUTILS_RET_OK; -} - -rcutils_ret_t -rcl_thread_attrs_fini(rcl_thread_attrs_t * thread_attrs) -{ - RCUTILS_CHECK_ARGUMENT_FOR_NULL(thread_attrs, RCUTILS_RET_INVALID_ARGUMENT); - rcutils_allocator_t * allocator = &thread_attrs->allocator; - if (NULL == thread_attrs->attributes) { - return RCUTILS_RET_OK; - } - // check the allocator only if attributes is available to avoid checking after zero-initialized - RCUTILS_CHECK_ALLOCATOR(allocator, return RCUTILS_RET_INVALID_ARGUMENT); - for (size_t i = 0; i < thread_attrs->num_attributes; ++i) { - rcl_thread_attr_t * attr = thread_attrs->attributes + i; - if (NULL != attr->name) { - allocator->deallocate(attr->name, allocator->state); - } - } - allocator->deallocate(thread_attrs->attributes, allocator->state); - *thread_attrs = rcl_get_zero_initialized_thread_attrs(); - - return RCUTILS_RET_OK; -} - /// /// Parse the YAML file and populate thread_attrs /// 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) { RCUTILS_CHECK_FOR_NULL_WITH_MSG( file_path, "YAML file path is NULL", return RCUTILS_RET_INVALID_ARGUMENT); + RCUTILS_CHECK_ARGUMENT_FOR_NULL(thread_attrs, RCUTILS_RET_INVALID_ARGUMENT); - if (NULL == thread_attrs) { + if (0 < thread_attrs->num_attributes) { RCUTILS_SAFE_FWRITE_TO_STDERR("Pass an initialized thread attr structure"); return RCUTILS_RET_ERROR; } @@ -143,15 +78,12 @@ rcutils_ret_t rcl_parse_yaml_thread_attrs_file( /// 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) { - RCUTILS_CHECK_ARGUMENT_FOR_NULL(yaml_value, false); - - if (0U == strlen(yaml_value)) { - return RCUTILS_RET_ERROR; - } + RCUTILS_CHECK_ARGUMENT_FOR_NULL(yaml_value, RCUTILS_RET_INVALID_ARGUMENT); + RCUTILS_CHECK_ARGUMENT_FOR_NULL(thread_attrs, RCUTILS_RET_INVALID_ARGUMENT); - if (NULL == thread_attrs) { + if (0 < thread_attrs->num_attributes) { RCUTILS_SAFE_FWRITE_TO_STDERR("Pass an initialized thread attr structure"); return RCUTILS_RET_ERROR; }