and_assign.h 7.11 KB
Newer Older
1 2 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
/*
 * Copyright (c) 2014, Siemens AG. All rights reserved.
 *
 * 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_C_INTERNAL_ATOMIC_AND_ASSIGN_H_
#define EMBB_BASE_C_INTERNAL_ATOMIC_AND_ASSIGN_H_

#ifndef DOXYGEN

#include <embb/base/c/internal/config.h>
#include <embb/base/c/internal/atomic/atomic_sizes.h>
#include <embb/base/c/internal/macro_helper.h>
#include <embb/base/c/internal/atomic/atomic_variables.h>
#include <string.h>

/*
 * The EMBB_DEFINE_[ATOMIC METHOD] macro defines, given the size in bytes,
 * the assembler prefix for that size (e.g., "b" for 1 byte), the assembler
 * implementation for [ATOMIC METHOD]. For GCC, inline assembler is used.
 * For MSVC, an external assembler implementation is called, as Microsoft
 * doesn't allow inline assembler with 64-bit code. In that case, the
 * assembly code is linked from an external object file.
 *
 * The generated function has the following signature (in the and_assign.h file,
 * analogous for other atomic methods):
 *
 *     static inline void embb_internal__atomic_and_assign_[BYTE_SIZE]
 *     (EMBB_BASE_BASIC_TYPE_SIZE_[BYTE_SIZE] volatile* pointer_to_value,
 *     EMBB_BASE_BASIC_TYPE_SIZE_[BYTE_SIZE] value)
 *
 * BYTE_SIZE is the number of bytes passed to the macro.
 *
 */
#ifdef EMBB_ARCH_X86

#ifdef EMBB_COMPILER_MSVC
#define EMBB_DEFINE_AND_ASSIGN(EMBB_PARAMETER_SIZE_BYTE, EMBB_ATOMIC_X86_SIZE_SUFFIX) \
  extern void __fastcall EMBB_CAT2(embb_internal__atomic_and_assign_, EMBB_PARAMETER_SIZE_BYTE)_asm( \
  EMBB_CAT2(EMBB_BASE_BASIC_TYPE_SIZE_, EMBB_PARAMETER_SIZE_BYTE) volatile* pointer_to_value, EMBB_CAT2(EMBB_BASE_BASIC_TYPE_SIZE_, EMBB_PARAMETER_SIZE_BYTE) value); \
  EMBB_INLINE void __fastcall EMBB_CAT2(embb_internal__atomic_and_assign_, EMBB_PARAMETER_SIZE_BYTE)(EMBB_CAT2(EMBB_BASE_BASIC_TYPE_SIZE_, EMBB_PARAMETER_SIZE_BYTE) volatile* pointer_to_value, \
  EMBB_CAT2(EMBB_BASE_BASIC_TYPE_SIZE_, EMBB_PARAMETER_SIZE_BYTE) value) { \
  _ReadWriteBarrier(); \
  EMBB_CAT2(embb_internal__atomic_and_assign_, EMBB_PARAMETER_SIZE_BYTE)_asm(pointer_to_value, value); \
  _ReadWriteBarrier(); \
  }
#elif defined(EMBB_COMPILER_GNUC)
#define EMBB_DEFINE_AND_ASSIGN(EMBB_PARAMETER_SIZE_BYTE, EMBB_ATOMIC_X86_SIZE_SUFFIX)\
  EMBB_INLINE void EMBB_CAT2(embb_internal__atomic_and_assign_, \
  EMBB_PARAMETER_SIZE_BYTE)(EMBB_CAT2(EMBB_BASE_BASIC_TYPE_SIZE_, EMBB_PARAMETER_SIZE_BYTE) volatile* pointer_to_value, EMBB_CAT2(EMBB_BASE_BASIC_TYPE_SIZE_, EMBB_PARAMETER_SIZE_BYTE) value) { \
  __asm__ __volatile__("lock and" EMBB_ATOMIC_X86_SIZE_SUFFIX " %1, %0" \
  : "+m" (*pointer_to_value), "+q" (value) \
  : \
  : "memory"); \
  }
#else
#error "No atomic fetch and store implementation found"
#endif

/*
 * The three or four macro calls below generate the methods for 1, 2, 4, and
 * bytes, as stated in the macro definition.
 */
EMBB_DEFINE_AND_ASSIGN(1, "b")
EMBB_DEFINE_AND_ASSIGN(2, "w")
EMBB_DEFINE_AND_ASSIGN(4, "l")
#ifdef EMBB_64_BIT_ATOMIC_AVAILABLE
EMBB_DEFINE_AND_ASSIGN(8, "q")
#endif

#elif defined(EMBB_ARCH_ARM)

#if defined(EMBB_COMPILER_GNUC)
#define EMBB_DEFINE_AND_ASSIGN(EMBB_PARAMETER_SIZE_BYTE, \
  EMBB_ATOMIC_ARM_SIZE_SUFFIX) \
  EMBB_INLINE \
  void EMBB_CAT2(embb_internal__atomic_and_assign_, \
  EMBB_PARAMETER_SIZE_BYTE)(\
  EMBB_CAT2(EMBB_BASE_BASIC_TYPE_SIZE_, EMBB_PARAMETER_SIZE_BYTE) \
  volatile* pointer_to_value, \
  EMBB_CAT2(EMBB_BASE_BASIC_TYPE_SIZE_, EMBB_PARAMETER_SIZE_BYTE) \
  value) { \
  register \
  EMBB_CAT2(EMBB_BASE_BASIC_TYPE_SIZE_, EMBB_PARAMETER_SIZE_BYTE) \
  t1, t2, t3; \
  __asm__ __volatile__ ( \
  "dmb\n\t" \
  "loop_%=:\n\t" \
  "ldrex" EMBB_ATOMIC_ARM_SIZE_SUFFIX " %2, %0\n\t" \
  "and %3, %2, %1\n\t" \
  "strex" EMBB_ATOMIC_ARM_SIZE_SUFFIX " %4, %3, %0\n\t" \
  "teq %4, #0\n\t" \
  "bne loop_%=\n\t" \
  "isb" \
  : "+m" (*pointer_to_value), "+r" (value), "=r" (t1), "=r" (t2), "=r" (t3) \
  :                   \
  : "memory", "cc" );               \
  }
#else
#error "No atomic fetch and store implementation found"
#endif

EMBB_DEFINE_AND_ASSIGN(1, "b")
EMBB_DEFINE_AND_ASSIGN(2, "h")
EMBB_DEFINE_AND_ASSIGN(4, "")

#else
#error "Unknown architecture"
#endif

/*
 * Now, using the basic functions above, we generate the respective functions
 * for all basic data types, like "unsigned short". For that purpose, the
 * following generator macro is used. This macro is called by the macros in the
 * generator header, defining the implementation for the basic data types.
 *
 * For unsigned short and for and_assign.h, the following method would be
 * generated (analogous for other atomic methods):
 *
 *      static inline void embb_atomic_and_assign_unsigned_short (
 *      embb_atomic_unsigned_short* variable, unsigned short value) {
 *      embb_internal__atomic_and_assign_2((EMBB_BASE_BASIC_TYPE_SIZE_2 volatile *) 
 *      (&(variable->internal_variable)), *((EMBB_BASE_BASIC_TYPE_SIZE_2*)
 *      (&value))); }
 *
 * This generated function is supposed to be called by the user of the lib.
 */
#define EMBB_ATOMIC_INTERNAL_DEFINE_AND_ASSIGN_METHOD(EMBB_ATOMIC_PARAMETER_TYPE_NATIVE, EMBB_ATOMIC_PARAMETER_ATOMIC_TYPE_SUFFIX, EMBB_ATOMIC_PARAMETER_TYPE_SIZE) \
  EMBB_INLINE void EMBB_CAT2(embb_atomic_and_assign_, EMBB_ATOMIC_PARAMETER_ATOMIC_TYPE_SUFFIX)(\
  EMBB_CAT2(embb_atomic_, EMBB_ATOMIC_PARAMETER_ATOMIC_TYPE_SUFFIX)* variable, EMBB_ATOMIC_PARAMETER_TYPE_NATIVE value) { \
  EMBB_CAT2(EMBB_BASE_BASIC_TYPE_SIZE_, EMBB_ATOMIC_PARAMETER_TYPE_SIZE) value_pun;\
  memcpy(&value_pun, &value, sizeof(EMBB_ATOMIC_PARAMETER_TYPE_NATIVE));\
  EMBB_CAT2(embb_internal__atomic_and_assign_, EMBB_ATOMIC_PARAMETER_TYPE_SIZE)((EMBB_CAT2(EMBB_BASE_BASIC_TYPE_SIZE_, EMBB_ATOMIC_PARAMETER_TYPE_SIZE) volatile *) \
  (&(variable->internal_variable)), value_pun); \
  }

#undef EMBB_ATOMIC_METHOD_TO_GENERATE
#define EMBB_ATOMIC_METHOD_TO_GENERATE AND_ASSIGN_METHOD
#include <embb/base/c/internal/atomic/generate_atomic_implementation_template.h>
#undef EMBB_ATOMIC_METHOD_TO_GENERATE

#endif //DOXYGEN

#endif //EMBB_BASE_C_INTERNAL_ATOMIC_AND_ASSIGN_H_