gnu/sched.h: fix CPU_* definitions to match glibc

add CPU_COUNT and cpu_set_t

Change-Id: I9b92cab27a8bb4fda71a99721801013647e02588
Reviewed-on: https://review.haiku-os.org/c/haiku/+/9618
Reviewed-by: Jérôme Duval <[email protected]>
Reviewed-by: waddlesplash <[email protected]>
Tested-by: Commit checker robot <[email protected]>
This commit is contained in:
Jérôme Duval
2025-09-01 08:00:26 +00:00
parent ff9a75a6f5
commit e1e1c75a1f
3 changed files with 35 additions and 9 deletions
+26 -5
View File
@@ -32,6 +32,7 @@ typedef __haiku_uint32 cpuset_mask;
typedef struct _cpuset { typedef struct _cpuset {
cpuset_mask bits[_howmany(CPU_SETSIZE, NCPUSETBITS)]; cpuset_mask bits[_howmany(CPU_SETSIZE, NCPUSETBITS)];
} cpuset_t; } cpuset_t;
typedef cpuset_t cpu_set_t;
#define _CPUSET_BITSINDEX(cpu) ((cpu) / NCPUSETBITS) #define _CPUSET_BITSINDEX(cpu) ((cpu) / NCPUSETBITS)
@@ -40,12 +41,32 @@ typedef struct _cpuset {
/* FD_ZERO uses memset */ /* FD_ZERO uses memset */
#include <string.h> #include <string.h>
#define CPUSET_ZERO(set) memset((set), 0, sizeof(cpuset_t))
#define CPUSET_SET(cpu, set) ((set)->bits[_CPUSET_BITSINDEX(cpu)] |= _CPUSET_BIT(cpu))
#define CPUSET_CLR(cpu, set) ((set)->bits[_CPUSET_BITSINDEX(cpu)] &= ~_CPUSET_BIT(cpu))
#define CPUSET_ISSET(cpu, set) ((set)->bits[_CPUSET_BITSINDEX(cpu)] & _CPUSET_BIT(cpu))
#define CPUSET_COPY(source, target) (*(target) = *(source))
static inline unsigned int
__cpu_count(cpuset_t *set)
{
unsigned int count = 0;
for (unsigned int i = 0; i < _howmany(CPU_SETSIZE, NCPUSETBITS); i++) {
cpuset_mask mask = set->bits[i];
#if __GNUC__ > 2
count += __builtin_popcount(mask);
#else
while (mask > 0) {
if ((mask & 1) == 1)
count++;
mask >>= 1;
}
#endif
}
return count;
}
#define CPU_ZERO(set) memset((set), 0, sizeof(cpuset_t))
#define CPU_SET(cpu, set) ((set)->bits[_CPUSET_BITSINDEX(cpu)] |= _CPUSET_BIT(cpu))
#define CPU_CLR(cpu, set) ((set)->bits[_CPUSET_BITSINDEX(cpu)] &= ~_CPUSET_BIT(cpu))
#define CPU_ISSET(cpu, set) ((set)->bits[_CPUSET_BITSINDEX(cpu)] & _CPUSET_BIT(cpu))
#define CPU_COPY(source, target) (*(target) = *(source))
#define CPU_COUNT(set) __cpu_count(set)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
+1 -1
View File
@@ -1,7 +1,7 @@
SubDir HAIKU_TOP src tests libs gnu ; SubDir HAIKU_TOP src tests libs gnu ;
UseHeaders [ FDirName $(HAIKU_TOP) headers compatibility gnu ] : true ; UseHeaders [ FDirName $(HAIKU_TOP) headers compatibility gnu ] : true ;
SubDirC++Flags [ FDefines _GNU_SOURCE=1 ] ; SubDirC++Flags [ FDefines _DEFAULT_SOURCE=1 ] ;
SimpleTest sched_getcpu_test : sched_getcpu_test.cpp : libgnu.so ; SimpleTest sched_getcpu_test : sched_getcpu_test.cpp : libgnu.so ;
SimpleTest sched_affinity_test : sched_affinity_test.cpp : libgnu.so ; SimpleTest sched_affinity_test : sched_affinity_test.cpp : libgnu.so ;
+8 -3
View File
@@ -8,6 +8,7 @@
#include <sched.h> #include <sched.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
int int
@@ -19,8 +20,12 @@ main(int argc, char** argv)
printf("with affinity on CPU 1\n"); printf("with affinity on CPU 1\n");
pthread_t thread = pthread_self(); pthread_t thread = pthread_self();
cpuset_t cpuset; cpuset_t cpuset;
CPUSET_ZERO(&cpuset); CPU_ZERO(&cpuset);
CPUSET_SET(1, &cpuset); CPU_SET(1, &cpuset);
if (CPU_COUNT(&cpuset) != 1) {
fprintf(stderr, "CPU_COUNT failed\n");
exit(1);
}
if (pthread_setaffinity_np(thread, sizeof(cpuset_t), &cpuset) != 0) { if (pthread_setaffinity_np(thread, sizeof(cpuset_t), &cpuset) != 0) {
fprintf(stderr, "pthread_setaffinity_np failed\n"); fprintf(stderr, "pthread_setaffinity_np failed\n");
exit(1); exit(1);
@@ -29,7 +34,7 @@ main(int argc, char** argv)
fprintf(stderr, "pthread_getaffinity_np failed\n"); fprintf(stderr, "pthread_getaffinity_np failed\n");
exit(1); exit(1);
} }
if (!CPUSET_ISSET(1, &cpuset)) if (!CPU_ISSET(1, &cpuset))
fprintf(stderr, "affinity not on CPU 1\n"); fprintf(stderr, "affinity not on CPU 1\n");
if (sched_getcpu() != 1) if (sched_getcpu() != 1)
fprintf(stderr, "not running on CPU 1\n"); fprintf(stderr, "not running on CPU 1\n");