OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 BASE_ATOMIC_SEQUENCE_NUM_H_ | 5 #ifndef BASE_ATOMIC_SEQUENCE_NUM_H_ |
6 #define BASE_ATOMIC_SEQUENCE_NUM_H_ | 6 #define BASE_ATOMIC_SEQUENCE_NUM_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/atomicops.h" | 9 #include "base/atomicops.h" |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 class AtomicSequenceNumber { | 14 class AtomicSequenceNumber; |
| 15 |
| 16 // Static (POD) AtomicSequenceNumber that MUST be used in global scope (or |
| 17 // non-function scope) ONLY. This implementation does not generate any static |
| 18 // initializer. Note that it does not implement any constructor which means |
| 19 // that its fields are not initialized except when it is stored in the global |
| 20 // data section (.data in ELF). If you want to allocate an atomic sequence |
| 21 // number on the stack (or heap), please use the AtomicSequenceNumber class |
| 22 // declared below. |
| 23 class StaticAtomicSequenceNumber { |
15 public: | 24 public: |
16 AtomicSequenceNumber() : seq_(0) { } | 25 inline int GetNext() { |
17 explicit AtomicSequenceNumber(base::LinkerInitialized x) { /* seq_ is 0 */ } | |
18 | |
19 int GetNext() { | |
20 return static_cast<int>( | 26 return static_cast<int>( |
21 base::subtle::NoBarrier_AtomicIncrement(&seq_, 1) - 1); | 27 base::subtle::NoBarrier_AtomicIncrement(&seq_, 1) - 1); |
22 } | 28 } |
23 | 29 |
24 private: | 30 private: |
| 31 friend class AtomicSequenceNumber; |
| 32 |
| 33 inline void Reset() { |
| 34 base::subtle::Release_Store(&seq_, 0); |
| 35 } |
| 36 |
25 base::subtle::Atomic32 seq_; | 37 base::subtle::Atomic32 seq_; |
| 38 }; |
| 39 |
| 40 // AtomicSequenceNumber that can be stored and used safely (i.e. its fields are |
| 41 // always initialized as opposed to StaticAtomicSequenceNumber declared above). |
| 42 // Please use StaticAtomicSequenceNumber if you want to declare an atomic |
| 43 // sequence number in the global scope. |
| 44 class AtomicSequenceNumber { |
| 45 public: |
| 46 AtomicSequenceNumber() { |
| 47 seq_.Reset(); |
| 48 } |
| 49 |
| 50 inline int GetNext() { |
| 51 return seq_.GetNext(); |
| 52 } |
| 53 |
| 54 private: |
| 55 StaticAtomicSequenceNumber seq_; |
26 DISALLOW_COPY_AND_ASSIGN(AtomicSequenceNumber); | 56 DISALLOW_COPY_AND_ASSIGN(AtomicSequenceNumber); |
27 }; | 57 }; |
28 | 58 |
29 } // namespace base | 59 } // namespace base |
30 | 60 |
31 #endif // BASE_ATOMIC_SEQUENCE_NUM_H_ | 61 #endif // BASE_ATOMIC_SEQUENCE_NUM_H_ |
OLD | NEW |