1 /* 2 * Copyright (c) 2021 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 #ifndef ECMASCRIPT_TASKPOOL_TASK_QUEUE_H 17 #define ECMASCRIPT_TASKPOOL_TASK_QUEUE_H 18 19 #include <algorithm> 20 #include <atomic> 21 #include <deque> 22 #include <memory> 23 24 #include "ecmascript/taskpool/task.h" 25 #include "libpandabase/os/mutex.h" 26 27 namespace panda::ecmascript { 28 class TaskQueue { 29 public: 30 TaskQueue() = default; 31 ~TaskQueue() = default; 32 33 NO_COPY_SEMANTIC(TaskQueue); 34 NO_MOVE_SEMANTIC(TaskQueue); 35 36 void PostTask(std::unique_ptr<Task> task); 37 std::unique_ptr<Task> PopTask(); 38 39 void Terminate(); 40 void TerminateTask(int32_t id, TaskType type); 41 42 private: 43 std::deque<std::unique_ptr<Task>> tasks_; 44 45 std::atomic_bool terminate_ = false; 46 os::memory::Mutex mtx_; 47 os::memory::ConditionVariable cv_; 48 }; 49 } // namespace panda::ecmascript 50 #endif // ECMASCRIPT_TASKPOOL_TASK_QUEUE_H 51