• 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_PALTFORM_PLATFORM_H
17 #define ECMASCRIPT_PALTFORM_PLATFORM_H
18 
19 #include <memory>
20 
21 #include "ecmascript/platform/runner.h"
22 #include "os/mutex.h"
23 
24 namespace panda::ecmascript {
25 class Platform {
26 public:
GetCurrentPlatform()27     static Platform *GetCurrentPlatform()
28     {
29         static Platform platform;
30         return &platform;
31     }
32 
33     Platform() = default;
~Platform()34     ~Platform()
35     {
36         os::memory::LockHolder lock(mutex_);
37         runner_->TerminateThread();
38         isInitialized_ = 0;
39     }
40 
41     NO_COPY_SEMANTIC(Platform);
42     NO_MOVE_SEMANTIC(Platform);
43 
44     void Initialize(int threadNum = DEFAULT_PLATFORM_THREAD_NUM);
45     void Destroy();
46 
PostTask(std::unique_ptr<Task> task)47     void PostTask(std::unique_ptr<Task> task) const
48     {
49         ASSERT(isInitialized_ > 0);
50         runner_->PostTask(std::move(task));
51     }
52 
GetTotalThreadNum()53     uint32_t GetTotalThreadNum() const
54     {
55         return runner_->GetTotalThreadNum();
56     }
57 
IsInThreadPool(std::thread::id id)58     bool IsInThreadPool(std::thread::id id) const
59     {
60         return runner_->IsInThreadPool(id);
61     }
62 
63 private:
64     uint32_t TheMostSuitableThreadNum(uint32_t threadNum) const;
65 
66     std::unique_ptr<Runner> runner_;
67     int isInitialized_ = 0;
68     os::memory::Mutex mutex_;
69 };
70 }  // namespace panda::ecmascript
71 #endif  // ECMASCRIPT_PALTFORM_PLATFORM_H
72