1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #include "kbf/task.h"
- #include <esp_log.h>
- using std::string;
- using std::vector;
- using std::shared_ptr;
- using std::make_shared;
- using namespace kbf;
- static uint32_t taskId = 0;
- std::vector<std::shared_ptr<Task>> Task::TaskList::tasks;
- void Task::TaskList::add(const shared_ptr<Task> &task) {
- ESP_LOGD(TAG, "%s(%d), refcount=%ld", __func__, task->id, task.use_count());
- tasks.push_back(task);
- };
- void Task::TaskList::remove(uint32_t id) {
- ESP_LOGD(TAG, "%s(%d)", __func__, id);
- for (auto it = tasks.begin(); it != tasks.end(); it++) {
- if ((*it)->id == id) {
- tasks.erase(it);
- return;
- }
- }
- ESP_LOGE(TAG, "%s(%d): not found", __func__, id);
- throw exception::KBFError("TaskList::remove(" + std::to_string(id) + ") failed");
- }
- Task::Task(string name, void *arg, const uint32_t stackSize, const uint32_t priority) :
- name(std::move(name)),
- stackSize(stackSize),
- priority(priority),
- id(taskId++),
- arg(arg) {
- ESP_LOGI(TAG, "%s(): id=%d, name=\"%s\"", __func__, id, this->name.c_str());
- }
- Task::~Task() {
- ESP_LOGI(TAG, "%s(%d)", __func__, id);
- }
- [[maybe_unused]] void Task::runInternal(void *arg) {
- ESP_LOGD(TAG, "%s()", __func__);
- auto task = static_cast<Task *>(arg);
- task->running_ = true;
- task->run(task->arg);
- ESP_LOGI(TAG, "task (%d, %s) finished", task->id, task->name.c_str());
- // TODO call onFinish
- task->running_ = false;
- TaskList::remove(task->id);
- vTaskDelete(nullptr);
- }
- void Task::stop() {
- if (!running_) {
- ESP_LOGW(TAG, "%s(%d): not running", __func__, id);
- return;
- }
- ESP_LOGI(TAG, "%s(%d)", __func__, id);
- TaskList::remove(id);
- vTaskDelete(handle);
- running_ = false;
- }
|