Merge branch 'master' into urcu/ht-shrink
[urcu.git] / urcu-defer-impl.h
index d9c1c3a37cbe4fadce55ac08ad9edf562d8323e6..34d99c92997641323175c909f13f793fa27df33d 100644 (file)
@@ -38,7 +38,6 @@
 #include <errno.h>
 #include <poll.h>
 #include <sys/time.h>
-#include <syscall.h>
 #include <unistd.h>
 #include <stdint.h>
 
@@ -62,7 +61,9 @@
  * Assumes that (void *)-2L is not used often. Used to encode non-aligned
  * functions and non-aligned data using extra space.
  * We encode the (void *)-2L fct as: -2L, fct, data.
- * We encode the (void *)-2L data as: -2L, fct, data.
+ * We encode the (void *)-2L data as either:
+ *   fct | DQ_FCT_BIT, data (if fct is aligned), or
+ *   -2L, fct, data (if fct is not aligned).
  * Here, DQ_FCT_MARK == ~DQ_FCT_BIT. Required for the test order.
  */
 #define DQ_FCT_BIT             (1 << 0)
@@ -123,6 +124,7 @@ static pthread_mutex_t rcu_defer_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t defer_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 static int32_t defer_thread_futex;
+static int32_t defer_thread_stop;
 
 /*
  * Written to only by each individual deferer. Read by both the deferer and
@@ -149,7 +151,6 @@ static void mutex_lock_defer(pthread_mutex_t *mutex)
                        perror("Error in pthread mutex lock");
                        exit(-1);
                }
-               pthread_testcancel();
                poll(NULL,0,10);
        }
 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
@@ -187,7 +188,13 @@ static unsigned long rcu_defer_num_callbacks(void)
 static void wait_defer(void)
 {
        uatomic_dec(&defer_thread_futex);
-       cmm_smp_mb();   /* Write futex before read queue */
+       /* Write futex before read queue */
+       /* Write futex before read defer_thread_stop */
+       cmm_smp_mb();
+       if (_CMM_LOAD_SHARED(defer_thread_stop)) {
+               uatomic_set(&defer_thread_futex, 0);
+               pthread_exit(0);
+       }
        if (rcu_defer_num_callbacks()) {
                cmm_smp_mb();   /* Read queue before write futex */
                /* Callbacks are queued, don't wait. */
@@ -317,14 +324,27 @@ void _defer_rcu(void (*fct)(void *p), void *p)
                assert(head - CMM_LOAD_SHARED(defer_queue.tail) == 0);
        }
 
-       if (unlikely(defer_queue.last_fct_in != fct)) {
+       /*
+        * Encode:
+        * if the function is not changed and the data is aligned and it is
+        * not the marker:
+        *      store the data
+        * otherwise if the function is aligned and its not the marker:
+        *      store the function with DQ_FCT_BIT
+        *      store the data
+        * otherwise:
+        *      store the marker (DQ_FCT_MARK)
+        *      store the function
+        *      store the data
+        *
+        * Decode: see the comments before 'struct defer_queue'
+        *         or the code in rcu_defer_barrier_queue().
+        */
+       if (unlikely(defer_queue.last_fct_in != fct
+                       || DQ_IS_FCT_BIT(p)
+                       || p == DQ_FCT_MARK)) {
                defer_queue.last_fct_in = fct;
                if (unlikely(DQ_IS_FCT_BIT(fct) || fct == DQ_FCT_MARK)) {
-                       /*
-                        * If the function to encode is not aligned or the
-                        * marker, write DQ_FCT_MARK followed by the function
-                        * pointer.
-                        */
                        _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
                                      DQ_FCT_MARK);
                        _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
@@ -334,17 +354,6 @@ void _defer_rcu(void (*fct)(void *p), void *p)
                        _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
                                      fct);
                }
-       } else {
-               if (unlikely(DQ_IS_FCT_BIT(p) || p == DQ_FCT_MARK)) {
-                       /*
-                        * If the data to encode is not aligned or the marker,
-                        * write DQ_FCT_MARK followed by the function pointer.
-                        */
-                       _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
-                                     DQ_FCT_MARK);
-                       _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
-                                     fct);
-               }
        }
        _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK], p);
        cmm_smp_wmb();  /* Publish new pointer before head */
@@ -360,7 +369,6 @@ void _defer_rcu(void (*fct)(void *p), void *p)
 void *thr_defer(void *args)
 {
        for (;;) {
-               pthread_testcancel();
                /*
                 * "Be green". Don't wake up the CPU if there is no RCU work
                 * to perform whatsoever. Aims at saving laptop battery life by
@@ -397,10 +405,17 @@ static void stop_defer_thread(void)
        int ret;
        void *tret;
 
-       pthread_cancel(tid_defer);
+       _CMM_STORE_SHARED(defer_thread_stop, 1);
+       /* Store defer_thread_stop before testing futex */
+       cmm_smp_mb();
        wake_up_defer();
+
        ret = pthread_join(tid_defer, &tret);
        assert(!ret);
+
+       CMM_STORE_SHARED(defer_thread_stop, 0);
+       /* defer thread should always exit when futex value is 0 */
+       assert(uatomic_read(&defer_thread_futex) == 0);
 }
 
 int rcu_defer_register_thread(void)
This page took 0.024131 seconds and 4 git commands to generate.