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

Decoupled the additional feature from rclcpp to rcpputils, reflecting… #179

Open
wants to merge 6 commits into
base: rolling
Choose a base branch
from
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ add_library(${PROJECT_NAME}
target_include_directories(${PROJECT_NAME} PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>")
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
target_sources(${PROJECT_NAME} PRIVATE
src/thread/detail/posix/thread.cpp
src/thread/detail/posix/thread_attribute.cpp
src/thread/detail/posix/linux/cpu_set.cpp
src/thread/detail/posix/linux/thread.cpp)
endif()
if(WIN32)
target_compile_definitions(${PROJECT_NAME}
PRIVATE "RCPPUTILS_BUILDING_LIBRARY")
Expand Down Expand Up @@ -140,6 +147,9 @@ if(BUILD_TESTING)

ament_add_gtest(test_accumulator test/test_accumulator.cpp)
target_link_libraries(test_accumulator ${PROJECT_NAME})

ament_add_gtest(test_thread test/test_thread.cpp)
target_link_libraries(test_thread ${PROJECT_NAME})
endif()

ament_package()
Expand Down
21 changes: 21 additions & 0 deletions include/rcpputils/thread.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2023 eSOL Co.,Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef RCPPUTILS__THREAD_HPP_
#define RCPPUTILS__THREAD_HPP_

#include "rcpputils/thread/thread.hpp"
#include "rcpputils/thread/thread_attribute.hpp"

#endif // RCPPUTILS__THREAD_HPP_
22 changes: 22 additions & 0 deletions include/rcpputils/thread/detail/posix/cpu_set.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2023 eSOL Co.,Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef RCPPUTILS__THREAD__DETAIL__POSIX__CPU_SET_HPP_
#define RCPPUTILS__THREAD__DETAIL__POSIX__CPU_SET_HPP_

#if __linux__
#include "rcpputils/thread/detail/posix/linux/cpu_set.hpp"
#endif

#endif // RCPPUTILS__THREAD__DETAIL__POSIX__CPU_SET_HPP_
81 changes: 81 additions & 0 deletions include/rcpputils/thread/detail/posix/linux/cpu_set.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2023 eSOL Co.,Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef RCPPUTILS__THREAD__DETAIL__POSIX__LINUX__CPU_SET_HPP_
#define RCPPUTILS__THREAD__DETAIL__POSIX__LINUX__CPU_SET_HPP_

#include <pthread.h>
#include <memory>
#include <utility>

#include "rcutils/thread_attr.h"
#include "rcpputils/thread/detail/posix/utilities.hpp"

#include "rcpputils/visibility_control.hpp"

namespace rcpputils
{

namespace thread
{
namespace detail
{

struct CpuSetDeleter
{
void operator()(cpu_set_t * p) const;
};
using UniqueNativeCpuSet = std::unique_ptr<cpu_set_t, CpuSetDeleter>;

} // namespace detail
} // namespace thread

struct CpuSet
{
using NativeCpuSetType = cpu_set_t *;

CpuSet() = default;
explicit CpuSet(rcutils_thread_core_affinity_t const & affinity);
CpuSet(const CpuSet & other);
CpuSet(CpuSet && other);

CpuSet & operator=(const CpuSet & other);
CpuSet & operator=(CpuSet && other);
void swap(CpuSet & other);
void set(std::size_t cpu);
void unset(std::size_t cpu);
void clear();
bool is_set(std::size_t cpu) const;
std::size_t count() const;

void set_rcutils_thread_core_affinity(rcutils_thread_core_affinity_t const & affinity);

static std::size_t num_processors();
CpuSet::NativeCpuSetType native_cpu_set() const;

private:
void init_cpu_set();
void valid_cpu(std::size_t cpu) const;
static std::size_t alloc_size();
thread::detail::UniqueNativeCpuSet cpu_set_;
};

inline void swap(CpuSet & a, CpuSet & b)
{
a.swap(b);
}

} // namespace rcpputils

#endif // RCPPUTILS__THREAD__DETAIL__POSIX__LINUX__CPU_SET_HPP_
50 changes: 50 additions & 0 deletions include/rcpputils/thread/detail/posix/sched_options.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2024 eSOL Co.,Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef RCPPUTILS__THREAD__DETAIL__POSIX__SCHED_OPTIONS_HPP_
#define RCPPUTILS__THREAD__DETAIL__POSIX__SCHED_OPTIONS_HPP_

#include <optional>
#include <utility>

#include "rcpputils/thread/detail/posix/cpu_set.hpp"
#include "rcpputils/thread/detail/posix/sched_policy.hpp"
#include "rcpputils/visibility_control.hpp"

namespace rcpputils
{

struct SchedOptions
{
void swap(SchedOptions & other)
{
using std::swap;
swap(policy, other.policy);
swap(priority, other.priority);
swap(core_affinity, other.core_affinity);
}

std::optional<int> priority;
std::optional<SchedPolicy> policy;
std::optional<CpuSet> core_affinity;
};

inline void swap(SchedOptions & a, SchedOptions & b)
{
a.swap(b);
}

} // namespace rcpputils

#endif // RCPPUTILS__THREAD__DETAIL__POSIX__SCHED_OPTIONS_HPP_
69 changes: 69 additions & 0 deletions include/rcpputils/thread/detail/posix/sched_policy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2024 eSOL Co.,Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef RCPPUTILS__THREAD__DETAIL__POSIX__SCHED_POLICY_HPP_
#define RCPPUTILS__THREAD__DETAIL__POSIX__SCHED_POLICY_HPP_

#include <sched.h>

#include "rcutils/thread_attr.h"

#include "rcpputils/visibility_control.hpp"

namespace rcpputils
{

namespace thread
{
namespace detail
{

constexpr unsigned int sched_policy_explicit_bit = 0x8000'0000;

} // namespace detail
} // namespace thread

enum struct SchedPolicy : unsigned int
{
inherit,
other = thread::detail::sched_policy_explicit_bit | SCHED_OTHER,
#ifdef SCHED_FIFO
fifo = thread::detail::sched_policy_explicit_bit | SCHED_FIFO,
#endif
#ifdef SCHED_RR
rr = thread::detail::sched_policy_explicit_bit | SCHED_RR,
#endif
#ifdef SCHED_IDLE
idle = thread::detail::sched_policy_explicit_bit | SCHED_IDLE,
#endif
#ifdef SCHED_BATCH
batch = thread::detail::sched_policy_explicit_bit | SCHED_BATCH,
#endif
#ifdef SCHED_SPORADIC
sporadic = thread::detail::sched_policy_explicit_bit | SCHED_SPORADIC,
#endif
// #if __linux__
// linux deadline scheduler requires more parameter, not supported now
// #ifdef SCHED_DEADLINE
// deadline = SCHED_DEADLINE,
// #endif
// #endif
};

SchedPolicy from_rcutils_thread_scheduling_policy(
rcutils_thread_scheduling_policy_t rcutils_sched_policy);

} // namespace rcpputils

#endif // RCPPUTILS__THREAD__DETAIL__POSIX__SCHED_POLICY_HPP_
Loading