Blame view

RIOT/core/atomic_sync.c 9.69 KB
a752c7ab   elopes   add first test an...
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
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
  /*
   * Copyright (C) 2015-2016 Eistec AB
   *
   * This file is subject to the terms and conditions of the GNU Lesser General
   * Public License v2.1. See the file LICENSE in the top level directory for more
   * details.
   */
  
  /**
   * @ingroup         core_util
   * @{
   *
   * @file
   *
   * @brief Implementation of __sync atomic operations for GCC versions prior to v4.7
   *
   * @see https://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary
   * @see https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
   * @see https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Atomic-Builtins.html
   *
   * @author Joakim Nohlgård <joakim.nohlgard@eistec.se>
   */
  
  #include <stddef.h>
  #include <stdlib.h>
  #include <stdint.h>
  #include <stdbool.h>
  #include <stdatomic.h>
  #include <string.h>
  #include "irq.h"
  
  #if !defined(__llvm__) && !defined(__clang__)
  /* GCC documentation refers to the types as I1, I2, I4, I8, I16 */
  typedef uint8_t  I1;
  typedef uint16_t I2;
  typedef uint32_t I4;
  typedef uint64_t I8;
  /* typedef __uint128_t I16; */ /* No 128 bit integer support yet */
  
  /**
   * @brief This is a macro that defines a function named __sync_lock_test_and_set_<em>n</em>
   *
   * This is an exchange operation, not a compare and swap
   *
   * @param[in]  n         width of the data, in bytes
   */
  #define TEMPLATE_SYNC_LOCK_TEST_AND_SET_N(n) \
      I##n __sync_lock_test_and_set_##n (I##n *ptr, I##n desired) \
      { \
          unsigned int mask = irq_disable(); \
          I##n old = *ptr;                   \
          *ptr = desired;                    \
          irq_restore(mask);                 \
          return old;                        \
      }
  
  /**
   * @brief This is a macro that defines a function named __sync_lock_release_<em>n</em>
   *
   * This will write 0 to @p *ptr
   *
   * @param[in]  n         width of the data, in bytes
   */
  #define TEMPLATE_SYNC_LOCK_RELEASE_N(n) \
      void __sync_lock_release_##n (I##n *ptr) \
      { \
          unsigned int mask = irq_disable(); \
          *ptr = 0;                          \
          irq_restore(mask);                 \
      }
  
  /**
   * @brief This is a macro that defines a function named __sync_bool_compare_and_swap_<em>n</em>
   *
   * @param[in]  n         width of the data, in bytes
   */
  #define TEMPLATE_SYNC_BOOL_COMPARE_AND_SWAP_N(n) \
      bool __sync_bool_compare_and_swap_##n (I##n *ptr, I##n expected, I##n desired) \
      { \
          unsigned int mask = irq_disable(); \
          I##n cur = *ptr;                   \
          if (cur != expected) {             \
              irq_restore(mask);             \
              return false;                  \
          }                                  \
                                             \
          *ptr = desired;                    \
          irq_restore(mask);                 \
          return true;                       \
      }
  
  /**
   * @brief This is a macro that defines a function named __sync_val_compare_and_swap_<em>n</em>
   *
   * @param[in]  n         width of the data, in bytes
   */
  #define TEMPLATE_SYNC_VAL_COMPARE_AND_SWAP_N(n) \
      I##n __sync_val_compare_and_swap_##n (I##n *ptr, I##n expected, I##n desired) \
      { \
          unsigned int mask = irq_disable(); \
          I##n cur = *ptr;                   \
          if (cur == expected) {             \
              *ptr = desired;                \
          }                                  \
                                             \
          irq_restore(mask);                 \
          return cur;                        \
      }
  
  /**
   * @brief This is a macro that defines a function named __sync_fetch_and_<em>opname</em>_<em>n</em>
   *
   * @param[in]  opname    operator name that will be used in the function name
   * @param[in]  op        actual C language operator
   * @param[in]  n         width of the data, in bytes
   * @param[in]  prefixop  optional prefix unary operator (use ~ for inverting, NAND, NOR etc)
   */
  #define TEMPLATE_SYNC_FETCH_AND_OP_N(opname, op, n, prefixop) \
      I##n __sync_fetch_and_##opname##_##n(I##n *ptr, I##n val) \
      { \
          unsigned int mask = irq_disable();             \
          I##n tmp = *ptr;                               \
          *ptr = prefixop(tmp op val);                   \
          irq_restore(mask);                             \
          return tmp;                                    \
      }
  
  /**
   * @brief This is a macro that defines a function named __sync_<em>opname</em>_and_fetch_<em>n</em>
   *
   * @param[in]  opname    operator name that will be used in the function name
   * @param[in]  op        actual C language operator
   * @param[in]  n         width of the data, in bytes
   * @param[in]  prefixop  optional prefix unary operator (use ~ for inverting, NAND, NOR etc)
   */
  #define TEMPLATE_SYNC_OP_AND_FETCH_N(opname, op, n, prefixop) \
      I##n __sync_##opname##_and_fetch_##n(I##n *ptr, I##n val) \
      { \
          unsigned int mask = irq_disable();                  \
          I##n tmp = prefixop((*ptr) op val);                 \
          *ptr = tmp;                                         \
          irq_restore(mask);                                  \
          return tmp;                                         \
      }
  
  /* Template instantiations below */
  TEMPLATE_SYNC_LOCK_TEST_AND_SET_N(1)        /* __sync_lock_test_and_set_1 */
  TEMPLATE_SYNC_LOCK_TEST_AND_SET_N(2)        /* __sync_lock_test_and_set_2 */
  TEMPLATE_SYNC_LOCK_TEST_AND_SET_N(4)        /* __sync_lock_test_and_set_4 */
  TEMPLATE_SYNC_LOCK_TEST_AND_SET_N(8)        /* __sync_lock_test_and_set_8 */
  
  TEMPLATE_SYNC_LOCK_RELEASE_N(1)             /* __sync_lock_release_1 */
  TEMPLATE_SYNC_LOCK_RELEASE_N(2)             /* __sync_lock_release_2 */
  TEMPLATE_SYNC_LOCK_RELEASE_N(4)             /* __sync_lock_release_4 */
  TEMPLATE_SYNC_LOCK_RELEASE_N(8)             /* __sync_lock_release_8 */
  
  TEMPLATE_SYNC_BOOL_COMPARE_AND_SWAP_N(1)    /* __sync_bool_compare_and_swap_1 */
  TEMPLATE_SYNC_BOOL_COMPARE_AND_SWAP_N(2)    /* __sync_bool_compare_and_swap_2 */
  TEMPLATE_SYNC_BOOL_COMPARE_AND_SWAP_N(4)    /* __sync_bool_compare_and_swap_4 */
  TEMPLATE_SYNC_BOOL_COMPARE_AND_SWAP_N(8)    /* __sync_bool_compare_and_swap_8 */
  
  TEMPLATE_SYNC_VAL_COMPARE_AND_SWAP_N(1)     /* __sync_val_compare_and_swap_1 */
  TEMPLATE_SYNC_VAL_COMPARE_AND_SWAP_N(2)     /* __sync_val_compare_and_swap_2 */
  TEMPLATE_SYNC_VAL_COMPARE_AND_SWAP_N(4)     /* __sync_val_compare_and_swap_4 */
  TEMPLATE_SYNC_VAL_COMPARE_AND_SWAP_N(8)     /* __sync_val_compare_and_swap_8 */
  
  TEMPLATE_SYNC_FETCH_AND_OP_N( add, +, 1,  ) /* __sync_fetch_and_add_1 */
  TEMPLATE_SYNC_FETCH_AND_OP_N( add, +, 2,  ) /* __sync_fetch_and_add_2 */
  TEMPLATE_SYNC_FETCH_AND_OP_N( add, +, 4,  ) /* __sync_fetch_and_add_4 */
  TEMPLATE_SYNC_FETCH_AND_OP_N( add, +, 8,  ) /* __sync_fetch_and_add_8 */
  
  TEMPLATE_SYNC_FETCH_AND_OP_N( sub, -, 1,  ) /* __sync_fetch_and_sub_1 */
  TEMPLATE_SYNC_FETCH_AND_OP_N( sub, -, 2,  ) /* __sync_fetch_and_sub_2 */
  TEMPLATE_SYNC_FETCH_AND_OP_N( sub, -, 4,  ) /* __sync_fetch_and_sub_4 */
  TEMPLATE_SYNC_FETCH_AND_OP_N( sub, -, 8,  ) /* __sync_fetch_and_sub_8 */
  
  TEMPLATE_SYNC_FETCH_AND_OP_N( and, &, 1,  ) /* __sync_fetch_and_and_1 */
  TEMPLATE_SYNC_FETCH_AND_OP_N( and, &, 2,  ) /* __sync_fetch_and_and_2 */
  TEMPLATE_SYNC_FETCH_AND_OP_N( and, &, 4,  ) /* __sync_fetch_and_and_4 */
  TEMPLATE_SYNC_FETCH_AND_OP_N( and, &, 8,  ) /* __sync_fetch_and_and_8 */
  
  TEMPLATE_SYNC_FETCH_AND_OP_N(  or, |, 1,  ) /* __sync_fetch_and_or_1 */
  TEMPLATE_SYNC_FETCH_AND_OP_N(  or, |, 2,  ) /* __sync_fetch_and_or_2 */
  TEMPLATE_SYNC_FETCH_AND_OP_N(  or, |, 4,  ) /* __sync_fetch_and_or_4 */
  TEMPLATE_SYNC_FETCH_AND_OP_N(  or, |, 8,  ) /* __sync_fetch_and_or_8 */
  
  TEMPLATE_SYNC_FETCH_AND_OP_N( xor, ^, 1,  ) /* __sync_fetch_and_xor_1 */
  TEMPLATE_SYNC_FETCH_AND_OP_N( xor, ^, 2,  ) /* __sync_fetch_and_xor_2 */
  TEMPLATE_SYNC_FETCH_AND_OP_N( xor, ^, 4,  ) /* __sync_fetch_and_xor_4 */
  TEMPLATE_SYNC_FETCH_AND_OP_N( xor, ^, 8,  ) /* __sync_fetch_and_xor_8 */
  
  TEMPLATE_SYNC_FETCH_AND_OP_N(nand, &, 1, ~) /* __sync_fetch_and_nand_1 */
  TEMPLATE_SYNC_FETCH_AND_OP_N(nand, &, 2, ~) /* __sync_fetch_and_nand_2 */
  TEMPLATE_SYNC_FETCH_AND_OP_N(nand, &, 4, ~) /* __sync_fetch_and_nand_4 */
  TEMPLATE_SYNC_FETCH_AND_OP_N(nand, &, 8, ~) /* __sync_fetch_and_nand_8 */
  
  TEMPLATE_SYNC_OP_AND_FETCH_N( add, +, 1,  ) /* __sync_add_and_fetch_1 */
  TEMPLATE_SYNC_OP_AND_FETCH_N( add, +, 2,  ) /* __sync_add_and_fetch_2 */
  TEMPLATE_SYNC_OP_AND_FETCH_N( add, +, 4,  ) /* __sync_add_and_fetch_4 */
  TEMPLATE_SYNC_OP_AND_FETCH_N( add, +, 8,  ) /* __sync_add_and_fetch_8 */
  
  TEMPLATE_SYNC_OP_AND_FETCH_N( sub, -, 1,  ) /* __sync_sub_and_fetch_1 */
  TEMPLATE_SYNC_OP_AND_FETCH_N( sub, -, 2,  ) /* __sync_sub_and_fetch_2 */
  TEMPLATE_SYNC_OP_AND_FETCH_N( sub, -, 4,  ) /* __sync_sub_and_fetch_4 */
  TEMPLATE_SYNC_OP_AND_FETCH_N( sub, -, 8,  ) /* __sync_sub_and_fetch_8 */
  
  TEMPLATE_SYNC_OP_AND_FETCH_N( and, &, 1,  ) /* __sync_and_and_fetch_1 */
  TEMPLATE_SYNC_OP_AND_FETCH_N( and, &, 2,  ) /* __sync_and_and_fetch_2 */
  TEMPLATE_SYNC_OP_AND_FETCH_N( and, &, 4,  ) /* __sync_and_and_fetch_4 */
  TEMPLATE_SYNC_OP_AND_FETCH_N( and, &, 8,  ) /* __sync_and_and_fetch_8 */
  
  TEMPLATE_SYNC_OP_AND_FETCH_N(  or, |, 1,  ) /* __sync_or_and_fetch_1 */
  TEMPLATE_SYNC_OP_AND_FETCH_N(  or, |, 2,  ) /* __sync_or_and_fetch_2 */
  TEMPLATE_SYNC_OP_AND_FETCH_N(  or, |, 4,  ) /* __sync_or_and_fetch_4 */
  TEMPLATE_SYNC_OP_AND_FETCH_N(  or, |, 8,  ) /* __sync_or_and_fetch_8 */
  
  TEMPLATE_SYNC_OP_AND_FETCH_N( xor, ^, 1,  ) /* __sync_xor_and_fetch_1 */
  TEMPLATE_SYNC_OP_AND_FETCH_N( xor, ^, 2,  ) /* __sync_xor_and_fetch_2 */
  TEMPLATE_SYNC_OP_AND_FETCH_N( xor, ^, 4,  ) /* __sync_xor_and_fetch_4 */
  TEMPLATE_SYNC_OP_AND_FETCH_N( xor, ^, 8,  ) /* __sync_xor_and_fetch_8 */
  
  TEMPLATE_SYNC_OP_AND_FETCH_N(nand, &, 1, ~) /* __sync_nand_and_fetch_1 */
  TEMPLATE_SYNC_OP_AND_FETCH_N(nand, &, 2, ~) /* __sync_nand_and_fetch_2 */
  TEMPLATE_SYNC_OP_AND_FETCH_N(nand, &, 4, ~) /* __sync_nand_and_fetch_4 */
  TEMPLATE_SYNC_OP_AND_FETCH_N(nand, &, 8, ~) /* __sync_nand_and_fetch_8 */
  #endif
  /** @} */