2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "task-queue.h"
21 TaskQueue& TaskQueue::GetInstance() {
22 static TaskQueue task_queue;
27 gboolean TaskQueue::WorkCallback<void>(gpointer data) {
28 QueueData<void>* d = static_cast<QueueData<void>*>(data);
36 void TaskQueue::Queue(const std::function<void()>& work, const std::function<void()>& after_work) {
37 auto do_work = [work, after_work]() {
41 this->queue_worker_.add_job(do_work);
44 void TaskQueue::Async(const std::function<void()>& work) {
45 this->async_worker_.add_job(work);
48 void TaskQueue::Async(const std::function<void(const common::AsyncToken& token)>& work,
49 const common::AsyncToken& token) {
50 auto do_work = [work, token]() { work(token); };
54 void TaskQueue::DeleteJobs() {
56 std::lock_guard<std::mutex> lck{this->queue_worker_.jobs_mtx};
57 this->queue_worker_.jobs.clear();
60 std::lock_guard<std::mutex> lck{this->async_worker_.jobs_mtx};
61 this->async_worker_.jobs.clear();
65 void TaskQueue::Stop() {
66 LoggerI("Stopping TaskQueue workers");
71 void TaskQueue::ScheduleWorkInMainThread(const std::function<void()>& work) {
72 QueueData<void>* d = new QueueData<void>();
73 d->work_callback_ = work;
74 if (!g_idle_add(WorkCallback<void>, d)) {
75 LoggerE("g_idle_add failed");