OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SK_REF_CNT_EXT_DEBUG_H_ | 5 #ifndef SK_REF_CNT_EXT_DEBUG_H_ |
6 #define SK_REF_CNT_EXT_DEBUG_H_ | 6 #define SK_REF_CNT_EXT_DEBUG_H_ |
7 | 7 |
8 #ifdef SK_REF_CNT_EXT_RELEASE_H_ | 8 #ifdef SK_REF_CNT_EXT_RELEASE_H_ |
9 #error Only one SkRefCnt should be used. | 9 #error Only one SkRefCnt should be used. |
10 #endif | 10 #endif |
11 | 11 |
| 12 #include <atomic> |
| 13 |
12 // Alternate implementation of SkRefCnt for Chromium debug builds | 14 // Alternate implementation of SkRefCnt for Chromium debug builds |
13 class SK_API SkRefCnt : public SkRefCntBase { | 15 class SK_API SkRefCnt : public SkRefCntBase { |
14 public: | 16 public: |
15 SkRefCnt() : flags_(0) {} | 17 SkRefCnt(); |
16 void ref() const { SkASSERT(flags_ != AdoptionRequired_Flag); SkRefCntBase::re
f(); } | 18 ~SkRefCnt() override; |
| 19 void ref() const { SkASSERT(flags_.load() != AdoptionRequired_Flag); SkRefCntB
ase::ref(); } |
17 void adopted() const { flags_ |= Adopted_Flag; } | 20 void adopted() const { flags_ |= Adopted_Flag; } |
18 void requireAdoption() const { flags_ |= AdoptionRequired_Flag; } | 21 void requireAdoption() const { flags_ |= AdoptionRequired_Flag; } |
19 void deref() const { SkRefCntBase::unref(); } | 22 void deref() const { SkRefCntBase::unref(); } |
20 private: | 23 private: |
| 24 |
21 enum { | 25 enum { |
22 Adopted_Flag = 0x1, | 26 Adopted_Flag = 0x1, |
23 AdoptionRequired_Flag = 0x2, | 27 AdoptionRequired_Flag = 0x2, |
24 }; | 28 }; |
25 | 29 |
26 mutable int flags_; | 30 mutable std::atomic<int> flags_; |
27 }; | 31 }; |
28 | 32 |
| 33 inline SkRefCnt::SkRefCnt() : flags_(0) { } |
| 34 |
| 35 inline SkRefCnt::~SkRefCnt() { } |
| 36 |
29 // Bootstrap for Blink's WTF::RefPtr | 37 // Bootstrap for Blink's WTF::RefPtr |
30 | 38 |
31 namespace WTF { | 39 namespace WTF { |
32 inline void adopted(const SkRefCnt* object) { | 40 inline void adopted(const SkRefCnt* object) { |
33 if (!object) | 41 if (!object) |
34 return; | 42 return; |
35 object->adopted(); | 43 object->adopted(); |
36 } | 44 } |
37 inline void requireAdoption(const SkRefCnt* object) { | 45 inline void requireAdoption(const SkRefCnt* object) { |
38 if (!object) | 46 if (!object) |
39 return; | 47 return; |
40 object->requireAdoption(); | 48 object->requireAdoption(); |
41 } | 49 } |
42 }; | 50 }; |
43 | 51 |
44 using WTF::adopted; | 52 using WTF::adopted; |
45 using WTF::requireAdoption; | 53 using WTF::requireAdoption; |
46 | 54 |
47 #endif | 55 #endif |
48 | 56 |
OLD | NEW |