The Pedigree Project 0.1
CpuAffinity.h
1/*
2 * Copyright (c) 2026, Pedigree Developers
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted.
6 */
7
8#ifndef PEDIGREE_KERNEL_PROCESS_CPUAFFINITY_H
9#define PEDIGREE_KERNEL_PROCESS_CPUAFFINITY_H
10
11#include "pedigree/kernel/compiler.h"
12#include "pedigree/kernel/processor/types.h"
13
14class Thread;
15
17 static constexpr size_t MaximumCpus = 1024;
18 static constexpr size_t WordBits = sizeof(unsigned long) * 8;
19 static constexpr size_t WordCount = MaximumCpus / WordBits;
20 static constexpr size_t ByteCount = MaximumCpus / 8;
21 unsigned long words[WordCount] = {};
22
23 void* data() {
24 return words;
25 }
26 const void* data() const {
27 return words;
28 }
29 bool contains(size_t cpu) const {
30 return cpu < MaximumCpus && (words[cpu / WordBits] & (1UL << (cpu % WordBits)));
31 }
32 void set(size_t cpu) {
33 if (cpu < MaximumCpus)
34 words[cpu / WordBits] |= 1UL << (cpu % WordBits);
35 }
36 bool empty() const {
37 for (size_t i = 0; i < WordCount; ++i)
38 if (words[i])
39 return false;
40 return true;
41 }
42 void intersect(const CpuAffinityMask& other) {
43 for (size_t i = 0; i < WordCount; ++i)
44 words[i] &= other.words[i];
45 }
46};
47
48struct EXPORTED_PUBLIC ThreadPlacement {
49 CpuAffinityMask allowed;
50 bool migratable = false;
51
52 static ThreadPlacement initialUser();
53 static ThreadPlacement inherit(Thread& creator);
54};
55
56enum class AffinityResult { Success, Invalid, Terminal, Pinned, Busy, Unsupported };
57
58#endif