* @brief Set the processor affinity of the current thread using the current platform's native API. This should work on Windows and Linux, but is not possible on macOS and Android as the native API does not allow it. Note that the thread affinity must be a subset of the process affinity (as obtained using `BS::get_os_process_affinity()`) for the containing process of a thread. * * @param
| 1046 | * @return `true` if the affinity was set successfully, `false` otherwise. On macOS and Android, this function always returns `false`. |
| 1047 | */ |
| 1048 | static bool set_os_thread_affinity([[maybe_unused]] const std::vector<bool>& affinity) |
| 1049 | { |
| 1050 | #if defined(_WIN32) |
| 1051 | DWORD_PTR thread_mask = 0; |
| 1052 | for (std::size_t i = 0; i < std::min<std::size_t>(affinity.size(), sizeof(DWORD_PTR) * 8); ++i) |
| 1053 | thread_mask |= (affinity[i] ? (1ULL << i) : 0ULL); |
| 1054 | return SetThreadAffinityMask(GetCurrentThread(), thread_mask) != 0; |
| 1055 | #elif defined(__linux__) && !defined(__ANDROID__) |
| 1056 | cpu_set_t cpu_set; |
| 1057 | CPU_ZERO(&cpu_set); |
| 1058 | for (std::size_t i = 0; i < std::min<std::size_t>(affinity.size(), CPU_SETSIZE); ++i) |
| 1059 | { |
| 1060 | if (affinity[i]) |
| 1061 | CPU_SET(i, &cpu_set); |
| 1062 | } |
| 1063 | return pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_set) == 0; |
| 1064 | #else |
| 1065 | return false; |
| 1066 | #endif |
| 1067 | } |
| 1068 | |
| 1069 | /** |
| 1070 | * @brief Get the name of the current thread using the current platform's native API. This should work on Windows, Linux, and macOS. |