#pragma once
#include "common.hpp"
#ifdef USE_THREADS
#include <pthread.h>
#endif


#define SMP_MAX 16


class Threads {
    public:
        typedef void (*cb_t)(void*);

    private:
#ifdef USE_THREADS
        typedef struct {
            Threads* inst;
            volatile void* arg;
            pthread_t thread;
        } work_t;
        work_t work[SMP_MAX];

        pthread_mutex_t mtx;
        pthread_cond_t cnd;

        volatile bool shutdown;
        volatile unsigned running;

        static void* cb(void*);
#endif
        cb_t user_cb;

    public:
        Threads(unsigned, cb_t);
        ~Threads();

        static void set_max(unsigned);
        void run(void* base, size_t len, size_t n);

        const unsigned num;
        static unsigned maxnum;
};