• 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_THREAD_POOL_WORKER_H
16 #define RUTNIME_COMPILER_THREAD_POOL_WORKER_H
17 
18 #include "runtime/compiler_worker.h"
19 #include "runtime/thread_pool.h"
20 #include "runtime/compiler_queue_interface.h"
21 
22 namespace ark {
23 
24 class CompilerProcessor : public ProcessorInterface<CompilerTask, Compiler *> {
25 public:
CompilerProcessor(Compiler * compiler)26     explicit CompilerProcessor(Compiler *compiler) : compiler_(compiler) {}
27     bool Process(CompilerTask &&task) override;
28 
29 private:
30     void InPlaceCompileMethod(CompilerTask &&ctx);
31     Compiler *compiler_;
32 };
33 
34 /// @brief Compiler worker task pool based on ThreadPool
35 class CompilerThreadPoolWorker : public CompilerWorker {
36 public:
37     CompilerThreadPoolWorker(mem::InternalAllocatorPtr internalAllocator, Compiler *compiler, bool &noAsyncJit,
38                              const RuntimeOptions &options);
39     NO_COPY_SEMANTIC(CompilerThreadPoolWorker);
40     NO_MOVE_SEMANTIC(CompilerThreadPoolWorker);
41     ~CompilerThreadPoolWorker() override;
42 
InitializeWorker()43     void InitializeWorker() override
44     {
45         threadPool_ = internalAllocator_->New<ThreadPool<CompilerTask, CompilerProcessor, Compiler *>>(
46             internalAllocator_, queue_, compiler_, 1, "JIT Thread");
47     }
48 
FinalizeWorker()49     void FinalizeWorker() override
50     {
51         if (threadPool_ != nullptr) {
52             JoinWorker();
53             internalAllocator_->Delete(threadPool_);
54             threadPool_ = nullptr;
55         }
56     }
57 
JoinWorker()58     void JoinWorker() override
59     {
60         if (threadPool_ != nullptr) {
61             threadPool_->Shutdown(true);
62         }
63     }
64 
IsWorkerJoined()65     bool IsWorkerJoined() override
66     {
67         return !threadPool_->IsActive();
68     }
69 
AddTask(CompilerTask && ctx)70     void AddTask(CompilerTask &&ctx) override
71     {
72         threadPool_->PutTask(std::move(ctx));
73     }
74 
GetThreadPool()75     ThreadPool<CompilerTask, CompilerProcessor, Compiler *> *GetThreadPool()
76     {
77         return threadPool_;
78     }
79 
80 private:
81     CompilerQueueInterface *CreateJITTaskQueue(const std::string &queueType, uint64_t maxLength, uint64_t taskLife,
82                                                uint64_t deathCounter, uint64_t epochDuration);
83 
84     // This queue is used only in ThreadPool. Do not use it from this class.
85     CompilerQueueInterface *queue_ {nullptr};
86     ThreadPool<CompilerTask, CompilerProcessor, Compiler *> *threadPool_ {nullptr};
87 };
88 
89 }  // namespace ark
90 
91 #endif  // RUTNIME_COMPILER_THREAD_POOL_WORKER_H
92