• 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 #ifndef ECMASCRIPT_TASKPOOL_TASKPOOL_H
17 #define ECMASCRIPT_TASKPOOL_TASKPOOL_H
18 
19 #include <memory>
20 
21 #include "ecmascript/common.h"
22 #include "ecmascript/taskpool/runner.h"
23 #include "ecmascript/platform/mutex.h"
24 #include "ecmascript/daemon/daemon_thread.h"
25 
26 namespace panda::ecmascript {
27 class PUBLIC_API Taskpool {
28 public:
29     PUBLIC_API static Taskpool *GetCurrentTaskpool();
30 
31     Taskpool() = default;
~Taskpool()32     PUBLIC_API ~Taskpool()
33     {
34         LockHolder lock(mutex_);
35         runner_->TerminateThread();
36         isInitialized_ = 0;
37     }
38 
39     NO_COPY_SEMANTIC(Taskpool);
40     NO_MOVE_SEMANTIC(Taskpool);
41 
42     void Initialize(int threadNum = DEFAULT_TASKPOOL_THREAD_NUM,
43         std::function<void(os::thread::native_handle_type)> prologueHook = nullptr,
44         const std::function<void(os::thread::native_handle_type)> epilogueHook = nullptr);
45     void Destroy(int32_t id);
46 
PostTask(std::unique_ptr<Task> task)47     void PostTask(std::unique_ptr<Task> task) const
48     {
49         if (isInitialized_ > 0) {
50             runner_->PostTask(std::move(task));
51         }
52     }
53 
PostDelayedTask(std::unique_ptr<Task> task,uint64_t delayMilliseconds)54     void PostDelayedTask(std::unique_ptr<Task> task, uint64_t delayMilliseconds) const
55     {
56         if (isInitialized_ > 0) {
57             runner_->PostDelayedTask(std::move(task), delayMilliseconds);
58         }
59     }
60 
61     // Terminate a task of a specified type
62     void TerminateTask(int32_t id, TaskType type = TaskType::ALL);
63 
GetTotalThreadNum()64     uint32_t GetTotalThreadNum() const
65     {
66         return runner_->GetTotalThreadNum();
67     }
68 
IsInThreadPool(std::thread::id id)69     bool IsInThreadPool(std::thread::id id) const
70     {
71         return runner_->IsInThreadPool(id);
72     }
73 
IsDaemonThreadOrInThreadPool(std::thread::id id)74     bool IsDaemonThreadOrInThreadPool(std::thread::id id) const
75     {
76         DaemonThread *dThread = DaemonThread::GetInstance();
77         return IsInThreadPool(id) || (dThread != nullptr
78             && dThread->GetThreadId() == JSThread::GetCurrentThreadId());
79     }
80 
SetThreadPriority(PriorityMode mode)81     void SetThreadPriority(PriorityMode mode)
82     {
83         runner_->SetQosPriority(mode);
84     }
85 
86     void ForEachTask(const std::function<void(Task*)> &f);
87 
88 private:
89     virtual uint32_t TheMostSuitableThreadNum(uint32_t threadNum) const;
90 
91     std::unique_ptr<Runner> runner_;
92     volatile int isInitialized_ = 0;
93     Mutex mutex_;
94 };
95 }  // namespace panda::ecmascript
96 #endif  // ECMASCRIPT_PALTFORM_PLATFORM_H
97