From: Mathieu Desnoyers Date: Fri, 21 Jun 2013 19:57:35 +0000 (-0400) Subject: doc/wfcqueue: cds_wfcq_splice X-Git-Tag: v0.8.0~45 X-Git-Url: https://git.lttng.org/?p=urcu.git;a=commitdiff_plain;h=8f09dfa72762669a41b322388a7c15ee010265f0 doc/wfcqueue: cds_wfcq_splice Signed-off-by: Mathieu Desnoyers --- diff --git a/.gitignore b/.gitignore index 01f9e92..ce5241d 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/doc/examples/Makefile.am b/doc/examples/Makefile.am index 5425ed2..d964c06 100644 --- a/doc/examples/Makefile.am +++ b/doc/examples/Makefile.am @@ -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 diff --git a/doc/examples/wfcqueue/Makefile b/doc/examples/wfcqueue/Makefile index 8256128..98b9def 100644 --- a/doc/examples/wfcqueue/Makefile +++ b/doc/examples/wfcqueue/Makefile @@ -14,8 +14,10 @@ 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 index 0000000..76b9f0a --- /dev/null +++ b/doc/examples/wfcqueue/Makefile.cds_wfcq_splice @@ -0,0 +1,21 @@ +# Copyright (C) 2013 Mathieu Desnoyers +# +# 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 diff --git a/doc/examples/wfcqueue/cds_wfcq_dequeue.c b/doc/examples/wfcqueue/cds_wfcq_dequeue.c index 6488d53..c278351 100644 --- a/doc/examples/wfcqueue/cds_wfcq_dequeue.c +++ b/doc/examples/wfcqueue/cds_wfcq_dequeue.c @@ -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; diff --git a/doc/examples/wfcqueue/cds_wfcq_enqueue.c b/doc/examples/wfcqueue/cds_wfcq_enqueue.c index 6af2c54..377f017 100644 --- a/doc/examples/wfcqueue/cds_wfcq_enqueue.c +++ b/doc/examples/wfcqueue/cds_wfcq_enqueue.c @@ -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 index 0000000..bb2e512 --- /dev/null +++ b/doc/examples/wfcqueue/cds_wfcq_splice.c @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2013 Mathieu Desnoyers + * + * 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 +#include + +#include /* Wait-free concurrent queue */ +#include /* 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; +}