• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 #include "core/common/container_scope.h"
17 #include "core/common/container_consts.h"
18 #include "base/utils/utils.h"
19 
20 namespace OHOS::Ace {
21 namespace {
22 // preview not support multi-instance, always using default instance id 0.
23 #if defined(PREVIEW)
24 constexpr int32_t DEFAULT_ID = 0;
25 #else
26 constexpr int32_t DEFAULT_ID = INSTANCE_ID_UNDEFINED;
27 #endif
28 
29 std::shared_mutex mutex_;
30 std::set<int32_t> containerSet_;
31 thread_local int32_t currentId_(DEFAULT_ID);
32 std::atomic<int32_t> recentActiveId_(DEFAULT_ID);
33 std::atomic<int32_t> recentForegroundId_(DEFAULT_ID);
34 }
35 
CurrentId()36 int32_t ContainerScope::CurrentId()
37 {
38     return currentId_;
39 }
40 
DefaultId()41 int32_t ContainerScope::DefaultId()
42 {
43     if (ContainerCount() > 0) {
44         std::shared_lock<std::shared_mutex> lock(mutex_);
45         return *containerSet_.end();
46     }
47     return INSTANCE_ID_UNDEFINED;
48 }
49 
SingletonId()50 int32_t ContainerScope::SingletonId()
51 {
52     if (ContainerCount() != 1) {
53         return INSTANCE_ID_UNDEFINED;
54     }
55     std::shared_lock<std::shared_mutex> lock(mutex_);
56     return *containerSet_.begin();
57 }
58 
RecentActiveId()59 int32_t ContainerScope::RecentActiveId()
60 {
61     return recentActiveId_.load(std::memory_order_relaxed);
62 }
63 
RecentForegroundId()64 int32_t ContainerScope::RecentForegroundId()
65 {
66     return recentForegroundId_.load(std::memory_order_relaxed);
67 }
68 
CurrentIdWithReason()69 std::pair<int32_t, InstanceIdGenReason> ContainerScope::CurrentIdWithReason()
70 {
71     int32_t currentId = CurrentId();
72     if (currentId >= 0) {
73         return { currentId, InstanceIdGenReason::SCOPE };
74     }
75     uint32_t containerCount = ContainerCount();
76     if (containerCount == 0) {
77         return { INSTANCE_ID_UNDEFINED, InstanceIdGenReason::UNDEFINED };
78     }
79     if (containerCount == 1) {
80         return { SingletonId(), InstanceIdGenReason::SINGLETON };
81     }
82     currentId = ContainerScope::RecentActiveId();
83     if (currentId >= 0) {
84         return { currentId, InstanceIdGenReason::ACTIVE };
85     }
86     currentId = ContainerScope::RecentForegroundId();
87     if (currentId >= 0) {
88         return { currentId, InstanceIdGenReason::FOREGROUND };
89     }
90     return { ContainerScope::DefaultId(), InstanceIdGenReason::DEFAULT };
91 }
92 
UpdateCurrent(int32_t id)93 void ContainerScope::UpdateCurrent(int32_t id)
94 {
95     currentId_ = id;
96 }
97 
UpdateRecentActive(int32_t id)98 void ContainerScope::UpdateRecentActive(int32_t id)
99 {
100     recentActiveId_.store(id, std::memory_order_relaxed);
101 }
102 
UpdateRecentForeground(int32_t id)103 void ContainerScope::UpdateRecentForeground(int32_t id)
104 {
105     recentForegroundId_.store(id, std::memory_order_relaxed);
106 }
107 
ContainerCount()108 uint32_t ContainerScope::ContainerCount()
109 {
110     std::shared_lock<std::shared_mutex> lock(mutex_);
111     return static_cast<uint32_t>(containerSet_.size());
112 }
113 
Add(int32_t id)114 void ContainerScope::Add(int32_t id)
115 {
116     std::unique_lock<std::shared_mutex> lock(mutex_);
117     containerSet_.emplace(id);
118 }
119 
Remove(int32_t id)120 void ContainerScope::Remove(int32_t id)
121 {
122     std::unique_lock<std::shared_mutex> lock(mutex_);
123     containerSet_.erase(id);
124 }
125 
RemoveAndCheck(int32_t id)126 void ContainerScope::RemoveAndCheck(int32_t id)
127 {
128     Remove(id);
129     if (RecentActiveId() == id) {
130         UpdateRecentActive(INSTANCE_ID_UNDEFINED);
131     }
132     if (RecentForegroundId() == id) {
133         UpdateRecentForeground(INSTANCE_ID_UNDEFINED);
134     }
135 }
136 
137 } // namespace OHOS::Ace
138