#ifndef __OTX_FLASH_TASKPOOL_HPP__
#define __OTX_FLASH_TASKPOOL_HPP__

#include "taskpool/Task.hpp"
#include <mutex>
#include <queue>
#include <thread>
#include <vector>
#include <atomic>

namespace otx {
namespace flash {

class TaskPool;

class ExecuteContext {
public:
  Task *mTask;
  TaskPool *mPool;
};

class TaskPool {
public:
  TaskPool(int parallelSize);
  TaskPool(const TaskPool &right) = default;
  TaskPool &operator=(const TaskPool &right) = default;
  virtual ~TaskPool();

private:
  int mParallelSize; // 允许的并发数。
  int mCurrentParallelSize;// 当前并发数。
  // 内存管理约定：由外部决定。
  std::queue<Task *> mTasks;// 待执行的任务队列。
  std::mutex mTaskLock; // 任务队列锁。
  std::vector<std::thread *> mThreads;
  std::atomic<bool> mShutdown;

public:
  void addTask(Task *task);
  void cancelAll();
  void shutdown();

private:
  static void threadWork(ExecuteContext*);
};
} // namespace flash
} // namespace otx

#endif // __OTX_FLASH_TASKPOOL_HPP__