atomic_base.h 5.41 KB
Newer Older
1
/*
2
 * Copyright (c) 2014-2015, 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
 *
 * 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;

 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);

  // The members below are documented in atomic.h
  BaseType operator=(BaseType val);
  operator BaseType() const;
  bool IsLockFree() const;
  bool IsArithmetic() const;
  bool IsInteger() const;
  bool IsPointer() const;
  void Store(BaseType val);
  BaseType Load() const;
  BaseType Swap(BaseType val);
  bool CompareAndSwap(BaseType& expected, BaseType desired);
};

template<typename BaseType>
inline AtomicBase<BaseType>::AtomicBase() : AtomicValue(0) {
}

template<typename BaseType>
inline AtomicBase<BaseType>::AtomicBase(BaseType val) /*: AtomicValue(val)*/ {
  memcpy(&AtomicValue, &val, sizeof(AtomicValue));
}

template<typename BaseType>
inline BaseType AtomicBase<BaseType>::operator=(BaseType val) {
  Store(val);
  return val;
}

template<typename BaseType>
inline AtomicBase<BaseType>::operator BaseType() const {
  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));

  store_implementation< NativeType >
    ::Store(&AtomicValue, storage_value);
}

template<typename BaseType>
inline BaseType AtomicBase<BaseType>::Load() const {
  BaseType return_value;

  NativeType storage_value =
    load_implementation< NativeType >::Load(&AtomicValue);

  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));

  NativeType storage_value2 = swap_implementation< NativeType >
    ::Swap(&AtomicValue, storage_value);

  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));

  bool return_val =
    (compare_and_swap_implementation<NativeType>::
    compare_and_swap(&AtomicValue, &native_expected, native_desired)) !=0
    ? true : false;

180
  memcpy(&expected, &native_expected, sizeof(expected));
181 182 183 184 185 186 187 188 189 190

  return return_val;
}

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

#endif // EMBB_BASE_INTERNAL_ATOMIC_ATOMIC_BASE_H_