diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-06-17 00:41:20 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-06-17 00:41:20 +0200 |
| commit | eb7112ae055dda38da52b07449a7e1cf62005702 (patch) | |
| tree | dd39655c0de50a6ec0d250817dcb03eab0088e54 /src | |
| parent | 8b2a9cbdf0d31e5a9df91c05a531c4a860635278 (diff) | |
| download | ffwd-eb7112ae055dda38da52b07449a7e1cf62005702.tar.gz ffwd-eb7112ae055dda38da52b07449a7e1cf62005702.zip | |
kernels: cap thread pool to Linux CPU affinity, widen generation counter
Diffstat (limited to 'src')
| -rw-r--r-- | src/threadpool.c | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/threadpool.c b/src/threadpool.c index ed7e859..40d22c7 100644 --- a/src/threadpool.c +++ b/src/threadpool.c @@ -1,6 +1,11 @@ +#if defined(__linux__) && !defined(_GNU_SOURCE) +#define _GNU_SOURCE +#endif + #include "threadpool.h" #include <pthread.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -8,6 +13,9 @@ #ifdef __APPLE__ #include <sys/sysctl.h> #else +#ifdef __linux__ +#include <sched.h> +#endif #include <unistd.h> #endif @@ -30,7 +38,7 @@ static struct { parallel_fn_t fn; void *arg; - int generation; + uint64_t generation; pthread_mutex_t mutex; pthread_cond_t cond_work; @@ -47,7 +55,7 @@ static struct { static void *worker_loop(void *arg) { int tid = *(int *)arg; - int my_gen = 0; + uint64_t my_gen = 0; for (;;) { pthread_mutex_lock(&tp.mutex); @@ -179,6 +187,14 @@ int embed_get_num_cpus(void) { int online = (int)sysconf(_SC_NPROCESSORS_ONLN); if (online < 1) online = 1; +#ifdef __linux__ + cpu_set_t set; + if (sched_getaffinity(0, sizeof(set), &set) == 0) { + int affinity = CPU_COUNT(&set); + if (affinity >= 1 && affinity < online) + online = affinity; + } +#endif /* Honor a cgroup CPU quota when it is tighter than the visible core count, * so the default thread pool does not oversubscribe a CPU-limited * container and get throttled. */ |
