atomic.h 12.2 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 61 62 63 64
 *
 * 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_ATOMIC_H_
#define EMBB_BASE_ATOMIC_H_

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

#include <embb/base/internal/atomic/atomic_base.h>
#include <embb/base/internal/atomic/atomic_pointer.h>
#include <embb/base/internal/atomic/atomic_integer.h>

namespace embb {
namespace base {
#ifdef DOXYGEN

/**
 * \defgroup CPP_BASE_ATOMIC Atomic
 * %Atomic operations.
 *
 * \ingroup CPP_BASE
 */

/**
 * Class representing atomic variables.
 *
 * The current implementation guarantees sequential consistency (full fences)
 * for all atomic operations. Relaxed memory models may be added in the future.
 *
 * \tparam BaseType Underlying type
 * \ingroup CPP_BASE_ATOMIC
 */
template<typename BaseType>
class Atomic {
 public:
  /**
   * Default constructor.
   *
65
   * Constructs an atomic variable holding zero.
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
   *
   * \waitfree
   *
   * \see Atomic(BaseType)
   */
  Atomic();

  /**
   * Valued-based constructor.
   *
   * Constructs an atomic variable holding the passed value.
   *
   * \param val Initial value
   *
   * \waitfree
   *
   * \note There is intentionally no copy constructor, since two different
   *       memory locations cannot be manipulated atomically.
   *
   * \see Atomic()
   */
  explicit Atomic(BaseType val);

  /**
   * Assignment operator.
   *
   * Assigns the passed value to the object.
   *
   * \waitfree
   *
   * \param val The value to assign
   *
   * \return A shallow copy of this object
   */
  BaseType operator=(BaseType val);

  /**
   * Type conversion.
   *
   * Returns the value of the object. Equivalent to Load().
   *
   * \waitfree
   *
   * \return Stored value
   *
   * \see Load()
   */
  operator BaseType() const;

  /**
   * Predicate representing support for arithmetic operations.
   *
   * Returns \c true if type \c BaseType supports arithmetic operations,
   * otherwise \c false. Only integers and non-void pointers support
   * arithmetic operations.
   *
   * \waitfree
   *
   * \return Boolean value indicating support for arithmetic operations
   *
   * \see IsInteger(), IsPointer()
   */
  bool IsArithmetic() const;

  /**
   * Predicate representing integers.
   *
   * Returns \c true if \c BaseType is an integer type, otherwise \c false.
   *
   * \waitfree
   *
   * \return Boolean value indicating whether \c BaseType is an integer
   *
   * \see IsArithmetic(), IsPointer()
   */
  bool IsInteger() const;

  /**
   * Predicate representing pointers.
   *
   * Returns \c true if \c BaseType is a non-void pointer type, otherwise
   * \c false.
   *
   * \waitfree
   *
   * \return Boolean value indicating whether \c BaseType is a non-void pointer
   *         type
   *
   * \see IsArithmetic(), IsInteger()
   */
  bool IsPointer() const;

  /**
   * Store operation.
   *
   * Stores the passed value in the object. Equivalent to assignment operator,
   * except that \c Store does not return anything.
   *
   * \waitfree
   *
   * \param val Value to be stored
   *
   * \see Load()
   */
  void Store(BaseType val);

  /**
   * Load operation.
   *
   * Loads and returns the stored value. Equivalent to type conversion.
   *
   * \waitfree
   *
   * \return Stored value
   *
   * \see Store()
   */
  BaseType Load() const;

  /**
   * Swap operation.
   *
   * Stores the given value in the object and returns the old value.
   *
   * \waitfree
   *
   * \param val New value
   *
   * \return Old value
   *
   * \see CompareAndSwap()
   */
  BaseType Swap(BaseType val);

  /**
   * Compare-and-Swap operation (CAS).
   *
   * Stores \c desired if the current value is equal to \c expected.
   * Otherwise, stores the current value in \c expected.
   *
   * \waitfree
   *
   * \param expected Expected value
   * \param desired Desired value
   *
   * \return \c true if CAS succeeded, otherwise \c false
   *
   * \see Swap()
   */
  bool CompareAndSwap(BaseType& expected, BaseType desired);

  /** @name Arithmetic members
   *
   * The following members are only available if \c BaseType supports arithmetic
   * operations (integer and non-void pointer types).
   *
   */

  /**@{*/

  /**
   * Fetch-and-Add operation.
   *
   * Adds the passed value and returns the old value.
   *
   * \waitfree
   *
   * \param val Addend
   *
   * \return Old value
   *
   * \see FetchAndSub()
   */
  BaseType FetchAndAdd(BaseType val);

  /**
   * Fetch-and-Sub operation.
   *
   * Subtracts the passed value and returns the old value.
   *
   * \waitfree
   *
   * \param val Subtrahend
   *
   * \return Old value
   *
   * \see FetchAndAdd()
   */
  BaseType FetchAndSub(BaseType val);

  /**
   * Post-increment operation.
   *
   * Increments the value and returns the old value.
   *
   * \waitfree
   *
   * \return Old value
   *
   * \see operator++()
   */
  BaseType operator++(int);

  /**
   * Post-decrement operation.
   *
   * Decrements the value and returns the old value.
   *
   * \waitfree
   *
   * \return Old value
   *
   * \see operator--()
   */
  BaseType operator--(int);

  /**
   * Pre-increment operation.
   *
   * Increments the value and returns the new value.
   *
   * \waitfree
   *
   * \return New value
   *
   * \see operator++(int)
   */
  BaseType operator++();

  /**
   * Pre-decrement operation.
   *
   * Decrements the value and returns the new value.
   *
   * \waitfree
   *
   * \return New value
   *
   * \see operator--(int)
   */
  BaseType operator--();

  /**
   * Assignment by sum operation.
   *
   * Adds the passed value and returns the new value.
   *
   * \param val Addend
   * \return New value
   *
   * \waitfree
   *
   * \see operator-=()
   */
  BaseType operator+=(BaseType val);

  /**
   * Assignment by difference operation.
   *
   * Subtracts the passed value and returns the new value.
   *
   * \param val Subtrahend
   * \return New value
   *
   * \waitfree
   *
   * \see operator+=()
   */
  BaseType operator-=(BaseType val);

  /**@}*/

  /** @name Integer members
   *
   * The following members are only available if \c BaseType is an integer type.
   *
   */

  /**@{*/

  /**
   * Assignment by bitwise AND.
   *
   * Stores the result of the bitwise AND in the current object.
   * Does not return anything, since this cannot be implemented
   * atomically on all architectures.
   *
   * \waitfree
   *
   * \param val Second operand of bitwise AND
   *
   * \see operator|=(), operator^=()
   */
  void operator&=(BaseType val);

  /**
   * Assignment by bitwise OR.
   *
   * Stores the result of the bitwise OR in the current object.
   * Does not return anything, since this cannot be implemented
   * atomically on all architectures.
   *
   * \waitfree
   *
   * \param val Second operand of bitwise OR
   *
   * \see operator&=(), operator^=()
   */
  void operator|=(BaseType val);

  /**
   * Assignment by bitwise XOR.
   *
   * Stores the result of the bitwise XOR in the current object.
   * Does not return anything, since this cannot be implemented
   * atomically on all architectures.
   *
   * \param val Second operand of bitwise XOR
   *
   * \waitfree
   *
   * \see operator&=(), operator|=()
   */
  void operator^=(BaseType val);

  /**@}*/

  /** @name Pointer members
   *
   * The following members are only available if \c BaseType is a non-void
   * pointer type.
   *
   */

  /**@{*/

  /**
   * Structure dereference operation.
   *
   * Used to access an element of an instance of a class or a structure
   * pointed to by the stored pointer.
   *
   * \return Stored pointer
   *
   * \waitfree
   *
   * \see operator*()
   */
  BaseType* operator->();

  /**
   * Dereference operation.
   *
   * Used to access the object pointed to by the stored pointer.
   *
   * \return Reference to the object
   *
   * \waitfree
   *
   * \see operator->()
   */
  BaseType& operator*();

  /**@}*/
};

#else

/**
  * Generic implementation that provides basic functionality.
  * See \c embb::base::AtomicBase for more information.
  *
  * \tparam BaseType Underlying type
  */
template<typename BaseType>
class Atomic : public embb::base::internal::atomic::AtomicBase < BaseType > {
 public:
  /**
   * Constructs an atomic variable holding an uninitialized value.
   */
  Atomic() : embb::base::internal::atomic::AtomicBase<BaseType>() {}

  /**
   * Constructs an atomic variable holding the passed value.
   *
   * \param val The value to assign.
   */
  explicit Atomic(BaseType val) : embb::base::internal::atomic::
    AtomicBase<BaseType>(val) {}

  /**
   * Assignment operator.
   *
   * \param val The value to assign.
   *
   * \return  A shallow copy of this object.
   */
  BaseType operator=(BaseType val) {
    return embb::base::internal::atomic::AtomicBase<BaseType>::
    operator=(val);
  }
};

/**
* Specialization for non-void pointer types.
* See \c embb::base::internal::atomic::AtomicPointer for more information.
*
* \tparam BaseType Type of the objects pointed to
*/
template<typename BaseType>
class Atomic<BaseType*> : public embb::base::internal::atomic::
  AtomicPointer < BaseType, ptrdiff_t, sizeof(BaseType*) > {
 public:
  Atomic() : embb::base::internal::atomic::
    AtomicPointer<BaseType, ptrdiff_t, sizeof(BaseType*)>() {}
481
  explicit Atomic(BaseType* p) : embb::base::internal::atomic::
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
    AtomicPointer<BaseType, ptrdiff_t, sizeof(BaseType*)>(p) {}

  BaseType* operator=(BaseType* p) {
    return embb::base::internal::atomic::
      AtomicPointer<BaseType, ptrdiff_t, sizeof(BaseType*)>::operator=(p);
  }
};

/**
* Specialization for void pointer types.
* See \c embb::base::internal::atomic::AtomicBase for more information.
* Unlike the specialization for non-void pointer types, this class
* does not permit dereferencing, incrementation, etc.
*/
template<>
class Atomic<void*> : public embb::base::internal::atomic::AtomicBase < void* > {
 public:
  Atomic() : embb::base::internal::atomic::AtomicBase<void*>() {}
  explicit Atomic(void* p) : embb::base::internal::atomic::AtomicBase<void*>(p)
  {}

  void* operator=(void* p) {
    return embb::base::internal::atomic::AtomicBase<void*>::operator=(p);
  }
};

/**
* Specializations for integers.
* See \c embb::base::internal::atomic::AtomicInteger for more information.
*/
#define __EMBB_ATOMIC_INTEGER_SPECIALIZATION(T) \
template<> \
class Atomic<T>: public embb::base::internal::atomic::AtomicInteger<T> { \
 public:  \
  \
  Atomic() : embb::base::internal::atomic::AtomicInteger<T>() {} \
  explicit Atomic(T val) : embb::base::internal::atomic::AtomicInteger<T>(val) \
  {} \
  \
  T operator=(T val) { return embb::base::internal::atomic::AtomicInteger<T>::\
  operator=(val); } \
  \
}

// Specializations for integers
__EMBB_ATOMIC_INTEGER_SPECIALIZATION(signed char);
__EMBB_ATOMIC_INTEGER_SPECIALIZATION(unsigned char);
__EMBB_ATOMIC_INTEGER_SPECIALIZATION(signed short);
__EMBB_ATOMIC_INTEGER_SPECIALIZATION(unsigned short);
__EMBB_ATOMIC_INTEGER_SPECIALIZATION(signed int);
__EMBB_ATOMIC_INTEGER_SPECIALIZATION(unsigned int);

534
#ifdef EMBB_PLATFORM_ARCH_X86_64
535 536 537 538 539 540 541 542
__EMBB_ATOMIC_INTEGER_SPECIALIZATION(size_t);
#endif

#endif
}  // namespace base
}  // namespace embb

#endif  // EMBB_BASE_ATOMIC_H_