|
| 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 RCLCPP__THREADS__POSIX__LINUX__CPU_SET_HPP_ |
| 16 | +#define RCLCPP__THREADS__POSIX__LINUX__CPU_SET_HPP_ |
| 17 | + |
| 18 | +#include <pthread.h> |
| 19 | +#include <vector> |
| 20 | +#include <utility> |
| 21 | +#include <memory> |
| 22 | + |
| 23 | +#include "rclcpp/visibility_control.hpp" |
| 24 | + |
| 25 | +namespace rclcpp |
| 26 | +{ |
| 27 | + |
| 28 | +namespace detail |
| 29 | +{ |
| 30 | + |
| 31 | +struct CpuSet |
| 32 | +{ |
| 33 | + using NativeCpuSetType = cpu_set_t; |
| 34 | + CpuSet() = default; |
| 35 | + explicit CpuSet(std::size_t cpu) |
| 36 | + { |
| 37 | + init_cpu_set(); |
| 38 | + CPU_ZERO_S(alloc_size(), cpu_set_.get()); |
| 39 | + CPU_SET_S(cpu, alloc_size(), cpu_set_.get()); |
| 40 | + } |
| 41 | + CpuSet(const CpuSet & other) |
| 42 | + { |
| 43 | + if (other.cpu_set_) { |
| 44 | + init_cpu_set(); |
| 45 | + memcpy(cpu_set_.get(), other.cpu_set_.get(), alloc_size()); |
| 46 | + } |
| 47 | + } |
| 48 | + CpuSet & operator=(const CpuSet & other) |
| 49 | + { |
| 50 | + if (other.cpu_set_) { |
| 51 | + init_cpu_set(); |
| 52 | + memcpy(cpu_set_.get(), other.cpu_set_.get(), alloc_size()); |
| 53 | + } else { |
| 54 | + clear(); |
| 55 | + } |
| 56 | + return *this; |
| 57 | + } |
| 58 | + CpuSet(CpuSet && other) |
| 59 | + : CpuSet() |
| 60 | + { |
| 61 | + swap(other); |
| 62 | + } |
| 63 | + CpuSet & operator=(CpuSet && other) |
| 64 | + { |
| 65 | + CpuSet tmp; |
| 66 | + other.swap(tmp); |
| 67 | + tmp.swap(*this); |
| 68 | + return *this; |
| 69 | + } |
| 70 | + void swap(CpuSet & other) |
| 71 | + { |
| 72 | + using std::swap; |
| 73 | + swap(cpu_set_, other.cpu_set_); |
| 74 | + swap(num_proc_, other.num_proc_); |
| 75 | + } |
| 76 | + void set(std::size_t cpu) |
| 77 | + { |
| 78 | + init_cpu_set(); |
| 79 | + valid_cpu(cpu); |
| 80 | + CPU_SET_S(cpu, alloc_size(), cpu_set_.get()); |
| 81 | + } |
| 82 | + void unset(std::size_t cpu) |
| 83 | + { |
| 84 | + init_cpu_set(); |
| 85 | + valid_cpu(cpu); |
| 86 | + CPU_CLR_S(cpu, alloc_size(), cpu_set_.get()); |
| 87 | + } |
| 88 | + void clear() |
| 89 | + { |
| 90 | + if (cpu_set_) { |
| 91 | + CPU_ZERO_S(alloc_size(), cpu_set_.get()); |
| 92 | + } |
| 93 | + } |
| 94 | + bool is_set(std::size_t cpu) const |
| 95 | + { |
| 96 | + if (cpu_set_) { |
| 97 | + valid_cpu(cpu); |
| 98 | + return CPU_ISSET_S(cpu, alloc_size(), cpu_set_.get()); |
| 99 | + } else { |
| 100 | + return false; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + std::size_t max_processors() const |
| 105 | + { |
| 106 | + return num_proc_; |
| 107 | + } |
| 108 | + std::size_t alloc_size() const |
| 109 | + { |
| 110 | + return CPU_ALLOC_SIZE(num_proc_); |
| 111 | + } |
| 112 | + NativeCpuSetType * native_cpu_set() const |
| 113 | + { |
| 114 | + return cpu_set_.get(); |
| 115 | + } |
| 116 | + |
| 117 | +private: |
| 118 | + void init_cpu_set() |
| 119 | + { |
| 120 | + if (cpu_set_) { |
| 121 | + return; |
| 122 | + } |
| 123 | + auto num_proc = sysconf(_SC_NPROCESSORS_ONLN); |
| 124 | + if (num_proc == -1) { |
| 125 | + return; |
| 126 | + } |
| 127 | + auto p = CPU_ALLOC(CPU_ALLOC_SIZE(num_proc)); |
| 128 | + cpu_set_ = std::unique_ptr<NativeCpuSetType, CpuSetDeleter>(p); |
| 129 | + num_proc_ = num_proc; |
| 130 | + } |
| 131 | + void valid_cpu(std::size_t cpu) const |
| 132 | + { |
| 133 | + if (num_proc_ <= cpu) { |
| 134 | + auto ec = std::make_error_code(std::errc::invalid_argument); |
| 135 | + throw std::system_error{ec, "cpu number is invaild"}; |
| 136 | + } |
| 137 | + } |
| 138 | + struct CpuSetDeleter |
| 139 | + { |
| 140 | + void operator()(NativeCpuSetType * cpu_set) const |
| 141 | + { |
| 142 | + CPU_FREE(cpu_set); |
| 143 | + } |
| 144 | + }; |
| 145 | + std::unique_ptr<NativeCpuSetType, CpuSetDeleter> cpu_set_; |
| 146 | + std::size_t num_proc_; |
| 147 | +}; |
| 148 | + |
| 149 | +inline void swap(CpuSet & a, CpuSet & b) |
| 150 | +{ |
| 151 | + a.swap(b); |
| 152 | +} |
| 153 | + |
| 154 | +} // namespace detail |
| 155 | + |
| 156 | +} // namespace rclcpp |
| 157 | + |
| 158 | +#endif // RCLCPP__THREADS__POSIX__LINUX__CPU_SET_HPP_ |
0 commit comments