• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ecmascript/taskpool/task_queue.h"
17 
18 namespace panda::ecmascript {
PostTask(std::unique_ptr<Task> task)19 void TaskQueue::PostTask(std::unique_ptr<Task> task)
20 {
21     os::memory::LockHolder holder(mtx_);
22     ASSERT(!terminate_);
23     tasks_.push_back(std::move(task));
24     cv_.Signal();
25 }
26 
PopTask()27 std::unique_ptr<Task> TaskQueue::PopTask()
28 {
29     os::memory::LockHolder holder(mtx_);
30     while (true) {
31         if (!tasks_.empty()) {
32             std::unique_ptr<Task> task = std::move(tasks_.front());
33             tasks_.pop_front();
34             return task;
35         }
36         if (terminate_) {
37             cv_.SignalAll();
38             return nullptr;
39         }
40         cv_.Wait(&mtx_);
41     }
42 }
43 
TerminateTask(int32_t id,TaskType type)44 void TaskQueue::TerminateTask(int32_t id, TaskType type)
45 {
46     os::memory::LockHolder holder(mtx_);
47     for (auto &task : tasks_) {
48         if (id != ALL_TASK_ID && id != task->GetId()) {
49             continue;
50         }
51         if (type != TaskType::ALL && type != task->GetTaskType()) {
52             continue;
53         }
54         task->Terminated();
55     }
56 }
57 
Terminate()58 void TaskQueue::Terminate()
59 {
60     os::memory::LockHolder holder(mtx_);
61     terminate_ = true;
62     cv_.SignalAll();
63 }
64 }  // namespace panda::ecmascript
65