michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_ATOMIC_SEQUENCE_NUM_H_ michael@0: #define BASE_ATOMIC_SEQUENCE_NUM_H_ michael@0: michael@0: #include "base/atomicops.h" michael@0: #include "base/basictypes.h" michael@0: michael@0: namespace base { michael@0: michael@0: class AtomicSequenceNumber; michael@0: michael@0: // Static (POD) AtomicSequenceNumber that MUST be used in global scope (or michael@0: // non-function scope) ONLY. This implementation does not generate any static michael@0: // initializer. Note that it does not implement any constructor which means michael@0: // that its fields are not initialized except when it is stored in the global michael@0: // data section (.data in ELF). If you want to allocate an atomic sequence michael@0: // number on the stack (or heap), please use the AtomicSequenceNumber class michael@0: // declared below. michael@0: class StaticAtomicSequenceNumber { michael@0: public: michael@0: inline int GetNext() { michael@0: return static_cast( michael@0: base::subtle::NoBarrier_AtomicIncrement(&seq_, 1) - 1); michael@0: } michael@0: michael@0: private: michael@0: friend class AtomicSequenceNumber; michael@0: michael@0: inline void Reset() { michael@0: base::subtle::Release_Store(&seq_, 0); michael@0: } michael@0: michael@0: base::subtle::Atomic32 seq_; michael@0: }; michael@0: michael@0: // AtomicSequenceNumber that can be stored and used safely (i.e. its fields are michael@0: // always initialized as opposed to StaticAtomicSequenceNumber declared above). michael@0: // Please use StaticAtomicSequenceNumber if you want to declare an atomic michael@0: // sequence number in the global scope. michael@0: class AtomicSequenceNumber { michael@0: public: michael@0: AtomicSequenceNumber() { michael@0: seq_.Reset(); michael@0: } michael@0: michael@0: inline int GetNext() { michael@0: return seq_.GetNext(); michael@0: } michael@0: michael@0: private: michael@0: StaticAtomicSequenceNumber seq_; michael@0: DISALLOW_COPY_AND_ASSIGN(AtomicSequenceNumber); michael@0: }; michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_ATOMIC_SEQUENCE_NUM_H_