| 131 | } |
| 132 | |
| 133 | int main() { |
| 134 | if (!host_sibling_layout()) return 1; |
| 135 | const auto all = cpu::physical_cores(false); |
| 136 | std::vector<int> cores; |
| 137 | std::vector<ULONG> ids; |
| 138 | for (int core : all) { |
| 139 | if (std::any_of(cores.begin(), cores.end(), [core](int other) { return other / 64 == core / 64; })) continue; |
| 140 | ULONG id = 0; |
| 141 | if (!cpu::detail::cpu_set_for_core(core, id)) continue; |
| 142 | cores.push_back(core); |
| 143 | ids.push_back(id); |
| 144 | } |
| 145 | if (!check(!cores.empty(), "no usable CPU Sets")) return 1; |
| 146 | if (!check(cores.size() == GetActiveProcessorGroupCount(), "CPU Sets do not cover all active groups")) return 1; |
| 147 | |
| 148 | // Hard pins belong on pool-owned threads; never contaminate the caller under test with one. |
| 149 | for (size_t i = 0; i < cores.size(); ++i) { |
| 150 | bool ok = false; |
| 151 | std::thread worker([&] { |
| 152 | if (!cpu::detail::set_thread_group_affinity(cores[i], (int) i)) return; |
| 153 | GROUP_AFFINITY pinned{}; |
| 154 | if (!GetThreadGroupAffinity(GetCurrentThread(), &pinned)) return; |
| 155 | ok = pinned.Group == cores[i] / 64 && pinned.Mask == (KAFFINITY(1) << (cores[i] & 63)); |
| 156 | // Host placement must also preserve an existing restrictive hard affinity. |
| 157 | const auto previous = cpu::pin_current_thread(cores.back()); |
| 158 | cpu::restore_thread_affinity(previous); |
| 159 | GROUP_AFFINITY after{}; |
| 160 | ok = ok && previous.valid && GetThreadGroupAffinity(GetCurrentThread(), &after) && |
| 161 | after.Group == pinned.Group && after.Mask == pinned.Mask; |
| 162 | }); |
| 163 | worker.join(); |
| 164 | if (!check(ok, "worker hard pin / caller hard-affinity preservation")) return 1; |
| 165 | } |
| 166 | |
| 167 | bool host_ok = false; |
| 168 | std::thread host([&] { host_ok = host_round_trips(cores, ids); }); |
| 169 | host.join(); |
| 170 | if (!host_ok || !inherited_default(cores.back(), ids.back(), cores.front(), ids.front())) return 1; |
| 171 | std::printf("Windows affinity test: worker pins, host CPU Sets and process-default restoration passed (%zu groups)\n", cores.size()); |
| 172 | return 0; |
| 173 | } |
nothing calls this directly
no test coverage detected