Commit | Line | Data |
---|---|---|
1c87adb3 MJ |
1 | // SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2 | // | |
3 | // SPDX-License-Identifier: MIT | |
4 | ||
a5fb5b81 | 5 | /* |
a5fb5b81 MD |
6 | * This example shows how to pop nodes from a lfstack. |
7 | */ | |
8 | ||
9 | #include <stdio.h> | |
10 | #include <stdlib.h> | |
11 | ||
12 | #include <urcu/lfstack.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_lfs_node node; /* Chaining in stack */ | |
21 | }; | |
22 | ||
70469b43 | 23 | int main(void) |
a5fb5b81 MD |
24 | { |
25 | int values[] = { -5, 42, 36, 24, }; | |
26 | struct cds_lfs_stack mystack; /* Stack */ | |
27 | unsigned int i; | |
28 | int ret = 0; | |
29 | ||
30 | cds_lfs_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_lfs_node_init(&node->node); | |
45 | node->value = values[i]; | |
46 | cds_lfs_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_lfs_node *snode; | |
55 | struct mynode *node; | |
56 | ||
57 | snode = cds_lfs_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: | |
6acc6ad4 | 67 | cds_lfs_destroy(&mystack); |
a5fb5b81 MD |
68 | return ret; |
69 | } |