1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "task_queue.h" 17 18 namespace Commonlibrary::Concurrent::TaskPoolModule { EnqueueTask(std::unique_ptr<Task> task)19void TaskQueue::EnqueueTask(std::unique_ptr<Task> task) 20 { 21 std::unique_lock<std::mutex> lock(mtx_); 22 tasks_.push(std::move(task)); 23 } 24 DequeueTask()25std::unique_ptr<Task> TaskQueue::DequeueTask() 26 { 27 std::unique_lock<std::mutex> lock(mtx_); 28 if (!tasks_.empty()) { 29 std::unique_ptr<Task> task = std::move(tasks_.front()); 30 tasks_.pop(); 31 return task; 32 } 33 34 return nullptr; 35 } 36 IsEmpty() const37bool TaskQueue::IsEmpty() const 38 { 39 std::unique_lock<std::mutex> lock(mtx_); 40 return tasks_.empty(); 41 } 42 } // namespace Commonlibrary::Concurrent::TaskPoolModule