From what I have read online and seen in struct.h
, cpu_set_t
is a struct that contains a bit mask, where each bit represents a CPU that can be used by the thread/process.
This is how it is defined:
typedef struct
{ __CPU_BITTYPE __bits[ CPU_SETSIZE / __CPU_BITS ];
} cpu_set_t
__CPU_BITTYPE
is an unsigned long int
.
Doesn't the above cpu_set_t
definition mean that the struct contains an array called __bits
of size CPU_SETSIZE / __CPU_BITS
? What am I missing here? How is it interpreted as a bit field?
CodePudding user response:
Yes, it's an array. This is to allow for the possibility that there may be more CPUs than the number of bits in an integer.
CPU_SETSIZE
is the total number of CPUs in a CPU set, and __CPU_BITS
is the number of bits used in each integer to hold the bit mask. To access the bit for a particular CPU, you divide the CPU number by __CPU_BITS
to get the array index, and use the modulus as a left shift to get to that bit in the integer. There's almost certainly a macro that performs this operation.
This is generally how bit arrays are implemented in C when you can't limit the number of elements to the size in bits of an integer type.