Files
EVEngine/src/modules/thread/Task.cpp
T
sunxfancyandCursor 87145e5ce2 feat: enhance system and thread modules with new functionalities and documentation updates
- Added the system module to provide OS, CPU, RAM, wall clock, sleep, clipboard, battery, and GPU information.
- Enhanced the thread module to support thread pools, tasks, and named channels for inter-thread communication.
- Updated documentation to reflect the new features in the system and thread modules, including usage examples and API references.
- Modified CMakeLists.txt to include the new system and thread modules, ensuring proper build configurations.
- Updated load.nut script to initialize the new system and thread functionalities.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-07 00:06:25 +08:00

69 lines
1.5 KiB
C++

#include "thread/Task.h"
#include "common/Exception.h"
namespace eve {
namespace thread {
Task::Task(std::function<void()> fn) : fn_(std::move(fn)) {
if (!fn_)
throw eve::Exception("Task function is null");
}
Task::~Task() = default;
std::string Task::getStatus() const {
std::lock_guard<std::mutex> lock(mu_);
return status_;
}
bool Task::isDone() const {
std::lock_guard<std::mutex> lock(mu_);
return status_ == "done" || status_ == "failed";
}
bool Task::hasFailed() const {
std::lock_guard<std::mutex> lock(mu_);
return status_ == "failed";
}
std::string Task::getError() const {
std::lock_guard<std::mutex> lock(mu_);
return error_;
}
void Task::wait() {
std::unique_lock<std::mutex> lock(mu_);
cv_.wait(lock, [this] { return status_ == "done" || status_ == "failed"; });
}
void Task::run() {
{
std::lock_guard<std::mutex> lock(mu_);
status_ = "running";
}
try {
fn_();
std::lock_guard<std::mutex> lock(mu_);
status_ = "done";
} catch (const eve::Exception &e) {
std::lock_guard<std::mutex> lock(mu_);
status_ = "failed";
error_ = e.what();
} catch (const std::exception &e) {
std::lock_guard<std::mutex> lock(mu_);
status_ = "failed";
error_ = e.what();
} catch (...) {
std::lock_guard<std::mutex> lock(mu_);
status_ = "failed";
error_ = "unknown exception";
}
cv_.notify_all();
}
} // namespace thread
} // namespace eve