Tervel  1.0.0
A collection of wait-free containers and algorithms.
padded_atomic.h
Go to the documentation of this file.
1 /*
2 The MIT License (MIT)
3 
4 Copyright (c) 2015 University of Central Florida's Computer Software Engineering
5 Scalable & Secure Systems (CSE - S3) Lab
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 */
25 #ifndef TERVEL_UTIL_PADDEDATOMIC_H
26 #define TERVEL_UTIL_PADDEDATOMIC_H
27 
28 #include <tervel/util/system.h>
29 #include <atomic>
30 
31 namespace tervel {
32 namespace util {
33 
34 template<class T>
35 class PaddedAtomic {
36  public:
37  explicit PaddedAtomic() {}
38  explicit PaddedAtomic(T value) : atomic(value) {}
39 
40  T load(std::memory_order memory_order = std::memory_order_seq_cst) {
41  return atomic.load(memory_order);
42  }
43  void store(T value, std::memory_order memory_order
44  = std::memory_order_seq_cst) {
45  atomic.store(value, memory_order);
46  }
47 
48  T exchange(T value, std::memory_order memory_order
49  = std::memory_order_seq_cst) {
50  return atomic.exchange(value, memory_order);
51  }
52 
53  bool compare_exchange_weak(T& expected, T desired,
54  std::memory_order success, std::memory_order failure ) {
55  return atomic.compare_exchange_weak(expected, desired, success, failure);
56  }
57 
58  bool compare_exchange_weak(T& expected, T desired,
59  std::memory_order order = std::memory_order_seq_cst ) {
60  return atomic.compare_exchange_weak(expected, desired, order);
61  }
62 
63  bool compare_exchange_strong(T& expected, T desired,
64  std::memory_order success, std::memory_order failure ) {
65  return atomic.compare_exchange_strong(expected, desired, success, failure);
66  }
67 
68  bool compare_exchange_strong(T& expected, T desired, std::memory_order order =
69  std::memory_order_seq_cst ) {
70  return atomic.compare_exchange_strong(expected, desired, order);
71  }
72 
73  T fetch_add(T arg, std::memory_order memory_order =
74  std::memory_order_seq_cst ) {
75  return atomic.fetch_add(arg, memory_order);
76  }
77 
78  std::atomic<T> atomic;
79 
80  private:
81  char padding[CACHE_LINE_SIZE-sizeof(std::atomic<T>)];
82 };
83 
84 
85 } // namespace util
86 } // namespace tervel
87 
88 #endif // TERVEL_UTIL_PADDEDATOMIC_H
T load(std::memory_order memory_order=std::memory_order_seq_cst)
Definition: padded_atomic.h:40
void store(T value, std::memory_order memory_order=std::memory_order_seq_cst)
Definition: padded_atomic.h:43
bool compare_exchange_strong(T &expected, T desired, std::memory_order order=std::memory_order_seq_cst)
Definition: padded_atomic.h:68
TODO(steven):
Definition: mcas.h:36
bool compare_exchange_weak(T &expected, T desired, std::memory_order success, std::memory_order failure)
Definition: padded_atomic.h:53
T exchange(T value, std::memory_order memory_order=std::memory_order_seq_cst)
Definition: padded_atomic.h:48
Definition: padded_atomic.h:35
char padding[CACHE_LINE_SIZE-sizeof(std::atomic< T >)]
Definition: padded_atomic.h:81
PaddedAtomic(T value)
Definition: padded_atomic.h:38
#define CACHE_LINE_SIZE
Definition: system.h:41
T fetch_add(T arg, std::memory_order memory_order=std::memory_order_seq_cst)
Definition: padded_atomic.h:73
PaddedAtomic()
Definition: padded_atomic.h:37
bool compare_exchange_weak(T &expected, T desired, std::memory_order order=std::memory_order_seq_cst)
Definition: padded_atomic.h:58
bool compare_exchange_strong(T &expected, T desired, std::memory_order success, std::memory_order failure)
Definition: padded_atomic.h:63
std::atomic< T > atomic
Definition: padded_atomic.h:78