#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "threadpool-common.h"
#include <pthread.h>
#include <unistd.h>
#if PTHREADPOOL_USE_FUTEX
#if defined(__linux__)
#include <sys/syscall.h>
#include <linux/futex.h>
#ifndef SYS_futex
#define SYS_futex …
#endif
#ifndef FUTEX_PRIVATE_FLAG
#define FUTEX_PRIVATE_FLAG …
#endif
#elif defined(__EMSCRIPTEN__)
#include <math.h>
#include <emscripten/threading.h>
#else
#error "Platform-specific implementation of futex_wait and futex_wake_all required"
#endif
#endif
#ifdef _WIN32
#include <sysinfoapi.h>
#endif
#if PTHREADPOOL_USE_CPUINFO
#include <cpuinfo.h>
#endif
#include <pthreadpool.h>
#include "threadpool-atomics.h"
#include "threadpool-object.h"
#include "threadpool-utils.h"
#if PTHREADPOOL_USE_FUTEX
#if defined(__linux__)
static int futex_wait(pthreadpool_atomic_uint32_t* address, uint32_t value) { … }
static int futex_wake_all(pthreadpool_atomic_uint32_t* address) { … }
#elif defined(__EMSCRIPTEN__)
static int futex_wait(pthreadpool_atomic_uint32_t* address, uint32_t value) {
return emscripten_futex_wait((volatile void*) address, value, INFINITY);
}
static int futex_wake_all(pthreadpool_atomic_uint32_t* address) {
return emscripten_futex_wake((volatile void*) address, INT_MAX);
}
#else
#error "Platform-specific implementation of futex_wait and futex_wake_all required"
#endif
#endif
static void checkin_worker_thread(struct pthreadpool* threadpool) { … }
static void wait_worker_threads(struct pthreadpool* threadpool) { … }
static uint32_t wait_for_new_command(
struct pthreadpool* threadpool,
uint32_t last_command,
uint32_t last_flags)
{ … }
static void* thread_main(void* arg) { … }
struct pthreadpool* pthreadpool_create(size_t threads_count) { … }
PTHREADPOOL_INTERNAL void pthreadpool_parallelize(
struct pthreadpool* threadpool,
thread_function_t thread_function,
const void* params,
size_t params_size,
void* task,
void* context,
size_t linear_range,
uint32_t flags)
{ … }
void pthreadpool_destroy(struct pthreadpool* threadpool) { … }