uatomic/x86: Remove redundant memory barriers
[urcu.git] / src / urcu-qsbr.c
index ab20ebe2ce17f5af69cd785a506df86d67677eb1..e7ee180a9f81119a091de2d3156203028debb2bb 100644 (file)
@@ -1,39 +1,27 @@
+// SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+// SPDX-FileCopyrightText: 2009 Paul E. McKenney, IBM Corporation.
+//
+// SPDX-License-Identifier: LGPL-2.1-or-later
+
 /*
- * urcu-qsbr.c
- *
  * Userspace RCU QSBR library
  *
- * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
  * IBM's contributions to this file may be relicensed under LGPLv2 or later.
  */
 
+#define URCU_NO_COMPAT_IDENTIFIERS
 #define _LGPL_SOURCE
 #include <stdio.h>
 #include <pthread.h>
 #include <signal.h>
-#include <assert.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
 #include <errno.h>
 #include <poll.h>
 
+#include <urcu/annotate.h>
+#include <urcu/assert.h>
 #include <urcu/wfcqueue.h>
 #include <urcu/map/urcu-qsbr.h>
 #define BUILD_QSBR_LIB
@@ -52,6 +40,7 @@
 #define _LGPL_SOURCE
 
 void __attribute__((destructor)) urcu_qsbr_exit(void);
+static void urcu_call_rcu_exit(void);
 
 /*
  * rcu_gp_lock ensures mutual exclusion between threads calling
@@ -69,7 +58,6 @@ static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
  */
 static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER;
 struct urcu_gp urcu_qsbr_gp = { .ctr = URCU_QSBR_GP_ONLINE };
-URCU_ATTR_ALIAS("urcu_qsbr_gp") extern struct urcu_gp rcu_gp_qsbr;
 
 /*
  * Active attempts to check for reader Q.S. before calling futex().
@@ -81,7 +69,6 @@ URCU_ATTR_ALIAS("urcu_qsbr_gp") extern struct urcu_gp rcu_gp_qsbr;
  * writers.
  */
 DEFINE_URCU_TLS(struct urcu_qsbr_reader, urcu_qsbr_reader);
-DEFINE_URCU_TLS_ALIAS(struct urcu_qsbr_reader, urcu_qsbr_reader, rcu_reader_qsbr);
 
 static CDS_LIST_HEAD(registry);
 
@@ -124,17 +111,25 @@ static void wait_gp(void)
 {
        /* Read reader_gp before read futex */
        cmm_smp_rmb();
-       if (uatomic_read(&urcu_qsbr_gp.futex) != -1)
-               return;
-       while (futex_noasync(&urcu_qsbr_gp.futex, FUTEX_WAIT, -1,
-                       NULL, NULL, 0)) {
+       while (uatomic_read(&urcu_qsbr_gp.futex) == -1) {
+               if (!futex_noasync(&urcu_qsbr_gp.futex, FUTEX_WAIT, -1, NULL, NULL, 0)) {
+                       /*
+                        * Prior queued wakeups queued by unrelated code
+                        * using the same address can cause futex wait to
+                        * return 0 even through the futex value is still
+                        * -1 (spurious wakeups). Check the value again
+                        * in user-space to validate whether it really
+                        * differs from -1.
+                        */
+                       continue;
+               }
                switch (errno) {
-               case EWOULDBLOCK:
+               case EAGAIN:
                        /* Value already changed. */
                        return;
                case EINTR:
                        /* Retry if interrupted by signal. */
-                       break;  /* Get out of switch. */
+                       break;  /* Get out of switch. Check again. */
                default:
                        /* Unexpected error. */
                        urcu_die(errno);
@@ -148,7 +143,8 @@ static void wait_gp(void)
  */
 static void wait_for_readers(struct cds_list_head *input_readers,
                        struct cds_list_head *cur_snap_readers,
-                       struct cds_list_head *qsreaders)
+                       struct cds_list_head *qsreaders,
+                       cmm_annotate_t *group)
 {
        unsigned int wait_loops = 0;
        struct urcu_qsbr_reader *index, *tmp;
@@ -175,7 +171,7 @@ static void wait_for_readers(struct cds_list_head *input_readers,
                        cmm_smp_mb();
                }
                cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
-                       switch (urcu_qsbr_reader_state(&index->ctr)) {
+                       switch (urcu_qsbr_reader_state(&index->ctr, group)) {
                        case URCU_READER_ACTIVE_CURRENT:
                                if (cur_snap_readers) {
                                        cds_list_move(&index->node,
@@ -200,8 +196,7 @@ static void wait_for_readers(struct cds_list_head *input_readers,
                if (cds_list_empty(input_readers)) {
                        if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
                                /* Read reader_gp before write futex */
-                               cmm_smp_mb();
-                               uatomic_set(&urcu_qsbr_gp.futex, 0);
+                               uatomic_store(&urcu_qsbr_gp.futex, 0, CMM_RELEASE);
                        }
                        break;
                } else {
@@ -230,6 +225,8 @@ static void wait_for_readers(struct cds_list_head *input_readers,
 #if (CAA_BITS_PER_LONG < 64)
 void urcu_qsbr_synchronize_rcu(void)
 {
+       cmm_annotate_define(acquire_group);
+       cmm_annotate_define(release_group);
        CDS_LIST_HEAD(cur_snap_readers);
        CDS_LIST_HEAD(qsreaders);
        unsigned long was_online;
@@ -250,6 +247,7 @@ void urcu_qsbr_synchronize_rcu(void)
                urcu_qsbr_thread_offline();
        else
                cmm_smp_mb();
+       cmm_annotate_group_mb_release(&release_group);
 
        /*
         * Add ourself to gp_waiters queue of threads awaiting to wait
@@ -279,9 +277,9 @@ void urcu_qsbr_synchronize_rcu(void)
        /*
         * Wait for readers to observe original parity or be quiescent.
         * wait_for_readers() can release and grab again rcu_registry_lock
-        * interally.
+        * internally.
         */
-       wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
+       wait_for_readers(&registry, &cur_snap_readers, &qsreaders, &acquire_group);
 
        /*
         * Must finish waiting for quiescent state for original parity
@@ -301,7 +299,8 @@ void urcu_qsbr_synchronize_rcu(void)
        cmm_smp_mb();
 
        /* Switch parity: 0 -> 1, 1 -> 0 */
-       CMM_STORE_SHARED(urcu_qsbr_gp.ctr, urcu_qsbr_gp.ctr ^ URCU_QSBR_GP_CTR);
+       cmm_annotate_group_mem_release(&release_group, &urcu_qsbr_gp.ctr);
+       uatomic_store(&urcu_qsbr_gp.ctr, urcu_qsbr_gp.ctr ^ URCU_QSBR_GP_CTR, CMM_RELAXED);
 
        /*
         * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
@@ -322,9 +321,9 @@ void urcu_qsbr_synchronize_rcu(void)
        /*
         * Wait for readers to observe new parity or be quiescent.
         * wait_for_readers() can release and grab again rcu_registry_lock
-        * interally.
+        * internally.
         */
-       wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
+       wait_for_readers(&cur_snap_readers, NULL, &qsreaders, &acquire_group);
 
        /*
         * Put quiescent reader list back into registry.
@@ -339,6 +338,8 @@ gp_end:
         * Finish waiting for reader threads before letting the old ptr being
         * freed.
         */
+       cmm_annotate_group_mb_acquire(&acquire_group);
+
        if (was_online)
                urcu_qsbr_thread_online();
        else
@@ -347,6 +348,8 @@ gp_end:
 #else /* !(CAA_BITS_PER_LONG < 64) */
 void urcu_qsbr_synchronize_rcu(void)
 {
+       cmm_annotate_define(acquire_group);
+       cmm_annotate_define(release_group);
        CDS_LIST_HEAD(qsreaders);
        unsigned long was_online;
        DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
@@ -363,6 +366,7 @@ void urcu_qsbr_synchronize_rcu(void)
                urcu_qsbr_thread_offline();
        else
                cmm_smp_mb();
+       cmm_annotate_group_mb_release(&release_group);
 
        /*
         * Add ourself to gp_waiters queue of threads awaiting to wait
@@ -390,7 +394,8 @@ void urcu_qsbr_synchronize_rcu(void)
                goto out;
 
        /* Increment current G.P. */
-       CMM_STORE_SHARED(urcu_qsbr_gp.ctr, urcu_qsbr_gp.ctr + URCU_QSBR_GP_CTR);
+       cmm_annotate_group_mem_release(&release_group, &urcu_qsbr_gp.ctr);
+       uatomic_store(&urcu_qsbr_gp.ctr, urcu_qsbr_gp.ctr + URCU_QSBR_GP_CTR, CMM_RELAXED);
 
        /*
         * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
@@ -411,9 +416,9 @@ void urcu_qsbr_synchronize_rcu(void)
        /*
         * Wait for readers to observe new count of be quiescent.
         * wait_for_readers() can release and grab again rcu_registry_lock
-        * interally.
+        * internally.
         */
-       wait_for_readers(&registry, NULL, &qsreaders);
+       wait_for_readers(&registry, NULL, &qsreaders, &acquire_group);
 
        /*
         * Put quiescent reader list back into registry.
@@ -428,10 +433,10 @@ gp_end:
                urcu_qsbr_thread_online();
        else
                cmm_smp_mb();
+
+       cmm_annotate_group_mb_acquire(&acquire_group);
 }
 #endif  /* !(CAA_BITS_PER_LONG < 64) */
-URCU_ATTR_ALIAS("urcu_qsbr_synchronize_rcu")
-void synchronize_rcu_qsbr();
 
 /*
  * library wrappers to be used by non-LGPL compatible source code.
@@ -441,56 +446,47 @@ void urcu_qsbr_read_lock(void)
 {
        _urcu_qsbr_read_lock();
 }
-URCU_ATTR_ALIAS("urcu_qsbr_read_lock") void rcu_read_lock_qsbr();
 
 void urcu_qsbr_read_unlock(void)
 {
        _urcu_qsbr_read_unlock();
 }
-URCU_ATTR_ALIAS("urcu_qsbr_read_unlock") void rcu_read_unlock_qsbr();
 
 int urcu_qsbr_read_ongoing(void)
 {
        return _urcu_qsbr_read_ongoing();
 }
-URCU_ATTR_ALIAS("urcu_qsbr_read_ongoing")
 void rcu_read_ongoing_qsbr();
 
 void urcu_qsbr_quiescent_state(void)
 {
        _urcu_qsbr_quiescent_state();
 }
-URCU_ATTR_ALIAS("urcu_qsbr_quiescent_state")
 void rcu_quiescent_state_qsbr();
 
 void urcu_qsbr_thread_offline(void)
 {
        _urcu_qsbr_thread_offline();
 }
-URCU_ATTR_ALIAS("urcu_qsbr_thread_offline")
 void rcu_thread_offline_qsbr();
 
 void urcu_qsbr_thread_online(void)
 {
        _urcu_qsbr_thread_online();
 }
-URCU_ATTR_ALIAS("urcu_qsbr_thread_online")
-void rcu_thread_online_qsbr();
 
 void urcu_qsbr_register_thread(void)
 {
        URCU_TLS(urcu_qsbr_reader).tid = pthread_self();
-       assert(URCU_TLS(urcu_qsbr_reader).ctr == 0);
+       urcu_posix_assert(URCU_TLS(urcu_qsbr_reader).ctr == 0);
 
        mutex_lock(&rcu_registry_lock);
-       assert(!URCU_TLS(urcu_qsbr_reader).registered);
+       urcu_posix_assert(!URCU_TLS(urcu_qsbr_reader).registered);
        URCU_TLS(urcu_qsbr_reader).registered = 1;
        cds_list_add(&URCU_TLS(urcu_qsbr_reader).node, &registry);
        mutex_unlock(&rcu_registry_lock);
        _urcu_qsbr_thread_online();
 }
-URCU_ATTR_ALIAS("urcu_qsbr_register_thread")
-void rcu_register_thread_qsbr();
 
 void urcu_qsbr_unregister_thread(void)
 {
@@ -499,27 +495,25 @@ void urcu_qsbr_unregister_thread(void)
         * with a waiting writer.
         */
        _urcu_qsbr_thread_offline();
-       assert(URCU_TLS(urcu_qsbr_reader).registered);
+       urcu_posix_assert(URCU_TLS(urcu_qsbr_reader).registered);
        URCU_TLS(urcu_qsbr_reader).registered = 0;
        mutex_lock(&rcu_registry_lock);
        cds_list_del(&URCU_TLS(urcu_qsbr_reader).node);
        mutex_unlock(&rcu_registry_lock);
 }
-URCU_ATTR_ALIAS("urcu_qsbr_unregister_thread")
-void rcu_unregister_thread_qsbr();
 
 void urcu_qsbr_exit(void)
 {
        /*
         * Assertion disabled because call_rcu threads are now rcu
         * readers, and left running at exit.
-        * assert(cds_list_empty(&registry));
+        * urcu_posix_assert(cds_list_empty(&registry));
         */
+       urcu_call_rcu_exit();
 }
-URCU_ATTR_ALIAS("urcu_qsbr_exit") void rcu_exit_qsbr();
 
 DEFINE_RCU_FLAVOR(rcu_flavor);
-DEFINE_RCU_FLAVOR_ALIAS(rcu_flavor, alias_rcu_flavor);
 
 #include "urcu-call-rcu-impl.h"
 #include "urcu-defer-impl.h"
+#include "urcu-poll-impl.h"
This page took 0.026999 seconds and 4 git commands to generate.