}
void Task::Dispose() {
+ if(use_thread_) {
+ std::unique_lock<std::mutex> quit_lock(loop_mutex_);
+ cond_var_.wait(quit_lock, [this]() {
+ return (loop_ == nullptr);
+ });
+ }
context_->SetLoop(nullptr);
ContextManager::GetInst().Dispose(name_);
}
}
void Task::Quit() {
- if (loop_ && g_main_loop_is_running(loop_))
- g_main_loop_quit(loop_);
+ if(use_thread_) {
+ std::unique_lock<std::mutex> quit_lock(loop_mutex_);
+ if (loop_ && g_main_loop_is_running(loop_))
+ g_main_loop_quit(loop_);
+ } else {
+ 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_);
+ if(loop_ != nullptr) {
+ _W("Task GMainLoop already in progress");
+ return;
+ }
+ loop_ = g_main_loop_new(context_->GetHandle(), FALSE);
idle_entered_ = false;
thread_ = std::thread([&]() -> void {
auto self = shared_from_this();
});
cond_var_.wait(lock, [this]() { return idle_entered_; });
} else {
+ if(loop_ != nullptr) {
+ _W("Task GMainLoop already in progress");
+ return;
+ }
+ loop_ = g_main_loop_new(context_->GetHandle(), FALSE);
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());
tid_ = -1;
+ g_main_loop_unref(loop_);
+ loop_ = nullptr;
}
}
bool Task::IsRunning() const {
- if (loop_ == nullptr) return false;
- return g_main_loop_is_running(loop_);
+ if(use_thread_) {
+ std::unique_lock<std::mutex> loop_lock(loop_mutex_);
+ return (loop_ && g_main_loop_is_running(loop_));
+ } else {
+ return (loop_ && g_main_loop_is_running(loop_));
+ }
}
std::shared_ptr<ISource> Task::AddIdleJob(std::function<bool()> job) {
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());
-
g_main_context_pop_thread_default(context_->GetHandle());
+
+ {
+ std::unique_lock<std::mutex> loop_lock(loop_mutex_);
+ g_main_loop_unref(loop_);
+ loop_ = nullptr;
+ }
+ cond_var_.notify_one();
+
_D("[%s] END", name_.c_str());
}