blob: 3e7e95e5b693dcf79f725b5544ff0875eb2ae848 [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_
7
tzikad6d8a9b2017-07-12 09:54:218#include <atomic>
9
avi9b6f42932015-12-26 22:15:1410#include "base/macros.h"
[email protected]611dbe02008-08-05 09:57:3611
12namespace base {
13
tzikad6d8a9b2017-07-12 09:54:2114// 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.
[email protected]8a8443f2012-03-13 12:07:1917class AtomicSequenceNumber {
18 public:
tzikad6d8a9b2017-07-12 09:54:2119 constexpr AtomicSequenceNumber() {}
[email protected]8a8443f2012-03-13 12:07:1920
tzikad6d8a9b2017-07-12 09:54:2121 // 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); }
[email protected]8a8443f2012-03-13 12:07:1924
25 private:
tzikad6d8a9b2017-07-12 09:54:2126 std::atomic_int seq_{0};
27
[email protected]611dbe02008-08-05 09:57:3628 DISALLOW_COPY_AND_ASSIGN(AtomicSequenceNumber);
29};
30
tzikad6d8a9b2017-07-12 09:54:2131// TODO(tzik): Replace all usage of StaticAtomicSequenceNumber with
32// AtomicSequenceNumber, and remove this alias.
33using StaticAtomicSequenceNumber = AtomicSequenceNumber;
34
[email protected]611dbe02008-08-05 09:57:3635} // namespace base
36
37#endif // BASE_ATOMIC_SEQUENCE_NUM_H_