}
void Task::Quit() {
+ std::unique_lock<std::mutex> lock(loop_mutex_);
if (g_main_loop_is_running(loop_))
g_main_loop_quit(loop_);
}
void Task::Run() {
+ std::unique_lock<std::mutex> lock(loop_mutex_);
if (use_thread_) {
thread_ = std::thread([&]() -> void {
tid_ = gettid();
tid_ = -1;
cpu_boosting_level_ = TIZEN_CORE_CPU_BOOSTING_LEVEL_NONE;
});
+ cond_var_.wait(lock, [this]() { return g_main_loop_is_running(loop_); });
} else {
tid_ = getpid();
+ lock.unlock();
g_main_loop_run(loop_);
tid_ = -1;
}
}
bool Task::IsRunning() const {
+ std::unique_lock<std::mutex> lock(loop_mutex_);
return g_main_loop_is_running(loop_);
}
}
void Task::ThreadLoop() {
- g_main_context_push_thread_default(context_->GetHandle());
+ {
+ std::unique_lock<std::mutex> lock(loop_mutex_);
+ GSource* source = g_idle_source_new();
+ g_source_set_callback(
+ source, [](gpointer user_data) {
+ auto* task = static_cast<Task*>(user_data);
+ std::unique_lock<std::mutex> lock(task->loop_mutex_);
+ task->cond_var_.notify_one();
+ return G_SOURCE_REMOVE;
+ }, this,
+ nullptr);
+ g_source_set_priority(source, G_PRIORITY_HIGH);
+ g_source_attach(source, context_->GetHandle());
+ g_source_unref(source);
+
+ g_main_context_push_thread_default(context_->GetHandle());
+ }
+
g_main_loop_run(loop_);
g_main_context_pop_thread_default(context_->GetHandle());
}
bool use_thread_;
std::shared_ptr<Context> context_;
std::thread thread_;
+ std::condition_variable cond_var_;
+ mutable std::mutex loop_mutex_;
GMainLoop* loop_ = nullptr;
std::list<std::shared_ptr<ISource>> event_sources_;
mutable std::recursive_mutex mutex_;