context_ = ContextManager::GetInst().Create(name_, use_thread_);
if (context_ == nullptr)
throw std::invalid_argument("failed to create context");
-
- loop_ = g_main_loop_new(context_->GetHandle(), FALSE);
}
Task::~Task() {
_W("~Task: %s", name_.c_str());
- Quit();
if (use_thread_ && thread_.joinable()) {
if (pthread_self() == thread_.native_handle())
thread_.detach();
else
thread_.join();
}
-
- g_main_loop_unref(loop_);
}
std::shared_ptr<Task> Task::Create(std::string name, bool use_thread) {
}
void Task::Quit() {
- if (g_main_loop_is_running(loop_))
+ if (loop_ && g_main_loop_is_running(loop_))
g_main_loop_quit(loop_);
}
void Task::Run() {
+ loop_ = g_main_loop_new(context_->GetHandle(), FALSE);
if (use_thread_) {
std::unique_lock<std::mutex> lock(loop_mutex_);
idle_entered_ = false;
} else {
tid_ = getpid();
g_main_loop_run(loop_);
+ g_main_loop_unref(loop_);
+ loop_ = nullptr;
while (g_main_context_pending(context_->GetHandle()))
g_main_context_dispatch(context_->GetHandle());
}
bool Task::IsRunning() const {
+ if (loop_ == nullptr) return false;
return g_main_loop_is_running(loop_);
}
void Task::ThreadLoop() {
_D("[%s] BEGIN", name_.c_str());
+ auto self = shared_from_this();
{
std::unique_lock<std::mutex> lock(loop_mutex_);
GSource* source = g_idle_source_new();
pthread_setname_np(pthread_self(), name_.c_str());
g_main_loop_run(loop_);
+ g_main_loop_unref(loop_);
+ loop_ = nullptr;
while (g_main_context_pending(context_->GetHandle()))
g_main_context_dispatch(context_->GetHandle());