| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
// This file gets included into refcount.h as a means of extending the behavior of |
| 5 |
// atomic increment, decrement, and test. |
| 6 |
#ifndef REFCOUNT_OS_H__LINUX |
| 7 |
#define REFCOUNT_OS_H__LINUX |
| 8 |
|
| 9 |
|
| 10 |
// This Linux-specific header offers 3 strategies: |
| 11 |
// REFCOUNT_ATOMIC_DONTCARE -- no atomicity guarantee |
| 12 |
// REFCOUNT_USE_STD_ATOMIC -- C11 atomicity |
| 13 |
// REFCOUNT_USE_GNU_C_ATOMIC -- GNU-specific atomicity |
| 14 |
|
| 15 |
#if defined(__GNUC__) |
| 16 |
#define REFCOUNT_USE_GNU_C_ATOMIC 1 |
| 17 |
#endif |
| 18 |
|
| 19 |
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ == 201112) |
| 20 |
#define REFCOUNT_USE_STD_ATOMIC 1 |
| 21 |
#undef REFCOUNT_USE_GNU_C_ATOMIC |
| 22 |
#endif |
| 23 |
|
| 24 |
// This FREERTOS_ARCH_ESP8266 behavior is deprecated. Microcontrollers |
| 25 |
// using the Azure IoT C SDK should include the c-utility file |
| 26 |
// pal/generic/refcount_os.h instead of this one. |
| 27 |
#if defined(FREERTOS_ARCH_ESP8266) |
| 28 |
#define REFCOUNT_ATOMIC_DONTCARE 1 |
| 29 |
#undef REFCOUNT_USE_STD_ATOMIC |
| 30 |
#undef REFCOUNT_USE_GNU_C_ATOMIC |
| 31 |
#endif |
| 32 |
|
| 33 |
#if defined(REFCOUNT_ATOMIC_DONTCARE) |
| 34 |
#define COUNT_TYPE uint32_t |
| 35 |
#elif defined(REFCOUNT_USE_STD_ATOMIC) |
| 36 |
#define COUNT_TYPE _Atomic uint32_t |
| 37 |
#else // REFCOUNT_USE_GNU_C_ATOMIC |
| 38 |
#define COUNT_TYPE uint32_t |
| 39 |
#endif // defined(REFCOUNT_ATOMIC_DONTCARE) |
| 40 |
|
| 41 |
|
| 42 |
/*the following macros increment/decrement a ref count in an atomic way, depending on the platform*/ |
| 43 |
/*The following mechanisms are considered in this order |
| 44 |
REFCOUNT_ATOMIC_DONTCARE does not use atomic operations |
| 45 |
- will result in ++/-- used for increment/decrement. |
| 46 |
C11 |
| 47 |
- will result in #include <stdatomic.h> |
| 48 |
- will use atomic_fetch_add/sub; |
| 49 |
- about the return value: "Atomically, the value pointed to by object immediately before the effects" |
| 50 |
gcc |
| 51 |
- will result in no include (for gcc these are intrinsics build in) |
| 52 |
- will use __sync_fetch_and_add/sub |
| 53 |
- about the return value: "... returns the value that had previously been in memory." (https://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Atomic-Builtins.html#Atomic-Builtins) |
| 54 |
*/ |
| 55 |
|
| 56 |
|
| 57 |
/*if macro DEC_REF returns DEC_RETURN_ZERO that means the ref count has reached zero.*/ |
| 58 |
#if defined(REFCOUNT_ATOMIC_DONTCARE) |
| 59 |
#define DEC_RETURN_ZERO (0) |
| 60 |
#define INC_REF_VAR(count) ++(count) |
| 61 |
#define DEC_REF_VAR(count) --(count) |
| 62 |
#define INIT_REF_VAR(count) do { count = 1; } while((void)0,0) |
| 63 |
|
| 64 |
#elif defined(REFCOUNT_USE_STD_ATOMIC) |
| 65 |
#include <stdatomic.h> |
| 66 |
#define DEC_RETURN_ZERO (1) |
| 67 |
#define INC_REF_VAR(count) atomic_fetch_add(&(count), 1) |
| 68 |
#define DEC_REF_VAR(count) atomic_fetch_sub(&(count), 1) |
| 69 |
#define INIT_REF_VAR(count) atomic_store(&(count), 1) |
| 70 |
|
| 71 |
#elif defined(REFCOUNT_USE_GNU_C_ATOMIC) |
| 72 |
#define DEC_RETURN_ZERO (0) |
| 73 |
#define INC_REF_VAR(count) __sync_add_and_fetch(&(count), 1) |
| 74 |
#define DEC_REF_VAR(count) __sync_sub_and_fetch(&(count), 1) |
| 75 |
#define INIT_REF_VAR(count) do { count = 1; __sync_synchronize(); } while((void)0,0) |
| 76 |
|
| 77 |
#endif /*defined(REFCOUNT_USE_GNU_C_ATOMIC)*/ |
| 78 |
|
| 79 |
#endif // REFCOUNT_OS_H__LINUX |