1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
// SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
//
// SPDX-License-Identifier: MIT
/*
* 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(void)
{
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:
cds_wfcq_destroy(&head_A, &tail_A);
return ret;
}
|