Disable signals in URCU background threads
[urcu.git] / src / urcu.c
index 2cac0b602d6694a945bef8692a9b38ed97c13152..cf4d6d03f711750c05e91991f2a64687640b4438 100644 (file)
  * IBM's contributions to this file may be relicensed under LGPLv2 or later.
  */
 
+#define URCU_NO_COMPAT_IDENTIFIERS
 #define _BSD_SOURCE
 #define _LGPL_SOURCE
 #define _DEFAULT_SOURCE
 #include <stdio.h>
 #include <pthread.h>
 #include <signal.h>
-#include <assert.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
@@ -37,6 +37,8 @@
 #include <stdbool.h>
 #include <poll.h>
 
+#include <urcu/config.h>
+#include <urcu/assert.h>
 #include <urcu/arch.h>
 #include <urcu/wfcqueue.h>
 #include <urcu/map/urcu.h>
@@ -92,8 +94,6 @@ static int urcu_memb_has_sys_membarrier_private_expedited;
  * uninitialized variable.
  */
 int urcu_memb_has_sys_membarrier = 0;
-URCU_ATTR_ALIAS("urcu_memb_has_sys_membarrier")
-extern int rcu_has_sys_membarrier_memb;
 #endif
 
 void __attribute__((constructor)) rcu_init(void);
@@ -103,8 +103,6 @@ void __attribute__((constructor)) rcu_init(void);
 void rcu_init(void)
 {
 }
-URCU_ATTR_ALIAS(urcu_stringify(rcu_init))
-void alias_rcu_init(void);
 #endif
 
 #ifdef RCU_SIGNAL
@@ -112,6 +110,8 @@ static int init_done;
 
 void __attribute__((constructor)) rcu_init(void);
 void __attribute__((destructor)) rcu_exit(void);
+
+static DEFINE_URCU_TLS(int, rcu_signal_was_blocked);
 #endif
 
 /*
@@ -130,15 +130,12 @@ static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
  */
 static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER;
 struct urcu_gp rcu_gp = { .ctr = URCU_GP_COUNT };
-URCU_ATTR_ALIAS(urcu_stringify(rcu_gp))
-extern struct urcu_gp alias_rcu_gp;
 
 /*
  * Written to only by each individual reader. Read by both the reader and the
  * writers.
  */
 DEFINE_URCU_TLS(struct urcu_reader, rcu_reader);
-DEFINE_URCU_TLS_ALIAS(struct urcu_reader, rcu_reader, alias_rcu_reader);
 
 static CDS_LIST_HEAD(registry);
 
@@ -264,17 +261,25 @@ static void wait_gp(void)
        smp_mb_master();
        /* Temporarily unlock the registry lock. */
        mutex_unlock(&rcu_registry_lock);
-       if (uatomic_read(&rcu_gp.futex) != -1)
-               goto end;
-       while (futex_async(&rcu_gp.futex, FUTEX_WAIT, -1,
-                       NULL, NULL, 0)) {
+       while (uatomic_read(&rcu_gp.futex) == -1) {
+               if (!futex_async(&rcu_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. */
                        goto end;
                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);
@@ -446,7 +451,7 @@ void 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);
 
@@ -488,7 +493,7 @@ void 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);
 
@@ -514,8 +519,6 @@ out:
         */
        urcu_wake_all_waiters(&waiters);
 }
-URCU_ATTR_ALIAS(urcu_stringify(synchronize_rcu))
-void alias_synchronize_rcu();
 
 /*
  * library wrappers to be used by non-LGPL compatible source code.
@@ -525,49 +528,85 @@ void rcu_read_lock(void)
 {
        _rcu_read_lock();
 }
-URCU_ATTR_ALIAS(urcu_stringify(rcu_read_lock))
-void alias_rcu_read_lock();
 
 void rcu_read_unlock(void)
 {
        _rcu_read_unlock();
 }
-URCU_ATTR_ALIAS(urcu_stringify(rcu_read_unlock))
-void alias_rcu_read_unlock();
 
 int rcu_read_ongoing(void)
 {
        return _rcu_read_ongoing();
 }
-URCU_ATTR_ALIAS(urcu_stringify(rcu_read_ongoing))
-void alias_rcu_read_ongoing();
+
+#ifdef RCU_SIGNAL
+/*
+ * Make sure the signal used by the urcu-signal flavor is unblocked
+ * while the thread is registered.
+ */
+static
+void urcu_signal_unblock(void)
+{
+       sigset_t mask, oldmask;
+       int ret;
+
+       ret = sigemptyset(&mask);
+       urcu_posix_assert(!ret);
+       ret = sigaddset(&mask, SIGRCU);
+       urcu_posix_assert(!ret);
+       ret = pthread_sigmask(SIG_UNBLOCK, &mask, &oldmask);
+       urcu_posix_assert(!ret);
+       URCU_TLS(rcu_signal_was_blocked) = sigismember(&oldmask, SIGRCU);
+}
+
+static
+void urcu_signal_restore(void)
+{
+       sigset_t mask;
+       int ret;
+
+       if (!URCU_TLS(rcu_signal_was_blocked))
+               return;
+       ret = sigemptyset(&mask);
+       urcu_posix_assert(!ret);
+       ret = sigaddset(&mask, SIGRCU);
+       urcu_posix_assert(!ret);
+       ret = pthread_sigmask(SIG_BLOCK, &mask, NULL);
+       urcu_posix_assert(!ret);
+}
+#else
+static
+void urcu_signal_unblock(void) { }
+static
+void urcu_signal_restore(void) { }
+#endif
 
 void rcu_register_thread(void)
 {
+       urcu_signal_unblock();
+
        URCU_TLS(rcu_reader).tid = pthread_self();
-       assert(URCU_TLS(rcu_reader).need_mb == 0);
-       assert(!(URCU_TLS(rcu_reader).ctr & URCU_GP_CTR_NEST_MASK));
+       urcu_posix_assert(URCU_TLS(rcu_reader).need_mb == 0);
+       urcu_posix_assert(!(URCU_TLS(rcu_reader).ctr & URCU_GP_CTR_NEST_MASK));
 
        mutex_lock(&rcu_registry_lock);
-       assert(!URCU_TLS(rcu_reader).registered);
+       urcu_posix_assert(!URCU_TLS(rcu_reader).registered);
        URCU_TLS(rcu_reader).registered = 1;
        rcu_init();     /* In case gcc does not support constructor attribute */
        cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
        mutex_unlock(&rcu_registry_lock);
 }
-URCU_ATTR_ALIAS(urcu_stringify(rcu_register_thread))
-void alias_rcu_register_thread();
 
 void rcu_unregister_thread(void)
 {
        mutex_lock(&rcu_registry_lock);
-       assert(URCU_TLS(rcu_reader).registered);
+       urcu_posix_assert(URCU_TLS(rcu_reader).registered);
        URCU_TLS(rcu_reader).registered = 0;
        cds_list_del(&URCU_TLS(rcu_reader).node);
        mutex_unlock(&rcu_registry_lock);
+
+       urcu_signal_restore();
 }
-URCU_ATTR_ALIAS(urcu_stringify(rcu_unregister_thread))
-void alias_rcu_unregister_thread();
 
 #ifdef RCU_MEMBARRIER
 
@@ -615,12 +654,12 @@ void rcu_init(void)
        init_done = 1;
        rcu_sys_membarrier_init();
 }
-URCU_ATTR_ALIAS(urcu_stringify(rcu_init))
-void alias_rcu_init(void);
 #endif
 
 #ifdef RCU_SIGNAL
-static void sigrcu_handler(int signo, siginfo_t *siginfo, void *context)
+static void sigrcu_handler(int signo __attribute__((unused)),
+               siginfo_t *siginfo __attribute__((unused)),
+               void *context __attribute__((unused)))
 {
        /*
         * Executing this cmm_smp_mb() is the only purpose of this signal handler.
@@ -656,8 +695,6 @@ void rcu_init(void)
        if (ret)
                urcu_die(errno);
 }
-URCU_ATTR_ALIAS(urcu_stringify(rcu_init))
-void alias_rcu_init(void);
 
 void rcu_exit(void)
 {
@@ -667,16 +704,13 @@ void rcu_exit(void)
         * application exits.
         * 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_ATTR_ALIAS(urcu_stringify(rcu_exit))
-void alias_rcu_exit(void);
 
 #endif /* #ifdef RCU_SIGNAL */
 
 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"
This page took 0.028495 seconds and 4 git commands to generate.