2 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
7 * Permission is hereby granted to use or copy this program for any
8 * purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is
10 * granted, provided the above notices are retained, and a notice that
11 * the code was modified is included with the above copyright notice.
13 * This example shows how to splice nodes from a source wfcqueue A into
14 * a destination wfcqueue B.
20 #include <urcu/wfcqueue.h> /* Wait-free concurrent queue */
21 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
24 * Nodes populated into the queue.
27 int value
; /* Node content */
28 struct cds_wfcq_node node
; /* Chaining in queue */
32 int enqueue_values(struct cds_wfcq_head
*head
,
33 struct cds_wfcq_tail
*tail
,
40 for (i
= 0; i
< nr_values
; i
++) {
43 node
= malloc(sizeof(*node
));
48 cds_wfcq_node_init(&node
->node
);
49 node
->value
= values
[i
];
50 cds_wfcq_enqueue(head
, tail
, &node
->node
);
57 void print_queue(struct cds_wfcq_head
*head
,
58 struct cds_wfcq_tail
*tail
,
61 struct cds_wfcq_node
*qnode
;
64 __cds_wfcq_for_each_blocking(head
, tail
, qnode
) {
66 caa_container_of(qnode
, struct mynode
, node
);
67 printf(" %d", node
->value
);
72 int main(int argc
, char **argv
)
74 int values_A
[] = { -5, 42, 36, 24, };
75 int values_B
[] = { 200, 300, 400, };
76 struct cds_wfcq_head head_A
; /* Queue A head */
77 struct cds_wfcq_tail tail_A
; /* Queue A tail */
78 struct cds_wfcq_head head_B
; /* Queue B head */
79 struct cds_wfcq_tail tail_B
; /* Queue B tail */
82 cds_wfcq_init(&head_A
, &tail_A
);
83 /* Enqueue nodes into A. */
84 ret
= enqueue_values(&head_A
, &tail_A
, values_A
,
85 CAA_ARRAY_SIZE(values_A
));
88 cds_wfcq_init(&head_B
, &tail_B
);
89 /* Enqueue nodes into B. */
90 ret
= enqueue_values(&head_B
, &tail_B
, values_B
,
91 CAA_ARRAY_SIZE(values_B
));
95 print_queue(&head_A
, &tail_A
, "queue A content before splice");
96 print_queue(&head_B
, &tail_B
, "queue B content before splice");
99 * Splice nodes from A into B.
101 printf("Splicing queue A into queue B\n");
102 (void) cds_wfcq_splice_blocking(&head_B
, &tail_B
,
105 print_queue(&head_A
, &tail_A
, "queue A content after splice");
106 print_queue(&head_B
, &tail_B
, "queue B content after splice");
108 cds_wfcq_destroy(&head_A
, &tail_A
);
This page took 0.034017 seconds and 4 git commands to generate.