security/sandbox/chromium/base/atomic_sequence_num.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial