Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
atomic.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Roc authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_core/target_gcc/roc_core/atomic.h
10 //! @brief Atomic integer.
11 
12 #ifndef ROC_CORE_ATOMIC_H_
13 #define ROC_CORE_ATOMIC_H_
14 
15 #include "roc_core/noncopyable.h"
16 
17 namespace roc {
18 namespace core {
19 
20 //! Atomic integer.
21 class Atomic : public NonCopyable<> {
22 public:
23  //! Initialize with given value.
24  explicit Atomic(long value = 0)
25  : value_(value) {
26  }
27 
28  //! Atomic load.
29  operator long() const {
30  return __sync_add_and_fetch(&value_, 0);
31  }
32 
33  //! Atomic store.
34  //! @remarks
35  //! Only boolean values may be implemented in a cross-platform way
36  //! using GCC legacy __sync builtins.
37  long operator=(bool v) {
38  if (v) {
39  __sync_lock_test_and_set(&value_, 1);
40  } else {
41  __sync_and_and_fetch(&value_, 0);
42  }
43  return v;
44  }
45 
46  //! Atomic increment.
47  long operator++() {
48  return __sync_add_and_fetch(&value_, 1);
49  }
50 
51  //! Atomic decrement.
52  long operator--() {
53  return __sync_sub_and_fetch(&value_, 1);
54  }
55 
56 private:
57  mutable long value_;
58 };
59 
60 } // namespace core
61 } // namespace roc
62 
63 #endif // ROC_CORE_ATOMIC_H_
long operator=(bool v)
Atomic store.
Definition: atomic.h:37
long operator++()
Atomic increment.
Definition: atomic.h:47
Root namespace.
Atomic integer.
Definition: atomic.h:21
Base class for non-copyable objects.
Definition: noncopyable.h:23
Atomic(long value=0)
Initialize with given value.
Definition: atomic.h:24
Non-copyable object.
long operator--()
Atomic decrement.
Definition: atomic.h:52