• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 #ifndef RUTNIME_COMPILER_TASK_MANAGER_WORKER_H
16 #define RUTNIME_COMPILER_TASK_MANAGER_WORKER_H
17 
18 #include "runtime/compiler_worker.h"
19 #include "runtime/include/runtime.h"
20 #include "runtime/include/mem/panda_containers.h"
21 #include "libpandabase/taskmanager/task.h"
22 #include "libpandabase/taskmanager/task_queue.h"
23 #include "libpandabase/taskmanager/task_scheduler.h"
24 
25 namespace ark {
26 
27 /// @brief Compiler worker task pool based on common TaskManager (TaskQueue)
28 class CompilerTaskManagerWorker : public CompilerWorker {
29 public:
30     /* Compiler task queue (TaskManager) specific variables */
31     static constexpr taskmanager::TaskProperties JIT_TASK_PROPERTIES {
32         taskmanager::TaskType::JIT, taskmanager::VMType::STATIC_VM, taskmanager::TaskExecutionMode::BACKGROUND};
33 
34     CompilerTaskManagerWorker(mem::InternalAllocatorPtr internalAllocator, Compiler *compiler);
35 
36     NO_COPY_SEMANTIC(CompilerTaskManagerWorker);
37     NO_MOVE_SEMANTIC(CompilerTaskManagerWorker);
38 
InitializeWorker()39     void InitializeWorker() override
40     {
41         compilerWorkerJoined_ = false;
42     }
43 
FinalizeWorker()44     void FinalizeWorker() override
45     {
46         JoinWorker();
47     }
48 
49     void JoinWorker() override;
50 
IsWorkerJoined()51     bool IsWorkerJoined() override
52     {
53         return compilerWorkerJoined_;
54     }
55 
56     void AddTask(CompilerTask &&task) override;
57 
~CompilerTaskManagerWorker()58     ~CompilerTaskManagerWorker() override
59     {
60         taskmanager::TaskScheduler::GetTaskScheduler()
61             ->UnregisterAndDestroyTaskQueue<decltype(internalAllocator_->Adapter())>(compilerTaskManagerQueue_);
62     }
63 
64 private:
65     void BackgroundCompileMethod(CompilerTask &&ctx);
66 
67     taskmanager::TaskQueueInterface *compilerTaskManagerQueue_ {nullptr};
68     os::memory::Mutex taskQueueLock_;
69     // This queue is used for methods need to be compiled inside TaskScheduler without compilation_lock_.
70     PandaDeque<CompilerTask> compilerTaskDeque_ GUARDED_BY(taskQueueLock_);
71     std::atomic<bool> compilerWorkerJoined_ {true};
72 };
73 
74 }  // namespace ark
75 
76 #endif  // RUTNIME_COMPILER_TASK_MANAGER_WORKER_H
77