#include <string.h>
#include <errno.h>
#include <poll.h>
+#include <linux/futex.h>
+#include <sys/time.h>
+#include <syscall.h>
+#include <unistd.h>
#include "urcu-defer-static.h"
/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
#include "urcu-defer.h"
+#define futex(...) syscall(__NR_futex, __VA_ARGS__)
+
void __attribute__((destructor)) urcu_defer_exit(void);
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;
+
/*
* Written to only by each individual deferer. Read by both the deferer and
* the reclamation tread.
static int num_deferers, alloc_deferers;
static pthread_t tid_defer;
-static int exit_defer;
+
+/*
+ * Wake-up any waiting defer thread. Called from many concurrent threads.
+ */
+void wake_up_defer(void)
+{
+ if (unlikely(atomic_read(&defer_thread_futex) == -1))
+ atomic_set(&defer_thread_futex, 0);
+ futex(&defer_thread_futex, FUTEX_WAKE,
+ 0, NULL, NULL, 0);
+}
+
+/*
+ * Defer thread waiting. Single thread.
+ */
+static void wait_defer(void)
+{
+ atomic_dec(&defer_thread_futex);
+ if (atomic_read(&defer_thread_futex) == -1)
+ futex(&defer_thread_futex, FUTEX_WAIT, -1,
+ NULL, NULL, 0);
+}
static void internal_urcu_lock(pthread_mutex_t *mutex)
{
perror("Error in pthread mutex lock");
exit(-1);
}
+ pthread_testcancel();
poll(NULL,0,10);
}
#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
static void _rcu_defer_barrier_thread(void)
{
- unsigned long head;
+ unsigned long head, num_items;
head = defer_queue.head;
+ num_items = head - defer_queue.tail;
+ if (unlikely(!num_items))
+ return;
synchronize_rcu();
rcu_defer_barrier_queue(&defer_queue, head);
}
internal_urcu_unlock(&urcu_defer_mutex);
}
+/*
+ * rcu_defer_barrier - Execute all queued rcu callbacks.
+ *
+ * Execute all RCU callbacks queued before rcu_defer_barrier() execution.
+ * All callbacks queued on the local thread prior to a rcu_defer_barrier() call
+ * are guaranteed to be executed.
+ * Callbacks queued by other threads concurrently with rcu_defer_barrier()
+ * execution are not guaranteed to be executed in the current batch (could
+ * be left for the next batch). These callbacks queued by other threads are only
+ * guaranteed to be executed if there is explicit synchronization between
+ * the thread adding to the queue and the thread issuing the defer_barrier call.
+ */
+
void rcu_defer_barrier(void)
{
struct deferer_registry *index;
+ unsigned long num_items = 0;
if (!registry)
return;
internal_urcu_lock(&urcu_defer_mutex);
- for (index = registry; index < registry + num_deferers; index++)
+ for (index = registry; index < registry + num_deferers; index++) {
index->last_head = LOAD_SHARED(index->defer_queue->head);
+ num_items += index->last_head - index->defer_queue->tail;
+ }
+ if (likely(!num_items)) {
+ /*
+ * We skip the grace period because there are no queued
+ * callbacks to execute.
+ */
+ goto end;
+ }
synchronize_rcu();
for (index = registry; index < registry + num_deferers; index++)
rcu_defer_barrier_queue(index->defer_queue,
index->last_head);
+end:
internal_urcu_unlock(&urcu_defer_mutex);
}
void *thr_defer(void *args)
{
for (;;) {
- if (LOAD_SHARED(exit_defer))
- break;
- poll(NULL,0,100); /* wait for 100ms */
+ 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");
rcu_defer_barrier();
+ printf("perform deferred call_rcu() from worker thread %lu.\n",
+ time(NULL));
}
return NULL;
int ret;
void *tret;
- STORE_SHARED(exit_defer, 1);
+ pthread_cancel(tid_defer);
+ wake_up_defer();
ret = pthread_join(tid_defer, &tret);
assert(!ret);
}