atomic_base.h 6.04 KB
Newer Older
1
/*
Marcus Winter committed
2
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef EMBB_BASE_INTERNAL_ATOMIC_ATOMIC_BASE_H_
#define EMBB_BASE_INTERNAL_ATOMIC_ATOMIC_BASE_H_

#include <stdint.h>
#include <string.h>
#include <cstddef>
#include <cassert>
#include <utility>
#include <algorithm>

#include <embb/base/internal/atomic/atomic_utility.h>
#include <embb/base/internal/atomic/atomic_implementation.h>

namespace embb {
namespace base {
namespace internal {
namespace atomic {

template<typename BaseType>
class AtomicBase {
  // The copy constructor is undefined, since it cannot be implemented
  // atomically.
  AtomicBase(const AtomicBase&);
  // The assignment operator is undefined, since it cannot be implemented
  // atomically.
  AtomicBase& operator=(const AtomicBase&);

 protected:
  typedef typename embb::base::internal::atomic::
    AtomicTraits<sizeof(BaseType)>::
    NativeType NativeType;

  mutable NativeType AtomicValue;

61 62 63 64
#ifdef EMBB_THREADING_ANALYSIS_MODE
  embb_mutex_t internal_mutex;
#endif

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
 public:
  /**
   * Default constructor.
   * Initializes the object with zero.
   */
  AtomicBase();

  /**
   * Constructor.
   * Initializes the object with the passed value.
   *
   * \param val Initial value
   */
  explicit AtomicBase(BaseType val);

80 81 82 83 84
  /**
   * Destructor.
   */
  ~AtomicBase();

85 86
  // The members below are documented in atomic.h
  BaseType operator=(BaseType val);
87
  operator BaseType();
88 89 90 91 92
  bool IsLockFree() const;
  bool IsArithmetic() const;
  bool IsInteger() const;
  bool IsPointer() const;
  void Store(BaseType val);
93
  BaseType Load();
94 95 96 97 98 99
  BaseType Swap(BaseType val);
  bool CompareAndSwap(BaseType& expected, BaseType desired);
};

template<typename BaseType>
inline AtomicBase<BaseType>::AtomicBase() : AtomicValue(0) {
100
  EMBB_ATOMIC_MUTEX_INIT(internal_mutex);
101 102 103 104
}

template<typename BaseType>
inline AtomicBase<BaseType>::AtomicBase(BaseType val) /*: AtomicValue(val)*/ {
105
  EMBB_ATOMIC_MUTEX_INIT(internal_mutex);
106 107 108 109
  memcpy(&AtomicValue, &val, sizeof(AtomicValue));
}

template<typename BaseType>
110 111 112 113 114
inline AtomicBase<BaseType>::~AtomicBase() {
  EMBB_ATOMIC_MUTEX_DESTROY(internal_mutex);
}

template<typename BaseType>
115 116 117 118 119 120
inline BaseType AtomicBase<BaseType>::operator=(BaseType val) {
  Store(val);
  return val;
}

template<typename BaseType>
121
inline AtomicBase<BaseType>::operator BaseType() {
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  return Load();
}

template<typename BaseType>
inline bool AtomicBase<BaseType>::IsArithmetic() const {
  return false;
}

template<typename BaseType>
inline bool AtomicBase<BaseType>::IsInteger() const {
  return false;
}

template<typename BaseType>
inline bool AtomicBase<BaseType>::IsPointer() const {
  return false;
}

template<typename BaseType>
inline void AtomicBase<BaseType>::Store(BaseType val) {
  NativeType storage_value;
  // Justification for using memcpy instead of pointer casts
  // or union type punning:
  //  - with strict aliasing, type punning using pointer casts or
  //    unions is unsafe
  //  - memcpy is not slower, as the compiler will optimize it away
  //    anyway...
  memcpy(&storage_value, &val, sizeof(storage_value));

151
  EMBB_ATOMIC_MUTEX_LOCK(internal_mutex);
152 153
  store_implementation< NativeType >
    ::Store(&AtomicValue, storage_value);
154
  EMBB_ATOMIC_MUTEX_UNLOCK(internal_mutex);
155 156 157
}

template<typename BaseType>
158
inline BaseType AtomicBase<BaseType>::Load() {
159 160
  BaseType return_value;

161
  EMBB_ATOMIC_MUTEX_LOCK(internal_mutex);
162 163
  NativeType storage_value =
    load_implementation< NativeType >::Load(&AtomicValue);
164
  EMBB_ATOMIC_MUTEX_UNLOCK(internal_mutex);
165 166 167 168 169 170 171 172 173 174 175 176 177

  memcpy(&return_value, &storage_value, sizeof(return_value));

  return return_value;
}

template<typename BaseType>
inline BaseType AtomicBase<BaseType>::Swap(BaseType val) {
  NativeType storage_value;
  BaseType return_value;

  memcpy(&storage_value, &val, sizeof(storage_value));

178
  EMBB_ATOMIC_MUTEX_LOCK(internal_mutex);
179 180
  NativeType storage_value2 = swap_implementation< NativeType >
    ::Swap(&AtomicValue, storage_value);
181
  EMBB_ATOMIC_MUTEX_UNLOCK(internal_mutex);
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

  memcpy(&return_value, &storage_value2, sizeof(return_value));

  return return_value;
}

template<typename BaseType>
inline bool AtomicBase<BaseType>::
CompareAndSwap(BaseType& expected, BaseType desired) {
  NativeType native_expected;
  NativeType native_desired;

  memcpy(&native_expected, &expected, sizeof(expected));
  memcpy(&native_desired, &desired, sizeof(desired));

197
  EMBB_ATOMIC_MUTEX_LOCK(internal_mutex);
198 199 200 201
  bool return_val =
    (compare_and_swap_implementation<NativeType>::
    compare_and_swap(&AtomicValue, &native_expected, native_desired)) !=0
    ? true : false;
202
  EMBB_ATOMIC_MUTEX_UNLOCK(internal_mutex);
203

204
  memcpy(&expected, &native_expected, sizeof(expected));
205 206 207 208 209 210 211 212 213 214

  return return_val;
}

}  // namespace atomic
}  // namespace internal
}  // namespace base
}  // namespace embb

#endif // EMBB_BASE_INTERNAL_ATOMIC_ATOMIC_BASE_H_