1 /**
2  * Copyright (c) 2022-2024 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 PANDA_VERIFIER_JOBS_SERVICE_H
17 #define PANDA_VERIFIER_JOBS_SERVICE_H
18 
19 #include "libpandafile/include/source_lang_enum.h"
20 #include "runtime/include/mem/panda_containers.h"
21 
22 #include "verification/type/type_system.h"
23 
24 namespace ark::verifier {
25 
26 using SourceLang = panda_file::SourceLang;
27 class TaskProcessor;
28 
29 class VerifierService final {
30 public:
VerifierService(mem::InternalAllocatorPtr allocator, ClassLinker *linker)31     VerifierService(mem::InternalAllocatorPtr allocator, ClassLinker *linker) : allocator_ {allocator}, linker_ {linker}
32     {
33     }
34     ~VerifierService() = default;
35 
36     NO_COPY_SEMANTIC(VerifierService);
37     NO_MOVE_SEMANTIC(VerifierService);
38 
39     void Init();
40     void Destroy();
41 
GetClassLinker()42     ClassLinker *GetClassLinker()
43     {
44         return linker_;
45     }
46 
47     TaskProcessor *GetProcessor(SourceLang lang);
48     void ReleaseProcessor(TaskProcessor *processor);
49 
50 private:
51     struct LangData {
LangDataark::verifier::final::LangData52         explicit LangData(SourceLang language) : lang {language} {}
53 
54         // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
55         SourceLang lang;
56         size_t totalProcessors = 0;
57         PandaDeque<TaskProcessor *> queue;
58         // NOLINTEND(misc-non-private-member-variables-in-classes)
59     };
60 
61     mem::InternalAllocatorPtr allocator_;
62     ClassLinker *linker_;
63 
64     ark::os::memory::Mutex lock_;
65     ark::os::memory::ConditionVariable condVar_ GUARDED_BY(lock_);
66     PandaUnorderedMap<panda_file::SourceLang, LangData> processors_ GUARDED_BY(lock_);
67 
68     bool shutdown_ {false};
69 };
70 
71 class TaskProcessor final {
72 public:
TaskProcessor(VerifierService *service, SourceLang lang)73     explicit TaskProcessor(VerifierService *service, SourceLang lang) : service_ {service}, lang_ {lang} {};
74 
GetTypeSystem()75     TypeSystem *GetTypeSystem()
76     {
77         return &typeSystem_;
78     }
79 
GetLang()80     SourceLang GetLang()
81     {
82         return lang_;
83     }
84 
85 private:
86     VerifierService *service_;
87     SourceLang const lang_;
88     TypeSystem typeSystem_ {service_, lang_};
89 };
90 
91 }  // namespace ark::verifier
92 
93 #endif  //  PANDA_VERIFIER_JOBS_SERVICE_H
94