Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes to support including in c++ and fetch_add #129

Merged
merged 4 commits into from
Nov 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 58 additions & 9 deletions include/rcutils/stdatomic_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,45 @@
#ifndef RCUTILS__STDATOMIC_HELPER_H_
#define RCUTILS__STDATOMIC_HELPER_H_

#if !defined(_WIN32)
#include <stdbool.h>
#include <stdint.h>

#if defined(__GNUC__) && __GNUC__ <= 4 && __GNUC_MINOR__ <= 9
// If GCC and below GCC-4.9, use the compatability header.
#include "stdatomic_helper/gcc/stdatomic.h"
#elif defined(__clang__) && defined(__has_feature)
#if !__has_feature(c_atomic)
// If Clang and no c_atomics (true for some older versions), use the compatability header.
#include "stdatomic_helper/gcc/stdatomic.h"
// disable unused function warnings within this file, due to inline in a header
#if !defined(_WIN32)
# pragma GCC diagnostic push
wjwwood marked this conversation as resolved.
Show resolved Hide resolved
# if defined(__clang__)
# pragma clang diagnostic ignored "-Wunused-function"
# endif
#endif

#if !defined(_WIN32)

// The my__has_feature avoids a preprocessor error when you check for it and
// use it on the same line below.
#if defined(__has_feature)
#define my__has_feature(...) __has_feature(__VAR_ARGS__)
#else
#include <stdatomic.h>
#define my__has_feature(...) 0
#endif

#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ <= 4 && __GNUC_MINOR__ <= 9
// If GCC and below GCC-4.9, use the compatability header.
# include "stdatomic_helper/gcc/stdatomic.h"
#else // !defined(__clang__) && defined(__GNUC__) && __GNUC__ <= 4 && __GNUC_MINOR__ <= 9
# if __cplusplus
// NOLINTNEXTLINE
# error "cannot be used with C++ due to a conflict with the C++ <atomic> header, see: p0943r1"
// See: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0943r1.html"
# else
# if defined(__has_feature) && !my__has_feature(c_atomic)
// If Clang and no c_atomics (true for some older versions), use the compatability header.
# include "stdatomic_helper/gcc/stdatomic.h"
# else
# include <stdatomic.h>
# endif
# endif
#endif // !defined(__clang__) && defined(__GNUC__) && __GNUC__ <= 4 && __GNUC_MINOR__ <= 9

#define rcutils_atomic_load(object, out) (out) = atomic_load(object)

#define rcutils_atomic_compare_exchange_strong(object, out, expected, desired) \
Expand All @@ -38,6 +63,8 @@

#define rcutils_atomic_store(object, desired) atomic_store(object, desired)

#define rcutils_atomic_fetch_add(object, out, arg) (out) = atomic_fetch_add(object, arg)

#else // !defined(_WIN32)

#include "./stdatomic_helper/win32/stdatomic.h"
Expand All @@ -52,6 +79,8 @@

#define rcutils_atomic_store(object, desired) rcutils_win32_atomic_store(object, desired)

#define rcutils_atomic_fetch_add(object, out, arg) rcutils_win32_atomic_fetch_add(object, out, arg)

#endif // !defined(_WIN32)

static inline bool
Expand Down Expand Up @@ -91,7 +120,15 @@ rcutils_atomic_compare_exchange_strong_uint_least64_t(
atomic_uint_least64_t * a_uint_least64_t, uint64_t * expected, uint64_t desired)
{
bool result;
#if defined(__clang__)
# pragma clang diagnostic push
// we know it's a gnu feature, but clang supports it, so suppress pedantic warning
# pragma clang diagnostic ignored "-Wgnu-statement-expression"
#endif
rcutils_atomic_compare_exchange_strong(a_uint_least64_t, result, expected, desired);
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
return result;
}

Expand Down Expand Up @@ -127,4 +164,16 @@ rcutils_atomic_exchange_uintptr_t(atomic_uintptr_t * a_uintptr_t, uintptr_t desi
return result;
}

static inline uint64_t
rcutils_atomic_fetch_add_uint64_t(atomic_uint_least64_t * a_uint64_t, uint64_t arg)
{
uint64_t result;
rcutils_atomic_fetch_add(a_uint64_t, result, arg);
return result;
}

#if !defined(_WIN32)
# pragma GCC diagnostic pop
#endif

#endif // RCUTILS__STDATOMIC_HELPER_H_