Last active
December 18, 2015 01:39
-
-
Save jepler/5705802 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if defined(__clang__) && __clang__ | |
#define RTAPI_USE_ATOMIC 1 | |
#elif defined(__GNUC__) && (__GNUC__ > 4) && (__GNUC_MINOR__ >= 7) | |
#define RTAPI_USE_ATOMIC 1 | |
#else | |
#define RTAPI_USE_ATOMIC 0 | |
#endif | |
typedef unsigned long rtapi_atomic_type; | |
#define RTAPI_DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) | |
#define RTAPI_BITS_PER_LONG (CHAR_BIT * sizeof(rtapi_atomic_type)) | |
#define RTAPI_BIT_MASK(nr) (1UL << ((nr) % RTAPI_BITS_PER_LONG)) | |
#define RTAPI_BIT_WORD(nr) ((nr) / RTAPI_BITS_PER_LONG) | |
#define RTAPI_BITMAP_SIZE(nr) RTAPI_DIV_ROUND_UP(nr, RTAPI_BITS_PER_LONG) | |
#define RTAPI_BITMAP_BYTES(nr) \ | |
(sizeof(rtapi_atomic_type) * RTAPI_BITMAP_SIZE(nr)) | |
#define RTAPI_BIT_SET(a, b) ((a)[RTAPI_BIT_WORD(b)] |= RTAPI_BIT_MASK(b)) | |
#define RTAPI_BIT_CLEAR(a, b) ((a)[RTAPI_BIT_WORD(b)] &= ~ RTAPI_BIT_MASK(b)) | |
#define RTAPI_BIT_TEST(a, b) ((a)[RTAPI_BIT_WORD(b)] & RTAPI_BIT_MASK(b)) | |
#define RTAPI_DECLARE_BITMAP(name,bits) \ | |
rtapi_atomic_type name[RTAPI_BITMAP_SIZE(bits)] | |
#define RTAPI_ZERO_BITMAP(name,bits) memset(name, 0, RTAPI_BITMAP_BYTES(bits)) | |
#define RTAPI_SET_BITMAP(name,bits) memset(name, ~0, RTAPI_BITMAP_BYTES(bits)) | |
#ifdef RTAPI_USE_ATOMIC | |
#define RTAPI_MEMORY_MODEL __ATOMIC_SEQ_CST | |
static inline int rtapi_test_and_set_bit(int nr, void *bitmap) | |
{ | |
rtapi_atomic_type *value = (rtapi_atomic_type*)bitmap; | |
return (__atomic_fetch_or(value + RTAPI_BIT_WORD(nr), | |
RTAPI_BIT_MASK(nr)) & RTAPI_BIT_MASK(nr), | |
RTAPI_MEMORY_MODEL) != 0; | |
} | |
#else | |
static inline int rtapi_test_and_set_bit(int nr, void *bitmap) | |
{ | |
rtapi_atomic_type *value = (rtapi_atomic_type*)bitmap; | |
return (__sync_fetch_and_or(value + RTAPI_BIT_WORD(nr), | |
RTAPI_BIT_MASK(nr)) & RTAPI_BIT_MASK(nr)) != 0; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment