• 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/platform/runner.h"
17 
18 #include "os/thread.h"
19 
20 namespace panda::ecmascript {
Runner(uint32_t threadNum)21 Runner::Runner(uint32_t threadNum) : totalThreadNum_(threadNum)
22 {
23     for (uint32_t i = 0; i < threadNum; i++) {
24         // main thread is 0;
25         std::unique_ptr<std::thread> thread = std::make_unique<std::thread>(&Runner::Run, this, i + 1);
26         os::thread::SetThreadName(thread->native_handle(), "GC_WorkerThread");
27         threadPool_.emplace_back(std::move(thread));
28     }
29 
30     for (uint32_t i = 0; i < runningTask_.size(); i++) {
31         runningTask_[i] = nullptr;
32     }
33 }
34 
TerminateTask()35 void Runner::TerminateTask()
36 {
37     for (uint32_t i = 0; i < runningTask_.size(); i++) {
38         if (runningTask_[i] != nullptr) {
39             runningTask_[i]->Terminated();
40         }
41     }
42 }
43 
TerminateThread()44 void Runner::TerminateThread()
45 {
46     taskQueue_.Terminate();
47     TerminateTask();
48 
49     int threadNum = threadPool_.size();
50     for (int i = 0; i < threadNum; i++) {
51         threadPool_.at(i)->join();
52     }
53     threadPool_.clear();
54 }
55 
Run(uint32_t threadId)56 void Runner::Run(uint32_t threadId)
57 {
58     while (std::unique_ptr<Task> task = taskQueue_.PopTask()) {
59         runningTask_[threadId] = task.get();
60         task->Run(threadId);
61         runningTask_[threadId] = nullptr;
62     }
63 }
64 }  // namespace panda::ecmascript
65