Skip to contentSkip to conversation

Fix: wfcqueue nonblocking dequeue

PATCH New Mathieu Desnoyers 3 participants 33 messages Nov 20, 2012 – Dec 6, 2012 16 messages not in the archive Kbuild

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/16 Fix: wfcqueue nonblocking dequeue

Failures were not handled in the nonblocking dequeue implementation.

---
 urcu/static/wfcqueue.h |   11 +++++++++++
 1 file changed, 11 insertions(+)
urcu/static/wfcqueue.h +11 -0
index 8733771..a284b58 100644
@@ -312,6 +312,8 @@ ___cds_wfcq_dequeue(struct cds_wfcq_head *head,
 		return NULL;
 
 	node = ___cds_wfcq_node_sync_next(&head->node, blocking);
+	if (!blocking && node == CDS_WFCQ_WOULDBLOCK)
+		return CDS_WFCQ_WOULDBLOCK;
 
 	if ((next = CMM_LOAD_SHARED(node->next)) == NULL) {
 		/*
@@ -332,6 +334,15 @@ ___cds_wfcq_dequeue(struct cds_wfcq_head *head,
 		if (uatomic_cmpxchg(&tail->p, node, &head->node) == node)
 			return node;
 		next = ___cds_wfcq_node_sync_next(node, blocking);
+		/*
+		 * In nonblocking mode, if we would need to block to
+		 * get node's next, set the head next node pointer
+		 * (currently NULL) back to its original value.
+		 */
+		if (!blocking && next == CDS_WFCQ_WOULDBLOCK) {
+			head->node.next = node;
+			return CDS_WFCQ_WOULDBLOCK;
+		}
 	}
 
 	/*
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH 2/16 wfcqueue: enqueue and splice return queue state

In reply to a message not in the archive

enqueue can return whether the queue was empty or not prior to enqueue.

splice can return this information about destination queue too, but there are more cases to handle, because we don't touch the destination queue if the source queue was empty, and in the nonblocking case, we return that we would need to block on the source queue.

The destination queue state is sampled atomically with enqueue/splice to destination operations.

Knowing this state is useful when "ownership" on a batch of queue items can be assigned to those enqueuing the first items, e.g. to implement wait/wakeup schemes.

CC Paul E. McKenney <[email protected]>
CC Lai Jiangshan <[email protected]>
---
 urcu/static/wfcqueue.h |   47 ++++++++++++++++++++++++++++++++---------------
 urcu/wfcqueue.h        |   28 ++++++++++++++++++++++------
 wfcqueue.c             |   14 +++++++-------
 3 files changed, 61 insertions(+), 28 deletions(-)
urcu/static/wfcqueue.h +32 -15
index a284b58..1200227 100644
@@ -128,7 +128,7 @@ static inline void _cds_wfcq_dequeue_unlock(struct cds_wfcq_head *head,
 	assert(!ret);
 }
 
-static inline void ___cds_wfcq_append(struct cds_wfcq_head *head,
+static inline bool ___cds_wfcq_append(struct cds_wfcq_head *head,
 		struct cds_wfcq_tail *tail,
 		struct cds_wfcq_node *new_head,
 		struct cds_wfcq_node *new_tail)
@@ -152,6 +152,11 @@ static inline void ___cds_wfcq_append(struct cds_wfcq_head *head,
 	 * perspective.
 	 */
 	CMM_STORE_SHARED(old_tail->next, new_head);
+	/*
+	 * Return false if queue was empty prior to adding the node,
+	 * else return true.
+	 */
+	return old_tail != &head->node;
 }
 
 /*
@@ -159,12 +164,15 @@ static inline void ___cds_wfcq_append(struct cds_wfcq_head *head,
  *
  * Issues a full memory barrier before enqueue. No mutual exclusion is
  * required.
+ *
+ * Returns false if the queue was empty prior to adding the node.
+ * Returns true otherwise.
  */
-static inline void _cds_wfcq_enqueue(struct cds_wfcq_head *head,
+static inline bool _cds_wfcq_enqueue(struct cds_wfcq_head *head,
 		struct cds_wfcq_tail *tail,
 		struct cds_wfcq_node *new_tail)
 {
-	___cds_wfcq_append(head, tail, new_tail, new_tail);
+	return ___cds_wfcq_append(head, tail, new_tail, new_tail);
 }
 
 /*
@@ -384,7 +392,7 @@ ___cds_wfcq_dequeue_nonblocking(struct cds_wfcq_head *head,
 	return ___cds_wfcq_dequeue(head, tail, 0);
 }
 
-static inline int
+static inline enum cds_wfcq_ret
 ___cds_wfcq_splice(
 		struct cds_wfcq_head *dest_q_head,
 		struct cds_wfcq_tail *dest_q_tail,
@@ -395,11 +403,11 @@ ___cds_wfcq_splice(
 	struct cds_wfcq_node *head, *tail;
 
 	if (_cds_wfcq_empty(src_q_head, src_q_tail))
-		return 0;
+		return CDS_WFCQ_RET_SRC_EMPTY;
 
 	head = ___cds_wfcq_node_sync_next(&src_q_head->node, blocking);
 	if (head == CDS_WFCQ_WOULDBLOCK)
-		return -1;
+		return CDS_WFCQ_RET_WOULDBLOCK;
 	_cds_wfcq_node_init(&src_q_head->node);
 
 	/*
@@ -414,8 +422,10 @@ ___cds_wfcq_splice(
 	 * Append the spliced content of src_q into dest_q. Does not
 	 * require mutual exclusion on dest_q (wait-free).
 	 */
-	___cds_wfcq_append(dest_q_head, dest_q_tail, head, tail);
-	return 0;
+	if (___cds_wfcq_append(dest_q_head, dest_q_tail, head, tail))
+		return CDS_WFCQ_RET_DEST_NON_EMPTY;
+	else
+		return CDS_WFCQ_RET_DEST_EMPTY;
 }
 
 
@@ -426,25 +436,27 @@ ___cds_wfcq_splice(
  * dest_q must be already initialized.
  * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
  * by the caller.
+ * Returns enum cds_wfcq_ret which indicates the state of the src or
+ * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
-static inline void
+static inline enum cds_wfcq_ret
 ___cds_wfcq_splice_blocking(
 		struct cds_wfcq_head *dest_q_head,
 		struct cds_wfcq_tail *dest_q_tail,
 		struct cds_wfcq_head *src_q_head,
 		struct cds_wfcq_tail *src_q_tail)
 {
-	(void) ___cds_wfcq_splice(dest_q_head, dest_q_tail,
+	return ___cds_wfcq_splice(dest_q_head, dest_q_tail,
 		src_q_head, src_q_tail, 1);
 }
 
 /*
  * __cds_wfcq_splice_nonblocking: enqueue all src_q nodes at the end of dest_q.
  *
- * Same as __cds_wfcq_splice_blocking, but returns nonzero if it needs to
- * block.
+ * Same as __cds_wfcq_splice_blocking, but returns
+ * CDS_WFCQ_RET_WOULDBLOCK if it needs to block.
  */
-static inline int
+static inline enum cds_wfcq_ret
 ___cds_wfcq_splice_nonblocking(
 		struct cds_wfcq_head *dest_q_head,
 		struct cds_wfcq_tail *dest_q_tail,
@@ -485,18 +497,23 @@ _cds_wfcq_dequeue_blocking(struct cds_wfcq_head *head,
  * consistent, but no other memory ordering is ensured.
  * Mutual exlusion with cds_wfcq_dequeue_blocking and dequeue lock is
  * ensured.
+ * Returns enum cds_wfcq_ret which indicates the state of the src or
+ * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
-static inline void
+static inline enum cds_wfcq_ret
 _cds_wfcq_splice_blocking(
 		struct cds_wfcq_head *dest_q_head,
 		struct cds_wfcq_tail *dest_q_tail,
 		struct cds_wfcq_head *src_q_head,
 		struct cds_wfcq_tail *src_q_tail)
 {
+	enum cds_wfcq_ret ret;
+
 	_cds_wfcq_dequeue_lock(src_q_head, src_q_tail);
-	___cds_wfcq_splice_blocking(dest_q_head, dest_q_tail,
+	ret = ___cds_wfcq_splice_blocking(dest_q_head, dest_q_tail,
 			src_q_head, src_q_tail);
 	_cds_wfcq_dequeue_unlock(src_q_head, src_q_tail);
+	return ret;
 }
 
 #ifdef __cplusplus
urcu/wfcqueue.h +22 -6
index fe2862e..ddf6b87 100644
@@ -45,6 +45,13 @@ extern "C" {
 
 #define CDS_WFCQ_WOULDBLOCK	((void *) -1UL)
 
+enum cds_wfcq_ret {
+	CDS_WFCQ_RET_WOULDBLOCK = 	-1,
+	CDS_WFCQ_RET_DEST_EMPTY =	0,
+	CDS_WFCQ_RET_DEST_NON_EMPTY =	1,
+	CDS_WFCQ_RET_SRC_EMPTY = 	2,
+};
+
 struct cds_wfcq_node {
 	struct cds_wfcq_node *next;
 };
@@ -156,8 +163,11 @@ extern void cds_wfcq_dequeue_unlock(struct cds_wfcq_head *head,
  *
  * Issues a full memory barrier before enqueue. No mutual exclusion is
  * required.
+ *
+ * Returns false if the queue was empty prior to adding the node.
+ * Returns true otherwise.
  */
-extern void cds_wfcq_enqueue(struct cds_wfcq_head *head,
+extern bool cds_wfcq_enqueue(struct cds_wfcq_head *head,
 		struct cds_wfcq_tail *tail,
 		struct cds_wfcq_node *node);
 
@@ -183,8 +193,11 @@ extern struct cds_wfcq_node *cds_wfcq_dequeue_blocking(
  * consistent, but no other memory ordering is ensured.
  * Mutual exlusion with cds_wfcq_dequeue_blocking and dequeue lock is
  * ensured.
+ *
+ * Returns enum cds_wfcq_ret which indicates the state of the src or
+ * dest queue. Cannot block.
  */
-extern void cds_wfcq_splice_blocking(
+extern enum cds_wfcq_ret cds_wfcq_splice_blocking(
 		struct cds_wfcq_head *dest_q_head,
 		struct cds_wfcq_tail *dest_q_tail,
 		struct cds_wfcq_head *src_q_head,
@@ -222,8 +235,11 @@ extern struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking(
  * consistent, but no other memory ordering is ensured.
  * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
  * by the caller.
+ *
+ * Returns enum cds_wfcq_ret which indicates the state of the src or
+ * dest queue. Cannot block.
  */
-extern void __cds_wfcq_splice_blocking(
+extern enum cds_wfcq_ret __cds_wfcq_splice_blocking(
 		struct cds_wfcq_head *dest_q_head,
 		struct cds_wfcq_tail *dest_q_tail,
 		struct cds_wfcq_head *src_q_head,
@@ -232,10 +248,10 @@ extern void __cds_wfcq_splice_blocking(
 /*
  * __cds_wfcq_splice_nonblocking: enqueue all src_q nodes at the end of dest_q.
  *
- * Same as __cds_wfcq_splice_blocking, but returns nonzero if it needs to
- * block.
+ * Same as __cds_wfcq_splice_blocking, but returns
+ * CDS_WFCQ_RET_WOULDBLOCK if it needs to block.
  */
-extern int __cds_wfcq_splice_nonblocking(
+extern enum cds_wfcq_ret __cds_wfcq_splice_nonblocking(
 		struct cds_wfcq_head *dest_q_head,
 		struct cds_wfcq_tail *dest_q_tail,
 		struct cds_wfcq_head *src_q_head,
wfcqueue.c +7 -7
index 90b810e..207df95 100644
@@ -47,11 +47,11 @@ bool cds_wfcq_empty(struct cds_wfcq_head *head,
 	return _cds_wfcq_empty(head, tail);
 }
 
-void cds_wfcq_enqueue(struct cds_wfcq_head *head,
+bool cds_wfcq_enqueue(struct cds_wfcq_head *head,
 		struct cds_wfcq_tail *tail,
 		struct cds_wfcq_node *node)
 {
-	_cds_wfcq_enqueue(head, tail, node);
+	return _cds_wfcq_enqueue(head, tail, node);
 }
 
 void cds_wfcq_dequeue_lock(struct cds_wfcq_head *head,
@@ -73,13 +73,13 @@ struct cds_wfcq_node *cds_wfcq_dequeue_blocking(
 	return _cds_wfcq_dequeue_blocking(head, tail);
 }
 
-void cds_wfcq_splice_blocking(
+enum cds_wfcq_ret cds_wfcq_splice_blocking(
 		struct cds_wfcq_head *dest_q_head,
 		struct cds_wfcq_tail *dest_q_tail,
 		struct cds_wfcq_head *src_q_head,
 		struct cds_wfcq_tail *src_q_tail)
 {
-	_cds_wfcq_splice_blocking(dest_q_head, dest_q_tail,
+	return _cds_wfcq_splice_blocking(dest_q_head, dest_q_tail,
 				src_q_head, src_q_tail);
 }
 
@@ -97,17 +97,17 @@ struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking(
 	return ___cds_wfcq_dequeue_nonblocking(head, tail);
 }
 
-void __cds_wfcq_splice_blocking(
+enum cds_wfcq_ret __cds_wfcq_splice_blocking(
 		struct cds_wfcq_head *dest_q_head,
 		struct cds_wfcq_tail *dest_q_tail,
 		struct cds_wfcq_head *src_q_head,
 		struct cds_wfcq_tail *src_q_tail)
 {
-	___cds_wfcq_splice_blocking(dest_q_head, dest_q_tail,
+	return ___cds_wfcq_splice_blocking(dest_q_head, dest_q_tail,
 				src_q_head, src_q_tail);
 }
 
-int __cds_wfcq_splice_nonblocking(
+enum cds_wfcq_ret __cds_wfcq_splice_nonblocking(
 		struct cds_wfcq_head *dest_q_head,
 		struct cds_wfcq_tail *dest_q_tail,
 		struct cds_wfcq_head *src_q_head,
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH 3/16 test wfcqueue: add tests for queue state return value

In reply to a message not in the archive

with e.g. ./test_urcu_wfcq 2 2 10 -w

we can confirm that we see as many "enqueue to empty queue" as we see "splice from non-empty queue", which confirms that the queue state returned by enqueue is indeed sampled atomically with enqueue.

CC Paul E. McKenney <[email protected]>
CC Lai Jiangshan <[email protected]>
---
 tests/test_urcu_wfcq.c |  131 +++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 103 insertions(+), 28 deletions(-)
tests/test_urcu_wfcq.c +103 -28
index f90fc14..ccd5cf1 100644
@@ -75,7 +75,7 @@ enum test_sync {
 
 static enum test_sync test_sync;
 
-static volatile int test_go, test_stop;
+static volatile int test_go, test_stop_enqueue, test_stop_dequeue;
 
 static unsigned long rduration;
 
@@ -92,7 +92,8 @@ static inline void loop_sleep(unsigned long loops)
 
 static int verbose_mode;
 
-static int test_dequeue, test_splice;
+static int test_dequeue, test_splice, test_wait_empty;
+static int test_enqueue_stopped;
 
 #define printf_verbose(fmt, args...)		\
 	do {					\
@@ -150,12 +151,12 @@ static void set_affinity(void)
  */
 static int test_duration_dequeue(void)
 {
-	return !test_stop;
+	return !test_stop_dequeue;
 }
 
 static int test_duration_enqueue(void)
 {
-	return !test_stop;
+	return !test_stop_enqueue;
 }
 
 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
@@ -163,6 +164,8 @@ static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
 
 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
+static DEFINE_URCU_TLS(unsigned long long, nr_empty_dest_enqueues);
+static DEFINE_URCU_TLS(unsigned long long, nr_splice);
 
 static unsigned int nr_enqueuers;
 static unsigned int nr_dequeuers;
@@ -173,6 +176,7 @@ static struct cds_wfcq_tail __attribute__((aligned(CAA_CACHE_LINE_SIZE))) tail;
 static void *thr_enqueuer(void *_count)
 {
 	unsigned long long *count = _count;
+	bool was_nonempty;
 
 	printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
 			"enqueuer", (unsigned long) pthread_self(),
@@ -190,8 +194,10 @@ static void *thr_enqueuer(void *_count)
 		if (!node)
 			goto fail;
 		cds_wfcq_node_init(node);
-		cds_wfcq_enqueue(&head, &tail, node);
+		was_nonempty = cds_wfcq_enqueue(&head, &tail, node);
 		URCU_TLS(nr_successful_enqueues)++;
+		if (!was_nonempty)
+			URCU_TLS(nr_empty_dest_enqueues)++;
 
 		if (caa_unlikely(wdelay))
 			loop_sleep(wdelay);
@@ -201,13 +207,18 @@ fail:
 			break;
 	}
 
+	uatomic_inc(&test_enqueue_stopped);
 	count[0] = URCU_TLS(nr_enqueues);
 	count[1] = URCU_TLS(nr_successful_enqueues);
+	count[2] = URCU_TLS(nr_empty_dest_enqueues);
 	printf_verbose("enqueuer thread_end, thread id : %lx, tid %lu, "
-		       "enqueues %llu successful_enqueues %llu\n",
+		       "enqueues %llu successful_enqueues %llu, "
+		       "empty_dest_enqueues %llu\n",
 		       pthread_self(),
 			(unsigned long) gettid(),
-		       URCU_TLS(nr_enqueues), URCU_TLS(nr_successful_enqueues));
+		       URCU_TLS(nr_enqueues),
+		       URCU_TLS(nr_successful_enqueues),
+		       URCU_TLS(nr_empty_dest_enqueues));
 	return ((void*)1);
 
 }
@@ -233,16 +244,33 @@ static void do_test_splice(enum test_sync sync)
 	struct cds_wfcq_head tmp_head;
 	struct cds_wfcq_tail tmp_tail;
 	struct cds_wfcq_node *node, *n;
+	enum cds_wfcq_ret ret;
 
 	cds_wfcq_init(&tmp_head, &tmp_tail);
 
 	if (sync == TEST_SYNC_MUTEX)
-		cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
+		ret = cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
 			&head, &tail);
 	else
-		__cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
+		ret = __cds_wfcq_splice_blocking(&tmp_head, &tmp_tail,
 			&head, &tail);
 
+	switch (ret) {
+	case CDS_WFCQ_RET_WOULDBLOCK:
+		assert(0);	/* blocking call */
+		break;
+	case CDS_WFCQ_RET_DEST_EMPTY:
+		URCU_TLS(nr_splice)++;
+		/* ok */
+		break;
+	case CDS_WFCQ_RET_DEST_NON_EMPTY:
+		assert(0);	/* entirely unexpected */
+		break;
+	case CDS_WFCQ_RET_SRC_EMPTY:
+		/* ok, we could even skip iteration on dest if we wanted */
+		break;
+	}
+
 	__cds_wfcq_for_each_blocking_safe(&tmp_head, &tmp_tail, node, n) {
 		free(node);
 		URCU_TLS(nr_successful_dequeues)++;
@@ -286,12 +314,15 @@ static void *thr_dequeuer(void *_count)
 	}
 
 	printf_verbose("dequeuer thread_end, thread id : %lx, tid %lu, "
-		       "dequeues %llu, successful_dequeues %llu\n",
+		       "dequeues %llu, successful_dequeues %llu, "
+		       "nr_splice %llu\n",
 		       pthread_self(),
 			(unsigned long) gettid(),
-		       URCU_TLS(nr_dequeues), URCU_TLS(nr_successful_dequeues));
+		       URCU_TLS(nr_dequeues), URCU_TLS(nr_successful_dequeues),
+		       URCU_TLS(nr_splice));
 	count[0] = URCU_TLS(nr_dequeues);
 	count[1] = URCU_TLS(nr_successful_dequeues);
+	count[2] = URCU_TLS(nr_splice);
 	return ((void*)2);
 }
 
@@ -320,6 +351,7 @@ static void show_usage(int argc, char **argv)
 	printf(" [-M] (use mutex external synchronization)");
 	printf(" [-0] (use no external synchronization)");
 	printf("      Note: default: mutex external synchronization used.");
+	printf(" [-w] Wait for dequeuer to empty queue");
 	printf("\n");
 }
 
@@ -331,9 +363,11 @@ int main(int argc, char **argv)
 	unsigned long long *count_enqueuer, *count_dequeuer;
 	unsigned long long tot_enqueues = 0, tot_dequeues = 0;
 	unsigned long long tot_successful_enqueues = 0,
-			   tot_successful_dequeues = 0;
+			   tot_successful_dequeues = 0,
+			   tot_empty_dest_enqueues = 0,
+			   tot_splice = 0;
 	unsigned long long end_dequeues = 0;
-	int i, a;
+	int i, a, retval = 0;
 
 	if (argc < 4) {
 		show_usage(argc, argv);
@@ -401,6 +435,9 @@ int main(int argc, char **argv)
 		case '0':
 			test_sync = TEST_SYNC_NONE;
 			break;
+		case 'w':
+			test_wait_empty = 1;
+			break;
 		}
 	}
 
@@ -419,6 +456,8 @@ int main(int argc, char **argv)
 		printf_verbose("External sync: mutex.\n");
 	else
 		printf_verbose("External sync: none.\n");
+	if (test_wait_empty)
+		printf_verbose("Wait for dequeuers to empty queue.\n");
 	printf_verbose("Writer delay : %lu loops.\n", rduration);
 	printf_verbose("Reader duration : %lu loops.\n", wdelay);
 	printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
@@ -427,21 +466,21 @@ int main(int argc, char **argv)
 
 	tid_enqueuer = malloc(sizeof(*tid_enqueuer) * nr_enqueuers);
 	tid_dequeuer = malloc(sizeof(*tid_dequeuer) * nr_dequeuers);
-	count_enqueuer = malloc(2 * sizeof(*count_enqueuer) * nr_enqueuers);
-	count_dequeuer = malloc(2 * sizeof(*count_dequeuer) * nr_dequeuers);
+	count_enqueuer = malloc(3 * sizeof(*count_enqueuer) * nr_enqueuers);
+	count_dequeuer = malloc(3 * sizeof(*count_dequeuer) * nr_dequeuers);
 	cds_wfcq_init(&head, &tail);
 
 	next_aff = 0;
 
 	for (i = 0; i < nr_enqueuers; i++) {
 		err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
-				     &count_enqueuer[2 * i]);
+				     &count_enqueuer[3 * i]);
 		if (err != 0)
 			exit(1);
 	}
 	for (i = 0; i < nr_dequeuers; i++) {
 		err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
-				     &count_dequeuer[2 * i]);
+				     &count_dequeuer[3 * i]);
 		if (err != 0)
 			exit(1);
 	}
@@ -456,21 +495,34 @@ int main(int argc, char **argv)
 			write (1, ".", 1);
 	}
 
-	test_stop = 1;
+	test_stop_enqueue = 1;
+
+	if (test_wait_empty) {
+		while (nr_enqueuers != uatomic_read(&test_enqueue_stopped)) {
+			sleep(1);
+		}
+		while (!cds_wfcq_empty(&head, &tail)) {
+			sleep(1);
+		}
+	}
+
+	test_stop_dequeue = 1;
 
 	for (i = 0; i < nr_enqueuers; i++) {
 		err = pthread_join(tid_enqueuer[i], &tret);
 		if (err != 0)
 			exit(1);
-		tot_enqueues += count_enqueuer[2 * i];
-		tot_successful_enqueues += count_enqueuer[2 * i + 1];
+		tot_enqueues += count_enqueuer[3 * i];
+		tot_successful_enqueues += count_enqueuer[3 * i + 1];
+		tot_empty_dest_enqueues += count_enqueuer[3 * i + 2];
 	}
 	for (i = 0; i < nr_dequeuers; i++) {
 		err = pthread_join(tid_dequeuer[i], &tret);
 		if (err != 0)
 			exit(1);
-		tot_dequeues += count_dequeuer[2 * i];
-		tot_successful_dequeues += count_dequeuer[2 * i + 1];
+		tot_dequeues += count_dequeuer[3 * i];
+		tot_successful_dequeues += count_dequeuer[3 * i + 1];
+		tot_splice += count_dequeuer[3 * i + 2];
 	}
 	
 	test_end(&end_dequeues);
@@ -478,27 +530,50 @@ int main(int argc, char **argv)
 	printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
 		       tot_enqueues, tot_dequeues);
 	printf_verbose("total number of successful enqueues : %llu, "
-		       "successful dequeues %llu\n",
-		       tot_successful_enqueues, tot_successful_dequeues);
+		       "enqueues to empty dest : %llu, "
+		       "successful dequeues %llu, ",
+		       "splice : %llu\n",
+		       tot_successful_enqueues,
+		       tot_empty_dest_enqueues,
+		       tot_successful_dequeues,
+		       tot_splice);
 	printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
 		"nr_dequeuers %3u "
 		"rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
-		"successful enqueues %12llu successful dequeues %12llu "
+		"successful enqueues %12llu enqueues to empty dest %12llu "
+		"successful dequeues %12llu splice %12llu "
 		"end_dequeues %llu nr_ops %12llu\n",
 		argv[0], duration, nr_enqueuers, wdelay,
 		nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
 		tot_successful_enqueues,
-		tot_successful_dequeues, end_dequeues,
+		tot_empty_dest_enqueues,
+		tot_successful_dequeues, tot_splice, end_dequeues,
 		tot_enqueues + tot_dequeues);
-	if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues)
+
+	if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues) {
 		printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
 		       "succ. dequeues + end dequeues %llu.\n",
 		       tot_successful_enqueues,
 		       tot_successful_dequeues + end_dequeues);
+		retval = 1;
+	}
 
+	/*
+	 * If only using splice to dequeue, the enqueuer should see
+	 * exactly as many empty queues than the number of non-empty
+	 * src splice.
+	 */
+	if (test_wait_empty && test_splice && !test_dequeue
+			&& tot_empty_dest_enqueues != tot_splice) {
+		printf("WARNING! Discrepancy between empty enqueue (%llu) and "
+			"number of non-empty splice (%llu)\n",
+			tot_empty_dest_enqueues,
+			tot_splice);
+		retval = 1;
+	}
 	free(count_enqueuer);
 	free(count_dequeuer);
 	free(tid_enqueuer);
 	free(tid_dequeuer);
-	return 0;
+	return retval;
 }
Signature · 1 line
1.7.10.4
#4 lore raw

PATCH 4/16 urcu-call-rcu: use wait-free splice return value

In reply to a message not in the archive

We can now use the splice return value to know if the source queue was empty rather than testing for destination queue emptiness after the splice operation.

---
 urcu-call-rcu-impl.h |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
urcu-call-rcu-impl.h +6 -3
index 5dfdb92..6580397 100644
@@ -252,11 +252,14 @@ static void *call_rcu_thread(void *arg)
 		struct cds_wfcq_head cbs_tmp_head;
 		struct cds_wfcq_tail cbs_tmp_tail;
 		struct cds_wfcq_node *cbs, *cbs_tmp_n;
+		enum cds_wfcq_ret splice_ret;
 
 		cds_wfcq_init(&cbs_tmp_head, &cbs_tmp_tail);
-		__cds_wfcq_splice_blocking(&cbs_tmp_head, &cbs_tmp_tail,
-			&crdp->cbs_head, &crdp->cbs_tail);
-		if (!cds_wfcq_empty(&cbs_tmp_head, &cbs_tmp_tail)) {
+		splice_ret = __cds_wfcq_splice_blocking(&cbs_tmp_head,
+			&cbs_tmp_tail, &crdp->cbs_head, &crdp->cbs_tail);
+		assert(splice_ret != CDS_WFCQ_RET_WOULDBLOCK);
+		assert(splice_ret != CDS_WFCQ_RET_DEST_NON_EMPTY);
+		if (splice_ret != CDS_WFCQ_RET_SRC_EMPTY) {
 			synchronize_rcu();
 			cbcount = 0;
 			__cds_wfcq_for_each_blocking_safe(&cbs_tmp_head,
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH 5/16 wfcqueue: document empty criterion

In reply to a message not in the archive
---
 urcu/static/wfcqueue.h |    7 +++++++
 1 file changed, 7 insertions(+)
urcu/static/wfcqueue.h +7 -0
index 1200227..8774c03 100644
@@ -95,6 +95,13 @@ static inline void _cds_wfcq_init(struct cds_wfcq_head *head,
  * cds_wfcq_empty: return whether wait-free queue is empty.
  *
  * No memory barrier is issued. No mutual exclusion is required.
+ *
+ * We perform the test on head->node.next to check if the queue is
+ * possibly empty, but we confirm this by checking if the tail pointer
+ * points to the head node because the tail pointer is the linearisation
+ * point of the enqueuers. Just checking the head next pointer could
+ * make a queue appear empty if an enqueuer is preempted for a long time
+ * between xchg() and setting the previous node's next pointer.
  */
 static inline bool _cds_wfcq_empty(struct cds_wfcq_head *head,
 		struct cds_wfcq_tail *tail)
Signature · 1 line
1.7.10.4
#6 lore raw

PATCH 6/16 wfcqueue: implement mutex-free splice

In reply to a message not in the archive

A carefully crafted splice operation does not need to use an external mutex to synchronize against other splice operations.

The trick is atomically exchange the head next pointer with NULL. If the pointer we replaced was NULL, it means the queue was possibly empty. If head next was not NULL, by setting head to NULL, we ensure that concurrent splice operations are going to see an empty queue, even if concurrent enqueue operations move tail further. This means that as long as we are within splice, after setting head to NULL, but before moving tail back to head, concurrent splice operations will always see an empty queue, therefore acting as mutual exclusion.

If exchange returns a NULL head, we confirm that it was indeed empty by checking if the tail pointer points to the head node, busy-waiting if necessary.

Then the last step is to move the tail pointer to head. At that point, enqueuers are going to start enqueuing at head again, and other splice operations will be able to proceed.

---
 urcu/static/wfcqueue.h |   68 ++++++++++++++++++++++++++++++++++++------------
 urcu/wfcqueue.h        |   40 ++++++++++++++++++----------
 wfcqueue.c             |    2 +-
 3 files changed, 79 insertions(+), 31 deletions(-)
urcu/static/wfcqueue.h +52 -16
index 8774c03..4b2de50 100644
@@ -46,15 +46,30 @@ extern "C" {
  * half-wait-free/half-blocking queue implementation done by Paul E.
  * McKenney.
  *
- * Mutual exclusion of __cds_wfcq_* API
- *
- * Unless otherwise stated, the caller must ensure mutual exclusion of
- * queue update operations "dequeue" and "splice" (for source queue).
- * Queue read operations "first" and "next", which are used by
- * "for_each" iterations, need to be protected against concurrent
- * "dequeue" and "splice" (for source queue) by the caller.
- * "enqueue", "splice" (for destination queue), and "empty" are the only
- * operations that can be used without any mutual exclusion.
+ * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
+ *
+ * Synchronization table:
+ *
+ * External synchronization techniques described in the API below is
+ * required between pairs marked with "X". No external synchronization
+ * required between pairs marked with "-".
+ *
+ * Legend:
+ * [1] cds_wfcq_enqueue
+ * [2] __cds_wfcq_splice (destination queue)
+ * [3] __cds_wfcq_dequeue
+ * [4] __cds_wfcq_splice (source queue)
+ * [5] __cds_wfcq_first
+ * [6] __cds_wfcq_next
+ *
+ *     [1] [2] [3] [4] [5] [6]
+ * [1]  -   -   -   -   -   -
+ * [2]  -   -   -   -   -   -
+ * [3]  -   -   X   X   X   X
+ * [4]  -   -   X   -   X   X
+ * [5]  -   -   X   X   -   -
+ * [6]  -   -   X   X   -   -
+ *
  * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
  *
  * For convenience, cds_wfcq_dequeue_blocking() and
@@ -399,6 +414,16 @@ ___cds_wfcq_dequeue_nonblocking(struct cds_wfcq_head *head,
 	return ___cds_wfcq_dequeue(head, tail, 0);
 }
 
+/*
+ * __cds_wfcq_splice: enqueue all src_q nodes at the end of dest_q.
+ *
+ * Dequeue all nodes from src_q.
+ * dest_q must be already initialized.
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
+ * Returns enum cds_wfcq_ret which indicates the state of the src or
+ * dest queue.
+ */
 static inline enum cds_wfcq_ret
 ___cds_wfcq_splice(
 		struct cds_wfcq_head *dest_q_head,
@@ -408,14 +433,26 @@ ___cds_wfcq_splice(
 		int blocking)
 {
 	struct cds_wfcq_node *head, *tail;
+	int attempt = 0;
 
 	if (_cds_wfcq_empty(src_q_head, src_q_tail))
 		return CDS_WFCQ_RET_SRC_EMPTY;
 
-	head = ___cds_wfcq_node_sync_next(&src_q_head->node, blocking);
-	if (head == CDS_WFCQ_WOULDBLOCK)
-		return CDS_WFCQ_RET_WOULDBLOCK;
-	_cds_wfcq_node_init(&src_q_head->node);
+	for (;;) {
+		head = uatomic_xchg(&src_q_head->node.next, NULL);
+		if (head)
+			break;	/* non-empty */
+		if (CMM_LOAD_SHARED(src_q_tail->p) == &src_q_head->node)
+			return CDS_WFCQ_RET_SRC_EMPTY;
+		if (!blocking)
+			return CDS_WFCQ_RET_WOULDBLOCK;
+		if (++attempt >= WFCQ_ADAPT_ATTEMPTS) {
+			poll(NULL, 0, WFCQ_WAIT);	/* Wait for 10ms */
+			attempt = 0;
+		} else {
+			caa_cpu_relax();
+		}
+	}
 
 	/*
 	 * Memory barrier implied before uatomic_xchg() orders store to
@@ -435,14 +472,13 @@ ___cds_wfcq_splice(
 		return CDS_WFCQ_RET_DEST_EMPTY;
 }
 
-
 /*
  * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
  *
  * Dequeue all nodes from src_q.
  * dest_q must be already initialized.
- * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
- * by the caller.
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
  * Returns enum cds_wfcq_ret which indicates the state of the src or
  * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
urcu/wfcqueue.h +26 -14
index ddf6b87..d9ec534 100644
@@ -46,7 +46,7 @@ extern "C" {
 #define CDS_WFCQ_WOULDBLOCK	((void *) -1UL)
 
 enum cds_wfcq_ret {
-	CDS_WFCQ_RET_WOULDBLOCK = 	-1,
+	CDS_WFCQ_RET_WOULDBLOCK =	-1,
 	CDS_WFCQ_RET_DEST_EMPTY =	0,
 	CDS_WFCQ_RET_DEST_NON_EMPTY =	1,
 	CDS_WFCQ_RET_SRC_EMPTY = 	2,
@@ -110,13 +110,28 @@ struct cds_wfcq_tail {
 /*
  * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
  *
- * Unless otherwise stated, the caller must ensure mutual exclusion of
- * queue update operations "dequeue" and "splice" (for source queue).
- * Queue read operations "first" and "next", which are used by
- * "for_each" iterations, need to be protected against concurrent
- * "dequeue" and "splice" (for source queue) by the caller.
- * "enqueue", "splice" (for destination queue), and "empty" are the only
- * operations that can be used without any mutual exclusion.
+ * Synchronization table:
+ *
+ * External synchronization techniques described in the API below is
+ * required between pairs marked with "X". No external synchronization
+ * required between pairs marked with "-".
+ *
+ * Legend:
+ * [1] cds_wfcq_enqueue
+ * [2] __cds_wfcq_splice (destination queue)
+ * [3] __cds_wfcq_dequeue
+ * [4] __cds_wfcq_splice (source queue)
+ * [5] __cds_wfcq_first
+ * [6] __cds_wfcq_next
+ *
+ *     [1] [2] [3] [4] [5] [6]
+ * [1]  -   -   -   -   -   -
+ * [2]  -   -   -   -   -   -
+ * [3]  -   -   X   X   X   X
+ * [4]  -   -   X   -   X   X
+ * [5]  -   -   X   X   -   -
+ * [6]  -   -   X   X   -   -
+ *
  * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
  *
  * For convenience, cds_wfcq_dequeue_blocking() and
@@ -231,13 +246,10 @@ extern struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking(
  *
  * Dequeue all nodes from src_q.
  * dest_q must be already initialized.
- * Content written into the node before enqueue is guaranteed to be
- * consistent, but no other memory ordering is ensured.
- * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
- * by the caller.
- *
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
  * Returns enum cds_wfcq_ret which indicates the state of the src or
- * dest queue. Cannot block.
+ * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
 extern enum cds_wfcq_ret __cds_wfcq_splice_blocking(
 		struct cds_wfcq_head *dest_q_head,
wfcqueue.c +1 -1
index 207df95..ab0eb93 100644
@@ -1,7 +1,7 @@
 /*
  * wfcqueue.c
  *
- * Userspace RCU library - Concurrent queue with Wait-Free Enqueue/Blocking Dequeue
+ * Userspace RCU library - Concurrent Queue with Wait-Free Enqueue/Blocking Dequeue
  *
  * Copyright 2010-2012 - Mathieu Desnoyers <[email protected]>
  * Copyright 2011-2012 - Lai Jiangshan <[email protected]>
Signature · 1 line
1.7.10.4
#7 lore raw

PATCH 7/16 urcu-qsbr: improve 2-phase wait scheme

In reply to a message not in the archive

In the single-bit, 2-phase grace period scheme, all we need to do is to observe each reader going through a quiescent state while we are in the grace period.

We therefore only need to perform one global counter update, surrounded by 2 iterations on readers to observe change in their snapshot.

We can therefore remove the first counter update (prior to the first iteration on readers): it was useless and was only slowing down the grace period.

---
 urcu-qsbr.c |   92 ++++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 56 insertions(+), 36 deletions(-)
urcu-qsbr.c +56 -36
index c6a1b18..eb167d1 100644
@@ -116,38 +116,16 @@ static void wait_gp(void)
 		      NULL, NULL, 0);
 }
 
-static void update_counter_and_wait(void)
+static void wait_for_readers(void)
 {
 	CDS_LIST_HEAD(qsreaders);
 	int wait_loops = 0;
 	struct rcu_reader *index, *tmp;
 
-#if (CAA_BITS_PER_LONG < 64)
-	/* Switch parity: 0 -> 1, 1 -> 0 */
-	CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR);
-#else	/* !(CAA_BITS_PER_LONG < 64) */
-	/* Increment current G.P. */
-	CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr + RCU_GP_CTR);
-#endif	/* !(CAA_BITS_PER_LONG < 64) */
-
-	/*
-	 * Must commit rcu_gp_ctr update to memory before waiting for
-	 * quiescent state. Failure to do so could result in the writer
-	 * waiting forever while new readers are always accessing data
-	 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
-	 * before load URCU_TLS(rcu_reader).ctr.
-	 */
-	cmm_barrier();
-
-	/*
-	 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
-	 * model easier to understand. It does not have a big performance impact
-	 * anyway, given this is the write-side.
-	 */
-	cmm_smp_mb();
-
 	/*
-	 * Wait for each thread rcu_reader_qs_gp count to become 0.
+	 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
+	 * indicate quiescence (offline), or for them to observe the
+	 * current rcu_gp_ctr value.
 	 */
 	for (;;) {
 		wait_loops++;
@@ -223,17 +201,17 @@ void synchronize_rcu(void)
 		goto out;
 
 	/*
-	 * Wait for previous parity to be empty of readers.
+	 * Wait for readers to observe original parity or be quiescent.
 	 */
-	update_counter_and_wait();	/* 0 -> 1, wait readers in parity 0 */
+	wait_for_readers();
 
 	/*
-	 * Must finish waiting for quiescent state for parity 0 before
-	 * committing next rcu_gp_ctr update to memory. Failure to
-	 * do so could result in the writer waiting forever while new
+	 * Must finish waiting for quiescent state for original parity
+	 * before committing next rcu_gp_ctr update to memory. Failure
+	 * to do so could result in the writer waiting forever while new
 	 * readers are always accessing data (no progress).  Enforce
-	 * compiler-order of load URCU_TLS(rcu_reader).ctr before store to
-	 * rcu_gp_ctr.
+	 * compiler-order of load URCU_TLS(rcu_reader).ctr before store
+	 * to rcu_gp_ctr.
 	 */
 	cmm_barrier();
 
@@ -244,10 +222,29 @@ void synchronize_rcu(void)
 	 */
 	cmm_smp_mb();
 
+	/* Switch parity: 0 -> 1, 1 -> 0 */
+	CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR);
+
 	/*
-	 * Wait for previous parity to be empty of readers.
+	 * Must commit rcu_gp_ctr update to memory before waiting for
+	 * quiescent state. Failure to do so could result in the writer
+	 * waiting forever while new readers are always accessing data
+	 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
+	 * before load URCU_TLS(rcu_reader).ctr.
 	 */
-	update_counter_and_wait();	/* 1 -> 0, wait readers in parity 1 */
+	cmm_barrier();
+
+	/*
+	 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
+	 * model easier to understand. It does not have a big performance impact
+	 * anyway, given this is the write-side.
+	 */
+	cmm_smp_mb();
+
+	/*
+	 * Wait for readers to observe new parity or be quiescent.
+	 */
+	wait_for_readers();
 out:
 	mutex_unlock(&rcu_gp_lock);
 
@@ -280,7 +277,30 @@ void synchronize_rcu(void)
 	mutex_lock(&rcu_gp_lock);
 	if (cds_list_empty(&registry))
 		goto out;
-	update_counter_and_wait();
+
+	/* Increment current G.P. */
+	CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr + RCU_GP_CTR);
+
+	/*
+	 * Must commit rcu_gp_ctr update to memory before waiting for
+	 * quiescent state. Failure to do so could result in the writer
+	 * waiting forever while new readers are always accessing data
+	 * (no progress). Enforce compiler-order of store to rcu_gp_ctr
+	 * before load URCU_TLS(rcu_reader).ctr.
+	 */
+	cmm_barrier();
+
+	/*
+	 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
+	 * model easier to understand. It does not have a big performance impact
+	 * anyway, given this is the write-side.
+	 */
+	cmm_smp_mb();
+
+	/*
+	 * Wait for readers to observe new count of be quiescent.
+	 */
+	wait_for_readers();
 out:
 	mutex_unlock(&rcu_gp_lock);
 
Signature · 1 line
1.7.10.4
#8 lore raw

PATCH 8/16 urcu-mb/signal/membarrier: improve 2-phase wait scheme

In reply to a message not in the archive

In the single-bit, 2-phase grace period scheme, all we need to do is to observe each reader going through a quiescent state while we are in the grace period.

We therefore only need to perform one global counter update, surrounded by 2 iterations on readers to observe change in their snapshot.

We can therefore remove the first counter update (prior to the first iteration on readers): it was useless and was only slowing down the grace period.

CC Paul E. McKenney <[email protected]>
CC Lai Jiangshan <[email protected]>
CC Alan Stern <[email protected]>
---
 urcu.c |   54 ++++++++++++++++++++++++++++--------------------------
 1 file changed, 28 insertions(+), 26 deletions(-)
urcu.c +28 -26
index c421846..2d5c510 100644
@@ -215,33 +215,16 @@ static void wait_gp(void)
 		      NULL, NULL, 0);
 }
 
-static void update_counter_and_wait(void)
+static void wait_for_readers(void)
 {
 	CDS_LIST_HEAD(qsreaders);
 	int wait_loops = 0;
 	struct rcu_reader *index, *tmp;
 
-	/* Switch parity: 0 -> 1, 1 -> 0 */
-	CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE);
-
-	/*
-	 * Must commit rcu_gp_ctr update to memory before waiting for quiescent
-	 * state. Failure to do so could result in the writer waiting forever
-	 * while new readers are always accessing data (no progress). Enforce
-	 * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr.
-	 */
-	cmm_barrier();
-
-	/*
-	 *
-	 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
-	 * model easier to understand. It does not have a big performance impact
-	 * anyway, given this is the write-side.
-	 */
-	cmm_smp_mb();
-
 	/*
-	 * Wait for each thread URCU_TLS(rcu_reader).ctr count to become 0.
+	 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
+	 * indicate quiescence (not nested), or observe the current
+	 * rcu_gp_ctr value.
 	 */
 	for (;;) {
 		wait_loops++;
@@ -316,12 +299,12 @@ void synchronize_rcu(void)
 	smp_mb_master(RCU_MB_GROUP);
 
 	/*
-	 * Wait for previous parity to be empty of readers.
+	 * Wait for readers to observe original parity or be quiescent.
 	 */
-	update_counter_and_wait();	/* 0 -> 1, wait readers in parity 0 */
+	wait_for_readers();
 
 	/*
-	 * Must finish waiting for quiescent state for parity 0 before
+	 * Must finish waiting for quiescent state for original parity before
 	 * committing next rcu_gp_ctr update to memory. Failure to do so could
 	 * result in the writer waiting forever while new readers are always
 	 * accessing data (no progress).  Enforce compiler-order of load
@@ -336,10 +319,29 @@ void synchronize_rcu(void)
 	 */
 	cmm_smp_mb();
 
+	/* Switch parity: 0 -> 1, 1 -> 0 */
+	CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE);
+
+	/*
+	 * Must commit rcu_gp_ctr update to memory before waiting for quiescent
+	 * state. Failure to do so could result in the writer waiting forever
+	 * while new readers are always accessing data (no progress). Enforce
+	 * compiler-order of store to rcu_gp_ctr before load rcu_reader ctr.
+	 */
+	cmm_barrier();
+
+	/*
+	 *
+	 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
+	 * model easier to understand. It does not have a big performance impact
+	 * anyway, given this is the write-side.
+	 */
+	cmm_smp_mb();
+
 	/*
-	 * Wait for previous parity to be empty of readers.
+	 * Wait for readers to observe new parity or be quiescent.
 	 */
-	update_counter_and_wait();	/* 1 -> 0, wait readers in parity 1 */
+	wait_for_readers();
 
 	/* Finish waiting for reader threads before letting the old ptr being
 	 * freed. Must be done within rcu_gp_lock because it iterates on reader
Signature · 1 line
1.7.10.4
#9 lore raw

PATCH 9/16 urcu-bp: improve 2-phase wait scheme

In reply to a message not in the archive

In the single-bit, 2-phase grace period scheme, all we need to do is to observe each reader going through a quiescent state while we are in the grace period.

We therefore only need to perform one global counter update, surrounded by 2 iterations on readers to observe change in their snapshot.

We can therefore remove the first counter update (prior to the first iteration on readers): it was useless and was only slowing down the grace period.

CC Paul E. McKenney <[email protected]>
CC Lai Jiangshan <[email protected]>
CC Alan Stern <[email protected]>
---
 urcu-bp.c |   48 +++++++++++++++++++++++++-----------------------
 1 file changed, 25 insertions(+), 23 deletions(-)
urcu-bp.c +25 -23
index b07a1bb..4b3cf01 100644
@@ -164,31 +164,16 @@ static void mutex_unlock(pthread_mutex_t *mutex)
 		urcu_die(ret);
 }
 
-static void update_counter_and_wait(void)
+static void wait_for_readers(void)
 {
 	CDS_LIST_HEAD(qsreaders);
 	int wait_loops = 0;
 	struct rcu_reader *index, *tmp;
 
-	/* Switch parity: 0 -> 1, 1 -> 0 */
-	CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE);
-
-	/*
-	 * Must commit qparity update to memory before waiting for other parity
-	 * quiescent state. Failure to do so could result in the writer waiting
-	 * forever while new readers are always accessing data (no progress).
-	 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
-	 */
-
-	/*
-	 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
-	 * model easier to understand. It does not have a big performance impact
-	 * anyway, given this is the write-side.
-	 */
-	cmm_smp_mb();
-
 	/*
-	 * Wait for each thread rcu_reader.ctr count to become 0.
+	 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
+	 * indicate quiescence (not nested), or observe the current
+	 * rcu_gp_ctr value.
 	 */
 	for (;;) {
 		wait_loops++;
@@ -234,9 +219,26 @@ void synchronize_rcu(void)
 	rcu_gc_registry();
 
 	/*
-	 * Wait for previous parity to be empty of readers.
+	 * Wait for readers to observe original parity or be quiescent.
+	 */
+	wait_for_readers();
+
+	/*
+	 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
+	 * model easier to understand. It does not have a big performance impact
+	 * anyway, given this is the write-side.
+	 */
+	cmm_smp_mb();
+
+	/* Switch parity: 0 -> 1, 1 -> 0 */
+	CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE);
+
+	/*
+	 * Must commit qparity update to memory before waiting for other parity
+	 * quiescent state. Failure to do so could result in the writer waiting
+	 * forever while new readers are always accessing data (no progress).
+	 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
 	 */
-	update_counter_and_wait();	/* 0 -> 1, wait readers in parity 0 */
 
 	/*
 	 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
@@ -246,9 +248,9 @@ void synchronize_rcu(void)
 	cmm_smp_mb();
 
 	/*
-	 * Wait for previous parity to be empty of readers.
+	 * Wait for readers to observe new parity or be quiescent.
 	 */
-	update_counter_and_wait();	/* 1 -> 0, wait readers in parity 1 */
+	wait_for_readers();
 
 	/*
 	 * Finish waiting for reader threads before letting the old ptr being
Signature · 1 line
1.7.10.4
#10 lore raw

PATCH 10/16 urcu-qsbr: move offline threads to separate list

In reply to a message not in the archive

Accelerate 2-phase grace period by not having to iterate on offline threads twice.

CC Paul E. McKenney <[email protected]>
CC Lai Jiangshan <[email protected]>
CC Alan Stern <[email protected]>
---
 urcu-qsbr.c             |   54 ++++++++++++++++++++++++++++++++++++-----------
 urcu/static/urcu-qsbr.h |   14 ++++++++++--
 2 files changed, 54 insertions(+), 14 deletions(-)
urcu-qsbr.c +42 -12
index eb167d1..5b341b5 100644
@@ -116,9 +116,10 @@ static void wait_gp(void)
 		      NULL, NULL, 0);
 }
 
-static void wait_for_readers(void)
+static void wait_for_readers(struct cds_list_head *input_readers,
+			struct cds_list_head *cur_snap_readers,
+			struct cds_list_head *qsreaders)
 {
-	CDS_LIST_HEAD(qsreaders);
 	int wait_loops = 0;
 	struct rcu_reader *index, *tmp;
 
@@ -136,18 +137,36 @@ static void wait_for_readers(void)
 			 * reads them in the opposite order).
 			 */
 			cmm_smp_wmb();
-			cds_list_for_each_entry(index, &registry, node) {
+			cds_list_for_each_entry(index, input_readers, node) {
 				_CMM_STORE_SHARED(index->waiting, 1);
 			}
 			/* Write futex before read reader_gp */
 			cmm_smp_mb();
 		}
-		cds_list_for_each_entry_safe(index, tmp, &registry, node) {
-			if (!rcu_gp_ongoing(&index->ctr))
-				cds_list_move(&index->node, &qsreaders);
+		cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
+			switch (rcu_reader_state(&index->ctr)) {
+			case RCU_READER_ACTIVE_CURRENT:
+				if (cur_snap_readers) {
+					cds_list_move(&index->node,
+						cur_snap_readers);
+					break;
+				}
+				/* Fall-through */
+			case RCU_READER_INACTIVE:
+				cds_list_move(&index->node, qsreaders);
+				break;
+			case RCU_READER_ACTIVE_OLD:
+				/*
+				 * Old snapshot. Leaving node in
+				 * input_readers will make us busy-loop
+				 * until the snapshot becomes current or
+				 * the reader becomes inactive.
+				 */
+				break;
+			}
 		}
 
-		if (cds_list_empty(&registry)) {
+		if (cds_list_empty(input_readers)) {
 			if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
 				/* Read reader_gp before write futex */
 				cmm_smp_mb();
@@ -166,8 +185,6 @@ static void wait_for_readers(void)
 			}
 		}
 	}
-	/* put back the reader list in the registry */
-	cds_list_splice(&qsreaders, &registry);
 }
 
 /*
@@ -178,6 +195,8 @@ static void wait_for_readers(void)
 #if (CAA_BITS_PER_LONG < 64)
 void synchronize_rcu(void)
 {
+	CDS_LIST_HEAD(cur_snap_readers);
+	CDS_LIST_HEAD(qsreaders);
 	unsigned long was_online;
 
 	was_online = URCU_TLS(rcu_reader).ctr;
@@ -203,7 +222,7 @@ void synchronize_rcu(void)
 	/*
 	 * Wait for readers to observe original parity or be quiescent.
 	 */
-	wait_for_readers();
+	wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
 
 	/*
 	 * Must finish waiting for quiescent state for original parity
@@ -244,7 +263,12 @@ void synchronize_rcu(void)
 	/*
 	 * Wait for readers to observe new parity or be quiescent.
 	 */
-	wait_for_readers();
+	wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
+
+	/*
+	 * Put quiescent reader list back into registry.
+	 */
+	cds_list_splice(&qsreaders, &registry);
 out:
 	mutex_unlock(&rcu_gp_lock);
 
@@ -260,6 +284,7 @@ out:
 #else /* !(CAA_BITS_PER_LONG < 64) */
 void synchronize_rcu(void)
 {
+	CDS_LIST_HEAD(qsreaders);
 	unsigned long was_online;
 
 	was_online = URCU_TLS(rcu_reader).ctr;
@@ -300,7 +325,12 @@ void synchronize_rcu(void)
 	/*
 	 * Wait for readers to observe new count of be quiescent.
 	 */
-	wait_for_readers();
+	wait_for_readers(&registry, NULL, &qsreaders);
+
+	/*
+	 * Put quiescent reader list back into registry.
+	 */
+	cds_list_splice(&qsreaders, &registry);
 out:
 	mutex_unlock(&rcu_gp_lock);
 
urcu/static/urcu-qsbr.h +12 -2
index f314956..f6e5580 100644
@@ -62,6 +62,12 @@ extern "C" {
 #define rcu_assert(args...)
 #endif
 
+enum rcu_state {
+	RCU_READER_ACTIVE_CURRENT,
+	RCU_READER_ACTIVE_OLD,
+	RCU_READER_INACTIVE,
+};
+
 #ifdef DEBUG_YIELD
 #include <sched.h>
 #include <time.h>
@@ -149,12 +155,16 @@ static inline void wake_up_gp(void)
 	}
 }
 
-static inline int rcu_gp_ongoing(unsigned long *ctr)
+static inline enum rcu_state rcu_reader_state(unsigned long *ctr)
 {
 	unsigned long v;
 
 	v = CMM_LOAD_SHARED(*ctr);
-	return v && (v != rcu_gp_ctr);
+	if (!v)
+		return RCU_READER_INACTIVE;
+	if (v == rcu_gp_ctr)
+		return RCU_READER_ACTIVE_CURRENT;
+	return RCU_READER_ACTIVE_OLD;
 }
 
 /*
Signature · 1 line
1.7.10.4
#11 lore raw

PATCH 11/16 urcu-mb/signal/membarrier: move quiescent threads to separate list

In reply to a message not in the archive

Accelerate 2-phase grace period by not having to iterate twice on threads not nested within a RCU read-side lock.

CC Paul E. McKenney <[email protected]>
CC Lai Jiangshan <[email protected]>
CC Alan Stern <[email protected]>
---
 urcu.c             |   47 ++++++++++++++++++++++++++++++++++++-----------
 urcu/static/urcu.h |   15 ++++++++++++---
 2 files changed, 48 insertions(+), 14 deletions(-)
urcu.c +36 -11
index 2d5c510..e6ff0f3 100644
@@ -215,9 +215,10 @@ static void wait_gp(void)
 		      NULL, NULL, 0);
 }
 
-static void wait_for_readers(void)
+static void wait_for_readers(struct cds_list_head *input_readers,
+			struct cds_list_head *cur_snap_readers,
+			struct cds_list_head *qsreaders)
 {
-	CDS_LIST_HEAD(qsreaders);
 	int wait_loops = 0;
 	struct rcu_reader *index, *tmp;
 
@@ -234,13 +235,31 @@ static void wait_for_readers(void)
 			smp_mb_master(RCU_MB_GROUP);
 		}
 
-		cds_list_for_each_entry_safe(index, tmp, &registry, node) {
-			if (!rcu_gp_ongoing(&index->ctr))
-				cds_list_move(&index->node, &qsreaders);
+		cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
+			switch (rcu_reader_state(&index->ctr)) {
+			case RCU_READER_ACTIVE_CURRENT:
+				if (cur_snap_readers) {
+					cds_list_move(&index->node,
+						cur_snap_readers);
+					break;
+				}
+				/* Fall-through */
+			case RCU_READER_INACTIVE:
+				cds_list_move(&index->node, qsreaders);
+				break;
+			case RCU_READER_ACTIVE_OLD:
+				/*
+				 * Old snapshot. Leaving node in
+				 * input_readers will make us busy-loop
+				 * until the snapshot becomes current or
+				 * the reader becomes inactive.
+				 */
+				break;
+			}
 		}
 
 #ifndef HAS_INCOHERENT_CACHES
-		if (cds_list_empty(&registry)) {
+		if (cds_list_empty(input_readers)) {
 			if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
 				/* Read reader_gp before write futex */
 				smp_mb_master(RCU_MB_GROUP);
@@ -259,7 +278,7 @@ static void wait_for_readers(void)
 		 * URCU_TLS(rcu_reader).ctr update to memory if we wait
 		 * for too long.
 		 */
-		if (cds_list_empty(&registry)) {
+		if (cds_list_empty(input_readers)) {
 			if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) {
 				/* Read reader_gp before write futex */
 				smp_mb_master(RCU_MB_GROUP);
@@ -281,12 +300,13 @@ static void wait_for_readers(void)
 		}
 #endif /* #else #ifndef HAS_INCOHERENT_CACHES */
 	}
-	/* put back the reader list in the registry */
-	cds_list_splice(&qsreaders, &registry);
 }
 
 void synchronize_rcu(void)
 {
+	CDS_LIST_HEAD(cur_snap_readers);
+	CDS_LIST_HEAD(qsreaders);
+
 	mutex_lock(&rcu_gp_lock);
 
 	if (cds_list_empty(&registry))
@@ -301,7 +321,7 @@ void synchronize_rcu(void)
 	/*
 	 * Wait for readers to observe original parity or be quiescent.
 	 */
-	wait_for_readers();
+	wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
 
 	/*
 	 * Must finish waiting for quiescent state for original parity before
@@ -341,7 +361,12 @@ void synchronize_rcu(void)
 	/*
 	 * Wait for readers to observe new parity or be quiescent.
 	 */
-	wait_for_readers();
+	wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
+
+	/*
+	 * Put quiescent reader list back into registry.
+	 */
+	cds_list_splice(&qsreaders, &registry);
 
 	/* Finish waiting for reader threads before letting the old ptr being
 	 * freed. Must be done within rcu_gp_lock because it iterates on reader
urcu/static/urcu.h +12 -3
index f32f300..973826a 100644
@@ -96,6 +96,12 @@ extern "C" {
 #define SIGRCU SIGUSR1
 #endif
 
+enum rcu_state {
+	RCU_READER_ACTIVE_CURRENT,
+	RCU_READER_ACTIVE_OLD,
+	RCU_READER_INACTIVE,
+};
+
 #ifdef DEBUG_RCU
 #define rcu_assert(args...)	assert(args)
 #else
@@ -239,7 +245,7 @@ static inline void wake_up_gp(void)
 	}
 }
 
-static inline int rcu_gp_ongoing(unsigned long *ctr)
+static inline enum rcu_state rcu_reader_state(unsigned long *ctr)
 {
 	unsigned long v;
 
@@ -248,8 +254,11 @@ static inline int rcu_gp_ongoing(unsigned long *ctr)
 	 * to insure consistency.
 	 */
 	v = CMM_LOAD_SHARED(*ctr);
-	return (v & RCU_GP_CTR_NEST_MASK) &&
-		 ((v ^ rcu_gp_ctr) & RCU_GP_CTR_PHASE);
+	if (!(v & RCU_GP_CTR_NEST_MASK))
+		return RCU_READER_INACTIVE;
+	if (!((v ^ rcu_gp_ctr) & RCU_GP_CTR_PHASE))
+		return RCU_READER_ACTIVE_CURRENT;
+	return RCU_READER_ACTIVE_OLD;
 }
 
 /*
Signature · 1 line
1.7.10.4
#12 lore raw

PATCH 12/16 urcu-bp: move quiescent threads to separate list

In reply to a message not in the archive

Accelerate 2-phase grace period by not having to iterate twice on threads not within RCU read-side critical section.

CC Paul E. McKenney <[email protected]>
CC Lai Jiangshan <[email protected]>
CC Alan Stern <[email protected]>
---
 urcu-bp.c             |   46 +++++++++++++++++++++++++++++++++++-----------
 urcu/static/urcu-bp.h |   29 +++++++++++++++++++----------
 2 files changed, 54 insertions(+), 21 deletions(-)
urcu-bp.c +35 -11
index 4b3cf01..f99c0e5 100644
@@ -115,7 +115,7 @@ DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
  * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
  * Written to only by writer with mutex taken. Read by both writer and readers.
  */
-long rcu_gp_ctr = RCU_GP_COUNT;
+unsigned long rcu_gp_ctr = RCU_GP_COUNT;
 
 /*
  * Pointer to registry elements. Written to only by each individual reader. Read
@@ -164,9 +164,10 @@ static void mutex_unlock(pthread_mutex_t *mutex)
 		urcu_die(ret);
 }
 
-static void wait_for_readers(void)
+static void wait_for_readers(struct cds_list_head *input_readers,
+			struct cds_list_head *cur_snap_readers,
+			struct cds_list_head *qsreaders)
 {
-	CDS_LIST_HEAD(qsreaders);
 	int wait_loops = 0;
 	struct rcu_reader *index, *tmp;
 
@@ -177,12 +178,30 @@ static void wait_for_readers(void)
 	 */
 	for (;;) {
 		wait_loops++;
-		cds_list_for_each_entry_safe(index, tmp, &registry, node) {
-			if (!rcu_old_gp_ongoing(&index->ctr))
-				cds_list_move(&index->node, &qsreaders);
+		cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
+			switch (rcu_reader_state(&index->ctr)) {
+			case RCU_READER_ACTIVE_CURRENT:
+				if (cur_snap_readers) {
+					cds_list_move(&index->node,
+						cur_snap_readers);
+					break;
+				}
+				/* Fall-through */
+			case RCU_READER_INACTIVE:
+				cds_list_move(&index->node, qsreaders);
+				break;
+			case RCU_READER_ACTIVE_OLD:
+				/*
+				 * Old snapshot. Leaving node in
+				 * input_readers will make us busy-loop
+				 * until the snapshot becomes current or
+				 * the reader becomes inactive.
+				 */
+				break;
+			}
 		}
 
-		if (cds_list_empty(&registry)) {
+		if (cds_list_empty(input_readers)) {
 			break;
 		} else {
 			if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS)
@@ -191,12 +210,12 @@ static void wait_for_readers(void)
 				caa_cpu_relax();
 		}
 	}
-	/* put back the reader list in the registry */
-	cds_list_splice(&qsreaders, &registry);
 }
 
 void synchronize_rcu(void)
 {
+	CDS_LIST_HEAD(cur_snap_readers);
+	CDS_LIST_HEAD(qsreaders);
 	sigset_t newmask, oldmask;
 	int ret;
 
@@ -221,7 +240,7 @@ void synchronize_rcu(void)
 	/*
 	 * Wait for readers to observe original parity or be quiescent.
 	 */
-	wait_for_readers();
+	wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
 
 	/*
 	 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
@@ -250,7 +269,12 @@ void synchronize_rcu(void)
 	/*
 	 * Wait for readers to observe new parity or be quiescent.
 	 */
-	wait_for_readers();
+	wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
+
+	/*
+	 * Put quiescent reader list back into registry.
+	 */
+	cds_list_splice(&qsreaders, &registry);
 
 	/*
 	 * Finish waiting for reader threads before letting the old ptr being
urcu/static/urcu-bp.h +19 -10
index c52a688..c7f5326 100644
@@ -58,6 +58,12 @@ extern "C" {
 #define rcu_assert(args...)
 #endif
 
+enum rcu_state {
+	RCU_READER_ACTIVE_CURRENT,
+	RCU_READER_ACTIVE_OLD,
+	RCU_READER_INACTIVE,
+};
+
 #ifdef DEBUG_YIELD
 #include <sched.h>
 #include <time.h>
@@ -129,11 +135,11 @@ extern void rcu_bp_register(void);
  * Using a int rather than a char to eliminate false register dependencies
  * causing stalls on some architectures.
  */
-extern long rcu_gp_ctr;
+extern unsigned long rcu_gp_ctr;
 
 struct rcu_reader {
 	/* Data used by both reader and synchronize_rcu() */
-	long ctr;
+	unsigned long ctr;
 	/* Data used for registry */
 	struct cds_list_head node __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
 	pthread_t tid;
@@ -147,19 +153,22 @@ struct rcu_reader {
  */
 extern DECLARE_URCU_TLS(struct rcu_reader *, rcu_reader);
 
-static inline int rcu_old_gp_ongoing(long *value)
+static inline enum rcu_state rcu_reader_state(unsigned long *ctr)
 {
-	long v;
+	unsigned long v;
 
-	if (value == NULL)
-		return 0;
+	if (ctr == NULL)
+		return RCU_READER_INACTIVE;
 	/*
 	 * Make sure both tests below are done on the same version of *value
 	 * to insure consistency.
 	 */
-	v = CMM_LOAD_SHARED(*value);
-	return (v & RCU_GP_CTR_NEST_MASK) &&
-		 ((v ^ rcu_gp_ctr) & RCU_GP_CTR_PHASE);
+	v = CMM_LOAD_SHARED(*ctr);
+	if (!(v & RCU_GP_CTR_NEST_MASK))
+		return RCU_READER_INACTIVE;
+	if (!((v ^ rcu_gp_ctr) & RCU_GP_CTR_PHASE))
+		return RCU_READER_ACTIVE_CURRENT;
+	return RCU_READER_ACTIVE_OLD;
 }
 
 /*
@@ -190,7 +199,7 @@ static inline void _rcu_read_lock_update(unsigned long tmp)
  */
 static inline void _rcu_read_lock(void)
 {
-	long tmp;
+	unsigned long tmp;
 
 	if (caa_unlikely(!URCU_TLS(rcu_reader)))
 		rcu_bp_register(); /* If not yet registered. */
Signature · 1 line
1.7.10.4
#13 lore raw

PATCH 13/16 tests: use standard malloc/free for synchronize_rcu()

In reply to a message not in the archive

Allows removing mutex from tests, which allow testing scalability of concurrent synchronize_rcu() executions.

CC Paul E. McKenney <[email protected]>
CC Lai Jiangshan <[email protected]>
CC Alan Stern <[email protected]>
---
 tests/test_urcu.c      |   60 ++++++++---------------------------------------
 tests/test_urcu_bp.c   |   59 +++++++---------------------------------------
 tests/test_urcu_qsbr.c |   61 ++++++++----------------------------------------
 3 files changed, 29 insertions(+), 151 deletions(-)
tests/test_urcu.c +10 -50
index 6463d67..87972aa 100644
@@ -66,15 +66,11 @@ static inline pid_t gettid(void)
 #endif
 #include <urcu.h>
 
-struct test_array {
-	int a;
-};
-
 static volatile int test_go, test_stop;
 
 static unsigned long wdelay;
 
-static struct test_array *test_rcu_pointer;
+static int *test_rcu_pointer;
 
 static unsigned long duration;
 
@@ -185,43 +181,10 @@ void rcu_copy_mutex_unlock(void)
 	}
 }
 
-/*
- * malloc/free are reusing memory areas too quickly, which does not let us
- * test races appropriately. Use a large circular array for allocations.
- * ARRAY_SIZE is larger than nr_writers, and we keep the mutex across
- * both alloc and free, which insures we never run over our tail.
- */
-#define ARRAY_SIZE (1048576 * nr_writers)
-#define ARRAY_POISON 0xDEADBEEF
-static int array_index;
-static struct test_array *test_array;
-
-static struct test_array *test_array_alloc(void)
-{
-	struct test_array *ret;
-	int index;
-
-	index = array_index % ARRAY_SIZE;
-	assert(test_array[index].a == ARRAY_POISON ||
-		test_array[index].a == 0);
-	ret = &test_array[index];
-	array_index++;
-	if (array_index == ARRAY_SIZE)
-		array_index = 0;
-	return ret;
-}
-
-static void test_array_free(struct test_array *ptr)
-{
-	if (!ptr)
-		return;
-	ptr->a = ARRAY_POISON;
-}
-
 void *thr_reader(void *_count)
 {
 	unsigned long long *count = _count;
-	struct test_array *local_ptr;
+	int *local_ptr;
 
 	printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
 			"reader", (unsigned long) pthread_self(),
@@ -241,7 +204,7 @@ void *thr_reader(void *_count)
 		local_ptr = rcu_dereference(test_rcu_pointer);
 		rcu_debug_yield_read();
 		if (local_ptr)
-			assert(local_ptr->a == 8);
+			assert(*local_ptr == 8);
 		if (caa_unlikely(rduration))
 			loop_sleep(rduration);
 		rcu_read_unlock();
@@ -267,7 +230,7 @@ void *thr_reader(void *_count)
 void *thr_writer(void *_count)
 {
 	unsigned long long *count = _count;
-	struct test_array *new, *old;
+	int *new, *old;
 
 	printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
 			"writer", (unsigned long) pthread_self(),
@@ -281,17 +244,16 @@ void *thr_writer(void *_count)
 	cmm_smp_mb();
 
 	for (;;) {
-		rcu_copy_mutex_lock();
-		new = test_array_alloc();
-		new->a = 8;
+		new = malloc(sizeof(int));
+		assert(new);
+		*new = 8;
 		old = rcu_xchg_pointer(&test_rcu_pointer, new);
 		if (caa_unlikely(wduration))
 			loop_sleep(wduration);
 		synchronize_rcu();
 		if (old)
-			old->a = 0;
-		test_array_free(old);
-		rcu_copy_mutex_unlock();
+			*old = 0;
+		free(old);
 		URCU_TLS(nr_writes)++;
 		if (caa_unlikely(!test_duration_write()))
 			break;
@@ -409,7 +371,6 @@ int main(int argc, char **argv)
 			"main", (unsigned long) pthread_self(),
 			(unsigned long)gettid());
 
-	test_array = calloc(1, sizeof(*test_array) * ARRAY_SIZE);
 	tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
 	tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
 	count_reader = malloc(sizeof(*count_reader) * nr_readers);
@@ -459,8 +420,7 @@ int main(int argc, char **argv)
 		argv[0], duration, nr_readers, rduration, wduration,
 		nr_writers, wdelay, tot_reads, tot_writes,
 		tot_reads + tot_writes);
-	test_array_free(test_rcu_pointer);
-	free(test_array);
+	free(test_rcu_pointer);
 	free(tid_reader);
 	free(tid_writer);
 	free(count_reader);
tests/test_urcu_bp.c +9 -50

113 lines not on this page · raw message

tests/test_urcu_qsbr.c +10 -51

115 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#14 lore raw

PATCH 14/16 urcu-qsbr: batch concurrent synchronize_rcu()

In reply to a message not in the archive

Here are benchmarks on batching of synchronize_rcu(), and it leads to very interesting scalability improvement and speedups, e.g., on a 24-core AMD, with a write-heavy scenario (4 readers threads, 20 updater threads, each updater using synchronize_rcu()):

* Serialized grace periods :

./test_urcu_qsbr 4 20 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4
rdur 0 wdur 0 nr_writers 20 wdelay 0
nr_reads 20251412728 nr_writes 1826331 nr_ops 20253239059

* Batched grace periods :

./test_urcu_qsbr 4 20 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4
rdur 0 wdur 0 nr_writers 20 wdelay 0
nr_reads 15141994746 nr_writes 9382515 nr_ops 15151377261

For a 9382515/1826331 = 5.13 speedup for 20 updaters.

Of course, we can see that readers have slowed down, probably due to increased update traffic, given there is no change to the read-side code whatsoever.

Now let's see the penality of managing the stack for single-updater. With 4 readers, single updater:

* Serialized grace periods :

./test_urcu_qsbr 4 1 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4

rdur      0 wdur      0 nr_writers   1 wdelay      0
nr_reads  19240784755 nr_writes      2130839 nr_ops  19242915594

* Batched grace periods :

./test_urcu_qsbr 4 1 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4

rdur      0 wdur      0 nr_writers   1 wdelay      0
nr_reads  19160162768 nr_writes      2253068 nr_ops  1916241583

2253068 vs 2137036 -> a couple of runs show that this difference lost in the noise for single updater.

CC Paul E. McKenney <[email protected]>
CC Lai Jiangshan <[email protected]>
CC Alan Stern <[email protected]>
---
 urcu-qsbr.c |  151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 151 insertions(+)
urcu-qsbr.c +151 -0
index 5b341b5..7f747ed 100644
@@ -36,6 +36,7 @@
 #include <poll.h>
 
 #include "urcu/wfcqueue.h"
+#include "urcu/wfstack.h"
 #include "urcu/map/urcu-qsbr.h"
 #define BUILD_QSBR_LIB
 #include "urcu/static/urcu-qsbr.h"
@@ -78,6 +79,35 @@ DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
 
 static CDS_LIST_HEAD(registry);
 
+/*
+ * Number of busy-loop attempts before waiting on futex for grace period
+ * batching.
+ */
+#define RCU_AWAKE_ATTEMPTS 1000
+
+enum adapt_wakeup_state {
+	/* AWAKE_WAITING is compared directly (futex compares it). */
+	AWAKE_WAITING =		0,
+	/* non-zero are used as masks. */
+	AWAKE_WAKEUP =		(1 << 0),
+	AWAKE_AWAKENED =	(1 << 1),
+	AWAKE_TEARDOWN =	(1 << 2),
+};
+
+struct gp_waiters_thread {
+	struct cds_wfs_node node;
+	int32_t wait_futex;
+};
+
+/*
+ * Stack keeping threads awaiting to wait for a grace period. Contains
+ * struct gp_waiters_thread objects.
+ */
+static struct cds_wfs_stack gp_waiters = {
+	.head = CDS_WFS_END,
+	.lock = PTHREAD_MUTEX_INITIALIZER,
+};
+
 static void mutex_lock(pthread_mutex_t *mutex)
 {
 	int ret;
@@ -116,6 +146,58 @@ static void wait_gp(void)
 		      NULL, NULL, 0);
 }
 
+/*
+ * Note: urcu_adaptative_wake_up needs "value" to stay allocated
+ * throughout its execution. In this scheme, the waiter owns the futex
+ * memory, and we only allow it to free this memory when it receives the
+ * AWAKE_TEARDOWN flag.
+ */
+static void urcu_adaptative_wake_up(int32_t *value)
+{
+	cmm_smp_mb();
+	assert(uatomic_read(value) == AWAKE_WAITING);
+	uatomic_set(value, AWAKE_WAKEUP);
+	if (!(uatomic_read(value) & AWAKE_AWAKENED))
+		futex_noasync(value, FUTEX_WAKE, 1, NULL, NULL, 0);
+	/* Allow teardown of "value" memory. */
+	uatomic_or(value, AWAKE_TEARDOWN);
+}
+
+/*
+ * Caller must initialize "value" to AWAKE_WAITING before passing its
+ * memory to waker thread.
+ */
+static void urcu_adaptative_busy_wait(int32_t *value)
+{
+	unsigned int i;
+
+	/* Load and test condition before read futex */
+	cmm_smp_rmb();
+	for (i = 0; i < RCU_AWAKE_ATTEMPTS; i++) {
+		if (uatomic_read(value) != AWAKE_WAITING)
+			goto skip_futex_wait;
+		caa_cpu_relax();
+	}
+	futex_noasync(value, FUTEX_WAIT, AWAKE_WAITING, NULL, NULL, 0);
+skip_futex_wait:
+
+	/* Tell waker thread than we are awakened. */
+	uatomic_or(value, AWAKE_AWAKENED);
+
+	/*
+	 * Wait until waker thread lets us know it's ok to tear down
+	 * memory allocated for value.
+	 */
+	for (i = 0; i < RCU_AWAKE_ATTEMPTS; i++) {
+		if (uatomic_read(value) & AWAKE_TEARDOWN)
+			break;
+		caa_cpu_relax();
+	}
+	while (!(uatomic_read(value) & AWAKE_TEARDOWN))
+		poll(NULL, 0, 10);
+	assert(uatomic_read(value) & AWAKE_TEARDOWN);
+}
+
 static void wait_for_readers(struct cds_list_head *input_readers,
 			struct cds_list_head *cur_snap_readers,
 			struct cds_list_head *qsreaders)
@@ -198,6 +280,9 @@ void synchronize_rcu(void)
 	CDS_LIST_HEAD(cur_snap_readers);
 	CDS_LIST_HEAD(qsreaders);
 	unsigned long was_online;
+	struct gp_waiters_thread gp_waiters_thread;
+	struct cds_wfs_head *gp_waiters_head;
+	struct cds_wfs_node *waiters_iter, *waiters_iter_n;
 
 	was_online = URCU_TLS(rcu_reader).ctr;
 
@@ -214,8 +299,26 @@ void synchronize_rcu(void)
 	else
 		cmm_smp_mb();
 
+	/*
+	 * Add ourself to gp_waiters stack of threads awaiting to wait
+	 * for a grace period. Proceed to perform the grace period only
+	 * if we are the first thread added into the stack.
+	 */
+	cds_wfs_node_init(&gp_waiters_thread.node);
+	gp_waiters_thread.wait_futex = AWAKE_WAITING;
+	if (cds_wfs_push(&gp_waiters, &gp_waiters_node) != 0) {
+		/* Not first in stack: will be awakened by another thread. */
+		urcu_adaptative_busy_wait(&gp_waiters_thread.wait_futex);
+		goto gp_end;
+	}
+
 	mutex_lock(&rcu_gp_lock);
 
+	/*
+	 * Pop all waiters into our local stack head.
+	 */
+	gp_waiters_head = __cds_wfs_pop_all(&gp_waiters);
+
 	if (cds_list_empty(&registry))
 		goto out;
 
@@ -272,6 +375,19 @@ void synchronize_rcu(void)
 out:
 	mutex_unlock(&rcu_gp_lock);
 
+	/* Wake all waiters in our stack head, excluding ourself. */
+	cds_wfs_for_each_blocking_safe(gp_waiters_head, waiters_iter,
+				waiters_iter_n) {
+		struct gp_waiters_thread *wt;
+
+		wt = caa_container_of(waiters_iter,
+				struct gp_waiters_thread, node);
+		if (wt == &gp_waiters_thread)
+			continue;
+		urcu_adaptative_wake_up(&wt->wait_futex);
+	}
+
+gp_end:
 	/*
 	 * Finish waiting for reader threads before letting the old ptr being
 	 * freed.
@@ -286,6 +402,9 @@ void synchronize_rcu(void)
 {
 	CDS_LIST_HEAD(qsreaders);
 	unsigned long was_online;
+	struct gp_waiters_thread gp_waiters_thread;
+	struct cds_wfs_head *gp_waiters_head;
+	struct cds_wfs_node *waiters_iter, *waiters_iter_n;
 
 	was_online = URCU_TLS(rcu_reader).ctr;
 
@@ -299,7 +418,26 @@ void synchronize_rcu(void)
 	else
 		cmm_smp_mb();
 
+	/*
+	 * Add ourself to gp_waiters stack of threads awaiting to wait
+	 * for a grace period. Proceed to perform the grace period only
+	 * if we are the first thread added into the stack.
+	 */
+	cds_wfs_node_init(&gp_waiters_thread.node);
+	gp_waiters_thread.wait_futex = AWAKE_WAITING;
+	if (cds_wfs_push(&gp_waiters, &gp_waiters_thread.node) != 0) {
+		/* Not first in stack: will be awakened by another thread. */
+		urcu_adaptative_busy_wait(&gp_waiters_thread.wait_futex);
+		goto gp_end;
+	}
+
 	mutex_lock(&rcu_gp_lock);
+
+	/*
+	 * Pop all waiters into our local stack head.
+	 */
+	gp_waiters_head = __cds_wfs_pop_all(&gp_waiters);
+
 	if (cds_list_empty(&registry))
 		goto out;
 
@@ -334,6 +472,19 @@ void synchronize_rcu(void)
 out:
 	mutex_unlock(&rcu_gp_lock);
 
+	/* Wake all waiters in our stack head, excluding ourself. */
+	cds_wfs_for_each_blocking_safe(gp_waiters_head, waiters_iter,
+				waiters_iter_n) {
+		struct gp_waiters_thread *wt;
+
+		wt = caa_container_of(waiters_iter,
+				struct gp_waiters_thread, node);
+		if (wt == &gp_waiters_thread)
+			continue;
+		urcu_adaptative_wake_up(&wt->wait_futex);
+	}
+
+gp_end:
 	if (was_online)
 		rcu_thread_online();
 	else
Signature · 1 line
1.7.10.4
#15 lore raw

PATCH 15/16 urcu-wait: move wait code into separate file

In reply to a message not in the archive
---
 Makefile.am |    2 +-
 urcu-qsbr.c |   82 +++++----------------------------------------
 urcu-wait.h |  107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 116 insertions(+), 75 deletions(-)
 create mode 100644 urcu-wait.h
Makefile.am +1 -1
index 195b89a..0a4d357 100644
@@ -23,7 +23,7 @@ nobase_dist_include_HEADERS = urcu/compiler.h urcu/hlist.h urcu/list.h \
 		urcu/tls-compat.h
 nobase_nodist_include_HEADERS = urcu/arch.h urcu/uatomic.h urcu/config.h
 
-dist_noinst_HEADERS = urcu-die.h
+dist_noinst_HEADERS = urcu-die.h urcu-wait.h
 
 EXTRA_DIST = $(top_srcdir)/urcu/arch/*.h $(top_srcdir)/urcu/uatomic/*.h \
 		gpl-2.0.txt lgpl-2.1.txt lgpl-relicensing.txt \
urcu-qsbr.c +8 -74

130 lines not on this page · raw message

urcu-wait.h new file +107 -0

107 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#16 lore raw

PATCH 16/16 urcu-wait: move queue management code into urcu-wait.h

In reply to a message not in the archive

Note: urcu-wait.h is not yet exposed outside of userspace RCU.

---
 urcu-qsbr.c |   72 ++++++++++--------------------------
 urcu-wait.h |  119 ++++++++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 117 insertions(+), 74 deletions(-)
urcu-qsbr.c +19 -53
index d691389..9ae17de 100644
@@ -80,19 +80,11 @@ DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
 
 static CDS_LIST_HEAD(registry);
 
-struct gp_waiters_thread {
-	struct cds_wfs_node node;
-	struct urcu_wait wait;
-};
-
 /*
  * Stack keeping threads awaiting to wait for a grace period. Contains
  * struct gp_waiters_thread objects.
  */
-static struct cds_wfs_stack gp_waiters = {
-	.head = CDS_WFS_END,
-	.lock = PTHREAD_MUTEX_INITIALIZER,
-};
+static DEFINE_URCU_WAIT_QUEUE(gp_waiters);
 
 static void mutex_lock(pthread_mutex_t *mutex)
 {
@@ -214,9 +206,8 @@ void synchronize_rcu(void)
 	CDS_LIST_HEAD(cur_snap_readers);
 	CDS_LIST_HEAD(qsreaders);
 	unsigned long was_online;
-	struct gp_waiters_thread gp_waiters_thread;
-	struct cds_wfs_head *gp_waiters_head;
-	struct cds_wfs_node *waiters_iter, *waiters_iter_n;
+	DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
+	struct urcu_waiters waiters;
 
 	was_online = URCU_TLS(rcu_reader).ctr;
 
@@ -238,20 +229,20 @@ void synchronize_rcu(void)
 	 * for a grace period. Proceed to perform the grace period only
 	 * if we are the first thread added into the stack.
 	 */
-	cds_wfs_node_init(&gp_waiters_thread.node);
-	urcu_wait_init(&gp_waiters_thread.wait);
-	if (cds_wfs_push(&gp_waiters, &gp_waiters_node) != 0) {
+	if (urcu_wait_add(&gp_waiters, &wait) != 0) {
 		/* Not first in stack: will be awakened by another thread. */
-		urcu_adaptative_busy_wait(&gp_waiters_thread.wait);
+		urcu_adaptative_busy_wait(&wait);
 		goto gp_end;
 	}
+	/* We won't need to wake ourself up */
+	urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
 
 	mutex_lock(&rcu_gp_lock);
 
 	/*
-	 * Pop all waiters into our local stack head.
+	 * Move all waiters into our local queue.
 	 */
-	gp_waiters_head = __cds_wfs_pop_all(&gp_waiters);
+	urcu_move_waiters(&waiters, &gp_waiters);
 
 	if (cds_list_empty(&registry))
 		goto out;
@@ -308,19 +299,7 @@ void synchronize_rcu(void)
 	cds_list_splice(&qsreaders, &registry);
 out:
 	mutex_unlock(&rcu_gp_lock);
-
-	/* Wake all waiters in our stack head, excluding ourself. */
-	cds_wfs_for_each_blocking_safe(gp_waiters_head, waiters_iter,
-				waiters_iter_n) {
-		struct gp_waiters_thread *wt;
-
-		wt = caa_container_of(waiters_iter,
-				struct gp_waiters_thread, node);
-		if (wt == &gp_waiters_thread)
-			continue;
-		urcu_adaptative_wake_up(&wt->wait);
-	}
-
+	urcu_wake_all_waiters(&waiters);
 gp_end:
 	/*
 	 * Finish waiting for reader threads before letting the old ptr being
@@ -336,9 +315,8 @@ void synchronize_rcu(void)
 {
 	CDS_LIST_HEAD(qsreaders);
 	unsigned long was_online;
-	struct gp_waiters_thread gp_waiters_thread;
-	struct cds_wfs_head *gp_waiters_head;
-	struct cds_wfs_node *waiters_iter, *waiters_iter_n;
+	DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
+	struct urcu_waiters waiters;
 
 	was_online = URCU_TLS(rcu_reader).ctr;
 
@@ -357,20 +335,20 @@ void synchronize_rcu(void)
 	 * for a grace period. Proceed to perform the grace period only
 	 * if we are the first thread added into the stack.
 	 */
-	cds_wfs_node_init(&gp_waiters_thread.node);
-	urcu_wait_init(&gp_waiters_thread.wait);
-	if (cds_wfs_push(&gp_waiters, &gp_waiters_thread.node) != 0) {
+	if (urcu_wait_add(&gp_waiters, &wait) != 0) {
 		/* Not first in stack: will be awakened by another thread. */
-		urcu_adaptative_busy_wait(&gp_waiters_thread.wait);
+		urcu_adaptative_busy_wait(&wait);
 		goto gp_end;
 	}
+	/* We won't need to wake ourself up */
+	urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
 
 	mutex_lock(&rcu_gp_lock);
 
 	/*
-	 * Pop all waiters into our local stack head.
+	 * Move all waiters into our local queue.
 	 */
-	gp_waiters_head = __cds_wfs_pop_all(&gp_waiters);
+	urcu_move_waiters(&waiters, &gp_waiters);
 
 	if (cds_list_empty(&registry))
 		goto out;
@@ -405,19 +383,7 @@ void synchronize_rcu(void)
 	cds_list_splice(&qsreaders, &registry);
 out:
 	mutex_unlock(&rcu_gp_lock);
-
-	/* Wake all waiters in our stack head, excluding ourself. */
-	cds_wfs_for_each_blocking_safe(gp_waiters_head, waiters_iter,
-				waiters_iter_n) {
-		struct gp_waiters_thread *wt;
-
-		wt = caa_container_of(waiters_iter,
-				struct gp_waiters_thread, node);
-		if (wt == &gp_waiters_thread)
-			continue;
-		urcu_adaptative_wake_up(&wt->wait);
-	}
-
+	urcu_wake_all_waiters(&waiters);
 gp_end:
 	if (was_online)
 		rcu_thread_online();
urcu-wait.h +98 -21

176 lines not on this page · raw message

Signature · 1 line
1.7.10.4

A message that is not in the archive (sent off-list or not yet received): [email protected]

#18 lore raw
Quoted 21 lines · Mathieu Desnoyers

Failures were not handled in the nonblocking dequeue implementation.

---
 urcu/static/wfcqueue.h |   11 +++++++++++
 1 file changed, 11 insertions(+)
urcu/static/wfcqueue.h +2 -0
index 8733771..a284b58 100644
@@ -312,6 +312,8 @@ ___cds_wfcq_dequeue(struct cds_wfcq_head *head,
 		return NULL;
 
 	node = ___cds_wfcq_node_sync_next(&head->node, blocking);
+	if (!blocking && node == CDS_WFCQ_WOULDBLOCK)
+		return CDS_WFCQ_WOULDBLOCK;
 
 	if ((next = CMM_LOAD_SHARED(node->next)) == NULL) {
 		/*

urcu/static/wfcqueue.h ___cds_wfcq_dequeue(struct cds_wfcq_head *head, · line 335

Quoted 2 lines
urcu/static/wfcqueue.h +0 -0
@@ -332,6 +334,15 @@ ___cds_wfcq_dequeue(struct cds_wfcq_head *head,
 		if (uatomic_cmpxchg(&tail->p, node, &head->node) == node)
 			return node;

Is it this simpler?
we had just loaded node->next via "if ((next = CMM_LOAD_SHARED(node->next)) == NULL)"

+ if (!blocking)
+ return CDS_WFCQ_WOULDBLOCK

urcu/static/wfcqueue.h ___cds_wfcq_dequeue(struct cds_wfcq_head *head, · line 336

Quoted 1 line
urcu/static/wfcqueue.h +0 -0
 		next = ___cds_wfcq_node_sync_next(node, blocking);

and remove the following.

Quoted 12 lines
urcu/static/wfcqueue.h +9 -0
+		/*
+		 * In nonblocking mode, if we would need to block to
+		 * get node's next, set the head next node pointer
+		 * (currently NULL) back to its original value.
+		 */
+		if (!blocking && next == CDS_WFCQ_WOULDBLOCK) {
+			head->node.next = node;
+			return CDS_WFCQ_WOULDBLOCK;
+		}
 	}
 
 	/*

A message that is not in the archive (sent off-list or not yet received): [email protected]

#20 lore raw
Quoted 32 lines · * Lai Jiangshan ([email protected]) wrote:
Quoted 21 lines · Mathieu Desnoyers

Failures were not handled in the nonblocking dequeue implementation.

---
 urcu/static/wfcqueue.h |   11 +++++++++++
 1 file changed, 11 insertions(+)
urcu/static/wfcqueue.h +2 -0
index 8733771..a284b58 100644
@@ -312,6 +312,8 @@ ___cds_wfcq_dequeue(struct cds_wfcq_head *head,
 		return NULL;
 
 	node = ___cds_wfcq_node_sync_next(&head->node, blocking);
+	if (!blocking && node == CDS_WFCQ_WOULDBLOCK)
+		return CDS_WFCQ_WOULDBLOCK;
 
 	if ((next = CMM_LOAD_SHARED(node->next)) == NULL) {
 		/*

urcu/static/wfcqueue.h ___cds_wfcq_dequeue(struct cds_wfcq_head *head, · line 335

Quoted 2 lines
urcu/static/wfcqueue.h +0 -0
@@ -332,6 +334,15 @@ ___cds_wfcq_dequeue(struct cds_wfcq_head *head,
 		if (uatomic_cmpxchg(&tail->p, node, &head->node) == node)
 			return node;

Is it this simpler?
we had just loaded node->next via "if ((next = CMM_LOAD_SHARED(node->next)) == NULL)"

(quoted diff) +2 -0
+		if (!blocking)
+			return CDS_WFCQ_WOULDBLOCK
Quoted 3 lines

urcu/static/wfcqueue.h ___cds_wfcq_dequeue(struct cds_wfcq_head *head, · line 336

Quoted 1 line
urcu/static/wfcqueue.h +0 -0
 		next = ___cds_wfcq_node_sync_next(node, blocking);

and remove the following.

So let's see. We have met the condition
"if ((next = CMM_LOAD_SHARED(node->next)) == NULL) {"

Therefore, it means that it appears, at that point, that the queue has only one node. So usually, after this test succeeds, we'll have to set the queue state to empty. There is only one case where this will not be
done: if a concurrent enqueue adds a node between this test and the cmpxchg that follows. In that case, we'll notice this because cmpxchg fails, and instead of setting the queue to "empty", we'll simply wait for enqueue to complete adding the new node, and move head forward.

The approach you propose is to consider that if cmpxchg fails, we will very likely have to block. Well, that would skip a call to ___cds_wfcq_node_sync_next, and semantically would be still right, because the only way cmpxchg can fail is if the tail pointer is being changed concurrently, which would typically make us block.

However, we'd still need to perform "head->node.next = node;" before we return CDS_WFCQ_WOULDBLOCK, because _cds_wfcq_node_init() has already set the head next pointer to NULL, and we need to undo that.

Thoughts ?

Thanks,

Mathieu

Quoted 13 lines
Quoted 13 lines
urcu/static/wfcqueue.h +9 -0
+		/*
+		 * In nonblocking mode, if we would need to block to
+		 * get node's next, set the head next node pointer
+		 * (currently NULL) back to its original value.
+		 */
+		if (!blocking && next == CDS_WFCQ_WOULDBLOCK) {
+			head->node.next = node;
+			return CDS_WFCQ_WOULDBLOCK;
+		}
 	}
 
 	/*
Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

A message that is not in the archive (sent off-list or not yet received): 20121121143716.GA21518@Krystal

#22 lore raw
Quoted 54 lines · Mathieu Desnoyers
Quoted 32 lines · * Lai Jiangshan ([email protected]) wrote:
Quoted 21 lines · Mathieu Desnoyers

Failures were not handled in the nonblocking dequeue implementation.

---
 urcu/static/wfcqueue.h |   11 +++++++++++
 1 file changed, 11 insertions(+)
urcu/static/wfcqueue.h +2 -0
index 8733771..a284b58 100644
@@ -312,6 +312,8 @@ ___cds_wfcq_dequeue(struct cds_wfcq_head *head,
 		return NULL;
 
 	node = ___cds_wfcq_node_sync_next(&head->node, blocking);
+	if (!blocking && node == CDS_WFCQ_WOULDBLOCK)
+		return CDS_WFCQ_WOULDBLOCK;
 
 	if ((next = CMM_LOAD_SHARED(node->next)) == NULL) {
 		/*

urcu/static/wfcqueue.h ___cds_wfcq_dequeue(struct cds_wfcq_head *head, · line 335

Quoted 2 lines
urcu/static/wfcqueue.h +0 -0
@@ -332,6 +334,15 @@ ___cds_wfcq_dequeue(struct cds_wfcq_head *head,
 		if (uatomic_cmpxchg(&tail->p, node, &head->node) == node)
 			return node;

Is it this simpler?
we had just loaded node->next via "if ((next = CMM_LOAD_SHARED(node->next)) == NULL)"

(quoted diff) +2 -0
+		if (!blocking)
+			return CDS_WFCQ_WOULDBLOCK
Quoted 3 lines

urcu/static/wfcqueue.h ___cds_wfcq_dequeue(struct cds_wfcq_head *head, · line 336

Quoted 1 line
urcu/static/wfcqueue.h +0 -0
 		next = ___cds_wfcq_node_sync_next(node, blocking);

and remove the following.

So let's see. We have met the condition
"if ((next = CMM_LOAD_SHARED(node->next)) == NULL) {"

Therefore, it means that it appears, at that point, that the queue has only one node. So usually, after this test succeeds, we'll have to set the queue state to empty. There is only one case where this will not be
done: if a concurrent enqueue adds a node between this test and the cmpxchg that follows. In that case, we'll notice this because cmpxchg fails, and instead of setting the queue to "empty", we'll simply wait for enqueue to complete adding the new node, and move head forward.

The approach you propose is to consider that if cmpxchg fails, we will very likely have to block. Well, that would skip a call to ___cds_wfcq_node_sync_next, and semantically would be still right, because the only way cmpxchg can fail is if the tail pointer is being changed concurrently, which would typically make us block.

Quoted 4 lines

However, we'd still need to perform "head->node.next = node;" before we return CDS_WFCQ_WOULDBLOCK, because _cds_wfcq_node_init() has already set the head next pointer to NULL, and we need to undo that.

You are right,
I just proposed some noise which is not simple as I claimed. sorry.

Thanks,
Lai

Quoted 15 lines
Quoted 14 lines
Quoted 14 lines
urcu/static/wfcqueue.h +9 -0
+		/*
+		 * In nonblocking mode, if we would need to block to
+		 * get node's next, set the head next node pointer
+		 * (currently NULL) back to its original value.
+		 */
+		if (!blocking && next == CDS_WFCQ_WOULDBLOCK) {
+			head->node.next = node;
+			return CDS_WFCQ_WOULDBLOCK;
+		}
 	}
 
 	/*

A message that is not in the archive (sent off-list or not yet received): 1353440429-19223-15-git-send-email-mathieu.desnoyers@efficios.com

#24 lore raw

Re: [PATCH 14/16] urcu-qsbr: batch concurrent synchronize_rcu()

Could you delay 14~16 for 40 days if I don't implement it in 40 days?

Quoted 274 lines · Mathieu Desnoyers

Here are benchmarks on batching of synchronize_rcu(), and it leads to very interesting scalability improvement and speedups, e.g., on a 24-core AMD, with a write-heavy scenario (4 readers threads, 20 updater threads, each updater using synchronize_rcu()):

* Serialized grace periods :

./test_urcu_qsbr 4 20 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4
rdur 0 wdur 0 nr_writers 20 wdelay 0
nr_reads 20251412728 nr_writes 1826331 nr_ops 20253239059

* Batched grace periods :

./test_urcu_qsbr 4 20 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4
rdur 0 wdur 0 nr_writers 20 wdelay 0
nr_reads 15141994746 nr_writes 9382515 nr_ops 15151377261

For a 9382515/1826331 = 5.13 speedup for 20 updaters.

Of course, we can see that readers have slowed down, probably due to increased update traffic, given there is no change to the read-side code whatsoever.

Now let's see the penality of managing the stack for single-updater. With 4 readers, single updater:

* Serialized grace periods :

./test_urcu_qsbr 4 1 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4

rdur      0 wdur      0 nr_writers   1 wdelay      0
nr_reads  19240784755 nr_writes      2130839 nr_ops  19242915594

* Batched grace periods :

./test_urcu_qsbr 4 1 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4

rdur      0 wdur      0 nr_writers   1 wdelay      0
nr_reads  19160162768 nr_writes      2253068 nr_ops  1916241583

2253068 vs 2137036 -> a couple of runs show that this difference lost in the noise for single updater.

CC Paul E. McKenney <[email protected]>
CC Lai Jiangshan <[email protected]>
CC Alan Stern <[email protected]>
---
 urcu-qsbr.c |  151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 151 insertions(+)
urcu-qsbr.c +151 -0
index 5b341b5..7f747ed 100644
@@ -36,6 +36,7 @@
 #include <poll.h>
 
 #include "urcu/wfcqueue.h"
+#include "urcu/wfstack.h"
 #include "urcu/map/urcu-qsbr.h"
 #define BUILD_QSBR_LIB
 #include "urcu/static/urcu-qsbr.h"
@@ -78,6 +79,35 @@ DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
 
 static CDS_LIST_HEAD(registry);
 
+/*
+ * Number of busy-loop attempts before waiting on futex for grace period
+ * batching.
+ */
+#define RCU_AWAKE_ATTEMPTS 1000
+
+enum adapt_wakeup_state {
+	/* AWAKE_WAITING is compared directly (futex compares it). */
+	AWAKE_WAITING =		0,
+	/* non-zero are used as masks. */
+	AWAKE_WAKEUP =		(1 << 0),
+	AWAKE_AWAKENED =	(1 << 1),
+	AWAKE_TEARDOWN =	(1 << 2),
+};
+
+struct gp_waiters_thread {
+	struct cds_wfs_node node;
+	int32_t wait_futex;
+};
+
+/*
+ * Stack keeping threads awaiting to wait for a grace period. Contains
+ * struct gp_waiters_thread objects.
+ */
+static struct cds_wfs_stack gp_waiters = {
+	.head = CDS_WFS_END,
+	.lock = PTHREAD_MUTEX_INITIALIZER,
+};
+
 static void mutex_lock(pthread_mutex_t *mutex)
 {
 	int ret;
@@ -116,6 +146,58 @@ static void wait_gp(void)
 		      NULL, NULL, 0);
 }
 
+/*
+ * Note: urcu_adaptative_wake_up needs "value" to stay allocated
+ * throughout its execution. In this scheme, the waiter owns the futex
+ * memory, and we only allow it to free this memory when it receives the
+ * AWAKE_TEARDOWN flag.
+ */
+static void urcu_adaptative_wake_up(int32_t *value)
+{
+	cmm_smp_mb();
+	assert(uatomic_read(value) == AWAKE_WAITING);
+	uatomic_set(value, AWAKE_WAKEUP);
+	if (!(uatomic_read(value) & AWAKE_AWAKENED))
+		futex_noasync(value, FUTEX_WAKE, 1, NULL, NULL, 0);
+	/* Allow teardown of "value" memory. */
+	uatomic_or(value, AWAKE_TEARDOWN);
+}
+
+/*
+ * Caller must initialize "value" to AWAKE_WAITING before passing its
+ * memory to waker thread.
+ */
+static void urcu_adaptative_busy_wait(int32_t *value)
+{
+	unsigned int i;
+
+	/* Load and test condition before read futex */
+	cmm_smp_rmb();
+	for (i = 0; i < RCU_AWAKE_ATTEMPTS; i++) {
+		if (uatomic_read(value) != AWAKE_WAITING)
+			goto skip_futex_wait;
+		caa_cpu_relax();
+	}
+	futex_noasync(value, FUTEX_WAIT, AWAKE_WAITING, NULL, NULL, 0);
+skip_futex_wait:
+
+	/* Tell waker thread than we are awakened. */
+	uatomic_or(value, AWAKE_AWAKENED);
+
+	/*
+	 * Wait until waker thread lets us know it's ok to tear down
+	 * memory allocated for value.
+	 */
+	for (i = 0; i < RCU_AWAKE_ATTEMPTS; i++) {
+		if (uatomic_read(value) & AWAKE_TEARDOWN)
+			break;
+		caa_cpu_relax();
+	}
+	while (!(uatomic_read(value) & AWAKE_TEARDOWN))
+		poll(NULL, 0, 10);
+	assert(uatomic_read(value) & AWAKE_TEARDOWN);
+}
+
 static void wait_for_readers(struct cds_list_head *input_readers,
 			struct cds_list_head *cur_snap_readers,
 			struct cds_list_head *qsreaders)
@@ -198,6 +280,9 @@ void synchronize_rcu(void)
 	CDS_LIST_HEAD(cur_snap_readers);
 	CDS_LIST_HEAD(qsreaders);
 	unsigned long was_online;
+	struct gp_waiters_thread gp_waiters_thread;
+	struct cds_wfs_head *gp_waiters_head;
+	struct cds_wfs_node *waiters_iter, *waiters_iter_n;
 
 	was_online = URCU_TLS(rcu_reader).ctr;
 
@@ -214,8 +299,26 @@ void synchronize_rcu(void)
 	else
 		cmm_smp_mb();
 
+	/*
+	 * Add ourself to gp_waiters stack of threads awaiting to wait
+	 * for a grace period. Proceed to perform the grace period only
+	 * if we are the first thread added into the stack.
+	 */
+	cds_wfs_node_init(&gp_waiters_thread.node);
+	gp_waiters_thread.wait_futex = AWAKE_WAITING;
+	if (cds_wfs_push(&gp_waiters, &gp_waiters_node) != 0) {
+		/* Not first in stack: will be awakened by another thread. */
+		urcu_adaptative_busy_wait(&gp_waiters_thread.wait_futex);
+		goto gp_end;
+	}
+
 	mutex_lock(&rcu_gp_lock);
 
+	/*
+	 * Pop all waiters into our local stack head.
+	 */
+	gp_waiters_head = __cds_wfs_pop_all(&gp_waiters);
+
 	if (cds_list_empty(&registry))
 		goto out;
 
@@ -272,6 +375,19 @@ void synchronize_rcu(void)
 out:
 	mutex_unlock(&rcu_gp_lock);
 
+	/* Wake all waiters in our stack head, excluding ourself. */
+	cds_wfs_for_each_blocking_safe(gp_waiters_head, waiters_iter,
+				waiters_iter_n) {
+		struct gp_waiters_thread *wt;
+
+		wt = caa_container_of(waiters_iter,
+				struct gp_waiters_thread, node);
+		if (wt == &gp_waiters_thread)
+			continue;
+		urcu_adaptative_wake_up(&wt->wait_futex);
+	}
+
+gp_end:
 	/*
 	 * Finish waiting for reader threads before letting the old ptr being
 	 * freed.
@@ -286,6 +402,9 @@ void synchronize_rcu(void)
 {
 	CDS_LIST_HEAD(qsreaders);
 	unsigned long was_online;
+	struct gp_waiters_thread gp_waiters_thread;
+	struct cds_wfs_head *gp_waiters_head;
+	struct cds_wfs_node *waiters_iter, *waiters_iter_n;
 
 	was_online = URCU_TLS(rcu_reader).ctr;
 
@@ -299,7 +418,26 @@ void synchronize_rcu(void)
 	else
 		cmm_smp_mb();
 
+	/*
+	 * Add ourself to gp_waiters stack of threads awaiting to wait
+	 * for a grace period. Proceed to perform the grace period only
+	 * if we are the first thread added into the stack.
+	 */
+	cds_wfs_node_init(&gp_waiters_thread.node);
+	gp_waiters_thread.wait_futex = AWAKE_WAITING;
+	if (cds_wfs_push(&gp_waiters, &gp_waiters_thread.node) != 0) {
+		/* Not first in stack: will be awakened by another thread. */
+		urcu_adaptative_busy_wait(&gp_waiters_thread.wait_futex);
+		goto gp_end;
+	}
+
 	mutex_lock(&rcu_gp_lock);
+
+	/*
+	 * Pop all waiters into our local stack head.
+	 */
+	gp_waiters_head = __cds_wfs_pop_all(&gp_waiters);
+
 	if (cds_list_empty(&registry))
 		goto out;
 
@@ -334,6 +472,19 @@ void synchronize_rcu(void)
 out:
 	mutex_unlock(&rcu_gp_lock);
 
+	/* Wake all waiters in our stack head, excluding ourself. */
+	cds_wfs_for_each_blocking_safe(gp_waiters_head, waiters_iter,
+				waiters_iter_n) {
+		struct gp_waiters_thread *wt;
+
+		wt = caa_container_of(waiters_iter,
+				struct gp_waiters_thread, node);
+		if (wt == &gp_waiters_thread)
+			continue;
+		urcu_adaptative_wake_up(&wt->wait_futex);
+	}
+
+gp_end:
 	if (was_online)
 		rcu_thread_online();
 	else

A message that is not in the archive (sent off-list or not yet received): [email protected]

#26 lore raw

Re: [PATCH 14/16] urcu-qsbr: batch concurrent synchronize_rcu()

Quoted 1 line · * Lai Jiangshan ([email protected]) wrote:

Could you delay 14~16 for 40 days if I don't implement it in 40 days?

I'm curious to know more about the changes you are planning. Is that another way to implement grace periods that would allow multiple threads to execute synchronize_rcu() concurrently ?

Please note that changes in these algorithms will need to go through very strict review/validation/verification. So I expect that if it takes 40 days to implement, we can plan at least 3-4 months of validation work.

With that in mind, would it make sense to merge the batching approach in the meantime ? The advantage of the batching approach is that it does not touch the core of the synchronization algorithm.

Thoughts ?

Thanks,

Mathieu

Quoted 277 lines · Mathieu Desnoyers
Quoted 274 lines

Here are benchmarks on batching of synchronize_rcu(), and it leads to very interesting scalability improvement and speedups, e.g., on a 24-core AMD, with a write-heavy scenario (4 readers threads, 20 updater threads, each updater using synchronize_rcu()):

* Serialized grace periods :

./test_urcu_qsbr 4 20 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4
rdur 0 wdur 0 nr_writers 20 wdelay 0
nr_reads 20251412728 nr_writes 1826331 nr_ops 20253239059

* Batched grace periods :

./test_urcu_qsbr 4 20 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4
rdur 0 wdur 0 nr_writers 20 wdelay 0
nr_reads 15141994746 nr_writes 9382515 nr_ops 15151377261

For a 9382515/1826331 = 5.13 speedup for 20 updaters.

Of course, we can see that readers have slowed down, probably due to increased update traffic, given there is no change to the read-side code whatsoever.

Now let's see the penality of managing the stack for single-updater. With 4 readers, single updater:

* Serialized grace periods :

./test_urcu_qsbr 4 1 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4

rdur      0 wdur      0 nr_writers   1 wdelay      0
nr_reads  19240784755 nr_writes      2130839 nr_ops  19242915594

* Batched grace periods :

./test_urcu_qsbr 4 1 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4

rdur      0 wdur      0 nr_writers   1 wdelay      0
nr_reads  19160162768 nr_writes      2253068 nr_ops  1916241583

2253068 vs 2137036 -> a couple of runs show that this difference lost in the noise for single updater.

CC Paul E. McKenney <[email protected]>
CC Lai Jiangshan <[email protected]>
CC Alan Stern <[email protected]>
---
 urcu-qsbr.c |  151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 151 insertions(+)
urcu-qsbr.c +151 -0
index 5b341b5..7f747ed 100644
@@ -36,6 +36,7 @@
 #include <poll.h>
 
 #include "urcu/wfcqueue.h"
+#include "urcu/wfstack.h"
 #include "urcu/map/urcu-qsbr.h"
 #define BUILD_QSBR_LIB
 #include "urcu/static/urcu-qsbr.h"
@@ -78,6 +79,35 @@ DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
 
 static CDS_LIST_HEAD(registry);
 
+/*
+ * Number of busy-loop attempts before waiting on futex for grace period
+ * batching.
+ */
+#define RCU_AWAKE_ATTEMPTS 1000
+
+enum adapt_wakeup_state {
+	/* AWAKE_WAITING is compared directly (futex compares it). */
+	AWAKE_WAITING =		0,
+	/* non-zero are used as masks. */
+	AWAKE_WAKEUP =		(1 << 0),
+	AWAKE_AWAKENED =	(1 << 1),
+	AWAKE_TEARDOWN =	(1 << 2),
+};
+
+struct gp_waiters_thread {
+	struct cds_wfs_node node;
+	int32_t wait_futex;
+};
+
+/*
+ * Stack keeping threads awaiting to wait for a grace period. Contains
+ * struct gp_waiters_thread objects.
+ */
+static struct cds_wfs_stack gp_waiters = {
+	.head = CDS_WFS_END,
+	.lock = PTHREAD_MUTEX_INITIALIZER,
+};
+
 static void mutex_lock(pthread_mutex_t *mutex)
 {
 	int ret;
@@ -116,6 +146,58 @@ static void wait_gp(void)
 		      NULL, NULL, 0);
 }
 
+/*
+ * Note: urcu_adaptative_wake_up needs "value" to stay allocated
+ * throughout its execution. In this scheme, the waiter owns the futex
+ * memory, and we only allow it to free this memory when it receives the
+ * AWAKE_TEARDOWN flag.
+ */
+static void urcu_adaptative_wake_up(int32_t *value)
+{
+	cmm_smp_mb();
+	assert(uatomic_read(value) == AWAKE_WAITING);
+	uatomic_set(value, AWAKE_WAKEUP);
+	if (!(uatomic_read(value) & AWAKE_AWAKENED))
+		futex_noasync(value, FUTEX_WAKE, 1, NULL, NULL, 0);
+	/* Allow teardown of "value" memory. */
+	uatomic_or(value, AWAKE_TEARDOWN);
+}
+
+/*
+ * Caller must initialize "value" to AWAKE_WAITING before passing its
+ * memory to waker thread.
+ */
+static void urcu_adaptative_busy_wait(int32_t *value)
+{
+	unsigned int i;
+
+	/* Load and test condition before read futex */
+	cmm_smp_rmb();
+	for (i = 0; i < RCU_AWAKE_ATTEMPTS; i++) {
+		if (uatomic_read(value) != AWAKE_WAITING)
+			goto skip_futex_wait;
+		caa_cpu_relax();
+	}
+	futex_noasync(value, FUTEX_WAIT, AWAKE_WAITING, NULL, NULL, 0);
+skip_futex_wait:
+
+	/* Tell waker thread than we are awakened. */
+	uatomic_or(value, AWAKE_AWAKENED);
+
+	/*
+	 * Wait until waker thread lets us know it's ok to tear down
+	 * memory allocated for value.
+	 */
+	for (i = 0; i < RCU_AWAKE_ATTEMPTS; i++) {
+		if (uatomic_read(value) & AWAKE_TEARDOWN)
+			break;
+		caa_cpu_relax();
+	}
+	while (!(uatomic_read(value) & AWAKE_TEARDOWN))
+		poll(NULL, 0, 10);
+	assert(uatomic_read(value) & AWAKE_TEARDOWN);
+}
+
 static void wait_for_readers(struct cds_list_head *input_readers,
 			struct cds_list_head *cur_snap_readers,
 			struct cds_list_head *qsreaders)
@@ -198,6 +280,9 @@ void synchronize_rcu(void)
 	CDS_LIST_HEAD(cur_snap_readers);
 	CDS_LIST_HEAD(qsreaders);
 	unsigned long was_online;
+	struct gp_waiters_thread gp_waiters_thread;
+	struct cds_wfs_head *gp_waiters_head;
+	struct cds_wfs_node *waiters_iter, *waiters_iter_n;
 
 	was_online = URCU_TLS(rcu_reader).ctr;
 
@@ -214,8 +299,26 @@ void synchronize_rcu(void)
 	else
 		cmm_smp_mb();
 
+	/*
+	 * Add ourself to gp_waiters stack of threads awaiting to wait
+	 * for a grace period. Proceed to perform the grace period only
+	 * if we are the first thread added into the stack.
+	 */
+	cds_wfs_node_init(&gp_waiters_thread.node);
+	gp_waiters_thread.wait_futex = AWAKE_WAITING;
+	if (cds_wfs_push(&gp_waiters, &gp_waiters_node) != 0) {
+		/* Not first in stack: will be awakened by another thread. */
+		urcu_adaptative_busy_wait(&gp_waiters_thread.wait_futex);
+		goto gp_end;
+	}
+
 	mutex_lock(&rcu_gp_lock);
 
+	/*
+	 * Pop all waiters into our local stack head.
+	 */
+	gp_waiters_head = __cds_wfs_pop_all(&gp_waiters);
+
 	if (cds_list_empty(&registry))
 		goto out;
 
@@ -272,6 +375,19 @@ void synchronize_rcu(void)
 out:
 	mutex_unlock(&rcu_gp_lock);
 
+	/* Wake all waiters in our stack head, excluding ourself. */
+	cds_wfs_for_each_blocking_safe(gp_waiters_head, waiters_iter,
+				waiters_iter_n) {
+		struct gp_waiters_thread *wt;
+
+		wt = caa_container_of(waiters_iter,
+				struct gp_waiters_thread, node);
+		if (wt == &gp_waiters_thread)
+			continue;
+		urcu_adaptative_wake_up(&wt->wait_futex);
+	}
+
+gp_end:
 	/*
 	 * Finish waiting for reader threads before letting the old ptr being
 	 * freed.
@@ -286,6 +402,9 @@ void synchronize_rcu(void)
 {
 	CDS_LIST_HEAD(qsreaders);
 	unsigned long was_online;
+	struct gp_waiters_thread gp_waiters_thread;
+	struct cds_wfs_head *gp_waiters_head;
+	struct cds_wfs_node *waiters_iter, *waiters_iter_n;
 
 	was_online = URCU_TLS(rcu_reader).ctr;
 
@@ -299,7 +418,26 @@ void synchronize_rcu(void)
 	else
 		cmm_smp_mb();
 
+	/*
+	 * Add ourself to gp_waiters stack of threads awaiting to wait
+	 * for a grace period. Proceed to perform the grace period only
+	 * if we are the first thread added into the stack.
+	 */
+	cds_wfs_node_init(&gp_waiters_thread.node);
+	gp_waiters_thread.wait_futex = AWAKE_WAITING;
+	if (cds_wfs_push(&gp_waiters, &gp_waiters_thread.node) != 0) {
+		/* Not first in stack: will be awakened by another thread. */
+		urcu_adaptative_busy_wait(&gp_waiters_thread.wait_futex);
+		goto gp_end;
+	}
+
 	mutex_lock(&rcu_gp_lock);
+
+	/*
+	 * Pop all waiters into our local stack head.
+	 */
+	gp_waiters_head = __cds_wfs_pop_all(&gp_waiters);
+
 	if (cds_list_empty(&registry))
 		goto out;
 
@@ -334,6 +472,19 @@ void synchronize_rcu(void)
 out:
 	mutex_unlock(&rcu_gp_lock);
 
+	/* Wake all waiters in our stack head, excluding ourself. */
+	cds_wfs_for_each_blocking_safe(gp_waiters_head, waiters_iter,
+				waiters_iter_n) {
+		struct gp_waiters_thread *wt;
+
+		wt = caa_container_of(waiters_iter,
+				struct gp_waiters_thread, node);
+		if (wt == &gp_waiters_thread)
+			continue;
+		urcu_adaptative_wake_up(&wt->wait_futex);
+	}
+
+gp_end:
 	if (was_online)
 		rcu_thread_online();
 	else
Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

A message that is not in the archive (sent off-list or not yet received): 20121121183304.GA25932@Krystal

#28 lore raw

Re: [PATCH 14/16] urcu-qsbr: batch concurrent synchronize_rcu()

Quoted 6 lines · Mathieu Desnoyers
Quoted 1 line · * Lai Jiangshan ([email protected]) wrote:

Could you delay 14~16 for 40 days if I don't implement it in 40 days?

I'm curious to know more about the changes you are planning. Is that another way to implement grace periods that would allow multiple threads to execute synchronize_rcu() concurrently ?

synchronize_rcu()s in this implement share coarse-grain step(1 GP) to achieve concurrence. My implement will use fine-grain step(1 check or 1 flip) like SRCU. and call_rcu() is also considered in this implement to avoid unneeded wait.

Quoted 4 lines

Please note that changes in these algorithms will need to go through very strict review/validation/verification. So I expect that if it takes 40 days to implement, we can plan at least 3-4 months of validation work.

I means I don't have time. If I can't steal some time from the late 40 days, this code is OK for me.

Quoted 289 lines

With that in mind, would it make sense to merge the batching approach in the meantime ? The advantage of the batching approach is that it does not touch the core of the synchronization algorithm.

Thoughts ?

Thanks,

Mathieu

Quoted 277 lines · Mathieu Desnoyers
Quoted 274 lines

Here are benchmarks on batching of synchronize_rcu(), and it leads to very interesting scalability improvement and speedups, e.g., on a 24-core AMD, with a write-heavy scenario (4 readers threads, 20 updater threads, each updater using synchronize_rcu()):

* Serialized grace periods :

./test_urcu_qsbr 4 20 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4
rdur 0 wdur 0 nr_writers 20 wdelay 0
nr_reads 20251412728 nr_writes 1826331 nr_ops 20253239059

* Batched grace periods :

./test_urcu_qsbr 4 20 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4
rdur 0 wdur 0 nr_writers 20 wdelay 0
nr_reads 15141994746 nr_writes 9382515 nr_ops 15151377261

For a 9382515/1826331 = 5.13 speedup for 20 updaters.

Of course, we can see that readers have slowed down, probably due to increased update traffic, given there is no change to the read-side code whatsoever.

Now let's see the penality of managing the stack for single-updater. With 4 readers, single updater:

* Serialized grace periods :

./test_urcu_qsbr 4 1 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4

rdur      0 wdur      0 nr_writers   1 wdelay      0
nr_reads  19240784755 nr_writes      2130839 nr_ops  19242915594

* Batched grace periods :

./test_urcu_qsbr 4 1 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4

rdur      0 wdur      0 nr_writers   1 wdelay      0
nr_reads  19160162768 nr_writes      2253068 nr_ops  1916241583

2253068 vs 2137036 -> a couple of runs show that this difference lost in the noise for single updater.

CC Paul E. McKenney <[email protected]>
CC Lai Jiangshan <[email protected]>
CC Alan Stern <[email protected]>
---
 urcu-qsbr.c |  151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 151 insertions(+)
urcu-qsbr.c +151 -0
index 5b341b5..7f747ed 100644
@@ -36,6 +36,7 @@
 #include <poll.h>
 
 #include "urcu/wfcqueue.h"
+#include "urcu/wfstack.h"
 #include "urcu/map/urcu-qsbr.h"
 #define BUILD_QSBR_LIB
 #include "urcu/static/urcu-qsbr.h"
@@ -78,6 +79,35 @@ DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
 
 static CDS_LIST_HEAD(registry);
 
+/*
+ * Number of busy-loop attempts before waiting on futex for grace period
+ * batching.
+ */
+#define RCU_AWAKE_ATTEMPTS 1000
+
+enum adapt_wakeup_state {
+	/* AWAKE_WAITING is compared directly (futex compares it). */
+	AWAKE_WAITING =		0,
+	/* non-zero are used as masks. */
+	AWAKE_WAKEUP =		(1 << 0),
+	AWAKE_AWAKENED =	(1 << 1),
+	AWAKE_TEARDOWN =	(1 << 2),
+};
+
+struct gp_waiters_thread {
+	struct cds_wfs_node node;
+	int32_t wait_futex;
+};
+
+/*
+ * Stack keeping threads awaiting to wait for a grace period. Contains
+ * struct gp_waiters_thread objects.
+ */
+static struct cds_wfs_stack gp_waiters = {
+	.head = CDS_WFS_END,
+	.lock = PTHREAD_MUTEX_INITIALIZER,
+};
+
 static void mutex_lock(pthread_mutex_t *mutex)
 {
 	int ret;
@@ -116,6 +146,58 @@ static void wait_gp(void)
 		      NULL, NULL, 0);
 }
 
+/*
+ * Note: urcu_adaptative_wake_up needs "value" to stay allocated
+ * throughout its execution. In this scheme, the waiter owns the futex
+ * memory, and we only allow it to free this memory when it receives the
+ * AWAKE_TEARDOWN flag.
+ */
+static void urcu_adaptative_wake_up(int32_t *value)
+{
+	cmm_smp_mb();
+	assert(uatomic_read(value) == AWAKE_WAITING);
+	uatomic_set(value, AWAKE_WAKEUP);
+	if (!(uatomic_read(value) & AWAKE_AWAKENED))
+		futex_noasync(value, FUTEX_WAKE, 1, NULL, NULL, 0);
+	/* Allow teardown of "value" memory. */
+	uatomic_or(value, AWAKE_TEARDOWN);
+}
+
+/*
+ * Caller must initialize "value" to AWAKE_WAITING before passing its
+ * memory to waker thread.
+ */
+static void urcu_adaptative_busy_wait(int32_t *value)
+{
+	unsigned int i;
+
+	/* Load and test condition before read futex */
+	cmm_smp_rmb();
+	for (i = 0; i < RCU_AWAKE_ATTEMPTS; i++) {
+		if (uatomic_read(value) != AWAKE_WAITING)
+			goto skip_futex_wait;
+		caa_cpu_relax();
+	}
+	futex_noasync(value, FUTEX_WAIT, AWAKE_WAITING, NULL, NULL, 0);
+skip_futex_wait:
+
+	/* Tell waker thread than we are awakened. */
+	uatomic_or(value, AWAKE_AWAKENED);
+
+	/*
+	 * Wait until waker thread lets us know it's ok to tear down
+	 * memory allocated for value.
+	 */
+	for (i = 0; i < RCU_AWAKE_ATTEMPTS; i++) {
+		if (uatomic_read(value) & AWAKE_TEARDOWN)
+			break;
+		caa_cpu_relax();
+	}
+	while (!(uatomic_read(value) & AWAKE_TEARDOWN))
+		poll(NULL, 0, 10);
+	assert(uatomic_read(value) & AWAKE_TEARDOWN);
+}
+
 static void wait_for_readers(struct cds_list_head *input_readers,
 			struct cds_list_head *cur_snap_readers,
 			struct cds_list_head *qsreaders)
@@ -198,6 +280,9 @@ void synchronize_rcu(void)
 	CDS_LIST_HEAD(cur_snap_readers);
 	CDS_LIST_HEAD(qsreaders);
 	unsigned long was_online;
+	struct gp_waiters_thread gp_waiters_thread;
+	struct cds_wfs_head *gp_waiters_head;
+	struct cds_wfs_node *waiters_iter, *waiters_iter_n;
 
 	was_online = URCU_TLS(rcu_reader).ctr;
 
@@ -214,8 +299,26 @@ void synchronize_rcu(void)
 	else
 		cmm_smp_mb();
 
+	/*
+	 * Add ourself to gp_waiters stack of threads awaiting to wait
+	 * for a grace period. Proceed to perform the grace period only
+	 * if we are the first thread added into the stack.
+	 */
+	cds_wfs_node_init(&gp_waiters_thread.node);
+	gp_waiters_thread.wait_futex = AWAKE_WAITING;
+	if (cds_wfs_push(&gp_waiters, &gp_waiters_node) != 0) {
+		/* Not first in stack: will be awakened by another thread. */
+		urcu_adaptative_busy_wait(&gp_waiters_thread.wait_futex);
+		goto gp_end;
+	}
+
 	mutex_lock(&rcu_gp_lock);
 
+	/*
+	 * Pop all waiters into our local stack head.
+	 */
+	gp_waiters_head = __cds_wfs_pop_all(&gp_waiters);
+
 	if (cds_list_empty(&registry))
 		goto out;
 
@@ -272,6 +375,19 @@ void synchronize_rcu(void)
 out:
 	mutex_unlock(&rcu_gp_lock);
 
+	/* Wake all waiters in our stack head, excluding ourself. */
+	cds_wfs_for_each_blocking_safe(gp_waiters_head, waiters_iter,
+				waiters_iter_n) {
+		struct gp_waiters_thread *wt;
+
+		wt = caa_container_of(waiters_iter,
+				struct gp_waiters_thread, node);
+		if (wt == &gp_waiters_thread)
+			continue;
+		urcu_adaptative_wake_up(&wt->wait_futex);
+	}
+
+gp_end:
 	/*
 	 * Finish waiting for reader threads before letting the old ptr being
 	 * freed.
@@ -286,6 +402,9 @@ void synchronize_rcu(void)
 {
 	CDS_LIST_HEAD(qsreaders);
 	unsigned long was_online;
+	struct gp_waiters_thread gp_waiters_thread;
+	struct cds_wfs_head *gp_waiters_head;
+	struct cds_wfs_node *waiters_iter, *waiters_iter_n;
 
 	was_online = URCU_TLS(rcu_reader).ctr;
 
@@ -299,7 +418,26 @@ void synchronize_rcu(void)
 	else
 		cmm_smp_mb();
 
+	/*
+	 * Add ourself to gp_waiters stack of threads awaiting to wait
+	 * for a grace period. Proceed to perform the grace period only
+	 * if we are the first thread added into the stack.
+	 */
+	cds_wfs_node_init(&gp_waiters_thread.node);
+	gp_waiters_thread.wait_futex = AWAKE_WAITING;
+	if (cds_wfs_push(&gp_waiters, &gp_waiters_thread.node) != 0) {
+		/* Not first in stack: will be awakened by another thread. */
+		urcu_adaptative_busy_wait(&gp_waiters_thread.wait_futex);
+		goto gp_end;
+	}
+
 	mutex_lock(&rcu_gp_lock);
+
+	/*
+	 * Pop all waiters into our local stack head.
+	 */
+	gp_waiters_head = __cds_wfs_pop_all(&gp_waiters);
+
 	if (cds_list_empty(&registry))
 		goto out;
 
@@ -334,6 +472,19 @@ void synchronize_rcu(void)
 out:
 	mutex_unlock(&rcu_gp_lock);
 
+	/* Wake all waiters in our stack head, excluding ourself. */
+	cds_wfs_for_each_blocking_safe(gp_waiters_head, waiters_iter,
+				waiters_iter_n) {
+		struct gp_waiters_thread *wt;
+
+		wt = caa_container_of(waiters_iter,
+				struct gp_waiters_thread, node);
+		if (wt == &gp_waiters_thread)
+			continue;
+		urcu_adaptative_wake_up(&wt->wait_futex);
+	}
+
+gp_end:
 	if (was_online)
 		rcu_thread_online();
 	else

A message that is not in the archive (sent off-list or not yet received): [email protected]

#30 lore raw

Re: [PATCH 14/16] urcu-qsbr: batch concurrent synchronize_rcu()

Quoted 14 lines · Lai Jiangshan
Quoted 6 lines · Mathieu Desnoyers
Quoted 1 line · * Lai Jiangshan ([email protected]) wrote:

Could you delay 14~16 for 40 days if I don't implement it in 40 days?

I'm curious to know more about the changes you are planning. Is that another way to implement grace periods that would allow multiple threads to execute synchronize_rcu() concurrently ?

synchronize_rcu()s in this implement share coarse-grain step(1 GP) to achieve concurrence. My implement will use fine-grain step(1 check or 1 flip) like SRCU. and call_rcu() is also considered in this implement to avoid unneeded wait.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Please note that changes in these algorithms will need to go through very strict review/validation/verification. So I expect that if it takes 40 days to implement, we can plan at least 3-4 months of validation work.

I means I don't have time. If I can't steal some time from the late 40 days, this code is OK for me.

Why don't we take the current code, which would allow some academic projects to test on large systems in the next few months, and then replace it with your code when available and if appropriate?

Thanx, Paul

Quoted 289 lines
Quoted 288 lines

With that in mind, would it make sense to merge the batching approach in the meantime ? The advantage of the batching approach is that it does not touch the core of the synchronization algorithm.

Thoughts ?

Thanks,

Mathieu

Quoted 277 lines · Mathieu Desnoyers
Quoted 274 lines

Here are benchmarks on batching of synchronize_rcu(), and it leads to very interesting scalability improvement and speedups, e.g., on a 24-core AMD, with a write-heavy scenario (4 readers threads, 20 updater threads, each updater using synchronize_rcu()):

* Serialized grace periods :

./test_urcu_qsbr 4 20 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4
rdur 0 wdur 0 nr_writers 20 wdelay 0
nr_reads 20251412728 nr_writes 1826331 nr_ops 20253239059

* Batched grace periods :

./test_urcu_qsbr 4 20 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4
rdur 0 wdur 0 nr_writers 20 wdelay 0
nr_reads 15141994746 nr_writes 9382515 nr_ops 15151377261

For a 9382515/1826331 = 5.13 speedup for 20 updaters.

Of course, we can see that readers have slowed down, probably due to increased update traffic, given there is no change to the read-side code whatsoever.

Now let's see the penality of managing the stack for single-updater. With 4 readers, single updater:

* Serialized grace periods :

./test_urcu_qsbr 4 1 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4

rdur      0 wdur      0 nr_writers   1 wdelay      0
nr_reads  19240784755 nr_writes      2130839 nr_ops  19242915594

* Batched grace periods :

./test_urcu_qsbr 4 1 20
SUMMARY ./test_urcu_qsbr testdur 20 nr_readers 4

rdur      0 wdur      0 nr_writers   1 wdelay      0
nr_reads  19160162768 nr_writes      2253068 nr_ops  1916241583

2253068 vs 2137036 -> a couple of runs show that this difference lost in the noise for single updater.

CC Paul E. McKenney <[email protected]>
CC Lai Jiangshan <[email protected]>
CC Alan Stern <[email protected]>
---
 urcu-qsbr.c |  151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 151 insertions(+)
urcu-qsbr.c +151 -0
index 5b341b5..7f747ed 100644
@@ -36,6 +36,7 @@
 #include <poll.h>
 
 #include "urcu/wfcqueue.h"
+#include "urcu/wfstack.h"
 #include "urcu/map/urcu-qsbr.h"
 #define BUILD_QSBR_LIB
 #include "urcu/static/urcu-qsbr.h"
@@ -78,6 +79,35 @@ DEFINE_URCU_TLS(unsigned int, rcu_rand_yield);
 
 static CDS_LIST_HEAD(registry);
 
+/*
+ * Number of busy-loop attempts before waiting on futex for grace period
+ * batching.
+ */
+#define RCU_AWAKE_ATTEMPTS 1000
+
+enum adapt_wakeup_state {
+	/* AWAKE_WAITING is compared directly (futex compares it). */
+	AWAKE_WAITING =		0,
+	/* non-zero are used as masks. */
+	AWAKE_WAKEUP =		(1 << 0),
+	AWAKE_AWAKENED =	(1 << 1),
+	AWAKE_TEARDOWN =	(1 << 2),
+};
+
+struct gp_waiters_thread {
+	struct cds_wfs_node node;
+	int32_t wait_futex;
+};
+
+/*
+ * Stack keeping threads awaiting to wait for a grace period. Contains
+ * struct gp_waiters_thread objects.
+ */
+static struct cds_wfs_stack gp_waiters = {
+	.head = CDS_WFS_END,
+	.lock = PTHREAD_MUTEX_INITIALIZER,
+};
+
 static void mutex_lock(pthread_mutex_t *mutex)
 {
 	int ret;
@@ -116,6 +146,58 @@ static void wait_gp(void)
 		      NULL, NULL, 0);
 }
 
+/*
+ * Note: urcu_adaptative_wake_up needs "value" to stay allocated
+ * throughout its execution. In this scheme, the waiter owns the futex
+ * memory, and we only allow it to free this memory when it receives the
+ * AWAKE_TEARDOWN flag.
+ */
+static void urcu_adaptative_wake_up(int32_t *value)
+{
+	cmm_smp_mb();
+	assert(uatomic_read(value) == AWAKE_WAITING);
+	uatomic_set(value, AWAKE_WAKEUP);
+	if (!(uatomic_read(value) & AWAKE_AWAKENED))
+		futex_noasync(value, FUTEX_WAKE, 1, NULL, NULL, 0);
+	/* Allow teardown of "value" memory. */
+	uatomic_or(value, AWAKE_TEARDOWN);
+}
+
+/*
+ * Caller must initialize "value" to AWAKE_WAITING before passing its
+ * memory to waker thread.
+ */
+static void urcu_adaptative_busy_wait(int32_t *value)
+{
+	unsigned int i;
+
+	/* Load and test condition before read futex */
+	cmm_smp_rmb();
+	for (i = 0; i < RCU_AWAKE_ATTEMPTS; i++) {
+		if (uatomic_read(value) != AWAKE_WAITING)
+			goto skip_futex_wait;
+		caa_cpu_relax();
+	}
+	futex_noasync(value, FUTEX_WAIT, AWAKE_WAITING, NULL, NULL, 0);
+skip_futex_wait:
+
+	/* Tell waker thread than we are awakened. */
+	uatomic_or(value, AWAKE_AWAKENED);
+
+	/*
+	 * Wait until waker thread lets us know it's ok to tear down
+	 * memory allocated for value.
+	 */
+	for (i = 0; i < RCU_AWAKE_ATTEMPTS; i++) {
+		if (uatomic_read(value) & AWAKE_TEARDOWN)
+			break;
+		caa_cpu_relax();
+	}
+	while (!(uatomic_read(value) & AWAKE_TEARDOWN))
+		poll(NULL, 0, 10);
+	assert(uatomic_read(value) & AWAKE_TEARDOWN);
+}
+
 static void wait_for_readers(struct cds_list_head *input_readers,
 			struct cds_list_head *cur_snap_readers,
 			struct cds_list_head *qsreaders)
@@ -198,6 +280,9 @@ void synchronize_rcu(void)
 	CDS_LIST_HEAD(cur_snap_readers);
 	CDS_LIST_HEAD(qsreaders);
 	unsigned long was_online;
+	struct gp_waiters_thread gp_waiters_thread;
+	struct cds_wfs_head *gp_waiters_head;
+	struct cds_wfs_node *waiters_iter, *waiters_iter_n;
 
 	was_online = URCU_TLS(rcu_reader).ctr;
 
@@ -214,8 +299,26 @@ void synchronize_rcu(void)
 	else
 		cmm_smp_mb();
 
+	/*
+	 * Add ourself to gp_waiters stack of threads awaiting to wait
+	 * for a grace period. Proceed to perform the grace period only
+	 * if we are the first thread added into the stack.
+	 */
+	cds_wfs_node_init(&gp_waiters_thread.node);
+	gp_waiters_thread.wait_futex = AWAKE_WAITING;
+	if (cds_wfs_push(&gp_waiters, &gp_waiters_node) != 0) {
+		/* Not first in stack: will be awakened by another thread. */
+		urcu_adaptative_busy_wait(&gp_waiters_thread.wait_futex);
+		goto gp_end;
+	}
+
 	mutex_lock(&rcu_gp_lock);
 
+	/*
+	 * Pop all waiters into our local stack head.
+	 */
+	gp_waiters_head = __cds_wfs_pop_all(&gp_waiters);
+
 	if (cds_list_empty(&registry))
 		goto out;
 
@@ -272,6 +375,19 @@ void synchronize_rcu(void)
 out:
 	mutex_unlock(&rcu_gp_lock);
 
+	/* Wake all waiters in our stack head, excluding ourself. */
+	cds_wfs_for_each_blocking_safe(gp_waiters_head, waiters_iter,
+				waiters_iter_n) {
+		struct gp_waiters_thread *wt;
+
+		wt = caa_container_of(waiters_iter,
+				struct gp_waiters_thread, node);
+		if (wt == &gp_waiters_thread)
+			continue;
+		urcu_adaptative_wake_up(&wt->wait_futex);
+	}
+
+gp_end:
 	/*
 	 * Finish waiting for reader threads before letting the old ptr being
 	 * freed.
@@ -286,6 +402,9 @@ void synchronize_rcu(void)
 {
 	CDS_LIST_HEAD(qsreaders);
 	unsigned long was_online;
+	struct gp_waiters_thread gp_waiters_thread;
+	struct cds_wfs_head *gp_waiters_head;
+	struct cds_wfs_node *waiters_iter, *waiters_iter_n;
 
 	was_online = URCU_TLS(rcu_reader).ctr;
 
@@ -299,7 +418,26 @@ void synchronize_rcu(void)
 	else
 		cmm_smp_mb();
 
+	/*
+	 * Add ourself to gp_waiters stack of threads awaiting to wait
+	 * for a grace period. Proceed to perform the grace period only
+	 * if we are the first thread added into the stack.
+	 */
+	cds_wfs_node_init(&gp_waiters_thread.node);
+	gp_waiters_thread.wait_futex = AWAKE_WAITING;
+	if (cds_wfs_push(&gp_waiters, &gp_waiters_thread.node) != 0) {
+		/* Not first in stack: will be awakened by another thread. */
+		urcu_adaptative_busy_wait(&gp_waiters_thread.wait_futex);
+		goto gp_end;
+	}
+
 	mutex_lock(&rcu_gp_lock);
+
+	/*
+	 * Pop all waiters into our local stack head.
+	 */
+	gp_waiters_head = __cds_wfs_pop_all(&gp_waiters);
+
 	if (cds_list_empty(&registry))
 		goto out;
 
@@ -334,6 +472,19 @@ void synchronize_rcu(void)
 out:
 	mutex_unlock(&rcu_gp_lock);
 
+	/* Wake all waiters in our stack head, excluding ourself. */
+	cds_wfs_for_each_blocking_safe(gp_waiters_head, waiters_iter,
+				waiters_iter_n) {
+		struct gp_waiters_thread *wt;
+
+		wt = caa_container_of(waiters_iter,
+				struct gp_waiters_thread, node);
+		if (wt == &gp_waiters_thread)
+			continue;
+		urcu_adaptative_wake_up(&wt->wait_futex);
+	}
+
+gp_end:
 	if (was_online)
 		rcu_thread_online();
 	else

A message that is not in the archive (sent off-list or not yet received): [email protected]

#32 lore raw

Re: [PATCH 14/16] urcu-qsbr: batch concurrent synchronize_rcu()

Quoted 19 lines · Paul E. McKenney
Quoted 18 lines · Lai Jiangshan
Quoted 6 lines · Mathieu Desnoyers
Quoted 1 line · * Lai Jiangshan ([email protected]) wrote:

Could you delay 14~16 for 40 days if I don't implement it in 40 days?

I'm curious to know more about the changes you are planning. Is that another way to implement grace periods that would allow multiple threads to execute synchronize_rcu() concurrently ?

synchronize_rcu()s in this implement share coarse-grain step(1 GP) to achieve concurrence. My implement will use fine-grain step(1 check or 1 flip) like SRCU. and call_rcu() is also considered in this implement to avoid unneeded wait.

Quoted 4 lines

Please note that changes in these algorithms will need to go through very strict review/validation/verification. So I expect that if it takes 40 days to implement, we can plan at least 3-4 months of validation work.

Quoted 7 lines
Quoted 2 lines

I means I don't have time. If I can't steal some time from the late 40 days, this code is OK for me.

Why don't we take the current code, which would allow some academic projects to test on large systems in the next few months, and then replace it with your code when available and if appropriate?

Agreed.

thanks,
Lai

A message that is not in the archive (sent off-list or not yet received): [email protected]

#34 lore raw

Re: [PATCH 06/16] wfcqueue: implement mutex-free splice

Quoted 89 lines · Mathieu Desnoyers

A carefully crafted splice operation does not need to use an external mutex to synchronize against other splice operations.

The trick is atomically exchange the head next pointer with NULL. If the pointer we replaced was NULL, it means the queue was possibly empty. If head next was not NULL, by setting head to NULL, we ensure that concurrent splice operations are going to see an empty queue, even if concurrent enqueue operations move tail further. This means that as long as we are within splice, after setting head to NULL, but before moving tail back to head, concurrent splice operations will always see an empty queue, therefore acting as mutual exclusion.

If exchange returns a NULL head, we confirm that it was indeed empty by checking if the tail pointer points to the head node, busy-waiting if necessary.

Then the last step is to move the tail pointer to head. At that point, enqueuers are going to start enqueuing at head again, and other splice operations will be able to proceed.

---
 urcu/static/wfcqueue.h |   68 ++++++++++++++++++++++++++++++++++++------------
 urcu/wfcqueue.h        |   40 ++++++++++++++++++----------
 wfcqueue.c             |    2 +-
 3 files changed, 79 insertions(+), 31 deletions(-)
urcu/static/wfcqueue.h +34 -9
index 8774c03..4b2de50 100644
@@ -46,15 +46,30 @@ extern "C" {
  * half-wait-free/half-blocking queue implementation done by Paul E.
  * McKenney.
  *
- * Mutual exclusion of __cds_wfcq_* API
- *
- * Unless otherwise stated, the caller must ensure mutual exclusion of
- * queue update operations "dequeue" and "splice" (for source queue).
- * Queue read operations "first" and "next", which are used by
- * "for_each" iterations, need to be protected against concurrent
- * "dequeue" and "splice" (for source queue) by the caller.
- * "enqueue", "splice" (for destination queue), and "empty" are the only
- * operations that can be used without any mutual exclusion.
+ * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
+ *
+ * Synchronization table:
+ *
+ * External synchronization techniques described in the API below is
+ * required between pairs marked with "X". No external synchronization
+ * required between pairs marked with "-".
+ *
+ * Legend:
+ * [1] cds_wfcq_enqueue
+ * [2] __cds_wfcq_splice (destination queue)
+ * [3] __cds_wfcq_dequeue
+ * [4] __cds_wfcq_splice (source queue)
+ * [5] __cds_wfcq_first
+ * [6] __cds_wfcq_next
+ *
+ *     [1] [2] [3] [4] [5] [6]
+ * [1]  -   -   -   -   -   -
+ * [2]  -   -   -   -   -   -
+ * [3]  -   -   X   X   X   X
+ * [4]  -   -   X   -   X   X
+ * [5]  -   -   X   X   -   -
+ * [6]  -   -   X   X   -   -
+ *
  * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
  *
  * For convenience, cds_wfcq_dequeue_blocking() and
@@ -399,6 +414,16 @@ ___cds_wfcq_dequeue_nonblocking(struct cds_wfcq_head *head,
 	return ___cds_wfcq_dequeue(head, tail, 0);
 }
 
+/*
+ * __cds_wfcq_splice: enqueue all src_q nodes at the end of dest_q.
+ *
+ * Dequeue all nodes from src_q.
+ * dest_q must be already initialized.
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
+ * Returns enum cds_wfcq_ret which indicates the state of the src or
+ * dest queue.
+ */
 static inline enum cds_wfcq_ret
 ___cds_wfcq_splice(
 		struct cds_wfcq_head *dest_q_head,

urcu/static/wfcqueue.h ___cds_wfcq_splice( · line 436

Quoted 4 lines
urcu/static/wfcqueue.h +1 -0
@@ -408,14 +433,26 @@ ___cds_wfcq_splice(
 		int blocking)
 {
 	struct cds_wfcq_node *head, *tail;
+	int attempt = 0;

+again:

Quoted 13 lines
urcu/static/wfcqueue.h +5 -4
 	if (_cds_wfcq_empty(src_q_head, src_q_tail))
 		return CDS_WFCQ_RET_SRC_EMPTY;
 
-	head = ___cds_wfcq_node_sync_next(&src_q_head->node, blocking);
-	if (head == CDS_WFCQ_WOULDBLOCK)
-		return CDS_WFCQ_RET_WOULDBLOCK;
-	_cds_wfcq_node_init(&src_q_head->node);
+	for (;;) {
+		head = uatomic_xchg(&src_q_head->node.next, NULL);
+		if (head)
+			break;	/* non-empty */
+		if (CMM_LOAD_SHARED(src_q_tail->p) == &src_q_head->node)

urcu/static/wfcqueue.h ___cds_wfcq_splice( · line 454

Quoted 10 lines
urcu/static/wfcqueue.h +10 -0
+			return CDS_WFCQ_RET_SRC_EMPTY;
+		if (!blocking)
+			return CDS_WFCQ_RET_WOULDBLOCK;
+		if (++attempt >= WFCQ_ADAPT_ATTEMPTS) {
+			poll(NULL, 0, WFCQ_WAIT);	/* Wait for 10ms */
+			attempt = 0;
+		} else {
+			caa_cpu_relax();
+		}
+	}

Is it OK:

- _cds_wfcq_node_init(&src_q_head->node);
+ head = uatomic_xchg(&src_q_head->node.next, NULL);
+ if (!head)
+ goto again;

Quoted 99 lines
urcu/static/wfcqueue.h +2 -3
 	/*
 	 * Memory barrier implied before uatomic_xchg() orders store to
@@ -435,14 +472,13 @@ ___cds_wfcq_splice(
 		return CDS_WFCQ_RET_DEST_EMPTY;
 }
 
-
 /*
  * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
  *
  * Dequeue all nodes from src_q.
  * dest_q must be already initialized.
- * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
- * by the caller.
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
  * Returns enum cds_wfcq_ret which indicates the state of the src or
  * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
urcu/wfcqueue.h +26 -14
index ddf6b87..d9ec534 100644
@@ -46,7 +46,7 @@ extern "C" {
 #define CDS_WFCQ_WOULDBLOCK	((void *) -1UL)
 
 enum cds_wfcq_ret {
-	CDS_WFCQ_RET_WOULDBLOCK = 	-1,
+	CDS_WFCQ_RET_WOULDBLOCK =	-1,
 	CDS_WFCQ_RET_DEST_EMPTY =	0,
 	CDS_WFCQ_RET_DEST_NON_EMPTY =	1,
 	CDS_WFCQ_RET_SRC_EMPTY = 	2,
@@ -110,13 +110,28 @@ struct cds_wfcq_tail {
 /*
  * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
  *
- * Unless otherwise stated, the caller must ensure mutual exclusion of
- * queue update operations "dequeue" and "splice" (for source queue).
- * Queue read operations "first" and "next", which are used by
- * "for_each" iterations, need to be protected against concurrent
- * "dequeue" and "splice" (for source queue) by the caller.
- * "enqueue", "splice" (for destination queue), and "empty" are the only
- * operations that can be used without any mutual exclusion.
+ * Synchronization table:
+ *
+ * External synchronization techniques described in the API below is
+ * required between pairs marked with "X". No external synchronization
+ * required between pairs marked with "-".
+ *
+ * Legend:
+ * [1] cds_wfcq_enqueue
+ * [2] __cds_wfcq_splice (destination queue)
+ * [3] __cds_wfcq_dequeue
+ * [4] __cds_wfcq_splice (source queue)
+ * [5] __cds_wfcq_first
+ * [6] __cds_wfcq_next
+ *
+ *     [1] [2] [3] [4] [5] [6]
+ * [1]  -   -   -   -   -   -
+ * [2]  -   -   -   -   -   -
+ * [3]  -   -   X   X   X   X
+ * [4]  -   -   X   -   X   X
+ * [5]  -   -   X   X   -   -
+ * [6]  -   -   X   X   -   -
+ *
  * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
  *
  * For convenience, cds_wfcq_dequeue_blocking() and
@@ -231,13 +246,10 @@ extern struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking(
  *
  * Dequeue all nodes from src_q.
  * dest_q must be already initialized.
- * Content written into the node before enqueue is guaranteed to be
- * consistent, but no other memory ordering is ensured.
- * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
- * by the caller.
- *
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
  * Returns enum cds_wfcq_ret which indicates the state of the src or
- * dest queue. Cannot block.
+ * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
 extern enum cds_wfcq_ret __cds_wfcq_splice_blocking(
 		struct cds_wfcq_head *dest_q_head,
wfcqueue.c +1 -1
index 207df95..ab0eb93 100644
@@ -1,7 +1,7 @@
 /*
  * wfcqueue.c
  *
- * Userspace RCU library - Concurrent queue with Wait-Free Enqueue/Blocking Dequeue
+ * Userspace RCU library - Concurrent Queue with Wait-Free Enqueue/Blocking Dequeue
  *
  * Copyright 2010-2012 - Mathieu Desnoyers <[email protected]>
  * Copyright 2011-2012 - Lai Jiangshan <[email protected]>

A message that is not in the archive (sent off-list or not yet received): [email protected]

#36 lore raw

Re: [PATCH 06/16] wfcqueue: implement mutex-free splice

Quoted 124 lines · * Lai Jiangshan ([email protected]) wrote:
Quoted 89 lines · Mathieu Desnoyers

A carefully crafted splice operation does not need to use an external mutex to synchronize against other splice operations.

The trick is atomically exchange the head next pointer with NULL. If the pointer we replaced was NULL, it means the queue was possibly empty. If head next was not NULL, by setting head to NULL, we ensure that concurrent splice operations are going to see an empty queue, even if concurrent enqueue operations move tail further. This means that as long as we are within splice, after setting head to NULL, but before moving tail back to head, concurrent splice operations will always see an empty queue, therefore acting as mutual exclusion.

If exchange returns a NULL head, we confirm that it was indeed empty by checking if the tail pointer points to the head node, busy-waiting if necessary.

Then the last step is to move the tail pointer to head. At that point, enqueuers are going to start enqueuing at head again, and other splice operations will be able to proceed.

---
 urcu/static/wfcqueue.h |   68 ++++++++++++++++++++++++++++++++++++------------
 urcu/wfcqueue.h        |   40 ++++++++++++++++++----------
 wfcqueue.c             |    2 +-
 3 files changed, 79 insertions(+), 31 deletions(-)
urcu/static/wfcqueue.h +34 -9
index 8774c03..4b2de50 100644
@@ -46,15 +46,30 @@ extern "C" {
  * half-wait-free/half-blocking queue implementation done by Paul E.
  * McKenney.
  *
- * Mutual exclusion of __cds_wfcq_* API
- *
- * Unless otherwise stated, the caller must ensure mutual exclusion of
- * queue update operations "dequeue" and "splice" (for source queue).
- * Queue read operations "first" and "next", which are used by
- * "for_each" iterations, need to be protected against concurrent
- * "dequeue" and "splice" (for source queue) by the caller.
- * "enqueue", "splice" (for destination queue), and "empty" are the only
- * operations that can be used without any mutual exclusion.
+ * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
+ *
+ * Synchronization table:
+ *
+ * External synchronization techniques described in the API below is
+ * required between pairs marked with "X". No external synchronization
+ * required between pairs marked with "-".
+ *
+ * Legend:
+ * [1] cds_wfcq_enqueue
+ * [2] __cds_wfcq_splice (destination queue)
+ * [3] __cds_wfcq_dequeue
+ * [4] __cds_wfcq_splice (source queue)
+ * [5] __cds_wfcq_first
+ * [6] __cds_wfcq_next
+ *
+ *     [1] [2] [3] [4] [5] [6]
+ * [1]  -   -   -   -   -   -
+ * [2]  -   -   -   -   -   -
+ * [3]  -   -   X   X   X   X
+ * [4]  -   -   X   -   X   X
+ * [5]  -   -   X   X   -   -
+ * [6]  -   -   X   X   -   -
+ *
  * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
  *
  * For convenience, cds_wfcq_dequeue_blocking() and
@@ -399,6 +414,16 @@ ___cds_wfcq_dequeue_nonblocking(struct cds_wfcq_head *head,
 	return ___cds_wfcq_dequeue(head, tail, 0);
 }
 
+/*
+ * __cds_wfcq_splice: enqueue all src_q nodes at the end of dest_q.
+ *
+ * Dequeue all nodes from src_q.
+ * dest_q must be already initialized.
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
+ * Returns enum cds_wfcq_ret which indicates the state of the src or
+ * dest queue.
+ */
 static inline enum cds_wfcq_ret
 ___cds_wfcq_splice(
 		struct cds_wfcq_head *dest_q_head,

urcu/static/wfcqueue.h ___cds_wfcq_splice( · line 436

Quoted 4 lines
urcu/static/wfcqueue.h +1 -0
@@ -408,14 +433,26 @@ ___cds_wfcq_splice(
 		int blocking)
 {
 	struct cds_wfcq_node *head, *tail;
+	int attempt = 0;

+again:

Quoted 13 lines
urcu/static/wfcqueue.h +5 -4
 	if (_cds_wfcq_empty(src_q_head, src_q_tail))
 		return CDS_WFCQ_RET_SRC_EMPTY;
 
-	head = ___cds_wfcq_node_sync_next(&src_q_head->node, blocking);
-	if (head == CDS_WFCQ_WOULDBLOCK)
-		return CDS_WFCQ_RET_WOULDBLOCK;
-	_cds_wfcq_node_init(&src_q_head->node);
+	for (;;) {
+		head = uatomic_xchg(&src_q_head->node.next, NULL);
+		if (head)
+			break;	/* non-empty */
+		if (CMM_LOAD_SHARED(src_q_tail->p) == &src_q_head->node)

urcu/static/wfcqueue.h ___cds_wfcq_splice( · line 454

Quoted 10 lines
urcu/static/wfcqueue.h +10 -0
+			return CDS_WFCQ_RET_SRC_EMPTY;
+		if (!blocking)
+			return CDS_WFCQ_RET_WOULDBLOCK;
+		if (++attempt >= WFCQ_ADAPT_ATTEMPTS) {
+			poll(NULL, 0, WFCQ_WAIT);	/* Wait for 10ms */
+			attempt = 0;
+		} else {
+			caa_cpu_relax();
+		}
+	}

Is it OK:

Review

Quoted 4 lines
(quoted diff) +3 -1
-	_cds_wfcq_node_init(&src_q_head->node);
+	head = uatomic_xchg(&src_q_head->node.next, NULL);
+	if (!head)
+		goto again;

You are right that we can simplify the code a bit by re-using _cds_wfcq_empty() to test validate emptiness of the source queue, rather than open-code it.

The only issue here is that the busy-loop (goto again) will not invoke caa_cpu_relax(), nor do adaptative waiting like ___cds_wfcq_node_sync_next() normally does. Also, it does not check for blocking/non-blocking caller. Also, whenever possible, I like to have for () or do/while constructs in place to make it clear that we can loop. So a modification of your proposal would look like:

/* Return 1 if nonblocking and needs to block, 0 otherwise */ static inline
bool ___cds_wfcq_busy_wait(int *attempt, int blocking)

{
        if (!blocking)
                return 1;
        if (+attempt >= WFCQ_ADAPT_ATTEMPTS) {
                poll(NULL, 0, WFCQ_WAIT);       /* Wait for 10ms */
                attempt = 0;
        } else {
                caa_cpu_relax();
        }
        return 0;
}

[...]

        struct cds_wfcq_node *head, *tail;
        int attempt = 0;

        for (;;) {
                if (_cds_wfcq_empty(src_q_head, src_q_tail))
                        return CDS_WFCQ_RET_SRC_EMPTY;
                head = uatomic_xchg(&src_q_head->node.next, NULL);
                if (head)
                        break;
                if (___cds_wfcq_busy_wait(&attempt, blocking))
                        return CDS_WFCQ_RET_WOULDBLOCK;
        }

Thoughts ?

Thanks,

Mathieu

Quoted 101 lines
Quoted 100 lines
urcu/static/wfcqueue.h +2 -3
 	/*
 	 * Memory barrier implied before uatomic_xchg() orders store to
@@ -435,14 +472,13 @@ ___cds_wfcq_splice(
 		return CDS_WFCQ_RET_DEST_EMPTY;
 }
 
-
 /*
  * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
  *
  * Dequeue all nodes from src_q.
  * dest_q must be already initialized.
- * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
- * by the caller.
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
  * Returns enum cds_wfcq_ret which indicates the state of the src or
  * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
urcu/wfcqueue.h +26 -14
index ddf6b87..d9ec534 100644
@@ -46,7 +46,7 @@ extern "C" {
 #define CDS_WFCQ_WOULDBLOCK	((void *) -1UL)
 
 enum cds_wfcq_ret {
-	CDS_WFCQ_RET_WOULDBLOCK = 	-1,
+	CDS_WFCQ_RET_WOULDBLOCK =	-1,
 	CDS_WFCQ_RET_DEST_EMPTY =	0,
 	CDS_WFCQ_RET_DEST_NON_EMPTY =	1,
 	CDS_WFCQ_RET_SRC_EMPTY = 	2,
@@ -110,13 +110,28 @@ struct cds_wfcq_tail {
 /*
  * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
  *
- * Unless otherwise stated, the caller must ensure mutual exclusion of
- * queue update operations "dequeue" and "splice" (for source queue).
- * Queue read operations "first" and "next", which are used by
- * "for_each" iterations, need to be protected against concurrent
- * "dequeue" and "splice" (for source queue) by the caller.
- * "enqueue", "splice" (for destination queue), and "empty" are the only
- * operations that can be used without any mutual exclusion.
+ * Synchronization table:
+ *
+ * External synchronization techniques described in the API below is
+ * required between pairs marked with "X". No external synchronization
+ * required between pairs marked with "-".
+ *
+ * Legend:
+ * [1] cds_wfcq_enqueue
+ * [2] __cds_wfcq_splice (destination queue)
+ * [3] __cds_wfcq_dequeue
+ * [4] __cds_wfcq_splice (source queue)
+ * [5] __cds_wfcq_first
+ * [6] __cds_wfcq_next
+ *
+ *     [1] [2] [3] [4] [5] [6]
+ * [1]  -   -   -   -   -   -
+ * [2]  -   -   -   -   -   -
+ * [3]  -   -   X   X   X   X
+ * [4]  -   -   X   -   X   X
+ * [5]  -   -   X   X   -   -
+ * [6]  -   -   X   X   -   -
+ *
  * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
  *
  * For convenience, cds_wfcq_dequeue_blocking() and
@@ -231,13 +246,10 @@ extern struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking(
  *
  * Dequeue all nodes from src_q.
  * dest_q must be already initialized.
- * Content written into the node before enqueue is guaranteed to be
- * consistent, but no other memory ordering is ensured.
- * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
- * by the caller.
- *
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
  * Returns enum cds_wfcq_ret which indicates the state of the src or
- * dest queue. Cannot block.
+ * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
 extern enum cds_wfcq_ret __cds_wfcq_splice_blocking(
 		struct cds_wfcq_head *dest_q_head,
wfcqueue.c +1 -1
index 207df95..ab0eb93 100644
@@ -1,7 +1,7 @@
 /*
  * wfcqueue.c
  *
- * Userspace RCU library - Concurrent queue with Wait-Free Enqueue/Blocking Dequeue
+ * Userspace RCU library - Concurrent Queue with Wait-Free Enqueue/Blocking Dequeue
  *
  * Copyright 2010-2012 - Mathieu Desnoyers <[email protected]>
  * Copyright 2011-2012 - Lai Jiangshan <[email protected]>
Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

A message that is not in the archive (sent off-list or not yet received): 20121121151823.GB21518@Krystal

#38 lore raw

Re: [PATCH 06/16] wfcqueue: implement mutex-free splice

Quoted 134 lines · Mathieu Desnoyers
Quoted 124 lines · * Lai Jiangshan ([email protected]) wrote:
Quoted 89 lines · Mathieu Desnoyers

A carefully crafted splice operation does not need to use an external mutex to synchronize against other splice operations.

The trick is atomically exchange the head next pointer with NULL. If the pointer we replaced was NULL, it means the queue was possibly empty. If head next was not NULL, by setting head to NULL, we ensure that concurrent splice operations are going to see an empty queue, even if concurrent enqueue operations move tail further. This means that as long as we are within splice, after setting head to NULL, but before moving tail back to head, concurrent splice operations will always see an empty queue, therefore acting as mutual exclusion.

If exchange returns a NULL head, we confirm that it was indeed empty by checking if the tail pointer points to the head node, busy-waiting if necessary.

Then the last step is to move the tail pointer to head. At that point, enqueuers are going to start enqueuing at head again, and other splice operations will be able to proceed.

---
 urcu/static/wfcqueue.h |   68 ++++++++++++++++++++++++++++++++++++------------
 urcu/wfcqueue.h        |   40 ++++++++++++++++++----------
 wfcqueue.c             |    2 +-
 3 files changed, 79 insertions(+), 31 deletions(-)
urcu/static/wfcqueue.h +34 -9
index 8774c03..4b2de50 100644
@@ -46,15 +46,30 @@ extern "C" {
  * half-wait-free/half-blocking queue implementation done by Paul E.
  * McKenney.
  *
- * Mutual exclusion of __cds_wfcq_* API
- *
- * Unless otherwise stated, the caller must ensure mutual exclusion of
- * queue update operations "dequeue" and "splice" (for source queue).
- * Queue read operations "first" and "next", which are used by
- * "for_each" iterations, need to be protected against concurrent
- * "dequeue" and "splice" (for source queue) by the caller.
- * "enqueue", "splice" (for destination queue), and "empty" are the only
- * operations that can be used without any mutual exclusion.
+ * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
+ *
+ * Synchronization table:
+ *
+ * External synchronization techniques described in the API below is
+ * required between pairs marked with "X". No external synchronization
+ * required between pairs marked with "-".
+ *
+ * Legend:
+ * [1] cds_wfcq_enqueue
+ * [2] __cds_wfcq_splice (destination queue)
+ * [3] __cds_wfcq_dequeue
+ * [4] __cds_wfcq_splice (source queue)
+ * [5] __cds_wfcq_first
+ * [6] __cds_wfcq_next
+ *
+ *     [1] [2] [3] [4] [5] [6]
+ * [1]  -   -   -   -   -   -
+ * [2]  -   -   -   -   -   -
+ * [3]  -   -   X   X   X   X
+ * [4]  -   -   X   -   X   X
+ * [5]  -   -   X   X   -   -
+ * [6]  -   -   X   X   -   -
+ *
  * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
  *
  * For convenience, cds_wfcq_dequeue_blocking() and
@@ -399,6 +414,16 @@ ___cds_wfcq_dequeue_nonblocking(struct cds_wfcq_head *head,
 	return ___cds_wfcq_dequeue(head, tail, 0);
 }
 
+/*
+ * __cds_wfcq_splice: enqueue all src_q nodes at the end of dest_q.
+ *
+ * Dequeue all nodes from src_q.
+ * dest_q must be already initialized.
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
+ * Returns enum cds_wfcq_ret which indicates the state of the src or
+ * dest queue.
+ */
 static inline enum cds_wfcq_ret
 ___cds_wfcq_splice(
 		struct cds_wfcq_head *dest_q_head,

urcu/static/wfcqueue.h ___cds_wfcq_splice( · line 436

Quoted 4 lines
urcu/static/wfcqueue.h +1 -0
@@ -408,14 +433,26 @@ ___cds_wfcq_splice(
 		int blocking)
 {
 	struct cds_wfcq_node *head, *tail;
+	int attempt = 0;

+again:

Quoted 13 lines
urcu/static/wfcqueue.h +5 -4
 	if (_cds_wfcq_empty(src_q_head, src_q_tail))
 		return CDS_WFCQ_RET_SRC_EMPTY;
 
-	head = ___cds_wfcq_node_sync_next(&src_q_head->node, blocking);
-	if (head == CDS_WFCQ_WOULDBLOCK)
-		return CDS_WFCQ_RET_WOULDBLOCK;
-	_cds_wfcq_node_init(&src_q_head->node);
+	for (;;) {
+		head = uatomic_xchg(&src_q_head->node.next, NULL);
+		if (head)
+			break;	/* non-empty */
+		if (CMM_LOAD_SHARED(src_q_tail->p) == &src_q_head->node)

urcu/static/wfcqueue.h ___cds_wfcq_splice( · line 454

Quoted 10 lines
urcu/static/wfcqueue.h +10 -0
+			return CDS_WFCQ_RET_SRC_EMPTY;
+		if (!blocking)
+			return CDS_WFCQ_RET_WOULDBLOCK;
+		if (++attempt >= WFCQ_ADAPT_ATTEMPTS) {
+			poll(NULL, 0, WFCQ_WAIT);	/* Wait for 10ms */
+			attempt = 0;
+		} else {
+			caa_cpu_relax();
+		}
+	}

Is it OK:

Review

Quoted 4 lines
(quoted diff) +3 -1
-	_cds_wfcq_node_init(&src_q_head->node);
+	head = uatomic_xchg(&src_q_head->node.next, NULL);
+	if (!head)
+		goto again;

You are right that we can simplify the code a bit by re-using _cds_wfcq_empty() to test validate emptiness of the source queue, rather than open-code it.

Quoted 3 lines

The only issue here is that the busy-loop (goto again) will not invoke caa_cpu_relax(), nor do adaptative waiting like ___cds_wfcq_node_sync_next() normally does.

Don't need, it will re-enter ___cds_wfcq_node_sync_next() to do it.

Quoted 2 lines

Also, it does not check for
blocking/non-blocking caller.

we can add code to check it.

but:

for () {
	see src_q_head->node.next is not NULL
	xchg fail
}

Is this a kind of blocking?

Quoted 21 lines

Also, whenever possible, I like to have
for () or do/while constructs in place to make it clear that we can loop. So a modification of your proposal would look like:

/* Return 1 if nonblocking and needs to block, 0 otherwise */ static inline
bool ___cds_wfcq_busy_wait(int *attempt, int blocking)

{
        if (!blocking)
                return 1;
        if (+attempt >= WFCQ_ADAPT_ATTEMPTS) {
                poll(NULL, 0, WFCQ_WAIT);       /* Wait for 10ms */
                attempt = 0;
        } else {
                caa_cpu_relax();
        }
        return 0;
}

[...]

Quoted 11 lines
        struct cds_wfcq_node *head, *tail;
        int attempt = 0;

        for (;;) {
                if (_cds_wfcq_empty(src_q_head, src_q_tail))
                        return CDS_WFCQ_RET_SRC_EMPTY;
                head = uatomic_xchg(&src_q_head->node.next, NULL);
                if (head)
                        break;
                if (___cds_wfcq_busy_wait(&attempt, blocking))
                        return CDS_WFCQ_RET_WOULDBLOCK;

return _cds_wfcq_empty(src_q_head, src_q_tail) ? CDS_WFCQ_RET_SRC_EMPTY : CDS_WFCQ_RET_WOULDBLOCK

Quoted 112 lines
        }

Thoughts ?

Thanks,

Mathieu

Quoted 101 lines
Quoted 100 lines
urcu/static/wfcqueue.h +2 -3
 	/*
 	 * Memory barrier implied before uatomic_xchg() orders store to
@@ -435,14 +472,13 @@ ___cds_wfcq_splice(
 		return CDS_WFCQ_RET_DEST_EMPTY;
 }
 
-
 /*
  * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
  *
  * Dequeue all nodes from src_q.
  * dest_q must be already initialized.
- * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
- * by the caller.
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
  * Returns enum cds_wfcq_ret which indicates the state of the src or
  * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
urcu/wfcqueue.h +26 -14
index ddf6b87..d9ec534 100644
@@ -46,7 +46,7 @@ extern "C" {
 #define CDS_WFCQ_WOULDBLOCK	((void *) -1UL)
 
 enum cds_wfcq_ret {
-	CDS_WFCQ_RET_WOULDBLOCK = 	-1,
+	CDS_WFCQ_RET_WOULDBLOCK =	-1,
 	CDS_WFCQ_RET_DEST_EMPTY =	0,
 	CDS_WFCQ_RET_DEST_NON_EMPTY =	1,
 	CDS_WFCQ_RET_SRC_EMPTY = 	2,
@@ -110,13 +110,28 @@ struct cds_wfcq_tail {
 /*
  * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
  *
- * Unless otherwise stated, the caller must ensure mutual exclusion of
- * queue update operations "dequeue" and "splice" (for source queue).
- * Queue read operations "first" and "next", which are used by
- * "for_each" iterations, need to be protected against concurrent
- * "dequeue" and "splice" (for source queue) by the caller.
- * "enqueue", "splice" (for destination queue), and "empty" are the only
- * operations that can be used without any mutual exclusion.
+ * Synchronization table:
+ *
+ * External synchronization techniques described in the API below is
+ * required between pairs marked with "X". No external synchronization
+ * required between pairs marked with "-".
+ *
+ * Legend:
+ * [1] cds_wfcq_enqueue
+ * [2] __cds_wfcq_splice (destination queue)
+ * [3] __cds_wfcq_dequeue
+ * [4] __cds_wfcq_splice (source queue)
+ * [5] __cds_wfcq_first
+ * [6] __cds_wfcq_next
+ *
+ *     [1] [2] [3] [4] [5] [6]
+ * [1]  -   -   -   -   -   -
+ * [2]  -   -   -   -   -   -
+ * [3]  -   -   X   X   X   X
+ * [4]  -   -   X   -   X   X
+ * [5]  -   -   X   X   -   -
+ * [6]  -   -   X   X   -   -
+ *
  * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
  *
  * For convenience, cds_wfcq_dequeue_blocking() and
@@ -231,13 +246,10 @@ extern struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking(
  *
  * Dequeue all nodes from src_q.
  * dest_q must be already initialized.
- * Content written into the node before enqueue is guaranteed to be
- * consistent, but no other memory ordering is ensured.
- * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
- * by the caller.
- *
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
  * Returns enum cds_wfcq_ret which indicates the state of the src or
- * dest queue. Cannot block.
+ * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
 extern enum cds_wfcq_ret __cds_wfcq_splice_blocking(
 		struct cds_wfcq_head *dest_q_head,
wfcqueue.c +1 -1
index 207df95..ab0eb93 100644
@@ -1,7 +1,7 @@
 /*
  * wfcqueue.c
  *
- * Userspace RCU library - Concurrent queue with Wait-Free Enqueue/Blocking Dequeue
+ * Userspace RCU library - Concurrent Queue with Wait-Free Enqueue/Blocking Dequeue
  *
  * Copyright 2010-2012 - Mathieu Desnoyers <[email protected]>
  * Copyright 2011-2012 - Lai Jiangshan <[email protected]>

A message that is not in the archive (sent off-list or not yet received): [email protected]

#40 lore raw

Re: [PATCH 06/16] wfcqueue: implement mutex-free splice

Quoted 135 lines · * Lai Jiangshan ([email protected]) wrote:
Quoted 134 lines · Mathieu Desnoyers
Quoted 124 lines · * Lai Jiangshan ([email protected]) wrote:
Quoted 89 lines · Mathieu Desnoyers

A carefully crafted splice operation does not need to use an external mutex to synchronize against other splice operations.

The trick is atomically exchange the head next pointer with NULL. If the pointer we replaced was NULL, it means the queue was possibly empty. If head next was not NULL, by setting head to NULL, we ensure that concurrent splice operations are going to see an empty queue, even if concurrent enqueue operations move tail further. This means that as long as we are within splice, after setting head to NULL, but before moving tail back to head, concurrent splice operations will always see an empty queue, therefore acting as mutual exclusion.

If exchange returns a NULL head, we confirm that it was indeed empty by checking if the tail pointer points to the head node, busy-waiting if necessary.

Then the last step is to move the tail pointer to head. At that point, enqueuers are going to start enqueuing at head again, and other splice operations will be able to proceed.

---
 urcu/static/wfcqueue.h |   68 ++++++++++++++++++++++++++++++++++++------------
 urcu/wfcqueue.h        |   40 ++++++++++++++++++----------
 wfcqueue.c             |    2 +-
 3 files changed, 79 insertions(+), 31 deletions(-)
urcu/static/wfcqueue.h +34 -9
index 8774c03..4b2de50 100644
@@ -46,15 +46,30 @@ extern "C" {
  * half-wait-free/half-blocking queue implementation done by Paul E.
  * McKenney.
  *
- * Mutual exclusion of __cds_wfcq_* API
- *
- * Unless otherwise stated, the caller must ensure mutual exclusion of
- * queue update operations "dequeue" and "splice" (for source queue).
- * Queue read operations "first" and "next", which are used by
- * "for_each" iterations, need to be protected against concurrent
- * "dequeue" and "splice" (for source queue) by the caller.
- * "enqueue", "splice" (for destination queue), and "empty" are the only
- * operations that can be used without any mutual exclusion.
+ * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
+ *
+ * Synchronization table:
+ *
+ * External synchronization techniques described in the API below is
+ * required between pairs marked with "X". No external synchronization
+ * required between pairs marked with "-".
+ *
+ * Legend:
+ * [1] cds_wfcq_enqueue
+ * [2] __cds_wfcq_splice (destination queue)
+ * [3] __cds_wfcq_dequeue
+ * [4] __cds_wfcq_splice (source queue)
+ * [5] __cds_wfcq_first
+ * [6] __cds_wfcq_next
+ *
+ *     [1] [2] [3] [4] [5] [6]
+ * [1]  -   -   -   -   -   -
+ * [2]  -   -   -   -   -   -
+ * [3]  -   -   X   X   X   X
+ * [4]  -   -   X   -   X   X
+ * [5]  -   -   X   X   -   -
+ * [6]  -   -   X   X   -   -
+ *
  * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
  *
  * For convenience, cds_wfcq_dequeue_blocking() and
@@ -399,6 +414,16 @@ ___cds_wfcq_dequeue_nonblocking(struct cds_wfcq_head *head,
 	return ___cds_wfcq_dequeue(head, tail, 0);
 }
 
+/*
+ * __cds_wfcq_splice: enqueue all src_q nodes at the end of dest_q.
+ *
+ * Dequeue all nodes from src_q.
+ * dest_q must be already initialized.
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
+ * Returns enum cds_wfcq_ret which indicates the state of the src or
+ * dest queue.
+ */
 static inline enum cds_wfcq_ret
 ___cds_wfcq_splice(
 		struct cds_wfcq_head *dest_q_head,

urcu/static/wfcqueue.h ___cds_wfcq_splice( · line 436

Quoted 4 lines
urcu/static/wfcqueue.h +1 -0
@@ -408,14 +433,26 @@ ___cds_wfcq_splice(
 		int blocking)
 {
 	struct cds_wfcq_node *head, *tail;
+	int attempt = 0;

+again:

Quoted 13 lines
urcu/static/wfcqueue.h +5 -4
 	if (_cds_wfcq_empty(src_q_head, src_q_tail))
 		return CDS_WFCQ_RET_SRC_EMPTY;
 
-	head = ___cds_wfcq_node_sync_next(&src_q_head->node, blocking);
-	if (head == CDS_WFCQ_WOULDBLOCK)
-		return CDS_WFCQ_RET_WOULDBLOCK;
-	_cds_wfcq_node_init(&src_q_head->node);
+	for (;;) {
+		head = uatomic_xchg(&src_q_head->node.next, NULL);
+		if (head)
+			break;	/* non-empty */
+		if (CMM_LOAD_SHARED(src_q_tail->p) == &src_q_head->node)

urcu/static/wfcqueue.h ___cds_wfcq_splice( · line 454

Quoted 10 lines
urcu/static/wfcqueue.h +10 -0
+			return CDS_WFCQ_RET_SRC_EMPTY;
+		if (!blocking)
+			return CDS_WFCQ_RET_WOULDBLOCK;
+		if (++attempt >= WFCQ_ADAPT_ATTEMPTS) {
+			poll(NULL, 0, WFCQ_WAIT);	/* Wait for 10ms */
+			attempt = 0;
+		} else {
+			caa_cpu_relax();
+		}
+	}

Is it OK:

Review

Quoted 4 lines
(quoted diff) +3 -1
-	_cds_wfcq_node_init(&src_q_head->node);
+	head = uatomic_xchg(&src_q_head->node.next, NULL);
+	if (!head)
+		goto again;

You are right that we can simplify the code a bit by re-using _cds_wfcq_empty() to test validate emptiness of the source queue, rather than open-code it.

Quoted 5 lines
Quoted 3 lines

The only issue here is that the busy-loop (goto again) will not invoke caa_cpu_relax(), nor do adaptative waiting like ___cds_wfcq_node_sync_next() normally does.

Don't need, it will re-enter ___cds_wfcq_node_sync_next() to do it.

The ___cds_wfcq_node_sync_next is actually removed. It is replaced by the xchg of the head's next pointer.

Quoted 6 lines
Quoted 3 lines

Also, it does not check for
blocking/non-blocking caller.

we can add code to check it.

Quoted 8 lines

but:

for () {
	see src_q_head->node.next is not NULL
	xchg fail
}

Is this a kind of blocking?

Not sure what your code snippet does, but if there is any way that we can have to busy-loop while we are in an intermediate transient state, then we could have to block.

Quoted 35 lines
Quoted 23 lines

Also, whenever possible, I like to have
for () or do/while constructs in place to make it clear that we can loop. So a modification of your proposal would look like:

/* Return 1 if nonblocking and needs to block, 0 otherwise */ static inline
bool ___cds_wfcq_busy_wait(int *attempt, int blocking)

{
        if (!blocking)
                return 1;
        if (+attempt >= WFCQ_ADAPT_ATTEMPTS) {
                poll(NULL, 0, WFCQ_WAIT);       /* Wait for 10ms */
                attempt = 0;
        } else {
                caa_cpu_relax();
        }
        return 0;
}

[...]

Quoted 11 lines
        struct cds_wfcq_node *head, *tail;
        int attempt = 0;

        for (;;) {
                if (_cds_wfcq_empty(src_q_head, src_q_tail))
                        return CDS_WFCQ_RET_SRC_EMPTY;
                head = uatomic_xchg(&src_q_head->node.next, NULL);
                if (head)
                        break;
                if (___cds_wfcq_busy_wait(&attempt, blocking))
                        return CDS_WFCQ_RET_WOULDBLOCK;
Quoted 1 line

return _cds_wfcq_empty(src_q_head, src_q_tail) ? CDS_WFCQ_RET_SRC_EMPTY : CDS_WFCQ_RET_WOULDBLOCK

I'm not sure it really buys us anything semantically: we already checked that the queue was non-empty, then xchg returns that head next pointer is NULL, so we have seen an null head next, but with tail not pointing to the head node, so we are in a state that could make us busy-wait. So rather than checking again if the queue is still in a transient state, we can return CDS_WFCQ_RET_WOULDBLOCK immediately, no ? The only advantage of checking it again is to catch a few cases where we would not have to block, but I'm not convinced this is really useful.

Thoughts ?

Thanks,

Mathieu

Quoted 114 lines
Quoted 113 lines
        }

Thoughts ?

Thanks,

Mathieu

Quoted 101 lines
Quoted 100 lines
urcu/static/wfcqueue.h +2 -3
 	/*
 	 * Memory barrier implied before uatomic_xchg() orders store to
@@ -435,14 +472,13 @@ ___cds_wfcq_splice(
 		return CDS_WFCQ_RET_DEST_EMPTY;
 }
 
-
 /*
  * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
  *
  * Dequeue all nodes from src_q.
  * dest_q must be already initialized.
- * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
- * by the caller.
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
  * Returns enum cds_wfcq_ret which indicates the state of the src or
  * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
urcu/wfcqueue.h +26 -14
index ddf6b87..d9ec534 100644
@@ -46,7 +46,7 @@ extern "C" {
 #define CDS_WFCQ_WOULDBLOCK	((void *) -1UL)
 
 enum cds_wfcq_ret {
-	CDS_WFCQ_RET_WOULDBLOCK = 	-1,
+	CDS_WFCQ_RET_WOULDBLOCK =	-1,
 	CDS_WFCQ_RET_DEST_EMPTY =	0,
 	CDS_WFCQ_RET_DEST_NON_EMPTY =	1,
 	CDS_WFCQ_RET_SRC_EMPTY = 	2,
@@ -110,13 +110,28 @@ struct cds_wfcq_tail {
 /*
  * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
  *
- * Unless otherwise stated, the caller must ensure mutual exclusion of
- * queue update operations "dequeue" and "splice" (for source queue).
- * Queue read operations "first" and "next", which are used by
- * "for_each" iterations, need to be protected against concurrent
- * "dequeue" and "splice" (for source queue) by the caller.
- * "enqueue", "splice" (for destination queue), and "empty" are the only
- * operations that can be used without any mutual exclusion.
+ * Synchronization table:
+ *
+ * External synchronization techniques described in the API below is
+ * required between pairs marked with "X". No external synchronization
+ * required between pairs marked with "-".
+ *
+ * Legend:
+ * [1] cds_wfcq_enqueue
+ * [2] __cds_wfcq_splice (destination queue)
+ * [3] __cds_wfcq_dequeue
+ * [4] __cds_wfcq_splice (source queue)
+ * [5] __cds_wfcq_first
+ * [6] __cds_wfcq_next
+ *
+ *     [1] [2] [3] [4] [5] [6]
+ * [1]  -   -   -   -   -   -
+ * [2]  -   -   -   -   -   -
+ * [3]  -   -   X   X   X   X
+ * [4]  -   -   X   -   X   X
+ * [5]  -   -   X   X   -   -
+ * [6]  -   -   X   X   -   -
+ *
  * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
  *
  * For convenience, cds_wfcq_dequeue_blocking() and
@@ -231,13 +246,10 @@ extern struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking(
  *
  * Dequeue all nodes from src_q.
  * dest_q must be already initialized.
- * Content written into the node before enqueue is guaranteed to be
- * consistent, but no other memory ordering is ensured.
- * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
- * by the caller.
- *
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
  * Returns enum cds_wfcq_ret which indicates the state of the src or
- * dest queue. Cannot block.
+ * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
 extern enum cds_wfcq_ret __cds_wfcq_splice_blocking(
 		struct cds_wfcq_head *dest_q_head,
wfcqueue.c +1 -1
index 207df95..ab0eb93 100644
@@ -1,7 +1,7 @@
 /*
  * wfcqueue.c
  *
- * Userspace RCU library - Concurrent queue with Wait-Free Enqueue/Blocking Dequeue
+ * Userspace RCU library - Concurrent Queue with Wait-Free Enqueue/Blocking Dequeue
  *
  * Copyright 2010-2012 - Mathieu Desnoyers <[email protected]>
  * Copyright 2011-2012 - Lai Jiangshan <[email protected]>
Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

A message that is not in the archive (sent off-list or not yet received): 20121122185455.GA17756@Krystal

#42 lore raw

Re: [PATCH 06/16] wfcqueue: implement mutex-free splice

Quoted 136 lines · Mathieu Desnoyers
Quoted 135 lines · * Lai Jiangshan ([email protected]) wrote:
Quoted 134 lines · Mathieu Desnoyers
Quoted 124 lines · * Lai Jiangshan ([email protected]) wrote:
Quoted 89 lines · Mathieu Desnoyers

A carefully crafted splice operation does not need to use an external mutex to synchronize against other splice operations.

The trick is atomically exchange the head next pointer with NULL. If the pointer we replaced was NULL, it means the queue was possibly empty. If head next was not NULL, by setting head to NULL, we ensure that concurrent splice operations are going to see an empty queue, even if concurrent enqueue operations move tail further. This means that as long as we are within splice, after setting head to NULL, but before moving tail back to head, concurrent splice operations will always see an empty queue, therefore acting as mutual exclusion.

If exchange returns a NULL head, we confirm that it was indeed empty by checking if the tail pointer points to the head node, busy-waiting if necessary.

Then the last step is to move the tail pointer to head. At that point, enqueuers are going to start enqueuing at head again, and other splice operations will be able to proceed.

---
 urcu/static/wfcqueue.h |   68 ++++++++++++++++++++++++++++++++++++------------
 urcu/wfcqueue.h        |   40 ++++++++++++++++++----------
 wfcqueue.c             |    2 +-
 3 files changed, 79 insertions(+), 31 deletions(-)
urcu/static/wfcqueue.h +34 -9
index 8774c03..4b2de50 100644
@@ -46,15 +46,30 @@ extern "C" {
  * half-wait-free/half-blocking queue implementation done by Paul E.
  * McKenney.
  *
- * Mutual exclusion of __cds_wfcq_* API
- *
- * Unless otherwise stated, the caller must ensure mutual exclusion of
- * queue update operations "dequeue" and "splice" (for source queue).
- * Queue read operations "first" and "next", which are used by
- * "for_each" iterations, need to be protected against concurrent
- * "dequeue" and "splice" (for source queue) by the caller.
- * "enqueue", "splice" (for destination queue), and "empty" are the only
- * operations that can be used without any mutual exclusion.
+ * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
+ *
+ * Synchronization table:
+ *
+ * External synchronization techniques described in the API below is
+ * required between pairs marked with "X". No external synchronization
+ * required between pairs marked with "-".
+ *
+ * Legend:
+ * [1] cds_wfcq_enqueue
+ * [2] __cds_wfcq_splice (destination queue)
+ * [3] __cds_wfcq_dequeue
+ * [4] __cds_wfcq_splice (source queue)
+ * [5] __cds_wfcq_first
+ * [6] __cds_wfcq_next
+ *
+ *     [1] [2] [3] [4] [5] [6]
+ * [1]  -   -   -   -   -   -
+ * [2]  -   -   -   -   -   -
+ * [3]  -   -   X   X   X   X
+ * [4]  -   -   X   -   X   X
+ * [5]  -   -   X   X   -   -
+ * [6]  -   -   X   X   -   -
+ *
  * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
  *
  * For convenience, cds_wfcq_dequeue_blocking() and
@@ -399,6 +414,16 @@ ___cds_wfcq_dequeue_nonblocking(struct cds_wfcq_head *head,
 	return ___cds_wfcq_dequeue(head, tail, 0);
 }
 
+/*
+ * __cds_wfcq_splice: enqueue all src_q nodes at the end of dest_q.
+ *
+ * Dequeue all nodes from src_q.
+ * dest_q must be already initialized.
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
+ * Returns enum cds_wfcq_ret which indicates the state of the src or
+ * dest queue.
+ */
 static inline enum cds_wfcq_ret
 ___cds_wfcq_splice(
 		struct cds_wfcq_head *dest_q_head,

urcu/static/wfcqueue.h ___cds_wfcq_splice( · line 436

Quoted 4 lines
urcu/static/wfcqueue.h +1 -0
@@ -408,14 +433,26 @@ ___cds_wfcq_splice(
 		int blocking)
 {
 	struct cds_wfcq_node *head, *tail;
+	int attempt = 0;

+again:

Quoted 13 lines
urcu/static/wfcqueue.h +5 -4
 	if (_cds_wfcq_empty(src_q_head, src_q_tail))
 		return CDS_WFCQ_RET_SRC_EMPTY;
 
-	head = ___cds_wfcq_node_sync_next(&src_q_head->node, blocking);
-	if (head == CDS_WFCQ_WOULDBLOCK)
-		return CDS_WFCQ_RET_WOULDBLOCK;
-	_cds_wfcq_node_init(&src_q_head->node);
+	for (;;) {
+		head = uatomic_xchg(&src_q_head->node.next, NULL);
+		if (head)
+			break;	/* non-empty */
+		if (CMM_LOAD_SHARED(src_q_tail->p) == &src_q_head->node)

urcu/static/wfcqueue.h ___cds_wfcq_splice( · line 454

Quoted 10 lines
urcu/static/wfcqueue.h +10 -0
+			return CDS_WFCQ_RET_SRC_EMPTY;
+		if (!blocking)
+			return CDS_WFCQ_RET_WOULDBLOCK;
+		if (++attempt >= WFCQ_ADAPT_ATTEMPTS) {
+			poll(NULL, 0, WFCQ_WAIT);	/* Wait for 10ms */
+			attempt = 0;
+		} else {
+			caa_cpu_relax();
+		}
+	}

Is it OK:

Review

Quoted 4 lines
(quoted diff) +3 -1
-	_cds_wfcq_node_init(&src_q_head->node);
+	head = uatomic_xchg(&src_q_head->node.next, NULL);
+	if (!head)
+		goto again;

You are right that we can simplify the code a bit by re-using _cds_wfcq_empty() to test validate emptiness of the source queue, rather than open-code it.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The only issue here is that the busy-loop (goto again) will not invoke caa_cpu_relax(), nor do adaptative waiting like ___cds_wfcq_node_sync_next() normally does.

Don't need, it will re-enter ___cds_wfcq_node_sync_next() to do it.

The ___cds_wfcq_node_sync_next is actually removed. It is replaced by the xchg of the head's next pointer.

It is kept.

{
struct cds_wfcq_node *head, *tail;

+agrain:
if (_cds_wfcq_empty(src_q_head, src_q_tail))
return CDS_WFCQ_RET_SRC_EMPTY;

	head = ___cds_wfcq_node_sync_next(&src_q_head->node, blocking);
	if (head == CDS_WFCQ_WOULDBLOCK)
		return CDS_WFCQ_RET_WOULDBLOCK;

- _cds_wfcq_node_init(&src_q_head->node);
+ head = uatomic_xchg(&src_q_head->node.next, NULL);
+ if (!head)
+ goto again;

/*
* Memory barrier implied before uatomic_xchg() orders store to

Quoted 14 lines
Quoted 14 lines
Quoted 4 lines

Also, it does not check for
blocking/non-blocking caller.

we can add code to check it.

but:

for () {
	see src_q_head->node.next is not NULL
	xchg fail
}
Quoted 6 lines
Quoted 1 line

Is this a kind of blocking?

Not sure what your code snippet does, but if there is any way that we can have to busy-loop while we are in an intermediate transient state, then we could have to block.

See above.

Quoted 37 lines
Quoted 35 lines
Quoted 23 lines

Also, whenever possible, I like to have
for () or do/while constructs in place to make it clear that we can loop. So a modification of your proposal would look like:

/* Return 1 if nonblocking and needs to block, 0 otherwise */ static inline
bool ___cds_wfcq_busy_wait(int *attempt, int blocking)

{
        if (!blocking)
                return 1;
        if (+attempt >= WFCQ_ADAPT_ATTEMPTS) {
                poll(NULL, 0, WFCQ_WAIT);       /* Wait for 10ms */
                attempt = 0;
        } else {
                caa_cpu_relax();
        }
        return 0;
}

[...]

Quoted 11 lines
        struct cds_wfcq_node *head, *tail;
        int attempt = 0;

        for (;;) {
                if (_cds_wfcq_empty(src_q_head, src_q_tail))
                        return CDS_WFCQ_RET_SRC_EMPTY;
                head = uatomic_xchg(&src_q_head->node.next, NULL);
                if (head)
                        break;
                if (___cds_wfcq_busy_wait(&attempt, blocking))
                        return CDS_WFCQ_RET_WOULDBLOCK;
Quoted 1 line

return _cds_wfcq_empty(src_q_head, src_q_tail) ? CDS_WFCQ_RET_SRC_EMPTY : CDS_WFCQ_RET_WOULDBLOCK

Quoted 8 lines

I'm not sure it really buys us anything semantically: we already checked that the queue was non-empty, then xchg returns that head next pointer is NULL, so we have seen an null head next, but with tail not pointing to the head node, so we are in a state that could make us busy-wait. So rather than checking again if the queue is still in a transient state, we can return CDS_WFCQ_RET_WOULDBLOCK immediately, no ? The only advantage of checking it again is to catch a few cases where we would not have to block, but I'm not convinced this is really useful.

If some other beat us and win, the queue is currently empty,
we should return CDS_WFCQ_RET_SRC_EMPTY which is correct and avoid to mislead the caller. (you patch is correct in this semantic)

CDS_WFCQ_RET_WOULDBLOCK: the caller knows it is not empty when call,
the caller will handle some other urgent thing or relax a little if no urgent thing and then call it again quickly.

CDS_WFCQ_RET_SRC_EMPTY: the caller knows it is currently empty, the caller may sleep or ...

Quoted 122 lines

Thoughts ?

Thanks,

Mathieu

Quoted 114 lines
Quoted 113 lines
        }

Thoughts ?

Thanks,

Mathieu

Quoted 101 lines
Quoted 100 lines
urcu/static/wfcqueue.h +2 -3
 	/*
 	 * Memory barrier implied before uatomic_xchg() orders store to
@@ -435,14 +472,13 @@ ___cds_wfcq_splice(
 		return CDS_WFCQ_RET_DEST_EMPTY;
 }
 
-
 /*
  * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
  *
  * Dequeue all nodes from src_q.
  * dest_q must be already initialized.
- * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
- * by the caller.
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
  * Returns enum cds_wfcq_ret which indicates the state of the src or
  * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
urcu/wfcqueue.h +26 -14
index ddf6b87..d9ec534 100644
@@ -46,7 +46,7 @@ extern "C" {
 #define CDS_WFCQ_WOULDBLOCK	((void *) -1UL)
 
 enum cds_wfcq_ret {
-	CDS_WFCQ_RET_WOULDBLOCK = 	-1,
+	CDS_WFCQ_RET_WOULDBLOCK =	-1,
 	CDS_WFCQ_RET_DEST_EMPTY =	0,
 	CDS_WFCQ_RET_DEST_NON_EMPTY =	1,
 	CDS_WFCQ_RET_SRC_EMPTY = 	2,
@@ -110,13 +110,28 @@ struct cds_wfcq_tail {
 /*
  * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
  *
- * Unless otherwise stated, the caller must ensure mutual exclusion of
- * queue update operations "dequeue" and "splice" (for source queue).
- * Queue read operations "first" and "next", which are used by
- * "for_each" iterations, need to be protected against concurrent
- * "dequeue" and "splice" (for source queue) by the caller.
- * "enqueue", "splice" (for destination queue), and "empty" are the only
- * operations that can be used without any mutual exclusion.
+ * Synchronization table:
+ *
+ * External synchronization techniques described in the API below is
+ * required between pairs marked with "X". No external synchronization
+ * required between pairs marked with "-".
+ *
+ * Legend:
+ * [1] cds_wfcq_enqueue
+ * [2] __cds_wfcq_splice (destination queue)
+ * [3] __cds_wfcq_dequeue
+ * [4] __cds_wfcq_splice (source queue)
+ * [5] __cds_wfcq_first
+ * [6] __cds_wfcq_next
+ *
+ *     [1] [2] [3] [4] [5] [6]
+ * [1]  -   -   -   -   -   -
+ * [2]  -   -   -   -   -   -
+ * [3]  -   -   X   X   X   X
+ * [4]  -   -   X   -   X   X
+ * [5]  -   -   X   X   -   -
+ * [6]  -   -   X   X   -   -
+ *
  * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
  *
  * For convenience, cds_wfcq_dequeue_blocking() and
@@ -231,13 +246,10 @@ extern struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking(
  *
  * Dequeue all nodes from src_q.
  * dest_q must be already initialized.
- * Content written into the node before enqueue is guaranteed to be
- * consistent, but no other memory ordering is ensured.
- * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
- * by the caller.
- *
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
  * Returns enum cds_wfcq_ret which indicates the state of the src or
- * dest queue. Cannot block.
+ * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
 extern enum cds_wfcq_ret __cds_wfcq_splice_blocking(
 		struct cds_wfcq_head *dest_q_head,
wfcqueue.c +1 -1
index 207df95..ab0eb93 100644
@@ -1,7 +1,7 @@
 /*
  * wfcqueue.c
  *
- * Userspace RCU library - Concurrent queue with Wait-Free Enqueue/Blocking Dequeue
+ * Userspace RCU library - Concurrent Queue with Wait-Free Enqueue/Blocking Dequeue
  *
  * Copyright 2010-2012 - Mathieu Desnoyers <[email protected]>
  * Copyright 2011-2012 - Lai Jiangshan <[email protected]>

A message that is not in the archive (sent off-list or not yet received): [email protected]

#44 lore raw

Re: [PATCH 06/16] wfcqueue: implement mutex-free splice

Quoted 158 lines · * Lai Jiangshan ([email protected]) wrote:
Quoted 136 lines · Mathieu Desnoyers
Quoted 135 lines · * Lai Jiangshan ([email protected]) wrote:
Quoted 134 lines · Mathieu Desnoyers
Quoted 124 lines · * Lai Jiangshan ([email protected]) wrote:
Quoted 89 lines · Mathieu Desnoyers

A carefully crafted splice operation does not need to use an external mutex to synchronize against other splice operations.

The trick is atomically exchange the head next pointer with NULL. If the pointer we replaced was NULL, it means the queue was possibly empty. If head next was not NULL, by setting head to NULL, we ensure that concurrent splice operations are going to see an empty queue, even if concurrent enqueue operations move tail further. This means that as long as we are within splice, after setting head to NULL, but before moving tail back to head, concurrent splice operations will always see an empty queue, therefore acting as mutual exclusion.

If exchange returns a NULL head, we confirm that it was indeed empty by checking if the tail pointer points to the head node, busy-waiting if necessary.

Then the last step is to move the tail pointer to head. At that point, enqueuers are going to start enqueuing at head again, and other splice operations will be able to proceed.

---
 urcu/static/wfcqueue.h |   68 ++++++++++++++++++++++++++++++++++++------------
 urcu/wfcqueue.h        |   40 ++++++++++++++++++----------
 wfcqueue.c             |    2 +-
 3 files changed, 79 insertions(+), 31 deletions(-)
urcu/static/wfcqueue.h +34 -9
index 8774c03..4b2de50 100644
@@ -46,15 +46,30 @@ extern "C" {
  * half-wait-free/half-blocking queue implementation done by Paul E.
  * McKenney.
  *
- * Mutual exclusion of __cds_wfcq_* API
- *
- * Unless otherwise stated, the caller must ensure mutual exclusion of
- * queue update operations "dequeue" and "splice" (for source queue).
- * Queue read operations "first" and "next", which are used by
- * "for_each" iterations, need to be protected against concurrent
- * "dequeue" and "splice" (for source queue) by the caller.
- * "enqueue", "splice" (for destination queue), and "empty" are the only
- * operations that can be used without any mutual exclusion.
+ * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
+ *
+ * Synchronization table:
+ *
+ * External synchronization techniques described in the API below is
+ * required between pairs marked with "X". No external synchronization
+ * required between pairs marked with "-".
+ *
+ * Legend:
+ * [1] cds_wfcq_enqueue
+ * [2] __cds_wfcq_splice (destination queue)
+ * [3] __cds_wfcq_dequeue
+ * [4] __cds_wfcq_splice (source queue)
+ * [5] __cds_wfcq_first
+ * [6] __cds_wfcq_next
+ *
+ *     [1] [2] [3] [4] [5] [6]
+ * [1]  -   -   -   -   -   -
+ * [2]  -   -   -   -   -   -
+ * [3]  -   -   X   X   X   X
+ * [4]  -   -   X   -   X   X
+ * [5]  -   -   X   X   -   -
+ * [6]  -   -   X   X   -   -
+ *
  * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
  *
  * For convenience, cds_wfcq_dequeue_blocking() and
@@ -399,6 +414,16 @@ ___cds_wfcq_dequeue_nonblocking(struct cds_wfcq_head *head,
 	return ___cds_wfcq_dequeue(head, tail, 0);
 }
 
+/*
+ * __cds_wfcq_splice: enqueue all src_q nodes at the end of dest_q.
+ *
+ * Dequeue all nodes from src_q.
+ * dest_q must be already initialized.
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
+ * Returns enum cds_wfcq_ret which indicates the state of the src or
+ * dest queue.
+ */
 static inline enum cds_wfcq_ret
 ___cds_wfcq_splice(
 		struct cds_wfcq_head *dest_q_head,

urcu/static/wfcqueue.h ___cds_wfcq_splice( · line 436

Quoted 4 lines
urcu/static/wfcqueue.h +1 -0
@@ -408,14 +433,26 @@ ___cds_wfcq_splice(
 		int blocking)
 {
 	struct cds_wfcq_node *head, *tail;
+	int attempt = 0;

+again:

Quoted 13 lines
urcu/static/wfcqueue.h +5 -4
 	if (_cds_wfcq_empty(src_q_head, src_q_tail))
 		return CDS_WFCQ_RET_SRC_EMPTY;
 
-	head = ___cds_wfcq_node_sync_next(&src_q_head->node, blocking);
-	if (head == CDS_WFCQ_WOULDBLOCK)
-		return CDS_WFCQ_RET_WOULDBLOCK;
-	_cds_wfcq_node_init(&src_q_head->node);
+	for (;;) {
+		head = uatomic_xchg(&src_q_head->node.next, NULL);
+		if (head)
+			break;	/* non-empty */
+		if (CMM_LOAD_SHARED(src_q_tail->p) == &src_q_head->node)

urcu/static/wfcqueue.h ___cds_wfcq_splice( · line 454

Quoted 10 lines
urcu/static/wfcqueue.h +10 -0
+			return CDS_WFCQ_RET_SRC_EMPTY;
+		if (!blocking)
+			return CDS_WFCQ_RET_WOULDBLOCK;
+		if (++attempt >= WFCQ_ADAPT_ATTEMPTS) {
+			poll(NULL, 0, WFCQ_WAIT);	/* Wait for 10ms */
+			attempt = 0;
+		} else {
+			caa_cpu_relax();
+		}
+	}

Is it OK:

Review

Quoted 4 lines
(quoted diff) +3 -1
-	_cds_wfcq_node_init(&src_q_head->node);
+	head = uatomic_xchg(&src_q_head->node.next, NULL);
+	if (!head)
+		goto again;

You are right that we can simplify the code a bit by re-using _cds_wfcq_empty() to test validate emptiness of the source queue, rather than open-code it.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The only issue here is that the busy-loop (goto again) will not invoke caa_cpu_relax(), nor do adaptative waiting like ___cds_wfcq_node_sync_next() normally does.

Don't need, it will re-enter ___cds_wfcq_node_sync_next() to do it.

The ___cds_wfcq_node_sync_next is actually removed. It is replaced by the xchg of the head's next pointer.

It is kept.

{

(quoted diff) +1 -0
 	struct cds_wfcq_node *head, *tail;
 
+agrain:
 	if (_cds_wfcq_empty(src_q_head, src_q_tail))
 		return CDS_WFCQ_RET_SRC_EMPTY;
 
	head = ___cds_wfcq_node_sync_next(&src_q_head->node, blocking);
	if (head == CDS_WFCQ_WOULDBLOCK)
		return CDS_WFCQ_RET_WOULDBLOCK;

Review

Quoted 4 lines
(quoted diff) +3 -1
-	_cds_wfcq_node_init(&src_q_head->node);
+	head = uatomic_xchg(&src_q_head->node.next, NULL);
+	if (!head)
+		goto again;

I'm afraid this won't work. Let's suppose we have a queue initially containing 1 node, and we have 2 threads executing splice():

Thread A Thread B

splice()
splice()
-> not empty

                                 -> not empty

-> sync_next fetch head
-> xchg head with NULL
-> finish splice.
-> sync_next finds NULL head.
[ busy-loop forever ]

As we see, we need the sync_next operation to re-check for queue emptiness rather than re-trying until it finds a non-NULL next pointer.

Thanks,

Mathieu

Quoted 208 lines
(quoted diff) +0 -0
 	/*
 	 * Memory barrier implied before uatomic_xchg() orders store to
Quoted 16 lines
Quoted 16 lines
Quoted 6 lines

Also, it does not check for
blocking/non-blocking caller.

we can add code to check it.

but:

for () {
	see src_q_head->node.next is not NULL
	xchg fail
}
Quoted 6 lines
Quoted 1 line

Is this a kind of blocking?

Not sure what your code snippet does, but if there is any way that we can have to busy-loop while we are in an intermediate transient state, then we could have to block.

See above.

Quoted 37 lines
Quoted 35 lines
Quoted 23 lines

Also, whenever possible, I like to have
for () or do/while constructs in place to make it clear that we can loop. So a modification of your proposal would look like:

/* Return 1 if nonblocking and needs to block, 0 otherwise */ static inline
bool ___cds_wfcq_busy_wait(int *attempt, int blocking)

{
        if (!blocking)
                return 1;
        if (+attempt >= WFCQ_ADAPT_ATTEMPTS) {
                poll(NULL, 0, WFCQ_WAIT);       /* Wait for 10ms */
                attempt = 0;
        } else {
                caa_cpu_relax();
        }
        return 0;
}

[...]

Quoted 11 lines
        struct cds_wfcq_node *head, *tail;
        int attempt = 0;

        for (;;) {
                if (_cds_wfcq_empty(src_q_head, src_q_tail))
                        return CDS_WFCQ_RET_SRC_EMPTY;
                head = uatomic_xchg(&src_q_head->node.next, NULL);
                if (head)
                        break;
                if (___cds_wfcq_busy_wait(&attempt, blocking))
                        return CDS_WFCQ_RET_WOULDBLOCK;
Quoted 1 line

return _cds_wfcq_empty(src_q_head, src_q_tail) ? CDS_WFCQ_RET_SRC_EMPTY : CDS_WFCQ_RET_WOULDBLOCK

Quoted 8 lines

I'm not sure it really buys us anything semantically: we already checked that the queue was non-empty, then xchg returns that head next pointer is NULL, so we have seen an null head next, but with tail not pointing to the head node, so we are in a state that could make us busy-wait. So rather than checking again if the queue is still in a transient state, we can return CDS_WFCQ_RET_WOULDBLOCK immediately, no ? The only advantage of checking it again is to catch a few cases where we would not have to block, but I'm not convinced this is really useful.

If some other beat us and win, the queue is currently empty,
we should return CDS_WFCQ_RET_SRC_EMPTY which is correct and avoid to mislead the caller. (you patch is correct in this semantic)

CDS_WFCQ_RET_WOULDBLOCK: the caller knows it is not empty when call,
the caller will handle some other urgent thing or relax a little if no urgent thing and then call it again quickly.

CDS_WFCQ_RET_SRC_EMPTY: the caller knows it is currently empty, the caller may sleep or ...

Quoted 122 lines

Thoughts ?

Thanks,

Mathieu

Quoted 114 lines
Quoted 113 lines
        }

Thoughts ?

Thanks,

Mathieu

Quoted 101 lines
Quoted 100 lines
urcu/static/wfcqueue.h +2 -3
 	/*
 	 * Memory barrier implied before uatomic_xchg() orders store to
@@ -435,14 +472,13 @@ ___cds_wfcq_splice(
 		return CDS_WFCQ_RET_DEST_EMPTY;
 }
 
-
 /*
  * __cds_wfcq_splice_blocking: enqueue all src_q nodes at the end of dest_q.
  *
  * Dequeue all nodes from src_q.
  * dest_q must be already initialized.
- * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
- * by the caller.
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
  * Returns enum cds_wfcq_ret which indicates the state of the src or
  * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
urcu/wfcqueue.h +26 -14
index ddf6b87..d9ec534 100644
@@ -46,7 +46,7 @@ extern "C" {
 #define CDS_WFCQ_WOULDBLOCK	((void *) -1UL)
 
 enum cds_wfcq_ret {
-	CDS_WFCQ_RET_WOULDBLOCK = 	-1,
+	CDS_WFCQ_RET_WOULDBLOCK =	-1,
 	CDS_WFCQ_RET_DEST_EMPTY =	0,
 	CDS_WFCQ_RET_DEST_NON_EMPTY =	1,
 	CDS_WFCQ_RET_SRC_EMPTY = 	2,
@@ -110,13 +110,28 @@ struct cds_wfcq_tail {
 /*
  * Mutual exclusion of cds_wfcq_* / __cds_wfcq_* API
  *
- * Unless otherwise stated, the caller must ensure mutual exclusion of
- * queue update operations "dequeue" and "splice" (for source queue).
- * Queue read operations "first" and "next", which are used by
- * "for_each" iterations, need to be protected against concurrent
- * "dequeue" and "splice" (for source queue) by the caller.
- * "enqueue", "splice" (for destination queue), and "empty" are the only
- * operations that can be used without any mutual exclusion.
+ * Synchronization table:
+ *
+ * External synchronization techniques described in the API below is
+ * required between pairs marked with "X". No external synchronization
+ * required between pairs marked with "-".
+ *
+ * Legend:
+ * [1] cds_wfcq_enqueue
+ * [2] __cds_wfcq_splice (destination queue)
+ * [3] __cds_wfcq_dequeue
+ * [4] __cds_wfcq_splice (source queue)
+ * [5] __cds_wfcq_first
+ * [6] __cds_wfcq_next
+ *
+ *     [1] [2] [3] [4] [5] [6]
+ * [1]  -   -   -   -   -   -
+ * [2]  -   -   -   -   -   -
+ * [3]  -   -   X   X   X   X
+ * [4]  -   -   X   -   X   X
+ * [5]  -   -   X   X   -   -
+ * [6]  -   -   X   X   -   -
+ *
  * Mutual exclusion can be ensured by holding cds_wfcq_dequeue_lock().
  *
  * For convenience, cds_wfcq_dequeue_blocking() and
@@ -231,13 +246,10 @@ extern struct cds_wfcq_node *__cds_wfcq_dequeue_nonblocking(
  *
  * Dequeue all nodes from src_q.
  * dest_q must be already initialized.
- * Content written into the node before enqueue is guaranteed to be
- * consistent, but no other memory ordering is ensured.
- * Dequeue/splice/iteration mutual exclusion for src_q should be ensured
- * by the caller.
- *
+ * Mutual exclusion for src_q should be ensured by the caller as
+ * specified in the "Synchronisation table".
  * Returns enum cds_wfcq_ret which indicates the state of the src or
- * dest queue. Cannot block.
+ * dest queue. Never returns CDS_WFCQ_RET_WOULDBLOCK.
  */
 extern enum cds_wfcq_ret __cds_wfcq_splice_blocking(
 		struct cds_wfcq_head *dest_q_head,
wfcqueue.c +1 -1
index 207df95..ab0eb93 100644
@@ -1,7 +1,7 @@
 /*
  * wfcqueue.c
  *
- * Userspace RCU library - Concurrent queue with Wait-Free Enqueue/Blocking Dequeue
+ * Userspace RCU library - Concurrent Queue with Wait-Free Enqueue/Blocking Dequeue
  *
  * Copyright 2010-2012 - Mathieu Desnoyers <[email protected]>
  * Copyright 2011-2012 - Lai Jiangshan <[email protected]>
Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#45 lore raw

Re: [URCU pull request review] urcu improvements

In reply to a message not in the archive
Quoted 4 lines · * Mathieu Desnoyers ([email protected]) wrote:

Here is a pull request to myself for my recent work on wfcqueue and urcu flavors into the userspace RCU master branch.

Please let me know if you see any immediate show stoppers.

FYI, after small modifications to answer the comments, I pulled these changes into master.

Thanks,

Mathieu

Quoted 44 lines

List of changes:

[PATCH 01/16] Fix: wfcqueue nonblocking dequeue
[PATCH 02/16] wfcqueue: enqueue and splice return queue state [PATCH 03/16] test wfcqueue: add tests for queue state return value [PATCH 04/16] urcu-call-rcu: use wait-free splice return value [PATCH 05/16] wfcqueue: document empty criterion
[PATCH 06/16] wfcqueue: implement mutex-free splice [PATCH 07/16] urcu-qsbr: improve 2-phase wait scheme [PATCH 08/16] urcu-mb/signal/membarrier: improve 2-phase wait scheme [PATCH 09/16] urcu-bp: improve 2-phase wait scheme
[PATCH 10/16] urcu-qsbr: move offline threads to separate list [PATCH 11/16] urcu-mb/signal/membarrier: move quiescent threads to [PATCH 12/16] urcu-bp: move quiescent threads to separate list [PATCH 13/16] tests: use standard malloc/free for synchronize_rcu() [PATCH 14/16] urcu-qsbr: batch concurrent synchronize_rcu() [PATCH 15/16] urcu-wait: move wait code into separate file [PATCH 16/16] urcu-wait: move queue management code into urcu-wait.h

diffstat:

 Makefile.am             |    2 
 tests/test_urcu.c       |   60 ++------------
 tests/test_urcu_bp.c    |   59 ++------------
 tests/test_urcu_qsbr.c  |   61 ++-------------
 tests/test_urcu_wfcq.c  |  131 +++++++++++++++++++++++++-------
 urcu-bp.c               |   88 ++++++++++++++-------
 urcu-call-rcu-impl.h    |    9 +-
 urcu-qsbr.c             |  193 ++++++++++++++++++++++++++++++++++++------------
 urcu-wait.h             |  184 +++++++++++++++++++++++++++++++++++++++++++++
 urcu.c                  |   95 +++++++++++++++--------
 urcu/static/urcu-bp.h   |   29 ++++---
 urcu/static/urcu-qsbr.h |   14 ++-
 urcu/static/urcu.h      |   15 ++-
 urcu/static/wfcqueue.h  |  133 +++++++++++++++++++++++++--------
 urcu/wfcqueue.h         |   62 +++++++++++----
 wfcqueue.c              |   16 +--
 16 files changed, 786 insertions(+), 365 deletions(-)

Thanks,

Mathieu

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#46 lore raw

Re: [URCU pull request review] urcu improvements

In reply to a message not in the archive

ACK for the whole patchset.

Thanks,
Lai

Quoted 49 lines · Mathieu Desnoyers

Here is a pull request to myself for my recent work on wfcqueue and urcu flavors into the userspace RCU master branch.

Please let me know if you see any immediate show stoppers.

List of changes:

[PATCH 01/16] Fix: wfcqueue nonblocking dequeue
[PATCH 02/16] wfcqueue: enqueue and splice return queue state [PATCH 03/16] test wfcqueue: add tests for queue state return value [PATCH 04/16] urcu-call-rcu: use wait-free splice return value [PATCH 05/16] wfcqueue: document empty criterion
[PATCH 06/16] wfcqueue: implement mutex-free splice [PATCH 07/16] urcu-qsbr: improve 2-phase wait scheme [PATCH 08/16] urcu-mb/signal/membarrier: improve 2-phase wait scheme [PATCH 09/16] urcu-bp: improve 2-phase wait scheme
[PATCH 10/16] urcu-qsbr: move offline threads to separate list [PATCH 11/16] urcu-mb/signal/membarrier: move quiescent threads to [PATCH 12/16] urcu-bp: move quiescent threads to separate list [PATCH 13/16] tests: use standard malloc/free for synchronize_rcu() [PATCH 14/16] urcu-qsbr: batch concurrent synchronize_rcu() [PATCH 15/16] urcu-wait: move wait code into separate file [PATCH 16/16] urcu-wait: move queue management code into urcu-wait.h

diffstat:

 Makefile.am             |    2 
 tests/test_urcu.c       |   60 ++------------
 tests/test_urcu_bp.c    |   59 ++------------
 tests/test_urcu_qsbr.c  |   61 ++-------------
 tests/test_urcu_wfcq.c  |  131 +++++++++++++++++++++++++-------
 urcu-bp.c               |   88 ++++++++++++++-------
 urcu-call-rcu-impl.h    |    9 +-
 urcu-qsbr.c             |  193 ++++++++++++++++++++++++++++++++++++------------
 urcu-wait.h             |  184 +++++++++++++++++++++++++++++++++++++++++++++
 urcu.c                  |   95 +++++++++++++++--------
 urcu/static/urcu-bp.h   |   29 ++++---
 urcu/static/urcu-qsbr.h |   14 ++-
 urcu/static/urcu.h      |   15 ++-
 urcu/static/wfcqueue.h  |  133 +++++++++++++++++++++++++--------
 urcu/wfcqueue.h         |   62 +++++++++++----
 wfcqueue.c              |   16 +--
 16 files changed, 786 insertions(+), 365 deletions(-)

Thanks,

Mathieu

A message that is not in the archive (sent off-list or not yet received): [email protected]

#48 lore raw

Re: [URCU pull request review] urcu improvements

Quoted 1 line · * Lai Jiangshan ([email protected]) wrote:

ACK for the whole patchset.

Thanks!

Unfortunately, the commits are already in the master branch, so I won't be able to edit the changelogs to add the acked-by tag, but I really appreciate your ack on the changeset.

Mathieu

Quoted 54 lines

Thanks,
Lai

Quoted 49 lines · Mathieu Desnoyers

Here is a pull request to myself for my recent work on wfcqueue and urcu flavors into the userspace RCU master branch.

Please let me know if you see any immediate show stoppers.

List of changes:

[PATCH 01/16] Fix: wfcqueue nonblocking dequeue
[PATCH 02/16] wfcqueue: enqueue and splice return queue state [PATCH 03/16] test wfcqueue: add tests for queue state return value [PATCH 04/16] urcu-call-rcu: use wait-free splice return value [PATCH 05/16] wfcqueue: document empty criterion
[PATCH 06/16] wfcqueue: implement mutex-free splice [PATCH 07/16] urcu-qsbr: improve 2-phase wait scheme [PATCH 08/16] urcu-mb/signal/membarrier: improve 2-phase wait scheme [PATCH 09/16] urcu-bp: improve 2-phase wait scheme
[PATCH 10/16] urcu-qsbr: move offline threads to separate list [PATCH 11/16] urcu-mb/signal/membarrier: move quiescent threads to [PATCH 12/16] urcu-bp: move quiescent threads to separate list [PATCH 13/16] tests: use standard malloc/free for synchronize_rcu() [PATCH 14/16] urcu-qsbr: batch concurrent synchronize_rcu() [PATCH 15/16] urcu-wait: move wait code into separate file [PATCH 16/16] urcu-wait: move queue management code into urcu-wait.h

diffstat:

 Makefile.am             |    2 
 tests/test_urcu.c       |   60 ++------------
 tests/test_urcu_bp.c    |   59 ++------------
 tests/test_urcu_qsbr.c  |   61 ++-------------
 tests/test_urcu_wfcq.c  |  131 +++++++++++++++++++++++++-------
 urcu-bp.c               |   88 ++++++++++++++-------
 urcu-call-rcu-impl.h    |    9 +-
 urcu-qsbr.c             |  193 ++++++++++++++++++++++++++++++++++++------------
 urcu-wait.h             |  184 +++++++++++++++++++++++++++++++++++++++++++++
 urcu.c                  |   95 +++++++++++++++--------
 urcu/static/urcu-bp.h   |   29 ++++---
 urcu/static/urcu-qsbr.h |   14 ++-
 urcu/static/urcu.h      |   15 ++-
 urcu/static/wfcqueue.h  |  133 +++++++++++++++++++++++++--------
 urcu/wfcqueue.h         |   62 +++++++++++----
 wfcqueue.c              |   16 +--
 16 files changed, 786 insertions(+), 365 deletions(-)

Thanks,

Mathieu

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com