#ifndef BASE_INCLUDE_THREAD_TIMED_TASK_H_
#define BASE_INCLUDE_THREAD_TIMED_TASK_H_
#include <memory>
#include <unordered_map>
#include <utility>
#include <vector>
#include "base/include/base_export.h"
#include "base/include/closure.h"
#include "base/include/fml/task_runner.h"
namespace lynx {
namespace base {
class BASE_EXPORT TimedTaskManager {
public:
explicit TimedTaskManager(bool need_stop_all_tasks_when_exit = true,
fml::RefPtr<fml::TaskRunner> runner = nullptr);
~TimedTaskManager();
TimedTaskManager(const TimedTaskManager&) = delete;
TimedTaskManager& operator=(const TimedTaskManager&) = delete;
TimedTaskManager(TimedTaskManager&&) = delete;
TimedTaskManager& operator=(TimedTaskManager&&) = delete;
uint32_t SetTimeout(closure closure, int64_t delay);
uint32_t SetInterval(closure closure, int64_t delay);
void StopTask(uint32_t id);
void StopAllTasks();
void Pause();
void Resume();
bool IsPaused() const { return paused_; }
private:
struct Controller {
explicit Controller(closure other) : closure(std::move(other)) {}
Controller(const Controller&) = delete;
Controller& operator=(const Controller&) = delete;
closure closure;
};
struct DeferredTask {
uint32_t id;
std::unique_ptr<Controller> controller;
bool is_interval;
int64_t interval;
int32_t alloc_slot;
};
class Scope {
public:
Scope(TimedTaskManager* manager, uint32_t current,
bool is_interval = false);
~Scope();
private:
TimedTaskManager* manager_;
bool is_interval_;
};
void PostTimeout(std::unique_ptr<TimedTaskManager::Controller> controller,
int64_t delay, uint32_t current, int32_t alloc_slot);
void SetInterval(std::unique_ptr<TimedTaskManager::Controller> controller,
int64_t delay, uint32_t current, int32_t alloc_slot,
int64_t first_delay);
uint32_t current_ = 0;
std::unordered_map<uint32_t, Controller*> controllers_;
bool paused_{false};
std::vector<DeferredTask> deferred_tasks_;
fml::RefPtr<fml::TaskRunner> runner_;
uint32_t current_executing_task_{0};
bool has_pending_remove_task_{false};
bool need_stop_all_tasks_when_exit_{true};
};
}
}
#endif