Skip to content

Commit de9fc87

Browse files
committed
Decoupled the additional feature from rclcpp to rcpputils, reflecting on the pointing out below.
ros2/rclcpp#2205 (comment) Signed-off-by: Shoji Morita <[email protected]>
1 parent 47ad866 commit de9fc87

File tree

13 files changed

+1368
-0
lines changed

13 files changed

+1368
-0
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ add_library(${PROJECT_NAME}
3232
target_include_directories(${PROJECT_NAME} PUBLIC
3333
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
3434
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>")
35+
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
36+
target_sources(${PROJECT_NAME} PRIVATE
37+
src/threads/posix_thread.cpp)
38+
elseif(WIN32)
39+
target_sources(${PROJECT_NAME} PRIVATE
40+
src/threads/windows_thread.cpp)
41+
endif()
3542
if(WIN32)
3643
target_compile_definitions(${PROJECT_NAME}
3744
PRIVATE "RCPPUTILS_BUILDING_LIBRARY")

include/rcpputils/threads.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2023 eSOL Co.,Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef RCPPUTILS__THREADS_HPP_
16+
#define RCPPUTILS__THREADS_HPP_
17+
18+
#if defined(__linux__)
19+
#include "rcpputils/threads/posix/thread.hpp"
20+
#elif defined(_WIN32)
21+
#include "rcpputils/threads/windows/thread.hpp"
22+
#else
23+
#include "rcpputils/threads/std/thread.hpp"
24+
#endif
25+
26+
#endif // RCPPUTILS__THREADS_HPP_
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright 2023 eSOL Co.,Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef RCPPUTILS__THREADS__POSIX__LINUX__CPU_SET_HPP_
16+
#define RCPPUTILS__THREADS__POSIX__LINUX__CPU_SET_HPP_
17+
18+
#include <pthread.h>
19+
#include <string.h>
20+
#include <vector>
21+
#include <utility>
22+
#include <memory>
23+
24+
#include "rcpputils/visibility_control.hpp"
25+
#include "rcpputils/threads/posix/utilities.hpp"
26+
27+
namespace rcpputils
28+
{
29+
30+
namespace detail
31+
{
32+
33+
struct CpuSet
34+
{
35+
using NativeCpuSetType = cpu_set_t;
36+
CpuSet() = default;
37+
explicit CpuSet(std::size_t cpu)
38+
{
39+
init_cpu_set();
40+
CPU_ZERO_S(alloc_size(), cpu_set_.get());
41+
CPU_SET_S(cpu, alloc_size(), cpu_set_.get());
42+
}
43+
CpuSet(const CpuSet & other)
44+
{
45+
if (other.cpu_set_) {
46+
init_cpu_set();
47+
memcpy(cpu_set_.get(), other.cpu_set_.get(), alloc_size());
48+
}
49+
}
50+
CpuSet & operator=(const CpuSet & other)
51+
{
52+
if (other.cpu_set_) {
53+
init_cpu_set();
54+
memcpy(cpu_set_.get(), other.cpu_set_.get(), alloc_size());
55+
} else {
56+
clear();
57+
}
58+
return *this;
59+
}
60+
CpuSet(CpuSet && other)
61+
: CpuSet()
62+
{
63+
swap(other);
64+
}
65+
CpuSet & operator=(CpuSet && other)
66+
{
67+
CpuSet tmp;
68+
other.swap(tmp);
69+
tmp.swap(*this);
70+
return *this;
71+
}
72+
void swap(CpuSet & other)
73+
{
74+
using std::swap;
75+
swap(cpu_set_, other.cpu_set_);
76+
swap(num_proc_, other.num_proc_);
77+
}
78+
void set(std::size_t cpu)
79+
{
80+
init_cpu_set();
81+
valid_cpu(cpu);
82+
CPU_SET_S(cpu, alloc_size(), cpu_set_.get());
83+
}
84+
void unset(std::size_t cpu)
85+
{
86+
init_cpu_set();
87+
valid_cpu(cpu);
88+
CPU_CLR_S(cpu, alloc_size(), cpu_set_.get());
89+
}
90+
void clear()
91+
{
92+
if (cpu_set_) {
93+
CPU_ZERO_S(alloc_size(), cpu_set_.get());
94+
}
95+
}
96+
bool is_set(std::size_t cpu) const
97+
{
98+
if (cpu_set_) {
99+
valid_cpu(cpu);
100+
return CPU_ISSET_S(cpu, alloc_size(), cpu_set_.get());
101+
} else {
102+
return false;
103+
}
104+
}
105+
106+
std::size_t max_processors() const
107+
{
108+
return num_proc_;
109+
}
110+
std::size_t alloc_size() const
111+
{
112+
return CPU_ALLOC_SIZE(num_proc_);
113+
}
114+
NativeCpuSetType * native_cpu_set() const
115+
{
116+
return cpu_set_.get();
117+
}
118+
119+
private:
120+
void init_cpu_set()
121+
{
122+
if (cpu_set_) {
123+
return;
124+
}
125+
auto num_proc = sysconf(_SC_NPROCESSORS_ONLN);
126+
if (num_proc <= 0) {
127+
throw_if_error(num_proc, "unrecognized sysconf(_SC_NPROCESSORS_ONLN) is not valid");
128+
}
129+
auto p = CPU_ALLOC(CPU_ALLOC_SIZE(num_proc));
130+
cpu_set_ = std::unique_ptr<NativeCpuSetType, CpuSetDeleter>(p);
131+
num_proc_ = num_proc;
132+
}
133+
void valid_cpu(std::size_t cpu) const
134+
{
135+
if (num_proc_ <= cpu) {
136+
auto ec = std::make_error_code(std::errc::invalid_argument);
137+
throw std::system_error{ec, "cpu number is invaild"};
138+
}
139+
}
140+
struct CpuSetDeleter
141+
{
142+
void operator()(NativeCpuSetType * cpu_set) const
143+
{
144+
CPU_FREE(cpu_set);
145+
}
146+
};
147+
std::unique_ptr<NativeCpuSetType, CpuSetDeleter> cpu_set_;
148+
std::size_t num_proc_;
149+
};
150+
151+
inline void swap(CpuSet & a, CpuSet & b)
152+
{
153+
a.swap(b);
154+
}
155+
156+
} // namespace detail
157+
158+
} // namespace rcpputils
159+
160+
#endif // RCPPUTILS__THREADS__POSIX__LINUX__CPU_SET_HPP_

0 commit comments

Comments
 (0)