Skip to content

Commit

Permalink
Modified the structure of member names to reflect the point made on t…
Browse files Browse the repository at this point in the history
…he thread below.

ros-infrastructure/rep#385 (comment)

Signed-off-by: Shoji Morita <s-morita@esol.co.jp>
  • Loading branch information
smorita-esol committed Jan 26, 2024
1 parent bfab6e3 commit d27d544
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
8 changes: 4 additions & 4 deletions include/rcutils/thread_attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ typedef struct rcutils_thread_attr_s
rcutils_thread_scheduling_policy_t scheduling_policy;
/// Thread priority.
int priority;
/// Thread name
char const * name;
/// Thread attribute tag
char const * tag;
} rcutils_thread_attr_t;

/// Hold thread attribute rules.
Expand Down Expand Up @@ -148,7 +148,7 @@ rcutils_thread_attrs_copy(
* \param[in] sched_policy thread scheduling policy of adding attribute
* \param[in] core_affinity thread core affinity of adding attribute
* \param[in] priority thread priority of adding attribute
* \param[in] name thread name of adding attribute
* \param[in] tag thread attribute tag of adding attribute
* \return #RCUTILS_RET_OK if the thread attribute was successfully added, or
* \return #RCUTILS_RET_INVALID_ARGUMENT if any function arguments are invalid, or
* \return #RCUTILS_RET_BAD_ALLOC if allocating memory failed, or
Expand All @@ -162,7 +162,7 @@ rcutils_thread_attrs_add_attr(
rcutils_thread_scheduling_policy_t sched_policy,
rcutils_thread_core_affinity_t const * core_affinity,
int priority,
char const * name);
char const * tag);

/**
* \brief Return a zero-initialized rcutils_thread_core_affinity_t object.
Expand Down
28 changes: 14 additions & 14 deletions src/thread_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ rcutils_thread_attrs_fini(rcutils_thread_attrs_t * thread_attrs)
RCUTILS_CHECK_ALLOCATOR(allocator, return RCUTILS_RET_INVALID_ARGUMENT);
for (size_t i = 0; i < thread_attrs->num_attributes; ++i) {
rcutils_thread_attr_t * attr = thread_attrs->attributes + i;
if (NULL != attr->name) {
allocator->deallocate((char *)attr->name, allocator->state);
if (NULL != attr->tag) {
allocator->deallocate((char *)attr->tag, allocator->state);
}
}
allocator->deallocate(thread_attrs->attributes, allocator->state);
Expand Down Expand Up @@ -121,13 +121,13 @@ rcutils_thread_attrs_copy(
}

for (i = 0; i < thread_attrs->num_attributes; ++i) {
char * dup_name = rcutils_strdup(thread_attrs->attributes[i].name, allocator);
if (NULL == dup_name) {
char * dup_tag = rcutils_strdup(thread_attrs->attributes[i].tag, allocator);
if (NULL == dup_tag) {
ret = RCUTILS_RET_BAD_ALLOC;
goto error;
}
new_attrs[i] = thread_attrs->attributes[i];
new_attrs[i].name = dup_name;
new_attrs[i].tag = dup_tag;
}
*out_thread_attrs = *thread_attrs;
out_thread_attrs->attributes = new_attrs;
Expand All @@ -137,7 +137,7 @@ rcutils_thread_attrs_copy(
error:
if (NULL != new_attrs) {
for (size_t j = 0; j < i; ++j) {
allocator.deallocate((char *)new_attrs[i].name, allocator.state);
allocator.deallocate((char *)new_attrs[i].tag, allocator.state);
}
allocator.deallocate(new_attrs, allocator.state);
}
Expand Down Expand Up @@ -173,14 +173,14 @@ rcutils_thread_attrs_add_attr(
rcutils_thread_scheduling_policy_t sched_policy,
rcutils_thread_core_affinity_t const * core_affinity,
int priority,
char const * name)
char const * tag)
{
RCUTILS_CHECK_ARGUMENT_FOR_NULL(thread_attrs, RCUTILS_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_ARGUMENT_FOR_NULL(name, RCUTILS_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_ARGUMENT_FOR_NULL(tag, RCUTILS_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_ARGUMENT_FOR_NULL(core_affinity, RCUTILS_RET_INVALID_ARGUMENT);

rcutils_ret_t ret;
char const * dup_name = NULL;
char const * dup_tag = NULL;
rcutils_thread_core_affinity_t new_affinity = rcutils_get_zero_initialized_thread_core_affinity();

if (thread_attrs->num_attributes == thread_attrs->capacity_attributes) {
Expand All @@ -197,8 +197,8 @@ rcutils_thread_attrs_add_attr(
}
}

dup_name = rcutils_strdup(name, thread_attrs->allocator);
if (NULL == dup_name) {
dup_tag = rcutils_strdup(tag, thread_attrs->allocator);
if (NULL == dup_tag) {
goto error;
}

Expand All @@ -213,15 +213,15 @@ rcutils_thread_attrs_add_attr(
attr->scheduling_policy = sched_policy;
attr->core_affinity = new_affinity;
attr->priority = priority;
attr->name = dup_name;
attr->tag = dup_tag;

++thread_attrs->num_attributes;

return RCUTILS_RET_OK;

error:
if (NULL != dup_name) {
thread_attrs->allocator.deallocate((char *)dup_name, thread_attrs->allocator.state);
if (NULL != dup_tag) {
thread_attrs->allocator.deallocate((char *)dup_tag, thread_attrs->allocator.state);
}
if (0 < new_affinity.core_count) {
rcutils_ret_t tmp_ret = rcutils_thread_core_affinity_fini(&new_affinity);
Expand Down
22 changes: 11 additions & 11 deletions test/test_thread_attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,11 @@ TEST_F(TestThreadAttrs, add_attribute) {
ret = rcutils_thread_core_affinity_init(&affinity, alloc);
ret = rcutils_thread_core_affinity_set(&affinity, 0xaa);

char thread_name[32];
char attr_tag[32];
for (size_t i = 0; i < 100; ++i) {
snprintf(thread_name, sizeof(thread_name), "thread name %lu", i);
snprintf(attr_tag, sizeof(attr_tag), "attr tag %lu", i);
ret = rcutils_thread_attrs_add_attr(
&attrs, RCUTILS_THREAD_SCHEDULING_POLICY_FIFO, &affinity, 0xbb, thread_name);
&attrs, RCUTILS_THREAD_SCHEDULING_POLICY_FIFO, &affinity, 0xbb, attr_tag);
EXPECT_EQ(RCUTILS_RET_OK, ret);
ASSERT_NE(nullptr, attrs.attributes);
ASSERT_LE(i + 1, attrs.capacity_attributes);
Expand All @@ -257,14 +257,14 @@ TEST_F(TestThreadAttrs, add_attribute) {
for (size_t i = 0; i < 100; ++i) {
rcutils_thread_attr_t attr = attrs.attributes[i];

snprintf(thread_name, sizeof(thread_name), "thread name %lu", i);
snprintf(attr_tag, sizeof(attr_tag), "attr tag %lu", i);
EXPECT_EQ(RCUTILS_THREAD_SCHEDULING_POLICY_FIFO, attr.scheduling_policy);
EXPECT_NE(affinity.set, attr.core_affinity.set);
EXPECT_EQ(affinity.core_count, attr.core_affinity.core_count);
EXPECT_EQ(0, memcmp(affinity.set, attr.core_affinity.set, affinity.core_count / CHAR_BIT));
EXPECT_EQ(0xbb, attr.priority);
EXPECT_NE(thread_name, attr.name);
EXPECT_STREQ(thread_name, attr.name);
EXPECT_NE(attr_tag, attr.tag);
EXPECT_STREQ(attr_tag, attr.tag);
}

ret = rcutils_thread_attrs_fini(&attrs);
Expand All @@ -277,11 +277,11 @@ TEST_F(TestThreadAttrs, copy) {
ret = rcutils_thread_core_affinity_init(&affinity, alloc);
ret = rcutils_thread_core_affinity_set(&affinity, 0xaa);

char thread_name[32];
char attr_tag[32];
for (size_t i = 0; i < 100; ++i) {
snprintf(thread_name, sizeof(thread_name), "thread name %lu", i);
snprintf(attr_tag, sizeof(attr_tag), "attr tag %lu", i);
ret = rcutils_thread_attrs_add_attr(
&attrs, RCUTILS_THREAD_SCHEDULING_POLICY_FIFO, &affinity, 0xbb, thread_name);
&attrs, RCUTILS_THREAD_SCHEDULING_POLICY_FIFO, &affinity, 0xbb, attr_tag);
ASSERT_EQ(RCUTILS_RET_OK, ret);
}

Expand All @@ -296,8 +296,8 @@ TEST_F(TestThreadAttrs, copy) {
EXPECT_EQ(RCUTILS_THREAD_SCHEDULING_POLICY_FIFO, attr.scheduling_policy);
EXPECT_EQ(0, memcmp(affinity.set, attr.core_affinity.set, affinity.core_count / CHAR_BIT));
EXPECT_EQ(0xbb, attr.priority);
EXPECT_NE(attrs.attributes[i].name, attr.name);
EXPECT_STREQ(attrs.attributes[i].name, attr.name);
EXPECT_NE(attrs.attributes[i].tag, attr.tag);
EXPECT_STREQ(attrs.attributes[i].tag, attr.tag);
}

ret = rcutils_thread_attrs_fini(&attrs);
Expand Down

0 comments on commit d27d544

Please sign in to comment.