| 1 | // Aseprite |
|---|---|
| 2 | // Copyright (C) 2019-2022 Igara Studio S.A. |
| 3 | // |
| 4 | // This program is distributed under the terms of |
| 5 | // the End-User License Agreement for Aseprite. |
| 6 | |
| 7 | #ifdef HAVE_CONFIG_H |
| 8 | #include "config.h" |
| 9 | #endif |
| 10 | |
| 11 | #include "app/task.h" |
| 12 | |
| 13 | #include "base/task.h" |
| 14 | #include "base/thread.h" |
| 15 | #include "base/thread_pool.h" |
| 16 | |
| 17 | namespace app { |
| 18 | |
| 19 | static base::thread_pool tasks_pool(4); |
| 20 | |
| 21 | Task::Task() |
| 22 | : m_token(nullptr) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | Task::~Task() |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | void Task::run(base::task::func_t&& func) |
| 31 | { |
| 32 | std::lock_guard lock(m_token_mutex); |
| 33 | m_task.on_execute(std::move(func)); |
| 34 | m_token = &m_task.start(tasks_pool); |
| 35 | } |
| 36 | |
| 37 | void Task::wait() |
| 38 | { |
| 39 | // TODO wait a condition variable |
| 40 | while (!m_task.completed()) { |
| 41 | base::this_thread::sleep_for(0.1); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | } // namespace app |
| 46 |