#ifndef TILE_BASE_INTERNAL_THREAD_POOL_H
#define TILE_BASE_INTERNAL_THREAD_POOL_H

#pragma once

#include "tile/base/thread/cond_var.h"
#include "tile/base/thread/mutex.h"

#include <atomic>
#include <queue>
#include <thread>

namespace tile {
namespace internal {
class ThreadPool {
public:
    using JobType = std::function<void()>;
    explicit ThreadPool(std::size_t workers, const std::vector<int> &afinity, int nice_value);
    void QueueJob(JobType &&job);
    void Stop();
    void Join();

private:
    void WorkerProc();

private:
    std::atomic<bool> exiting_{false};
    std::vector<std::thread> workers_;

    Mutex lock_;
    CondVar cv_;
    std::queue<JobType> jobs_;
};
}// namespace internal
}// namespace tile

#endif// TILE_BASE_INTERNAL_THREAD_POOL_H
