Use membarrier
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sun, 30 Oct 2022 19:56:40 +0000 (15:56 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sun, 30 Oct 2022 19:57:27 +0000 (15:57 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
src/rcu.c
src/rcu.h

index 45136ad5707f0ece0a411ae4f359066190a24d30..21dc1a3158ec29a7939d8efc3edeb42794e691cf 100644 (file)
--- a/src/rcu.c
+++ b/src/rcu.c
 #include <stdbool.h>
 #include <poll.h>
 #include <stdlib.h>
+#include <unistd.h>
+#include <sys/syscall.h>
+#include <linux/membarrier.h>
 
 #include "rcu.h"
 #include "smp.h"
 
+static int
+membarrier(int cmd, unsigned int flags, int cpu_id)
+{
+       return syscall(__NR_membarrier, cmd, flags, cpu_id);
+}
+
 /* active_readers is an input/output parameter. */
 static
 void check_active_readers(struct side_rcu_gp_state *gp_state, bool *active_readers)
@@ -44,7 +53,8 @@ void check_active_readers(struct side_rcu_gp_state *gp_state, bool *active_reade
         * incremented before "end", as guaranteed by memory barriers
         * (A) or (B).
         */
-       __atomic_thread_fence(__ATOMIC_SEQ_CST);
+       if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0, 0))
+               abort();
 
        for (i = 0; i < gp_state->nr_cpus; i++) {
                struct side_rcu_cpu_gp_state *cpu_state = &gp_state->percpu_state[i];
@@ -117,7 +127,8 @@ void side_rcu_wait_grace_period(struct side_rcu_gp_state *gp_state)
         * exist after the grace period completes are ordered after
         * loads and stores performed before the grace period.
         */
-       __atomic_thread_fence(__ATOMIC_SEQ_CST);
+       if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0, 0))
+               abort();
 
        /*
         * First scan through all cpus, for both period. If no readers
@@ -158,7 +169,8 @@ end:
         * are ordered before loads and stores performed after the grace
         * period.
         */
-       __atomic_thread_fence(__ATOMIC_SEQ_CST);
+       if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0, 0))
+               abort();
 }
 
 void side_rcu_gp_init(struct side_rcu_gp_state *rcu_gp)
@@ -171,6 +183,8 @@ void side_rcu_gp_init(struct side_rcu_gp_state *rcu_gp)
        rcu_gp->percpu_state = calloc(rcu_gp->nr_cpus, sizeof(struct side_rcu_cpu_gp_state));
        if (!rcu_gp->percpu_state)
                abort();
+       if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0, 0))
+               abort();
 }
 
 void side_rcu_gp_exit(struct side_rcu_gp_state *rcu_gp)
index 25655ca850bd92a2c67c7e5106c5276451c453da..d7b2de5faf62493aaf4316aab44d8487df491a31 100644 (file)
--- a/src/rcu.h
+++ b/src/rcu.h
@@ -34,7 +34,6 @@ struct side_rcu_gp_state {
        pthread_mutex_t gp_lock;
 };
 
-//TODO: replace acquire/release by membarrier+compiler barrier (when available)
 //TODO: implement wait/wakeup for grace period using sys_futex
 static inline
 unsigned int side_rcu_read_begin(struct side_rcu_gp_state *gp_state)
@@ -65,7 +64,7 @@ fence:
         * barrier (C). It is redundant with memory barrier (B) for that
         * purpose.
         */
-       __atomic_thread_fence(__ATOMIC_SEQ_CST);
+       rseq_barrier();
        return period;
 }
 
@@ -85,7 +84,7 @@ void side_rcu_read_end(struct side_rcu_gp_state *gp_state, unsigned int period)
         * barrier (C). It is redundant with memory barrier (A) for that
         * purpose.
         */
-       __atomic_thread_fence(__ATOMIC_SEQ_CST);
+       rseq_barrier();
 
        if (side_likely(rseq_offset > 0)) {
                cpu = rseq_cpu_start();
This page took 0.03346 seconds and 4 git commands to generate.