uatomic: Fix header guard comment
[userspace-rcu.git] / doc / examples / wfstack / cds_wfs_pop.c
1 // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 //
3 // SPDX-License-Identifier: MIT
4
5 /*
6 * This example shows how to pop nodes from a wfstack.
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include <urcu/wfstack.h> /* Wait-free stack */
13 #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */
14
15 /*
16 * Nodes populated into the stack.
17 */
18 struct mynode {
19 int value; /* Node content */
20 struct cds_wfs_node node; /* Chaining in stack */
21 };
22
23 int main(void)
24 {
25 int values[] = { -5, 42, 36, 24, };
26 struct cds_wfs_stack mystack; /* Stack */
27 unsigned int i;
28 int ret = 0;
29
30 cds_wfs_init(&mystack);
31
32 /*
33 * Push nodes.
34 */
35 for (i = 0; i < CAA_ARRAY_SIZE(values); i++) {
36 struct mynode *node;
37
38 node = malloc(sizeof(*node));
39 if (!node) {
40 ret = -1;
41 goto end;
42 }
43
44 cds_wfs_node_init(&node->node);
45 node->value = values[i];
46 cds_wfs_push(&mystack, &node->node);
47 }
48
49 /*
50 * Pop nodes from the stack, one by one, from newest to oldest.
51 */
52 printf("pop each mystack node:");
53 for (;;) {
54 struct cds_wfs_node *snode;
55 struct mynode *node;
56
57 snode = cds_wfs_pop_blocking(&mystack);
58 if (!snode) {
59 break;
60 }
61 node = caa_container_of(snode, struct mynode, node);
62 printf(" %d", node->value);
63 free(node);
64 }
65 printf("\n");
66 end:
67 cds_wfs_destroy(&mystack);
68 return ret;
69 }
This page took 0.044319 seconds and 5 git commands to generate.