Skip to content

Commit

Permalink
Merge pull request #72 from glessard/devel
Browse files Browse the repository at this point in the history
adjust CAS functions
  • Loading branch information
glessard authored Apr 5, 2020
2 parents 0d8ac91 + f916d82 commit 79608d7
Show file tree
Hide file tree
Showing 9 changed files with 2,309 additions and 2,614 deletions.
108 changes: 91 additions & 17 deletions Sources/CAtomics/include/CAtomics.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,38 @@ SWIFT_ENUM(CASType, closed)
parameterType CAtomics##opName(swiftType *_Nonnull atomic, parameterType pName, enum MemoryOrder order) \
{ return atomic_##op##_explicit(&(atomic->a), pName, order); }

#define CLANG_ATOMICS_WEAK_CAS(swiftType, parameterType) \
static __inline__ __attribute__((__always_inline__)) \
__attribute__((overloadable)) \
_Bool CAtomicsCompareAndExchangeWeak(swiftType *_Nonnull atomic, parameterType *_Nonnull current, parameterType future, \
enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \
{ \
assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \
assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \
return atomic_compare_exchange_weak_explicit(&(atomic->a), current, future, orderSwap, orderLoad); \
}

#define CLANG_ATOMICS_STRONG_CAS(swiftType, parameterType) \
static __inline__ __attribute__((__always_inline__)) \
__attribute__((overloadable)) \
_Bool CAtomicsCompareAndExchangeStrong(swiftType *_Nonnull atomic, parameterType *_Nonnull current, parameterType future, \
enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \
{ \
assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \
assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \
return atomic_compare_exchange_strong_explicit(&(atomic->a), current, future, orderSwap, orderLoad); \
}

#define CLANG_ATOMICS_CAS(swiftType, parameterType) \
static __inline__ __attribute__((__always_inline__)) \
__attribute__((overloadable)) \
_Bool CAtomicsCompareAndExchange(swiftType *_Nonnull atomic, parameterType *_Nonnull current, parameterType future, \
enum CASType type, enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \
{ \
assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \
assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \
if(type == __ATOMIC_CAS_TYPE_STRONG) \
return atomic_compare_exchange_strong_explicit(&(atomic->a), current, future, orderSwap, orderLoad); \
return CAtomicsCompareAndExchangeStrong(atomic, current, future, orderSwap, orderLoad); \
else \
return atomic_compare_exchange_weak_explicit(&(atomic->a), current, future, orderSwap, orderLoad); \
return CAtomicsCompareAndExchangeWeak(atomic, current, future, orderSwap, orderLoad); \
} \
static __inline__ __attribute__((__always_inline__)) \
__attribute__((overloadable)) \
Expand All @@ -166,6 +186,8 @@ SWIFT_ENUM(CASType, closed)
CLANG_ATOMICS_LOAD(swiftType, parameterType) \
CLANG_ATOMICS_STORE(swiftType, parameterType) \
CLANG_ATOMICS_SWAP(swiftType, parameterType) \
CLANG_ATOMICS_STRONG_CAS(swiftType, parameterType) \
CLANG_ATOMICS_WEAK_CAS(swiftType, parameterType) \
CLANG_ATOMICS_CAS(swiftType, parameterType)

// macro to generate atomic struct + functions for integer types
Expand Down Expand Up @@ -237,19 +259,41 @@ CLANG_ATOMICS_BOOL_GENERATE(AtomicBool, atomic_bool, _Bool, _Alignof(atomic_bool
parameterType nullability CAtomicsExchange(swiftType *_Nonnull atomic, parameterType nullability value, enum MemoryOrder order) \
{ return (parameterType) atomic_exchange_explicit(&(atomic->a), (uintptr_t)value, order); }

#define CLANG_ATOMICS_POINTER_WEAK_CAS(swiftType, parameterType, nullability) \
static __inline__ __attribute__((__always_inline__)) \
__attribute__((overloadable)) \
_Bool CAtomicsCompareAndExchangeWeak(swiftType *_Nonnull atomic, \
parameterType nullability* _Nonnull current, parameterType nullability future, \
enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \
{ \
assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \
assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \
return atomic_compare_exchange_weak_explicit(&(atomic->a), (uintptr_t*)current, (uintptr_t)future, orderSwap, orderLoad); \
}

#define CLANG_ATOMICS_POINTER_STRONG_CAS(swiftType, parameterType, nullability) \
static __inline__ __attribute__((__always_inline__)) \
__attribute__((overloadable)) \
_Bool CAtomicsCompareAndExchangeStrong(swiftType *_Nonnull atomic, \
parameterType nullability* _Nonnull current, parameterType nullability future, \
enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \
{ \
assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \
assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \
return atomic_compare_exchange_strong_explicit(&(atomic->a), (uintptr_t*)current, (uintptr_t)future, orderSwap, orderLoad); \
}

#define CLANG_ATOMICS_POINTER_CAS(swiftType, parameterType, nullability) \
static __inline__ __attribute__((__always_inline__)) \
__attribute__((overloadable)) \
_Bool CAtomicsCompareAndExchange(swiftType *_Nonnull atomic, \
parameterType nullability* _Nonnull current, parameterType nullability future, \
enum CASType type, enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \
{ \
assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \
assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \
if(type == __ATOMIC_CAS_TYPE_STRONG) \
return atomic_compare_exchange_strong_explicit(&(atomic->a), (uintptr_t*)current, (uintptr_t)future, orderSwap, orderLoad); \
return CAtomicsCompareAndExchangeStrong(atomic, current, future, orderSwap, orderLoad); \
else \
return atomic_compare_exchange_weak_explicit(&(atomic->a), (uintptr_t*)current, (uintptr_t)future, orderSwap, orderLoad); \
return CAtomicsCompareAndExchangeWeak(atomic, current, future, orderSwap, orderLoad); \
} \
static __inline__ __attribute__((__always_inline__)) \
__attribute__((overloadable)) \
Expand All @@ -271,6 +315,8 @@ CLANG_ATOMICS_BOOL_GENERATE(AtomicBool, atomic_bool, _Bool, _Alignof(atomic_bool
CLANG_ATOMICS_POINTER_LOAD(swiftType, parameterType, nullability) \
CLANG_ATOMICS_POINTER_STORE(swiftType, parameterType, nullability) \
CLANG_ATOMICS_POINTER_SWAP(swiftType, parameterType, nullability) \
CLANG_ATOMICS_POINTER_STRONG_CAS(swiftType, parameterType, nullability) \
CLANG_ATOMICS_POINTER_WEAK_CAS(swiftType, parameterType, nullability) \
CLANG_ATOMICS_POINTER_CAS(swiftType, parameterType, nullability)

// generate atomic pointer types + functions
Expand Down Expand Up @@ -345,18 +391,38 @@ CLANG_ATOMICS_POINTER_GENERATE(AtomicOptionalOpaquePointer, atomic_uintptr_t, st
structType CAtomicsExchange(atomicType *_Nonnull atomic, structType value, enum MemoryOrder order) \
{ structType rp; rp.tag_ptr = atomic_exchange_explicit(&(atomic->a), value.tag_ptr, order); return rp; }

#define CLANG_ATOMICS_TAGGED_POINTER_WEAK_CAS(swiftType, structType) \
static __inline__ __attribute__((__always_inline__)) \
__attribute__((overloadable)) \
_Bool CAtomicsCompareAndExchangeWeak(swiftType *_Nonnull atomic, structType *_Nonnull current, structType future, \
enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \
{ \
assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \
assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \
return atomic_compare_exchange_weak_explicit(&(atomic->a), &(current->tag_ptr), future.tag_ptr, orderSwap, orderLoad); \
}

#define CLANG_ATOMICS_TAGGED_POINTER_STRONG_CAS(swiftType, structType) \
static __inline__ __attribute__((__always_inline__)) \
__attribute__((overloadable)) \
_Bool CAtomicsCompareAndExchangeStrong(swiftType *_Nonnull atomic, structType *_Nonnull current, structType future, \
enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \
{ \
assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \
assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \
return atomic_compare_exchange_strong_explicit(&(atomic->a), &(current->tag_ptr), future.tag_ptr, orderSwap, orderLoad); \
}

#define CLANG_ATOMICS_TAGGED_POINTER_CAS(atomicType, structType) \
static __inline__ __attribute__((__always_inline__)) \
__attribute__((overloadable)) \
_Bool CAtomicsCompareAndExchange(atomicType *_Nonnull atomic, structType *_Nonnull current, structType future, \
enum CASType type, enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \
{ \
assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \
assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \
if(type == __ATOMIC_CAS_TYPE_STRONG) \
return atomic_compare_exchange_strong_explicit(&(atomic->a), &(current->tag_ptr), future.tag_ptr, orderSwap, orderLoad); \
return CAtomicsCompareAndExchangeStrong(atomic, current, future, orderSwap, orderLoad); \
else \
return atomic_compare_exchange_weak_explicit(&(atomic->a), &(current->tag_ptr), future.tag_ptr, orderSwap, orderLoad); \
return CAtomicsCompareAndExchangeWeak(atomic, current, future, orderSwap, orderLoad); \
} \
static __inline__ __attribute__((__always_inline__)) \
__attribute__((overloadable)) \
Expand Down Expand Up @@ -387,6 +453,8 @@ CLANG_ATOMICS_POINTER_GENERATE(AtomicOptionalOpaquePointer, atomic_uintptr_t, st
CLANG_ATOMICS_TAGGED_POINTER_LOAD(atomicType, structType) \
CLANG_ATOMICS_TAGGED_POINTER_STORE(atomicType, structType) \
CLANG_ATOMICS_TAGGED_POINTER_SWAP(atomicType, structType) \
CLANG_ATOMICS_TAGGED_POINTER_STRONG_CAS(atomicType, structType) \
CLANG_ATOMICS_TAGGED_POINTER_WEAK_CAS(atomicType, structType) \
CLANG_ATOMICS_TAGGED_POINTER_CAS(atomicType, structType)

CLANG_ATOMICS_TAGGED_POINTER_GENERATE(TaggedRawPointer, const void*, _Nonnull)
Expand Down Expand Up @@ -419,17 +487,23 @@ CLANG_ATOMICS_POINTER_SWAP(OpaqueUnmanagedHelper, const void*, _Nullable)
// this should only be used for debugging and testing
CLANG_ATOMICS_POINTER_LOAD(OpaqueUnmanagedHelper, const void*, _Nullable)

static __inline__ __attribute__((__always_inline__)) \
__attribute__((overloadable)) \
_Bool CAtomicsCompareAndExchangeStrong(OpaqueUnmanagedHelper *_Nonnull atomic,
const void *_Nullable current, const void *_Nullable future,
enum MemoryOrder order)
{
uintptr_t pointer = (uintptr_t) current;
return atomic_compare_exchange_strong_explicit(&(atomic->a), &pointer, (uintptr_t)future, order, memory_order_relaxed);
}

static __inline__ __attribute__((__always_inline__)) \
__attribute__((overloadable)) \
_Bool CAtomicsCompareAndExchange(OpaqueUnmanagedHelper *_Nonnull atomic,
const void *_Nullable current, const void *_Nullable future,
enum CASType type, enum MemoryOrder order)
{
uintptr_t pointer = (uintptr_t) current;
if(type == __ATOMIC_CAS_TYPE_WEAK)
return atomic_compare_exchange_weak_explicit(&(atomic->a), &pointer, (uintptr_t)future, order, memory_order_relaxed);
else
return atomic_compare_exchange_strong_explicit(&(atomic->a), &pointer, (uintptr_t)future, order, memory_order_relaxed);
return CAtomicsCompareAndExchangeStrong(atomic, current, future, order);
}

#endif
Loading

0 comments on commit 79608d7

Please sign in to comment.