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
17 #include "refbase.h"
18
19 namespace OHOS {
WeakRefCounter(RefCounter * counter,void * cookie)20 WeakRefCounter::WeakRefCounter(RefCounter *counter, void *cookie)
21 : atomicWeak_(0), refCounter_(counter), cookie_(cookie)
22 {
23 if (refCounter_ != nullptr) {
24 refCounter_->IncRefCount();
25 }
26 }
27
~WeakRefCounter()28 WeakRefCounter::~WeakRefCounter()
29 {
30 if (refCounter_ != nullptr) {
31 refCounter_->DecRefCount();
32 }
33 }
34
GetRefPtr()35 void* WeakRefCounter::GetRefPtr()
36 {
37 if ((cookie_ != nullptr) && (!refCounter_->IsRefPtrValid())) {
38 cookie_ = nullptr;
39 }
40 return cookie_;
41 }
42
IncWeakRefCount(const void * objectId)43 void WeakRefCounter::IncWeakRefCount(const void *objectId)
44 {
45 if (atomicWeak_.fetch_add(1, std::memory_order_relaxed) == 0) {
46 refCounter_->IncWeakRefCount(objectId);
47 }
48 }
49
DecWeakRefCount(const void * objectId)50 void WeakRefCounter::DecWeakRefCount(const void *objectId)
51 {
52 if (atomicWeak_.fetch_sub(1, std::memory_order_release) == 1) {
53 refCounter_->DecWeakRefCount(objectId);
54 delete this;
55 }
56 }
57
AttemptIncStrongRef(const void * objectId)58 bool WeakRefCounter::AttemptIncStrongRef(const void *objectId)
59 {
60 int unuse = 0;
61 return refCounter_->AttemptIncStrongRef(objectId, unuse);
62 }
63
RefCounter()64 RefCounter::RefCounter()
65 : atomicStrong_(INITIAL_PRIMARY_VALUE), atomicWeak_(0), atomicRefCount_(0), atomicFlags_(0), atomicAttempt_(0)
66 {
67 }
GetRefCount()68 int RefCounter::GetRefCount()
69 {
70 return atomicRefCount_.load(std::memory_order_relaxed);
71 }
72
IncRefCount()73 void RefCounter::IncRefCount()
74 {
75 atomicRefCount_.fetch_add(1, std::memory_order_relaxed);
76 }
77
DecRefCount()78 void RefCounter::DecRefCount()
79 {
80 if (atomicRefCount_.load(std::memory_order_relaxed) > 0) {
81 if (atomicRefCount_.fetch_sub(1, std::memory_order_release) == 1) {
82 delete (this);
83 }
84 }
85 }
86
SetCallback(const RefPtrCallback & callback)87 void RefCounter::SetCallback(const RefPtrCallback& callback)
88 {
89 callback_ = callback;
90 }
91
RemoveCallback()92 void RefCounter::RemoveCallback()
93 {
94 callback_ = nullptr;
95 }
96
IsRefPtrValid()97 bool RefCounter::IsRefPtrValid()
98 {
99 return callback_ != nullptr;
100 }
101
~RefCounter()102 RefCounter::~RefCounter()
103 {
104 }
105
IncStrongRefCount(const void *)106 int RefCounter::IncStrongRefCount(const void * /*objectId*/)
107 {
108 int curCount = atomicStrong_.load(std::memory_order_relaxed);
109 if (curCount >= 0) {
110 curCount = atomicStrong_.fetch_add(1, std::memory_order_relaxed);
111 if (curCount == INITIAL_PRIMARY_VALUE) {
112 atomicStrong_.fetch_sub(INITIAL_PRIMARY_VALUE, std::memory_order_release);
113 }
114 }
115 return curCount;
116 }
117
DecStrongRefCount(const void *)118 int RefCounter::DecStrongRefCount(const void * /*objectId*/)
119 {
120 int curCount = GetStrongRefCount();
121 if (curCount == INITIAL_PRIMARY_VALUE) {
122 // unexpected case: there had never a strong reference.
123 } else if (curCount > 0) {
124 // we should update the current count here.
125 // it may be changed after last operation.
126 curCount = atomicStrong_.fetch_sub(1, std::memory_order_release);
127 }
128 return curCount;
129 }
130
GetStrongRefCount()131 int RefCounter::GetStrongRefCount()
132 {
133 return atomicStrong_.load(std::memory_order_relaxed);
134 }
135
IncWeakRefCount(const void *)136 int RefCounter::IncWeakRefCount(const void * /*objectId*/)
137 {
138 return atomicWeak_.fetch_add(1, std::memory_order_relaxed);
139 }
140
DecWeakRefCount(const void *)141 int RefCounter::DecWeakRefCount(const void * /*objectId*/)
142 {
143 int curCount = GetWeakRefCount();
144 if (curCount > 0) {
145 curCount = atomicWeak_.fetch_sub(1, std::memory_order_release);
146 }
147 int strongRefCount = GetStrongRefCount();
148 if ((curCount == 1) || (strongRefCount == 0 && !IsLifeTimeExtended())) {
149 if (callback_) {
150 callback_();
151 }
152 }
153 return curCount;
154 }
155
GetWeakRefCount()156 int RefCounter::GetWeakRefCount()
157 {
158 return atomicWeak_.load(std::memory_order_relaxed);
159 }
160
SetAttemptAcquire()161 void RefCounter::SetAttemptAcquire()
162 {
163 (void)atomicAttempt_.fetch_add(1, std::memory_order_relaxed);
164 }
165
IsAttemptAcquireSet()166 bool RefCounter::IsAttemptAcquireSet()
167 {
168 return static_cast<bool>(atomicAttempt_.load(std::memory_order_relaxed) > 0);
169 }
170
ClearAttemptAcquire()171 void RefCounter::ClearAttemptAcquire()
172 {
173 atomicAttempt_.fetch_sub(1, std::memory_order_relaxed);
174 }
175
ExtendObjectLifetime()176 void RefCounter::ExtendObjectLifetime()
177 {
178 atomicFlags_.fetch_or(FLAG_EXTEND_LIFE_TIME, std::memory_order_relaxed);
179 }
180
IsLifeTimeExtended()181 bool RefCounter::IsLifeTimeExtended()
182 {
183 return static_cast<bool>(atomicFlags_.load(std::memory_order_relaxed) & FLAG_EXTEND_LIFE_TIME);
184 }
185
AttemptIncStrongRef(const void * objectId,int & outCount)186 bool RefCounter::AttemptIncStrongRef(const void *objectId, int &outCount)
187 {
188 int curCount = GetStrongRefCount();
189 IncWeakRefCount(objectId);
190
191 // if the object already had strong references.just promoting it.
192 while ((curCount > 0) && (curCount != INITIAL_PRIMARY_VALUE)) {
193 if (atomicStrong_.compare_exchange_weak(curCount, curCount + 1, std::memory_order_relaxed)) {
194 goto attempt_success;
195 }
196
197 // someone else changed the counter.re-acquire the counter value.
198 curCount = atomicStrong_.load(std::memory_order_relaxed);
199 }
200
201 if ((curCount == INITIAL_PRIMARY_VALUE) && !IsLifeTimeExtended()) {
202 // this object has a "normal" life-time,
203 while (curCount > 0) {
204 if (atomicStrong_.compare_exchange_weak(curCount, curCount + 1, std::memory_order_relaxed)) {
205 goto attempt_success;
206 }
207
208 curCount = atomicStrong_.load(std::memory_order_relaxed);
209 }
210 }
211
212 if (IsLifeTimeExtended()) {
213 curCount = atomicStrong_.fetch_add(1, std::memory_order_relaxed);
214 }
215
216 attempt_success:
217 if (curCount >= INITIAL_PRIMARY_VALUE) {
218 outCount = curCount;
219 atomicStrong_.fetch_sub(INITIAL_PRIMARY_VALUE, std::memory_order_release);
220 return true;
221 }
222
223 if (curCount < 0 || (!IsLifeTimeExtended() && curCount == 0)) {
224 // the object destroyed on strong reference count reduce to zero.
225 DecWeakRefCount(objectId);
226 return false;
227 }
228 return true;
229 }
230
RefBase()231 RefBase::RefBase() : refs_(new RefCounter())
232 {
233 refs_->IncRefCount();
234 refs_->SetCallback(std::bind(&RefBase::RefPtrCallback, this));
235 }
236
RefBase(const RefBase &)237 RefBase::RefBase(const RefBase& /*other*/)
238 {
239 refs_ = new RefCounter();
240 if (refs_ != nullptr) {
241 refs_->IncRefCount();
242 refs_->SetCallback(std::bind(&RefBase::RefPtrCallback, this));
243 }
244 }
245
RefPtrCallback()246 void RefBase::RefPtrCallback()
247 {
248 delete this;
249 }
250
251 /*
252 * The two ends of the assignment are two independent and exclusive,
253 * and the application should not share the reference counter.
254 * RISK: If there is a reference count on the left of the equal sign,
255 * it may cause a reference count exception
256 */
operator =(const RefBase &)257 RefBase &RefBase::operator=(const RefBase& /*other*/)
258 {
259 if (refs_ != nullptr) {
260 refs_->RemoveCallback();
261 refs_->DecRefCount();
262 }
263 refs_ = new RefCounter();
264 if (refs_ != nullptr) {
265 refs_->IncRefCount();
266 refs_->SetCallback(std::bind(&RefBase::RefPtrCallback, this));
267 }
268 return *this;
269 }
270
RefBase(RefBase && other)271 RefBase::RefBase(RefBase &&other) noexcept
272 {
273 refs_ = other.refs_;
274 other.refs_ = nullptr;
275 }
276
operator =(RefBase && other)277 RefBase &RefBase::operator=(RefBase &&other) noexcept
278 {
279 if (refs_ == other.refs_) {
280 return *this;
281 }
282 if (refs_ != nullptr) {
283 refs_->RemoveCallback();
284 refs_->DecRefCount();
285 }
286 refs_ = other.refs_;
287 other.refs_ = nullptr;
288 return *this;
289 }
290
~RefBase()291 RefBase::~RefBase()
292 {
293 if (refs_ != nullptr) {
294 refs_->RemoveCallback();
295 refs_->DecRefCount();
296 refs_ = nullptr;
297 }
298 }
299
ExtendObjectLifetime()300 void RefBase::ExtendObjectLifetime()
301 {
302 refs_->ExtendObjectLifetime();
303 }
304
IncStrongRef(const void * objectId)305 void RefBase::IncStrongRef(const void *objectId)
306 {
307 if (refs_ == nullptr) {
308 return;
309 }
310 const int curCount = refs_->IncStrongRefCount(objectId);
311 IncWeakRef(objectId);
312 if (curCount == INITIAL_PRIMARY_VALUE) {
313 OnFirstStrongRef(objectId);
314 }
315 if (refs_->IsAttemptAcquireSet()) {
316 refs_->ClearAttemptAcquire();
317 refs_->DecStrongRefCount(objectId);
318 }
319 }
320
DecStrongRef(const void * objectId)321 void RefBase::DecStrongRef(const void *objectId)
322 {
323 if (refs_ == nullptr) {
324 return;
325 }
326 const int curCount = refs_->DecStrongRefCount(objectId);
327 if (curCount == 1) {
328 OnLastStrongRef(objectId);
329 }
330 DecWeakRef(objectId);
331 }
332
GetSptrRefCount()333 int RefBase::GetSptrRefCount()
334 {
335 if (refs_ != nullptr) {
336 return refs_->GetStrongRefCount();
337 } else {
338 return 0;
339 }
340 }
341
CreateWeakRef(void * cookie)342 WeakRefCounter *RefBase::CreateWeakRef(void *cookie)
343 {
344 if (refs_ != nullptr) {
345 return new WeakRefCounter(refs_, cookie);
346 }
347
348 return nullptr;
349 }
350
IncWeakRef(const void * objectId)351 void RefBase::IncWeakRef(const void *objectId)
352 {
353 if (refs_ != nullptr) {
354 refs_->IncWeakRefCount(objectId);
355 }
356 }
357
DecWeakRef(const void * objectId)358 void RefBase::DecWeakRef(const void *objectId)
359 {
360 if (refs_ != nullptr) {
361 refs_->DecWeakRefCount(objectId);
362 }
363 }
364
GetWptrRefCount()365 int RefBase::GetWptrRefCount()
366 {
367 if (refs_ != nullptr) {
368 return refs_->GetWeakRefCount();
369 } else {
370 return 0;
371 }
372 }
373
AttemptAcquire(const void * objectId)374 bool RefBase::AttemptAcquire(const void *objectId)
375 {
376 if (refs_ != nullptr) {
377 int count = 0;
378 if (refs_->AttemptIncStrongRef(objectId, count)) {
379 if (count == INITIAL_PRIMARY_VALUE) {
380 OnFirstStrongRef(objectId);
381 }
382 refs_->SetAttemptAcquire();
383 return true;
384 }
385 }
386 return false;
387 }
388
AttemptIncStrongRef(const void * objectId)389 bool RefBase::AttemptIncStrongRef(const void *objectId)
390 {
391 if ((refs_ != nullptr) && (OnAttemptPromoted(objectId))) {
392 int count = 0;
393 bool ret = refs_->AttemptIncStrongRef(objectId, count);
394 if (count == INITIAL_PRIMARY_VALUE) {
395 OnFirstStrongRef(objectId);
396 }
397 return ret;
398 }
399 return false;
400 }
401
IsAttemptAcquireSet()402 bool RefBase::IsAttemptAcquireSet()
403 {
404 if (refs_ != nullptr) {
405 return refs_->IsAttemptAcquireSet();
406 }
407 return false;
408 }
409
IsExtendLifeTimeSet()410 bool RefBase::IsExtendLifeTimeSet()
411 {
412 if (refs_ != nullptr) {
413 return refs_->IsLifeTimeExtended();
414 }
415 return false;
416 }
417
OnFirstStrongRef(const void *)418 void RefBase::OnFirstStrongRef(const void *)
419 {}
420
OnLastStrongRef(const void *)421 void RefBase::OnLastStrongRef(const void *)
422 {}
423
OnLastWeakRef(const void *)424 void RefBase::OnLastWeakRef(const void *)
425 {}
426
OnAttemptPromoted(const void *)427 bool RefBase::OnAttemptPromoted(const void *)
428 {
429 return true;
430 }
431 } // namespace OHOS
432