Embedded Multicore Building Blocks V1.0.0
atomic.h
1 /*
2  * Copyright (c) 2014-2017, Siemens AG. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef EMBB_BASE_C_ATOMIC_H_
28 #define EMBB_BASE_C_ATOMIC_H_
29 
86 #ifdef DOXYGEN
87 
98  emb_atomic_TYPE* variable,
100  TYPE initial_value
102  );
103 
114  emb_atomic_TYPE* variable
116  );
117 
131  embb_atomic_TYPE* variable,
134  TYPE value
136  );
137 
158  embb_atomic_TYPE* variable,
160  TYPE* expected,
162  TYPE desired
164  );
165 
180  embb_atomic_TYPE* variable,
182  TYPE value
184  );
185 
200  const embb_atomic_TYPE* variable
202  );
203 
211 
225  embb_atomic_TYPE* variable,
228  TYPE value
230  );
231 
244  embb_atomic_TYPE* variable,
246  int value
248  );
249 
264  embb_atomic_TYPE* variable,
266  TYPE value
268  );
269 
283  embb_atomic_TYPE* variable,
286  TYPE value
288  );
289 #endif
290 
291 #include <embb/base/c/internal/config.h>
292 #include <embb/base/c/internal/platform.h>
293 
294 #ifdef __cplusplus
295 extern "C" {
296 #endif
297 
298 #ifdef EMBB_THREADING_ANALYSIS_MODE
299 
300 int embb_mutex_init(
301  embb_mutex_t* mutex,
302  int type
303  );
304 
305 int embb_mutex_lock(
306  embb_mutex_t* mutex
307  );
308 
310  embb_mutex_t* mutex
311  );
312 
313 void embb_mutex_destroy(
314  embb_mutex_t* mutex
315  );
316 
317 #define EMBB_ATOMIC_MUTEX_INIT(mutex) embb_mutex_init(&(mutex), 0)
318 #define EMBB_ATOMIC_MUTEX_LOCK(mutex) embb_mutex_lock(&(mutex))
319 #define EMBB_ATOMIC_MUTEX_UNLOCK(mutex) embb_mutex_unlock(&(mutex))
320 #define EMBB_ATOMIC_MUTEX_DESTROY(mutex) embb_mutex_destroy(&(mutex))
321 
322 #else
323 
324 #define EMBB_ATOMIC_MUTEX_INIT(...)
325 #define EMBB_ATOMIC_MUTEX_LOCK(...)
326 #define EMBB_ATOMIC_MUTEX_UNLOCK(...)
327 #define EMBB_ATOMIC_MUTEX_DESTROY(...)
328 
329 #endif
330 
331 #ifdef EMBB_DEBUG
332 
333 #include <assert.h>
334 #include <stdint.h>
335 
336 static int embb_atomic_test_marker(uint32_t marker) {
337  if (marker == 0x12345678) {
338  return 1;
339  } else {
340  return 0;
341  }
342 }
343 
344 #define EMBB_ATOMIC_INIT_CHECK(variable) assert(embb_atomic_test_marker(variable->marker))
345 #define EMBB_ATOMIC_INIT_MARKER(variable) variable->marker = 0x12345678
346 #define EMBB_ATOMIC_DESTROY_MARKER(variable) variable->marker = 0
347 
348 #else
349 
350 #define EMBB_ATOMIC_INIT_CHECK(variable) (void)(variable)
351 #define EMBB_ATOMIC_INIT_MARKER(variable) (void)(variable)
352 #define EMBB_ATOMIC_DESTROY_MARKER(variable) (void)(variable)
353 
354 #endif
355 
356 #ifdef __cplusplus
357 }
358 #endif
359 
360 #include <embb/base/c/internal/atomic/atomic.h>
361 
362 #endif //EMBB_BASE_C_ATOMIC_H_
int embb_atomic_compare_and_swap_TYPE(embb_atomic_TYPE *variable, TYPE *expected, TYPE desired)
Compares variable with expected and, if equivalent, swaps its value with desired. ...
void embb_atomic_destroy_TYPE(emb_atomic_TYPE *variable)
Destroys an atomic variable and frees its resources.
void embb_atomic_or_assign_TYPE(embb_atomic_TYPE *variable, TYPE value)
Computes the logical "or" of the value stored in variable and value.
TYPE embb_atomic_fetch_and_add_TYPE(embb_atomic_TYPE *variable, TYPE value)
Adds value to variable and returns its old value.
void embb_atomic_init_TYPE(emb_atomic_TYPE *variable, TYPE initial_value)
Initializes an atomic variable.
int embb_mutex_unlock(embb_mutex_t *mutex)
Unlocks a locked mutex.
TYPE embb_atomic_swap_TYPE(embb_atomic_TYPE *variable, TYPE value)
Swaps the current value of variable with value.
int embb_mutex_init(embb_mutex_t *mutex, int type)
Initializes a mutex.
void embb_atomic_memory_barrier()
Enforces a memory barrier (full fence).
void embb_atomic_store_TYPE(embb_atomic_TYPE *variable, int value)
Stores value in variable.
int embb_mutex_lock(embb_mutex_t *mutex)
Waits until the mutex can be locked and locks it.
void embb_mutex_destroy(embb_mutex_t *mutex)
Destroys a mutex and frees its resources.
opaque_type embb_mutex_t
Opaque type representing a mutex.
Definition: mutex.h:55
void embb_atomic_xor_assign_TYPE(embb_atomic_TYPE *variable, TYPE value)
Computes the logical "xor" of the value stored in variable and value.
TYPE embb_atomic_load_TYPE(const embb_atomic_TYPE *variable)
Loads the value of variable and returns it.
void embb_atomic_and_assign_TYPE(embb_atomic_TYPE *variable, TYPE value)
Computes the logical "and" of the value stored in variable and value.