Commit | Line | Data |
---|---|---|
95bba308 MD |
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 pop nodes from a wfstack. | |
14 | */ | |
15 | ||
16 | #include <stdio.h> | |
17 | #include <stdlib.h> | |
18 | ||
19 | #include <urcu/wfstack.h> /* Wait-free stack */ | |
20 | #include <urcu/compiler.h> /* For CAA_ARRAY_SIZE */ | |
21 | ||
22 | /* | |
23 | * Nodes populated into the stack. | |
24 | */ | |
25 | struct mynode { | |
26 | int value; /* Node content */ | |
27 | struct cds_wfs_node node; /* Chaining in stack */ | |
28 | }; | |
29 | ||
30 | int main(int argc, char **argv) | |
31 | { | |
32 | int values[] = { -5, 42, 36, 24, }; | |
33 | struct cds_wfs_stack mystack; /* Stack */ | |
34 | unsigned int i; | |
35 | int ret = 0; | |
36 | ||
37 | cds_wfs_init(&mystack); | |
38 | ||
39 | /* | |
40 | * Push nodes. | |
41 | */ | |
42 | for (i = 0; i < CAA_ARRAY_SIZE(values); i++) { | |
43 | struct mynode *node; | |
44 | ||
45 | node = malloc(sizeof(*node)); | |
46 | if (!node) { | |
47 | ret = -1; | |
48 | goto end; | |
49 | } | |
50 | ||
51 | cds_wfs_node_init(&node->node); | |
52 | node->value = values[i]; | |
53 | cds_wfs_push(&mystack, &node->node); | |
54 | } | |
55 | ||
56 | /* | |
57 | * Pop nodes from the stack, one by one, from newest to oldest. | |
58 | */ | |
59 | printf("pop each mystack node:"); | |
60 | for (;;) { | |
61 | struct cds_wfs_node *snode; | |
62 | struct mynode *node; | |
63 | ||
64 | snode = cds_wfs_pop_blocking(&mystack); | |
65 | if (!snode) { | |
66 | break; | |
67 | } | |
68 | node = caa_container_of(snode, struct mynode, node); | |
69 | printf(" %d", node->value); | |
70 | free(node); | |
71 | } | |
72 | printf("\n"); | |
73 | end: | |
74 | return ret; | |
75 | } |