| 1 | /* |
| 2 | * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 3 | * |
| 4 | * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 5 | * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 6 | * |
| 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. |
| 12 | * |
| 13 | * This example shows how to splice nodes from a source wfcqueue A into |
| 14 | * a destination wfcqueue B. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | |
| 20 | #include <urcu/wfcqueue.h> /* Wait-free concurrent queue */ |
| 21 | #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */ |
| 22 | |
| 23 | /* |
| 24 | * Nodes populated into the queue. |
| 25 | */ |
| 26 | struct mynode { |
| 27 | int value; /* Node content */ |
| 28 | struct cds_wfcq_node node; /* Chaining in queue */ |
| 29 | }; |
| 30 | |
| 31 | static |
| 32 | int enqueue_values(struct cds_wfcq_head *head, |
| 33 | struct cds_wfcq_tail *tail, |
| 34 | int *values, |
| 35 | size_t nr_values) |
| 36 | { |
| 37 | int ret = 0; |
| 38 | unsigned int i; |
| 39 | |
| 40 | for (i = 0; i < nr_values; i++) { |
| 41 | struct mynode *node; |
| 42 | |
| 43 | node = malloc(sizeof(*node)); |
| 44 | if (!node) { |
| 45 | ret = -1; |
| 46 | goto end; |
| 47 | } |
| 48 | cds_wfcq_node_init(&node->node); |
| 49 | node->value = values[i]; |
| 50 | cds_wfcq_enqueue(head, tail, &node->node); |
| 51 | } |
| 52 | end: |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | static |
| 57 | void print_queue(struct cds_wfcq_head *head, |
| 58 | struct cds_wfcq_tail *tail, |
| 59 | const char *qname) |
| 60 | { |
| 61 | struct cds_wfcq_node *qnode; |
| 62 | |
| 63 | printf("%s:", qname); |
| 64 | __cds_wfcq_for_each_blocking(head, tail, qnode) { |
| 65 | struct mynode *node = |
| 66 | caa_container_of(qnode, struct mynode, node); |
| 67 | printf(" %d", node->value); |
| 68 | } |
| 69 | printf("\n"); |
| 70 | } |
| 71 | |
| 72 | int main(int argc, char **argv) |
| 73 | { |
| 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 */ |
| 80 | int ret = 0; |
| 81 | |
| 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)); |
| 86 | if (ret) |
| 87 | goto end; |
| 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)); |
| 92 | if (ret) |
| 93 | goto end; |
| 94 | |
| 95 | print_queue(&head_A, &tail_A, "queue A content before splice"); |
| 96 | print_queue(&head_B, &tail_B, "queue B content before splice"); |
| 97 | |
| 98 | /* |
| 99 | * Splice nodes from A into B. |
| 100 | */ |
| 101 | printf("Splicing queue A into queue B\n"); |
| 102 | (void) cds_wfcq_splice_blocking(&head_B, &tail_B, |
| 103 | &head_A, &tail_A); |
| 104 | |
| 105 | print_queue(&head_A, &tail_A, "queue A content after splice"); |
| 106 | print_queue(&head_B, &tail_B, "queue B content after splice"); |
| 107 | end: |
| 108 | cds_wfcq_destroy(&head_A, &tail_A); |
| 109 | return ret; |
| 110 | } |