Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
atomic.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef IMPALA_COMMON_ATOMIC_H
16 #define IMPALA_COMMON_ATOMIC_H
17 
18 #include <algorithm>
19 
20 #include "common/compiler-util.h"
21 
22 namespace impala {
23 
24 class AtomicUtil {
25  public:
32  static inline void CpuWait() {
33  asm volatile("pause\n": : :"memory");
34  }
35 
36  static inline void MemoryBarrier() {
37  __sync_synchronize();
38  }
39 };
40 
45 template<typename T>
46 class AtomicInt {
47  public:
48  AtomicInt(T initial = 0) : value_(initial) {}
49 
50  operator T() const { return value_; }
51 
52  AtomicInt& operator=(T val) {
53  value_ = val;
54  return *this;
55  }
57  value_ = val.value_;
58  return *this;
59  }
60 
61  AtomicInt& operator+=(T delta) {
62  __sync_add_and_fetch(&value_, delta);
63  return *this;
64  }
65  AtomicInt& operator-=(T delta) {
66  __sync_add_and_fetch(&value_, -delta);
67  return *this;
68  }
69 
71  __sync_or_and_fetch(&value_, v);
72  return *this;
73  }
75  __sync_and_and_fetch(&value_, v);
76  return *this;
77  }
78 
81  __sync_add_and_fetch(&value_, 1);
82  return *this;
83  }
85  __sync_add_and_fetch(&value_, -1);
86  return *this;
87  }
88 
91  T prev = __sync_fetch_and_add(&value_, 1);
92  return AtomicInt<T>(prev);
93  }
95  T prev = __sync_fetch_and_add(&value_, -1);
96  return AtomicInt<T>(prev);
97  }
98 
100  T Read() {
101  return __sync_fetch_and_add(&value_, 0);
102  }
103 
105  T UpdateAndFetch(T delta) {
106  return __sync_add_and_fetch(&value_, delta);
107  }
108 
110  T FetchAndUpdate(T delta) {
111  return __sync_fetch_and_add(&value_, delta);
112  }
113 
115  void UpdateMax(T value) {
116  while (true) {
117  T old_value = value_;
118  T new_value = std::max(old_value, value);
119  if (LIKELY(CompareAndSwap(old_value, new_value))) break;
120  }
121  }
122  void UpdateMin(T value) {
123  while (true) {
124  T old_value = value_;
125  T new_value = std::min(old_value, value);
126  if (LIKELY(CompareAndSwap(old_value, new_value))) break;
127  }
128  }
129 
131  bool CompareAndSwap(T old_val, T new_val) {
132  return __sync_bool_compare_and_swap(&value_, old_val, new_val);
133  }
134 
137  T CompareAndSwapVal(T old_val, T new_val) {
138  return __sync_val_compare_and_swap(&value_, old_val, new_val);
139  }
140 
142  T Swap(const T& new_val) {
143  return __sync_lock_test_and_set(&value_, new_val);
144  }
145 
146  private:
148 };
149 
150 }
151 
152 #endif
T UpdateAndFetch(T delta)
Increments by delta (i.e. += delta) and returns the new val.
Definition: atomic.h:105
AtomicInt< T > operator++(int)
This is post increment, which needs to return a new object.
Definition: atomic.h:90
AtomicInt & operator+=(T delta)
Definition: atomic.h:61
AtomicInt< T > operator--(int)
Definition: atomic.h:94
void UpdateMin(T value)
Definition: atomic.h:122
AtomicInt & operator|=(T v)
Definition: atomic.h:70
T Swap(const T &new_val)
Atomically updates value_ with new_val. Returns the old value_.
Definition: atomic.h:142
static void CpuWait()
Definition: atomic.h:32
T FetchAndUpdate(T delta)
Increment by delta and returns the old val.
Definition: atomic.h:110
void UpdateMax(T value)
Updates the int to 'value' if value is larger.
Definition: atomic.h:115
T CompareAndSwapVal(T old_val, T new_val)
Definition: atomic.h:137
T Read()
Safe read of the value.
Definition: atomic.h:100
AtomicInt & operator=(T val)
Definition: atomic.h:52
bool CompareAndSwap(T old_val, T new_val)
Returns true if the atomic compare-and-swap was successful.
Definition: atomic.h:131
AtomicInt & operator&=(T v)
Definition: atomic.h:74
AtomicInt & operator-=(T delta)
Definition: atomic.h:65
static void MemoryBarrier()
Definition: atomic.h:36
AtomicInt & operator--()
Definition: atomic.h:84
#define LIKELY(expr)
Definition: compiler-util.h:32
AtomicInt & operator++()
These define the preincrement (i.e. –value) operators.
Definition: atomic.h:80
AtomicInt & operator=(const AtomicInt< T > &val)
Definition: atomic.h:56
AtomicInt(T initial=0)
Definition: atomic.h:48