| 728 | } // namespace |
| 729 | |
| 730 | ThreadAffinity pin_current_thread(int core) { |
| 731 | if (core < 0) return {}; |
| 732 | #if defined(_WIN32) |
| 733 | ThreadAffinity previous; |
| 734 | ULONG target = 0; |
| 735 | if (!detail::get_thread_cpu_sets(previous.cpu_sets) || !detail::cpu_set_for_core(core, target) || |
| 736 | !SetThreadSelectedCpuSets(GetCurrentThread(), &target, 1)) { |
| 737 | std::fprintf(stderr, "strata cpu pool: host CPU Set selection for processor %d failed: %lu; previous placement kept\n", |
| 738 | core, (unsigned long) GetLastError()); |
| 739 | return {}; |
| 740 | } |
| 741 | previous.valid = true; |
| 742 | return previous; |
| 743 | #else |
| 744 | ThreadAffinity previous; |
| 745 | int error = detail::get_thread_affinity(previous.mask); |
| 746 | if (error == 0) error = detail::pin_thread_to_cpu(core); |
| 747 | if (error != 0) { |
| 748 | std::fprintf(stderr, "strata cpu pool: host affinity for CPU %d failed: %d; previous affinity kept\n", core, error); |
| 749 | return {}; |
| 750 | } |
| 751 | previous.valid = true; |
| 752 | return previous; |
| 753 | #endif |
| 754 | } |
| 755 | |
| 756 | void restore_thread_affinity(const ThreadAffinity& previous) { |
| 757 | if (!previous.valid) return; |