Fix urcu-defer: add missing brackets.
[urcu.git] / urcu-defer.c
index 5b904f8321a23c2ef3b84752c64c84cc9f9c865a..3efe5ba2a5a47ccaff0a2f956efa957fb71bde01 100644 (file)
@@ -49,13 +49,13 @@ extern void synchronize_rcu(void);
 static pthread_mutex_t urcu_defer_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t defer_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
 
-int defer_thread_futex;
+static int defer_thread_futex;
 
 /*
  * Written to only by each individual deferer. Read by both the deferer and
  * the reclamation tread.
  */
-struct defer_queue __thread defer_queue;
+static struct defer_queue __thread defer_queue;
 
 /* Thread IDs of registered deferers */
 #define INIT_NUM_THREADS 4
@@ -74,12 +74,13 @@ static pthread_t tid_defer;
 /*
  * Wake-up any waiting defer thread. Called from many concurrent threads.
  */
-void wake_up_defer(void)
+static void wake_up_defer(void)
 {
-       if (unlikely(atomic_read(&defer_thread_futex) == -1))
+       if (unlikely(atomic_read(&defer_thread_futex) == -1)) {
                atomic_set(&defer_thread_futex, 0);
-               futex(&defer_thread_futex, FUTEX_WAKE,
-                     0, NULL, NULL, 0);
+               futex(&defer_thread_futex, FUTEX_WAKE, 0,
+                     NULL, NULL, 0);
+       }
 }
 
 /*
@@ -222,25 +223,82 @@ end:
        internal_urcu_unlock(&urcu_defer_mutex);
 }
 
+/*
+ * _rcu_defer_queue - Queue a RCU callback.
+ */
+void _rcu_defer_queue(void (*fct)(void *p), void *p)
+{
+       unsigned long head, tail;
+
+       /*
+        * Head is only modified by ourself. Tail can be modified by reclamation
+        * thread.
+        */
+       head = defer_queue.head;
+       tail = LOAD_SHARED(defer_queue.tail);
+
+       /*
+        * If queue is full, empty it ourself.
+        * Worse-case: must allow 2 supplementary entries for fct pointer.
+        */
+       if (unlikely(head - tail >= DEFER_QUEUE_SIZE - 2)) {
+               assert(head - tail <= DEFER_QUEUE_SIZE);
+               rcu_defer_barrier_thread();
+               assert(head - LOAD_SHARED(defer_queue.tail) == 0);
+       }
+
+       if (unlikely(defer_queue.last_fct_in != fct)) {
+               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.
+                        */
+                       _STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
+                                     DQ_FCT_MARK);
+                       _STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
+                                     fct);
+               } else {
+                       DQ_SET_FCT_BIT(fct);
+                       _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.
+                        */
+                       _STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
+                                     DQ_FCT_MARK);
+                       _STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
+                                     fct);
+               }
+       }
+       _STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK], p);
+       smp_wmb();      /* Publish new pointer before head */
+                       /* Write q[] before head. */
+       STORE_SHARED(defer_queue.head, head);
+       /*
+        * Wake-up any waiting defer thread.
+        */
+       wake_up_defer();
+}
+
 void *thr_defer(void *args)
 {
        for (;;) {
                pthread_testcancel();
-               printf("a\n");
                /*
                 * "Be green". Don't wake up the CPU if there is no RCU work
                 * to perform whatsoever. Aims at saving laptop battery life by
                 * leaving the processor in sleep state when idle.
                 */
-               printf("b\n");
                wait_defer();
-               printf("e\n");
                /* Sleeping after wait_defer to let many callbacks enqueue */
-               //TEST poll(NULL,0,100);        /* wait for 100ms */
-               printf("f\n");
+               poll(NULL,0,100);       /* wait for 100ms */
                rcu_defer_barrier();
-               printf("perform deferred call_rcu() from worker thread %lu.\n",
-                       time(NULL));
        }
 
        return NULL;
This page took 0.024725 seconds and 4 git commands to generate.