Skip to content

Commit

Permalink
Fix link fail caused by missing GCC char/short atomic primitives on r…
Browse files Browse the repository at this point in the history
…iscv64

Issue #46 (libatomic_ops).

gcc/riscv requires the client to add -latomic (or -pthread) flag
if char- or short-width compare-and-swap or an arithmetic primitive
is used.  To avoid such a requirement, the corresponding AO primitives
are not provided for now unless AO_PREFER_BUILTIN_ATOMICS is defined.

* src/atomic_ops/sysdeps/gcc/generic-arithm.h: Regenerate.
* src/atomic_ops/sysdeps/gcc/generic-arithm.template
(AO_XSIZE_fetch_and_add_XBAR): Do not define if AO_NO_XSIZE_ARITHM.
* src/atomic_ops/sysdeps/gcc/generic-arithm.template
[!AO_SKIPATOMIC_ANY_and_ANY] (AO_XSIZE_and_XBAR): Likewise.
* src/atomic_ops/sysdeps/gcc/generic-arithm.template
[!AO_SKIPATOMIC_ANY_or_ANY] (AO_XSIZE_or_XBAR): Likewise.
* src/atomic_ops/sysdeps/gcc/generic-arithm.template
[!AO_SKIPATOMIC_ANY_xor_ANY] (AO_XSIZE_xor_XBAR): Likewise.
* src/atomic_ops/sysdeps/gcc/riscv.h (AO_GCC_FORCE_HAVE_CAS): Define
only if __clang__ or AO_PREFER_BUILTIN_ATOMICS; update comment.
* src/atomic_ops/sysdeps/gcc/riscv.h [!__clang__
&& !AO_PREFER_BUILTIN_ATOMICS && !__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1]
(AO_NO_char_ARITHM): Define macro; add comment.
* src/atomic_ops/sysdeps/gcc/riscv.h [!__clang__
&& !AO_PREFER_BUILTIN_ATOMICS && !__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2]
(AO_NO_short_ARITHM): Likewise.
* src/atomic_ops/sysdeps/gcc/riscv.h (AO_NO_char_ARITHM,
AO_NO_short_ARITHM): Undefine after include generic.h.
  • Loading branch information
ivmai committed Sep 9, 2021
1 parent 48168a6 commit ce2a4df
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
64 changes: 64 additions & 0 deletions src/atomic_ops/sysdeps/gcc/generic-arithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*
*/

#ifndef AO_NO_char_ARITHM

AO_INLINE unsigned/**/char
AO_char_fetch_and_add(volatile unsigned/**/char *addr, unsigned/**/char incr)
{
Expand Down Expand Up @@ -48,6 +50,8 @@ AO_char_fetch_and_add(volatile unsigned/**/char *addr, unsigned/**/char incr)
}
# define AO_HAVE_char_xor
#endif

#endif /* !AO_NO_char_ARITHM */
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
Expand All @@ -65,6 +69,8 @@ AO_char_fetch_and_add(volatile unsigned/**/char *addr, unsigned/**/char incr)
*
*/

#ifndef AO_NO_short_ARITHM

AO_INLINE unsigned/**/short
AO_short_fetch_and_add(volatile unsigned/**/short *addr, unsigned/**/short incr)
{
Expand Down Expand Up @@ -98,6 +104,8 @@ AO_short_fetch_and_add(volatile unsigned/**/short *addr, unsigned/**/short incr)
}
# define AO_HAVE_short_xor
#endif

#endif /* !AO_NO_short_ARITHM */
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
Expand All @@ -115,6 +123,8 @@ AO_short_fetch_and_add(volatile unsigned/**/short *addr, unsigned/**/short incr)
*
*/

#ifndef AO_NO_int_ARITHM

AO_INLINE unsigned
AO_int_fetch_and_add(volatile unsigned *addr, unsigned incr)
{
Expand Down Expand Up @@ -148,6 +158,8 @@ AO_int_fetch_and_add(volatile unsigned *addr, unsigned incr)
}
# define AO_HAVE_int_xor
#endif

#endif /* !AO_NO_int_ARITHM */
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
Expand All @@ -165,6 +177,8 @@ AO_int_fetch_and_add(volatile unsigned *addr, unsigned incr)
*
*/

#ifndef AO_NO_ARITHM

AO_INLINE AO_t
AO_fetch_and_add(volatile AO_t *addr, AO_t incr)
{
Expand Down Expand Up @@ -198,6 +212,8 @@ AO_fetch_and_add(volatile AO_t *addr, AO_t incr)
}
# define AO_HAVE_xor
#endif

#endif /* !AO_NO_ARITHM */
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
Expand All @@ -215,6 +231,8 @@ AO_fetch_and_add(volatile AO_t *addr, AO_t incr)
*
*/

#ifndef AO_NO_char_ARITHM

AO_INLINE unsigned/**/char
AO_char_fetch_and_add_acquire(volatile unsigned/**/char *addr, unsigned/**/char incr)
{
Expand Down Expand Up @@ -248,6 +266,8 @@ AO_char_fetch_and_add_acquire(volatile unsigned/**/char *addr, unsigned/**/char
}
# define AO_HAVE_char_xor_acquire
#endif

#endif /* !AO_NO_char_ARITHM */
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
Expand All @@ -265,6 +285,8 @@ AO_char_fetch_and_add_acquire(volatile unsigned/**/char *addr, unsigned/**/char
*
*/

#ifndef AO_NO_short_ARITHM

AO_INLINE unsigned/**/short
AO_short_fetch_and_add_acquire(volatile unsigned/**/short *addr, unsigned/**/short incr)
{
Expand Down Expand Up @@ -298,6 +320,8 @@ AO_short_fetch_and_add_acquire(volatile unsigned/**/short *addr, unsigned/**/sho
}
# define AO_HAVE_short_xor_acquire
#endif

#endif /* !AO_NO_short_ARITHM */
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
Expand All @@ -315,6 +339,8 @@ AO_short_fetch_and_add_acquire(volatile unsigned/**/short *addr, unsigned/**/sho
*
*/

#ifndef AO_NO_int_ARITHM

AO_INLINE unsigned
AO_int_fetch_and_add_acquire(volatile unsigned *addr, unsigned incr)
{
Expand Down Expand Up @@ -348,6 +374,8 @@ AO_int_fetch_and_add_acquire(volatile unsigned *addr, unsigned incr)
}
# define AO_HAVE_int_xor_acquire
#endif

#endif /* !AO_NO_int_ARITHM */
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
Expand All @@ -365,6 +393,8 @@ AO_int_fetch_and_add_acquire(volatile unsigned *addr, unsigned incr)
*
*/

#ifndef AO_NO_ARITHM

AO_INLINE AO_t
AO_fetch_and_add_acquire(volatile AO_t *addr, AO_t incr)
{
Expand Down Expand Up @@ -398,6 +428,8 @@ AO_fetch_and_add_acquire(volatile AO_t *addr, AO_t incr)
}
# define AO_HAVE_xor_acquire
#endif

#endif /* !AO_NO_ARITHM */
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
Expand All @@ -415,6 +447,8 @@ AO_fetch_and_add_acquire(volatile AO_t *addr, AO_t incr)
*
*/

#ifndef AO_NO_char_ARITHM

AO_INLINE unsigned/**/char
AO_char_fetch_and_add_release(volatile unsigned/**/char *addr, unsigned/**/char incr)
{
Expand Down Expand Up @@ -448,6 +482,8 @@ AO_char_fetch_and_add_release(volatile unsigned/**/char *addr, unsigned/**/char
}
# define AO_HAVE_char_xor_release
#endif

#endif /* !AO_NO_char_ARITHM */
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
Expand All @@ -465,6 +501,8 @@ AO_char_fetch_and_add_release(volatile unsigned/**/char *addr, unsigned/**/char
*
*/

#ifndef AO_NO_short_ARITHM

AO_INLINE unsigned/**/short
AO_short_fetch_and_add_release(volatile unsigned/**/short *addr, unsigned/**/short incr)
{
Expand Down Expand Up @@ -498,6 +536,8 @@ AO_short_fetch_and_add_release(volatile unsigned/**/short *addr, unsigned/**/sho
}
# define AO_HAVE_short_xor_release
#endif

#endif /* !AO_NO_short_ARITHM */
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
Expand All @@ -515,6 +555,8 @@ AO_short_fetch_and_add_release(volatile unsigned/**/short *addr, unsigned/**/sho
*
*/

#ifndef AO_NO_int_ARITHM

AO_INLINE unsigned
AO_int_fetch_and_add_release(volatile unsigned *addr, unsigned incr)
{
Expand Down Expand Up @@ -548,6 +590,8 @@ AO_int_fetch_and_add_release(volatile unsigned *addr, unsigned incr)
}
# define AO_HAVE_int_xor_release
#endif

#endif /* !AO_NO_int_ARITHM */
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
Expand All @@ -565,6 +609,8 @@ AO_int_fetch_and_add_release(volatile unsigned *addr, unsigned incr)
*
*/

#ifndef AO_NO_ARITHM

AO_INLINE AO_t
AO_fetch_and_add_release(volatile AO_t *addr, AO_t incr)
{
Expand Down Expand Up @@ -598,6 +644,8 @@ AO_fetch_and_add_release(volatile AO_t *addr, AO_t incr)
}
# define AO_HAVE_xor_release
#endif

#endif /* !AO_NO_ARITHM */
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
Expand All @@ -615,6 +663,8 @@ AO_fetch_and_add_release(volatile AO_t *addr, AO_t incr)
*
*/

#ifndef AO_NO_char_ARITHM

AO_INLINE unsigned/**/char
AO_char_fetch_and_add_full(volatile unsigned/**/char *addr, unsigned/**/char incr)
{
Expand Down Expand Up @@ -648,6 +698,8 @@ AO_char_fetch_and_add_full(volatile unsigned/**/char *addr, unsigned/**/char inc
}
# define AO_HAVE_char_xor_full
#endif

#endif /* !AO_NO_char_ARITHM */
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
Expand All @@ -665,6 +717,8 @@ AO_char_fetch_and_add_full(volatile unsigned/**/char *addr, unsigned/**/char inc
*
*/

#ifndef AO_NO_short_ARITHM

AO_INLINE unsigned/**/short
AO_short_fetch_and_add_full(volatile unsigned/**/short *addr, unsigned/**/short incr)
{
Expand Down Expand Up @@ -698,6 +752,8 @@ AO_short_fetch_and_add_full(volatile unsigned/**/short *addr, unsigned/**/short
}
# define AO_HAVE_short_xor_full
#endif

#endif /* !AO_NO_short_ARITHM */
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
Expand All @@ -715,6 +771,8 @@ AO_short_fetch_and_add_full(volatile unsigned/**/short *addr, unsigned/**/short
*
*/

#ifndef AO_NO_int_ARITHM

AO_INLINE unsigned
AO_int_fetch_and_add_full(volatile unsigned *addr, unsigned incr)
{
Expand Down Expand Up @@ -748,6 +806,8 @@ AO_int_fetch_and_add_full(volatile unsigned *addr, unsigned incr)
}
# define AO_HAVE_int_xor_full
#endif

#endif /* !AO_NO_int_ARITHM */
/*
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
Expand All @@ -765,6 +825,8 @@ AO_int_fetch_and_add_full(volatile unsigned *addr, unsigned incr)
*
*/

#ifndef AO_NO_ARITHM

AO_INLINE AO_t
AO_fetch_and_add_full(volatile AO_t *addr, AO_t incr)
{
Expand Down Expand Up @@ -798,3 +860,5 @@ AO_fetch_and_add_full(volatile AO_t *addr, AO_t incr)
}
# define AO_HAVE_xor_full
#endif

#endif /* !AO_NO_ARITHM */
4 changes: 4 additions & 0 deletions src/atomic_ops/sysdeps/gcc/generic-arithm.template
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*
*/

#ifndef AO_NO_XSIZE_ARITHM

AO_INLINE XCTYPE
AO_XSIZE_fetch_and_add_XBAR(volatile XCTYPE *addr, XCTYPE incr)
{
Expand Down Expand Up @@ -48,3 +50,5 @@ AO_XSIZE_fetch_and_add_XBAR(volatile XCTYPE *addr, XCTYPE incr)
}
# define AO_HAVE_XSIZE_xor_XBAR
#endif

#endif /* !AO_NO_XSIZE_ARITHM */
22 changes: 16 additions & 6 deletions src/atomic_ops/sysdeps/gcc/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@
* modified is included with the above copyright notice.
*/

/* As of gcc-7.2.0, some __GCC_HAVE_SYNC_COMPARE_AND_SWAP_n are missing. */
/* The operations are lock-free (even for the types smaller than word). */
#define AO_GCC_FORCE_HAVE_CAS
#if defined(__clang__) || defined(AO_PREFER_BUILTIN_ATOMICS)
/* All __GCC_HAVE_SYNC_COMPARE_AND_SWAP_n macros are still missing. */
/* The operations are lock-free even for the types smaller than word. */
# define AO_GCC_FORCE_HAVE_CAS
#else

/* While double-word atomic operations are provided by the compiler */
/* (which requires -latomic currently), they are not lock-free as */
/* riscv itself does not have the double-word atomic operations. */
/* As of gcc-7.5, CAS and arithmetic atomic operations for char and */
/* short are supported by the compiler but require -latomic flag. */
# if !defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1)
# define AO_NO_char_ARITHM
# endif
# if !defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2)
# define AO_NO_short_ARITHM
# endif
#endif /* !__clang__ */

#include "generic.h"

#undef AO_GCC_FORCE_HAVE_CAS
#undef AO_NO_char_ARITHM
#undef AO_NO_short_ARITHM

0 comments on commit ce2a4df

Please sign in to comment.