| 78 | } |
| 79 | |
| 80 | static bool set_affinity(const std::vector<int>& ids) { |
| 81 | if (ids.empty()) return false; |
| 82 | const int highest = *std::max_element(ids.begin(), ids.end()); |
| 83 | if (highest < 0) return false; |
| 84 | const size_t count = (std::max)(initial_cpu_count(), (size_t) highest + 1); |
| 85 | DynamicCpuSet set(count); |
| 86 | if (!set) return false; |
| 87 | for (int id : ids) { |
| 88 | if (id < 0 || (size_t) id >= count) return false; |
| 89 | CPU_SET_S((size_t) id, set.bytes(), set.get()); |
| 90 | } |
| 91 | const int error = pthread_setaffinity_np(pthread_self(), set.bytes(), set.get()); |
| 92 | if (error != 0) { |
| 93 | std::fprintf(stderr, "pthread_setaffinity_np failed: %s\n", std::strerror(error)); |
| 94 | return false; |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | class OriginalAffinity { |
| 100 | public: |
no test coverage detected