Repository: google/benchmark
File: src/sysinfo.cc (C++)

Code before:

      cpu_set_t affinity;
      memcpy(&affinity, &previous_affinity, sizeof(affinity));

      bool is_first_cpu = true;

      for (int i = 0; i < CPU_SETSIZE; ++i)
        if (CPU_ISSET(i, &affinity)) {
          if (is_first_cpu)
            is_first_cpu = false;
          else
            CPU_CLR(i, &affinity);
        }

      if (is_first_cpu) return false;

      ret = pthread_setaffinity_np(self, sizeof(affinity), &affinity);
      return ret == 0;
#elif defined(BENCHMARK_OS_WINDOWS_WIN32)
      self = GetCurrentThread();
      DWORD_PTR mask = static_cast<DWORD_PTR>(1) << GetCurrentProcessorNumber();
      previous_affinity = SetThreadAffinityMask(self, mask);
      return previous_affinity != 0;
#else
      return false;
#endif
    }();

    if (!reset_affinity)
      std::cerr << "***WARNING*** Failed to set thread affinity. Estimated CPU "
                   "frequency may be incorrect."
                << std::endl;
  }

  ~ThreadAffinityGuard() {
    if (!reset_affinity) return;

#if defined(BENCHMARK_HAS_PTHREAD_AFFINITY)
    int ret = pthread_setaffinity_np(self, sizeof(previous_affinity),
                                     &previous_affinity);
    if (ret == 0) return;
#elif defined(BENCHMARK_OS_WINDOWS_WIN32)
    DWORD_PTR ret = SetThreadAffinityMask(self, previous_affinity);
    if (ret != 0) return;
#endif
    PrintErrorAndDie("Failed to reset thread affinity");
  }

  ThreadAffinityGuard(ThreadAffinityGuard&&) = delete;
  ThreadAffinityGuard(const ThreadAffinityGuard&) = delete;
  ThreadAffinityGuard& operator=(ThreadAffinityGuard&&) = delete;

Review comment (suggestion):
`#endif  // BENCHMARK_HAS_PTHREAD_AFFINITY`

or similar please, to make this easier to reason about.

Code after:
                                     &previous_affinity);
    if (ret == 0) return;
#elif defined(BENCHMARK_OS_WINDOWS_WIN32)
    DWORD_PTR ret = SetThreadAffinityMask(self, previous_affinity);
    if (ret != 0) return;
#endif  // def BENCHMARK_HAS_PTHREAD_AFFINITY
    PrintErrorAndDie("Failed to reset thread affinity");
  }

  ThreadAffinityGuard(ThreadAffinityGuard&&) = delete;
  ThreadAffinityGuard(const ThreadAffinityGuard&) = delete;
  ThreadAffinityGuard& operator=(ThreadAffinityGuard&&) = delete;
  ThreadAffinityGuard& operator=(const ThreadAffinityGuard&) = delete;

 private:
  bool SetAffinity() {
#if defined(BENCHMARK_HAS_PTHREAD_AFFINITY)
    int ret;
    self = pthread_self();
    ret = pthread_getaffinity_np(self, sizeof(previous_affinity),
                                 &previous_affinity);
    if (ret != 0) return false;

    cpu_set_t affinity;
    memcpy(&affinity, &previous_affinity, sizeof(affinity));

    bool is_first_cpu = true;

    for (int i = 0; i < CPU_SETSIZE; ++i)
      if (CPU_ISSET(i, &affinity)) {
        if (is_first_cpu)
          is_first_cpu = false;
        else
          CPU_CLR(i, &affinity);
      }

    if (is_first_cpu) return false;

    ret = pthread_setaffinity_np(self, sizeof(affinity), &affinity);
    return ret == 0;
#elif defined(BENCHMARK_OS_WINDOWS_WIN32)
    self = GetCurrentThread();
    DWORD_PTR mask = static_cast<DWORD_PTR>(1) << GetCurrentProcessorNumber();
    previous_affinity = SetThreadAffinityMask(self, mask);
    return previous_affinity != 0;
#else
    return false;
#endif  // def BENCHMARK_HAS_PTHREAD_AFFINITY
  }

#if defined(BENCHMARK_HAS_PTHREAD_AFFINITY)