1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_ATOMIC_SEQUENCE_NUM_H_
6 #define BASE_ATOMIC_SEQUENCE_NUM_H_
10 #include "base/macros.h"
14 // AtomicSequenceNumber is a thread safe increasing sequence number generator.
15 // Its constructor doesn't emit a static initializer, so it's safe to use as a
16 // global variable or static member.
17 class AtomicSequenceNumber {
19 constexpr AtomicSequenceNumber() = default;
21 // Returns an increasing sequence number starts from 0 for each call.
22 // This function can be called from any thread without data race.
23 inline int GetNext() { return seq_.fetch_add(1, std::memory_order_relaxed); }
26 std::atomic_int seq_{0};
28 DISALLOW_COPY_AND_ASSIGN(AtomicSequenceNumber);
33 #endif // BASE_ATOMIC_SEQUENCE_NUM_H_