mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Pincheng Wang <pincheng.plct@isrc.iscas.ac.cn>
To: musl@lists.openwall.com
Cc: pincheng.plct@isrc.iscas.ac.cn
Subject: [musl] [PATCH 1/1] riscv: add Zacas extension support for atomic CAS
Date: Fri, 19 Sep 2025 00:47:20 +0800	[thread overview]
Message-ID: <20250918164720.337994-2-pincheng.plct@isrc.iscas.ac.cn> (raw)
In-Reply-To: <20250918164720.337994-1-pincheng.plct@isrc.iscas.ac.cn>

Add compile-time detection for RISC-V Zacas extension and use
amocas.w.aqrl/amocas.d.aqrl instructions when available.

When __riscv_zacas is defined, a_cas() and a_cas_p() use single amocas
instructions instead of lr/sc loops. Falls back to existing lr/sc
implementation when Zacas is not available.

Signed-off-by: Pincheng Wang <pincheng.plct@isrc.iscas.ac.cn>
---
 arch/riscv32/atomic_arch.h | 17 +++++++++++++++++
 arch/riscv64/atomic_arch.h | 30 ++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/arch/riscv32/atomic_arch.h b/arch/riscv32/atomic_arch.h
index 4d418f63..64ef05b7 100644
--- a/arch/riscv32/atomic_arch.h
+++ b/arch/riscv32/atomic_arch.h
@@ -3,6 +3,21 @@ static inline void a_barrier()
 {
 	__asm__ __volatile__ ("fence rw,rw" : : : "memory");
 }
+#ifdef __riscv_zacas
+
+#define a_cas a_cas
+static inline int a_cas(volatile int *p, int t, int s)
+{
+	int old = t;
+	__asm__ __volatile__ (
+		"amocas.w.aqrl %0, %2, %1"
+		: "+r"(old), "+A"(*(volatile int *)p)
+		: "r"(s)
+		: "memory");
+	return old;
+}
+
+#else /* Fallback to lr/sc when Zacas is not available */
 
 #define a_cas a_cas
 static inline int a_cas(volatile int *p, int t, int s)
@@ -19,3 +34,5 @@ static inline int a_cas(volatile int *p, int t, int s)
 		: "memory");
 	return old;
 }
+
+#endif /* __riscv_zacas */
\ No newline at end of file
diff --git a/arch/riscv64/atomic_arch.h b/arch/riscv64/atomic_arch.h
index 0c382588..9681505e 100644
--- a/arch/riscv64/atomic_arch.h
+++ b/arch/riscv64/atomic_arch.h
@@ -4,6 +4,34 @@ static inline void a_barrier()
 	__asm__ __volatile__ ("fence rw,rw" : : : "memory");
 }
 
+#ifdef __riscv_zacas
+
+#define a_cas a_cas
+static inline int a_cas(volatile int *p, int t, int s)
+{
+	int old = t;
+	__asm__ __volatile__ (
+		"amocas.w.aqrl %0, %2, %1"
+		: "+r"(old), "+A"(*(volatile int *)p)
+		: "r"(s)
+		: "memory");
+	return old;
+}
+
+#define a_cas_p a_cas_p
+static inline void *a_cas_p(volatile void *p, void *t, void *s)
+{
+	void *old = t;
+	__asm__ __volatile__ (
+		"amocas.d.aqrl %0, %2, %1"
+		: "+r"(old), "+A"(*(void *volatile *)p)
+		: "r"(s)
+		: "memory");
+	return old;
+}
+
+#else /* Fallback to lr/sc when Zacas is not available */
+
 #define a_cas a_cas
 static inline int a_cas(volatile int *p, int t, int s)
 {
@@ -36,3 +64,5 @@ static inline void *a_cas_p(volatile void *p, void *t, void *s)
 		: "memory");
 	return old;
 }
+
+#endif /* __riscv_zacas */
-- 
2.39.5


  reply	other threads:[~2025-09-18 16:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-18 16:47 [musl] [PATCH 0/1] riscv: Add support for Zacas in atomic operations Pincheng Wang
2025-09-18 16:47 ` Pincheng Wang [this message]
2025-10-21  0:30   ` [musl] [PATCH 1/1] riscv: add Zacas extension support for atomic CAS Szabolcs Nagy
2025-10-21  3:05     ` Pincheng Wang
2025-10-16 15:37 ` [musl] [PATCH 0/1] riscv: Add support for Zacas in atomic operations Pincheng Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250918164720.337994-2-pincheng.plct@isrc.iscas.ac.cn \
    --to=pincheng.plct@isrc.iscas.ac.cn \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).