k_affinity_mask.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. // This file references various implementation details from Atmosphere, an open-source firmware for
  5. // the Nintendo Switch. Copyright 2018-2020 Atmosphere-NX.
  6. #pragma once
  7. #include "common/assert.h"
  8. #include "common/common_types.h"
  9. #include "core/hardware_properties.h"
  10. namespace Kernel {
  11. class KAffinityMask {
  12. public:
  13. constexpr KAffinityMask() = default;
  14. [[nodiscard]] constexpr u64 GetAffinityMask() const {
  15. return this->mask;
  16. }
  17. constexpr void SetAffinityMask(u64 new_mask) {
  18. ASSERT((new_mask & ~AllowedAffinityMask) == 0);
  19. this->mask = new_mask;
  20. }
  21. [[nodiscard]] constexpr bool GetAffinity(s32 core) const {
  22. return (this->mask & GetCoreBit(core)) != 0;
  23. }
  24. constexpr void SetAffinity(s32 core, bool set) {
  25. ASSERT(0 <= core && core < static_cast<s32>(Core::Hardware::NUM_CPU_CORES));
  26. if (set) {
  27. this->mask |= GetCoreBit(core);
  28. } else {
  29. this->mask &= ~GetCoreBit(core);
  30. }
  31. }
  32. constexpr void SetAll() {
  33. this->mask = AllowedAffinityMask;
  34. }
  35. private:
  36. [[nodiscard]] static constexpr u64 GetCoreBit(s32 core) {
  37. ASSERT(0 <= core && core < static_cast<s32>(Core::Hardware::NUM_CPU_CORES));
  38. return (1ULL << core);
  39. }
  40. static constexpr u64 AllowedAffinityMask = (1ULL << Core::Hardware::NUM_CPU_CORES) - 1;
  41. u64 mask{};
  42. };
  43. } // namespace Kernel