• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 - 2025 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 "compileQueue.h"
17 
18 #include "util/diagnostic.h"
19 #include "varbinder/varbinder.h"
20 #include "varbinder/scope.h"
21 #include "compiler/core/emitter.h"
22 #include "compiler/core/function.h"
23 #include "compiler/core/pandagen.h"
24 #include "public/public.h"
25 
26 namespace ark::es2panda::compiler {
CompileQueue(size_t threadCount)27 CompileQueue::CompileQueue(size_t threadCount)
28 {
29     threads_.reserve(threadCount);
30 
31     for (size_t i = 0; i < threadCount; i++) {
32         threads_.push_back(os::thread::ThreadStart(Worker, this));
33     }
34 }
35 
~CompileQueue()36 CompileQueue::~CompileQueue()
37 {
38     void *retval = nullptr;
39 
40     std::unique_lock<std::mutex> lock(m_);
41     terminate_ = true;
42     lock.unlock();
43     jobsAvailable_.notify_all();
44 
45     for (const auto handleId : threads_) {
46         os::thread::ThreadJoin(handleId, &retval);
47     }
48 }
49 
Schedule(public_lib::Context * context)50 void CompileQueue::Schedule(public_lib::Context *context)
51 {
52     ES2PANDA_ASSERT(jobsCount_ == 0);
53     std::unique_lock<std::mutex> lock(m_);
54     auto &functions = context->parserProgram->VarBinder()->Functions();
55     jobs_ = new CompileJob[functions.size()]();
56 
57     for (auto *function : functions) {
58         if (function->IsEmitted()) {
59             continue;
60         }
61         jobs_[jobsCount_++].SetContext(context, function);  // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
62         function->SetEmitted();
63     }
64 
65     totalJobsCount_ = jobsCount_;
66 
67     lock.unlock();
68     jobsAvailable_.notify_all();
69 }
70 
Worker(CompileQueue * queue)71 void CompileQueue::Worker(CompileQueue *queue)
72 {
73     while (true) {
74         std::unique_lock<std::mutex> lock(queue->m_);
75         queue->jobsAvailable_.wait(lock, [queue]() { return queue->terminate_ || queue->jobsCount_ != 0; });
76 
77         if (queue->terminate_) {
78             return;
79         }
80 
81         lock.unlock();
82 
83         queue->Consume();
84         queue->jobsFinished_.notify_one();
85     }
86 }
87 
Consume()88 void CompileQueue::Consume()
89 {
90     std::unique_lock<std::mutex> lock(m_);
91     activeWorkers_++;
92 
93     while (jobsCount_ > 0) {
94         --jobsCount_;
95         auto &job = jobs_[jobsCount_];  // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
96 
97         lock.unlock();
98 
99         try {
100             job.Run();
101         } catch (const util::ThrowableDiagnostic &e) {
102             lock.lock();
103             errors_.push_back(e);
104             lock.unlock();
105         }
106 
107         lock.lock();
108     }
109 
110     activeWorkers_--;
111 }
112 
Wait(const JobsFinishedCb & onFinishedCb)113 void CompileQueue::Wait(const JobsFinishedCb &onFinishedCb)
114 {
115     std::unique_lock<std::mutex> lock(m_);
116     jobsFinished_.wait(lock, [this]() { return activeWorkers_ == 0 && jobsCount_ == 0; });
117 
118     if (!errors_.empty()) {
119         delete[] jobs_;
120         // NOLINTNEXTLINE
121         throw errors_.front();
122     }
123 
124     for (uint32_t i = 0; i < totalJobsCount_; i++) {
125         onFinishedCb(jobs_ + i);  // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
126     }
127 
128     // CC-OFFNXT(G.VAR.05) false positive
129     delete[] jobs_;
130     jobs_ = nullptr;
131 }
132 }  // namespace ark::es2panda::compiler
133