blob: 7cfd4e8ceac15ac73f13c511d6791b2cbc5d8008 [file] [log] [blame]
[email protected]8a8443f2012-03-13 12:07:191// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]611dbe02008-08-05 09:57:364
5#ifndef BASE_ATOMIC_SEQUENCE_NUM_H_
6#define BASE_ATOMIC_SEQUENCE_NUM_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]611dbe02008-08-05 09:57:368
9#include "base/atomicops.h"
10#include "base/basictypes.h"
11
12namespace base {
13
[email protected]8a8443f2012-03-13 12:07:1914class AtomicSequenceNumber;
[email protected]611dbe02008-08-05 09:57:3615
[email protected]8a8443f2012-03-13 12:07:1916// 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.
23class StaticAtomicSequenceNumber {
24 public:
25 inline int GetNext() {
[email protected]611dbe02008-08-05 09:57:3626 return static_cast<int>(
27 base::subtle::NoBarrier_AtomicIncrement(&seq_, 1) - 1);
28 }
29
30 private:
[email protected]8a8443f2012-03-13 12:07:1931 friend class AtomicSequenceNumber;
32
33 inline void Reset() {
34 base::subtle::Release_Store(&seq_, 0);
35 }
36
[email protected]611dbe02008-08-05 09:57:3637 base::subtle::Atomic32 seq_;
[email protected]8a8443f2012-03-13 12:07:1938};
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.
44class AtomicSequenceNumber {
45 public:
46 AtomicSequenceNumber() {
47 seq_.Reset();
48 }