| 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 dequeue nodes from a RCU lock-free queue. |
| 14 | * This queue requires using a RCU scheme. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | |
| 20 | #include <urcu/urcu-memb.h> /* RCU flavor */ |
| 21 | #include <urcu/rculfqueue.h> /* RCU Lock-free queue */ |
| 22 | #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */ |
| 23 | |
| 24 | /* |
| 25 | * Nodes populated into the queue. |
| 26 | */ |
| 27 | struct mynode { |
| 28 | int value; /* Node content */ |
| 29 | struct cds_lfq_node_rcu node; /* Chaining in queue */ |
| 30 | struct rcu_head rcu_head; /* For call_rcu() */ |
| 31 | }; |
| 32 | |
| 33 | static |
| 34 | void free_node(struct rcu_head *head) |
| 35 | { |
| 36 | struct mynode *node = |
| 37 | caa_container_of(head, struct mynode, rcu_head); |
| 38 | |
| 39 | free(node); |
| 40 | } |
| 41 | |
| 42 | int main(void) |
| 43 | { |
| 44 | int values[] = { -5, 42, 36, 24, }; |
| 45 | struct cds_lfq_queue_rcu myqueue; /* Queue */ |
| 46 | unsigned int i; |
| 47 | int ret = 0; |
| 48 | |
| 49 | /* |
| 50 | * Each thread need using RCU read-side need to be explicitly |
| 51 | * registered. |
| 52 | */ |
| 53 | urcu_memb_register_thread(); |
| 54 | |
| 55 | cds_lfq_init_rcu(&myqueue, urcu_memb_call_rcu); |
| 56 | |
| 57 | /* |
| 58 | * Enqueue nodes. |
| 59 | */ |
| 60 | for (i = 0; i < CAA_ARRAY_SIZE(values); i++) { |
| 61 | struct mynode *node; |
| 62 | |
| 63 | node = malloc(sizeof(*node)); |
| 64 | if (!node) { |
| 65 | ret = -1; |
| 66 | goto end; |
| 67 | } |
| 68 | |
| 69 | cds_lfq_node_init_rcu(&node->node); |
| 70 | node->value = values[i]; |
| 71 | /* |
| 72 | * Both enqueue and dequeue need to be called within RCU |
| 73 | * read-side critical section. |
| 74 | */ |
| 75 | urcu_memb_read_lock(); |
| 76 | cds_lfq_enqueue_rcu(&myqueue, &node->node); |
| 77 | urcu_memb_read_unlock(); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Dequeue each node from the queue. Those will be dequeued from |
| 82 | * the oldest (first enqueued) to the newest (last enqueued). |
| 83 | */ |
| 84 | printf("dequeued content:"); |
| 85 | for (;;) { |
| 86 | struct cds_lfq_node_rcu *qnode; |
| 87 | struct mynode *node; |
| 88 | |
| 89 | /* |
| 90 | * Both enqueue and dequeue need to be called within RCU |
| 91 | * read-side critical section. |
| 92 | */ |
| 93 | urcu_memb_read_lock(); |
| 94 | qnode = cds_lfq_dequeue_rcu(&myqueue); |
| 95 | urcu_memb_read_unlock(); |
| 96 | if (!qnode) { |
| 97 | break; /* Queue is empty. */ |
| 98 | } |
| 99 | /* Getting the container structure from the node */ |
| 100 | node = caa_container_of(qnode, struct mynode, node); |
| 101 | printf(" %d", node->value); |
| 102 | urcu_memb_call_rcu(&node->rcu_head, free_node); |
| 103 | } |
| 104 | printf("\n"); |
| 105 | /* |
| 106 | * Release memory used by the queue. |
| 107 | */ |
| 108 | ret = cds_lfq_destroy_rcu(&myqueue); |
| 109 | if (ret) { |
| 110 | printf("Error destroying queue (non-empty)\n"); |
| 111 | } |
| 112 | end: |
| 113 | urcu_memb_unregister_thread(); |
| 114 | return ret; |
| 115 | } |