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#include <functional>
24
25#include "ecmascript/taskpool/task.h"
26#include "ecmascript/platform/mutex.h"
27
28namespace panda::ecmascript {
29class TaskQueue {
30public:
31    TaskQueue() = default;
32    ~TaskQueue() = default;
33
34    NO_COPY_SEMANTIC(TaskQueue);
35    NO_MOVE_SEMANTIC(TaskQueue);
36
37    void PostTask(std::unique_ptr<Task> task);
38    std::unique_ptr<Task> PopTask();
39
40    void Terminate();
41    void TerminateTask(int32_t id, TaskType type);
42    void ForEachTask(const std::function<void(Task*)> &f);
43
44private:
45    std::deque<std::unique_ptr<Task>> tasks_;
46
47    std::atomic_bool terminate_ = false;
48    Mutex mtx_;
49    ConditionVariable cv_;
50};
51}  // namespace panda::ecmascript
52#endif  // ECMASCRIPT_TASKPOOL_TASK_QUEUE_H
53