GigaAM-v4-c / src /ga_pool.c
eschmidbauer's picture
streaming runtime and conversion pipeline for GigaAM-v4
5b65ff7
Raw History Blame Contribute Delete
4.45 kB
#include "ga_pool.h"
#include <pthread.h>
#include <sched.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#define GA_SPIN_NS 2000000L
#define GA_SPIN_CHECK 256
#if defined(__aarch64__)
#define cpu_relax() __asm__ __volatile__("yield")
#elif defined(__x86_64__) || defined(__i386__)
#define cpu_relax() __builtin_ia32_pause()
#else
#define cpu_relax() ((void)0)
#endif
typedef struct ga_worker {
ga_pool *pool;
pthread_t thread;
int index;
} ga_worker;
struct ga_pool {
int threads;
ga_worker *workers;
pthread_mutex_t lock;
pthread_cond_t wake;
atomic_uint generation;
atomic_int pending;
atomic_int sleepers;
atomic_int stopping;
ga_pool_fn fn;
void *arg;
};
static int64_t now_ns(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
}
/* Spins up to GA_SPIN_NS (gaps inside a step), then sleeps (gaps between packets).
* Periodic sched_yield lets other streams' threads run when cores are oversubscribed. */
static unsigned wait_generation(ga_pool *p, unsigned seen)
{
int64_t until = now_ns() + GA_SPIN_NS;
unsigned now;
for (int i = 1;; i++) {
now = atomic_load_explicit(&p->generation, memory_order_acquire);
if (now != seen)
return now;
cpu_relax();
if (i % GA_SPIN_CHECK == 0) {
if (now_ns() > until)
break;
sched_yield();
}
}
pthread_mutex_lock(&p->lock);
atomic_fetch_add(&p->sleepers, 1);
while ((now = atomic_load(&p->generation)) == seen)
pthread_cond_wait(&p->wake, &p->lock);
atomic_fetch_sub(&p->sleepers, 1);
pthread_mutex_unlock(&p->lock);
return now;
}
static void *worker_main(void *arg)
{
ga_worker *w = arg;
ga_pool *p = w->pool;
unsigned seen = 0;
for (;;) {
seen = wait_generation(p, seen);
if (atomic_load(&p->stopping))
return NULL;
p->fn(p->arg, w->index, p->threads);
atomic_fetch_sub_explicit(&p->pending, 1, memory_order_release);
}
}
/* seq_cst pairs with sleepers++ in wait_generation: no lost wakeup. */
static void publish(ga_pool *p)
{
atomic_fetch_add(&p->generation, 1);
if (atomic_load(&p->sleepers)) {
pthread_mutex_lock(&p->lock);
pthread_cond_broadcast(&p->wake);
pthread_mutex_unlock(&p->lock);
}
}
static void pool_free(ga_pool *p)
{
if (!p)
return;
atomic_store(&p->stopping, 1);
publish(p);
for (int i = 1; i < p->threads; i++)
pthread_join(p->workers[i].thread, NULL);
pthread_cond_destroy(&p->wake);
pthread_mutex_destroy(&p->lock);
free(p->workers);
free(p);
}
static ga_pool *pool_new(int threads)
{
ga_pool *p;
if (threads <= 1)
return NULL;
p = calloc(1, sizeof(*p));
if (!p)
return NULL;
p->workers = calloc((size_t)threads, sizeof(*p->workers));
pthread_mutex_init(&p->lock, NULL);
pthread_cond_init(&p->wake, NULL);
atomic_init(&p->generation, 0);
atomic_init(&p->pending, 0);
atomic_init(&p->sleepers, 0);
atomic_init(&p->stopping, 0);
if (!p->workers) {
pool_free(p);
return NULL;
}
p->threads = 1;
for (int i = 1; i < threads; i++) {
p->workers[i].pool = p;
p->workers[i].index = i;
if (pthread_create(&p->workers[i].thread, NULL, worker_main, &p->workers[i]) != 0) {
pool_free(p);
return NULL;
}
p->threads = i + 1;
}
return p;
}
/* Every worker runs every task, so none can fall a generation behind. */
static void pool_run(ga_pool *p, ga_pool_fn fn, void *arg)
{
if (!p) {
fn(arg, 0, 1);
return;
}
p->fn = fn;
p->arg = arg;
atomic_store_explicit(&p->pending, p->threads - 1, memory_order_relaxed);
publish(p);
fn(arg, 0, p->threads);
for (int i = 1; atomic_load_explicit(&p->pending, memory_order_acquire) > 0; i++) {
cpu_relax();
if (i % GA_SPIN_CHECK == 0)
sched_yield();
}
}
ga_pool *ga_pool_new(int threads)
{
return pool_new(threads);
}
void ga_pool_free(ga_pool *pool)
{
pool_free(pool);
}
int ga_pool_size(const ga_pool *pool)
{
return pool ? pool->threads : 1;
}
void ga_pool_run(ga_pool *pool, ga_pool_fn fn, void *arg)
{
pool_run(pool, fn, arg);
}