doc/wfcqueue: cds_wfcq_splice
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 21 Jun 2013 19:57:35 +0000 (15:57 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 21 Jun 2013 19:57:35 +0000 (15:57 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
.gitignore
doc/examples/Makefile.am
doc/examples/wfcqueue/Makefile
doc/examples/wfcqueue/Makefile.cds_wfcq_splice [new file with mode: 0644]
doc/examples/wfcqueue/cds_wfcq_dequeue.c
doc/examples/wfcqueue/cds_wfcq_enqueue.c
doc/examples/wfcqueue/cds_wfcq_splice.c [new file with mode: 0644]

index 01f9e92fc9e91bae9985f9686021c0a08f4e638f..ce5241d3086127a5e82967b40dcc2147c4236ddb 100644 (file)
@@ -84,6 +84,7 @@ doc/examples/list/cds_list_replace_rcu
 
 doc/examples/wfcqueue/cds_wfcq_enqueue
 doc/examples/wfcqueue/cds_wfcq_dequeue
+doc/examples/wfcqueue/cds_wfcq_splice
 
 #automake
 /config.h
index 5425ed2fe0f42a4ce5eefd7bfac7011399d36f1a..d964c06733445826a7ad484b1b1573375e5f362c 100644 (file)
@@ -31,8 +31,10 @@ doc_examples_wfcqueuedir = ${doc_examplesdir}/wfcqueue
 dist_doc_examples_wfcqueue_DATA = \
        wfcqueue/Makefile.cds_wfcq_enqueue \
        wfcqueue/Makefile.cds_wfcq_dequeue \
+       wfcqueue/Makefile.cds_wfcq_splice \
        wfcqueue/cds_wfcq_enqueue.c \
-       wfcqueue/cds_wfcq_dequeue.c
+       wfcqueue/cds_wfcq_dequeue.c \
+       wfcqueue/cds_wfcq_splice.c
 
 if NO_SHARED
 # Don't build examples if shared libraries support was explicitly
index 8256128dcd7c4f8ccb022bc91eed385aaa3f2795..98b9def39def20722686d5789f359d20ae0954a0 100644 (file)
 all:
        $(MAKE) -f Makefile.cds_wfcq_enqueue
        $(MAKE) -f Makefile.cds_wfcq_dequeue
+       $(MAKE) -f Makefile.cds_wfcq_splice
 
 .PHONY: clean
 clean:
        $(MAKE) -f Makefile.cds_wfcq_enqueue clean
        $(MAKE) -f Makefile.cds_wfcq_dequeue clean
+       $(MAKE) -f Makefile.cds_wfcq_splice clean
diff --git a/doc/examples/wfcqueue/Makefile.cds_wfcq_splice b/doc/examples/wfcqueue/Makefile.cds_wfcq_splice
new file mode 100644 (file)
index 0000000..76b9f0a
--- /dev/null
@@ -0,0 +1,21 @@
+# Copyright (C) 2013  Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+#
+# THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
+# OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
+#
+# Permission is hereby granted to use or copy this program for any
+# purpose,  provided the above notices are retained on all copies.
+# Permission to modify the code and to distribute modified code is
+# granted, provided the above notices are retained, and a notice that
+# the code was modified is included with the above copyright notice.
+#
+# This makefile is purposefully kept simple to support GNU and BSD make.
+
+EXAMPLE_NAME = cds_wfcq_splice
+
+SOURCES = $(EXAMPLE_NAME).c
+OBJECTS = $(EXAMPLE_NAME).o
+BINARY = $(EXAMPLE_NAME)
+LIBS = -lurcu-common
+
+include ../Makefile.examples.template
index 6488d5363ad1a258126f0c049bd26492f2c49594..c2783510ce1e0d8311833455fdc3db3b91b55133 100644 (file)
@@ -38,8 +38,7 @@ int main(int argc, char **argv)
        cds_wfcq_init(&myqueue_head, &myqueue_tail);
 
        /*
-        * Adding nodes to the linked-list. Safe against concurrent
-        * RCU traversals, require mutual exclusion with list updates.
+        * Enqueue nodes.
         */
        for (i = 0; i < CAA_ARRAY_SIZE(values); i++) {
                struct mynode *node;
index 6af2c54e7cac2d73618f4b5c89651b2e42305c84..377f017ea3b4dda50312e40ed7ae6fab8e4b29ef 100644 (file)
@@ -39,8 +39,7 @@ int main(int argc, char **argv)
        cds_wfcq_init(&myqueue_head, &myqueue_tail);
 
        /*
-        * Adding nodes to the linked-list. Safe against concurrent
-        * RCU traversals, require mutual exclusion with list updates.
+        * Enqueue nodes.
         */
        for (i = 0; i < CAA_ARRAY_SIZE(values); i++) {
                struct mynode *node;
diff --git a/doc/examples/wfcqueue/cds_wfcq_splice.c b/doc/examples/wfcqueue/cds_wfcq_splice.c
new file mode 100644 (file)
index 0000000..bb2e512
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2013  Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
+ * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
+ *
+ * Permission is hereby granted to use or copy this program for any
+ * purpose,  provided the above notices are retained on all copies.
+ * Permission to modify the code and to distribute modified code is
+ * granted, provided the above notices are retained, and a notice that
+ * the code was modified is included with the above copyright notice.
+ *
+ * This example shows how to splice nodes from a source wfcqueue A into
+ * a destination wfcqueue B.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <urcu/wfcqueue.h>     /* Wait-free concurrent queue */
+#include <urcu/compiler.h>     /* For CAA_ARRAY_SIZE */
+
+/*
+ * Nodes populated into the queue.
+ */
+struct mynode {
+       int value;                      /* Node content */
+       struct cds_wfcq_node node;      /* Chaining in queue */
+};
+
+static
+int enqueue_values(struct cds_wfcq_head *head,
+               struct cds_wfcq_tail *tail,
+               int *values,
+               size_t nr_values)
+{
+       int ret = 0;
+       unsigned int i;
+
+       for (i = 0; i < nr_values; i++) {
+               struct mynode *node;
+
+               node = malloc(sizeof(*node));
+               if (!node) {
+                       ret = -1;
+                       goto end;
+               }
+               cds_wfcq_node_init(&node->node);
+               node->value = values[i];
+               cds_wfcq_enqueue(head, tail, &node->node);
+       }
+end:
+       return ret;
+}
+
+static
+void print_queue(struct cds_wfcq_head *head,
+               struct cds_wfcq_tail *tail,
+               const char *qname)
+{
+       struct cds_wfcq_node *qnode;
+
+       printf("%s:", qname);
+       __cds_wfcq_for_each_blocking(head, tail, qnode) {
+               struct mynode *node =
+                       caa_container_of(qnode, struct mynode, node);
+               printf(" %d", node->value);
+       }
+       printf("\n");
+}
+
+int main(int argc, char **argv)
+{
+       int values_A[] = { -5, 42, 36, 24, };
+       int values_B[] = { 200, 300, 400, };
+       struct cds_wfcq_head head_A;    /* Queue A head */
+       struct cds_wfcq_tail tail_A;    /* Queue A tail */
+       struct cds_wfcq_head head_B;    /* Queue B head */
+       struct cds_wfcq_tail tail_B;    /* Queue B tail */
+       int ret = 0;
+
+       cds_wfcq_init(&head_A, &tail_A);
+       /* Enqueue nodes into A. */
+       ret = enqueue_values(&head_A, &tail_A, values_A,
+                       CAA_ARRAY_SIZE(values_A));
+       if (ret)
+               goto end;
+       cds_wfcq_init(&head_B, &tail_B);
+       /* Enqueue nodes into B. */
+       ret = enqueue_values(&head_B, &tail_B, values_B,
+                       CAA_ARRAY_SIZE(values_B));
+       if (ret)
+               goto end;
+
+       print_queue(&head_A, &tail_A, "queue A content before splice");
+       print_queue(&head_B, &tail_B, "queue B content before splice");
+
+       /*
+        * Splice nodes from A into B.
+        */
+       printf("Splicing queue A into queue B\n");
+       (void) cds_wfcq_splice_blocking(&head_B, &tail_B,
+                       &head_A, &tail_A);
+
+       print_queue(&head_A, &tail_A, "queue A content after splice");
+       print_queue(&head_B, &tail_B, "queue B content after splice");
+end:
+       return ret;
+}
This page took 0.028345 seconds and 4 git commands to generate.