1 // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: MIT
6 * This example shows how to dequeue nodes from a RCU lock-free queue.
7 * This queue requires using a RCU scheme.
13 #include <urcu/urcu-memb.h> /* RCU flavor */
14 #include <urcu/rculfqueue.h> /* RCU Lock-free queue */
15 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
18 * Nodes populated into the queue.
21 int value
; /* Node content */
22 struct cds_lfq_node_rcu node
; /* Chaining in queue */
23 struct rcu_head rcu_head
; /* For call_rcu() */
27 void free_node(struct rcu_head
*head
)
30 caa_container_of(head
, struct mynode
, rcu_head
);
37 int values
[] = { -5, 42, 36, 24, };
38 struct cds_lfq_queue_rcu myqueue
; /* Queue */
43 * Each thread need using RCU read-side need to be explicitly
46 urcu_memb_register_thread();
48 cds_lfq_init_rcu(&myqueue
, urcu_memb_call_rcu
);
53 for (i
= 0; i
< CAA_ARRAY_SIZE(values
); i
++) {
56 node
= malloc(sizeof(*node
));
62 cds_lfq_node_init_rcu(&node
->node
);
63 node
->value
= values
[i
];
65 * Both enqueue and dequeue need to be called within RCU
66 * read-side critical section.
68 urcu_memb_read_lock();
69 cds_lfq_enqueue_rcu(&myqueue
, &node
->node
);
70 urcu_memb_read_unlock();
74 * Dequeue each node from the queue. Those will be dequeued from
75 * the oldest (first enqueued) to the newest (last enqueued).
77 printf("dequeued content:");
79 struct cds_lfq_node_rcu
*qnode
;
83 * Both enqueue and dequeue need to be called within RCU
84 * read-side critical section.
86 urcu_memb_read_lock();
87 qnode
= cds_lfq_dequeue_rcu(&myqueue
);
88 urcu_memb_read_unlock();
90 break; /* Queue is empty. */
92 /* Getting the container structure from the node */
93 node
= caa_container_of(qnode
, struct mynode
, node
);
94 printf(" %d", node
->value
);
95 urcu_memb_call_rcu(&node
->rcu_head
, free_node
);
99 * Release memory used by the queue.
101 ret
= cds_lfq_destroy_rcu(&myqueue
);
103 printf("Error destroying queue (non-empty)\n");
106 urcu_memb_unregister_thread();