Commit | Line | Data |
---|---|---|
1c87adb3 MJ |
1 | // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2 | // | |
3 | // SPDX-License-Identifier: MIT | |
4 | ||
8f09dfa7 | 5 | /* |
8f09dfa7 MD |
6 | * This example shows how to splice nodes from a source wfcqueue A into |
7 | * a destination wfcqueue B. | |
8 | */ | |
9 | ||
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | ||
13 | #include <urcu/wfcqueue.h> /* Wait-free concurrent queue */ | |
14 | #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */ | |
15 | ||
16 | /* | |
17 | * Nodes populated into the queue. | |
18 | */ | |
19 | struct mynode { | |
20 | int value; /* Node content */ | |
21 | struct cds_wfcq_node node; /* Chaining in queue */ | |
22 | }; | |
23 | ||
24 | static | |
25 | int enqueue_values(struct cds_wfcq_head *head, | |
26 | struct cds_wfcq_tail *tail, | |
27 | int *values, | |
28 | size_t nr_values) | |
29 | { | |
30 | int ret = 0; | |
31 | unsigned int i; | |
32 | ||
33 | for (i = 0; i < nr_values; i++) { | |
34 | struct mynode *node; | |
35 | ||
36 | node = malloc(sizeof(*node)); | |
37 | if (!node) { | |
38 | ret = -1; | |
39 | goto end; | |
40 | } | |
41 | cds_wfcq_node_init(&node->node); | |
42 | node->value = values[i]; | |
43 | cds_wfcq_enqueue(head, tail, &node->node); | |
44 | } | |
45 | end: | |
46 | return ret; | |
47 | } | |
48 | ||
49 | static | |
50 | void print_queue(struct cds_wfcq_head *head, | |
51 | struct cds_wfcq_tail *tail, | |
52 | const char *qname) | |
53 | { | |
54 | struct cds_wfcq_node *qnode; | |
55 | ||
56 | printf("%s:", qname); | |
57 | __cds_wfcq_for_each_blocking(head, tail, qnode) { | |
58 | struct mynode *node = | |
59 | caa_container_of(qnode, struct mynode, node); | |
60 | printf(" %d", node->value); | |
61 | } | |
62 | printf("\n"); | |
63 | } | |
64 | ||
70469b43 | 65 | int main(void) |
8f09dfa7 MD |
66 | { |
67 | int values_A[] = { -5, 42, 36, 24, }; | |
68 | int values_B[] = { 200, 300, 400, }; | |
69 | struct cds_wfcq_head head_A; /* Queue A head */ | |
70 | struct cds_wfcq_tail tail_A; /* Queue A tail */ | |
71 | struct cds_wfcq_head head_B; /* Queue B head */ | |
72 | struct cds_wfcq_tail tail_B; /* Queue B tail */ | |
73 | int ret = 0; | |
74 | ||
75 | cds_wfcq_init(&head_A, &tail_A); | |
76 | /* Enqueue nodes into A. */ | |
77 | ret = enqueue_values(&head_A, &tail_A, values_A, | |
78 | CAA_ARRAY_SIZE(values_A)); | |
79 | if (ret) | |
80 | goto end; | |
81 | cds_wfcq_init(&head_B, &tail_B); | |
82 | /* Enqueue nodes into B. */ | |
83 | ret = enqueue_values(&head_B, &tail_B, values_B, | |
84 | CAA_ARRAY_SIZE(values_B)); | |
85 | if (ret) | |
86 | goto end; | |
87 | ||
88 | print_queue(&head_A, &tail_A, "queue A content before splice"); | |
89 | print_queue(&head_B, &tail_B, "queue B content before splice"); | |
90 | ||
91 | /* | |
92 | * Splice nodes from A into B. | |
93 | */ | |
94 | printf("Splicing queue A into queue B\n"); | |
95 | (void) cds_wfcq_splice_blocking(&head_B, &tail_B, | |
96 | &head_A, &tail_A); | |
97 | ||
98 | print_queue(&head_A, &tail_A, "queue A content after splice"); | |
99 | print_queue(&head_B, &tail_B, "queue B content after splice"); | |
100 | end: | |
6acc6ad4 | 101 | cds_wfcq_destroy(&head_A, &tail_A); |
8f09dfa7 MD |
102 | return ret; |
103 | } |