uatomic/x86: Remove redundant memory barriers
[urcu.git] / doc / examples / wfstack / cds_wfs_pop_all_blocking.c
CommitLineData
1c87adb3
MJ
1// SPDX-FileCopyrightText: 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2//
3// SPDX-License-Identifier: MIT
4
53a77191 5/*
53a77191
MD
6 * This example shows how to pop all 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 */
18struct mynode {
19 int value; /* Node content */
20 struct cds_wfs_node node; /* Chaining in stack */
21};
22
70469b43 23int main(void)
53a77191
MD
24{
25 int values[] = { -5, 42, 36, 24, };
26 struct cds_wfs_stack mystack; /* Stack */
27 unsigned int i;
28 int ret = 0;
bb641aa3 29 struct cds_wfs_node *snode, *sn;
53a77191
MD
30 struct cds_wfs_head *shead;
31
32 cds_wfs_init(&mystack);
33
34 /*
35 * Push nodes.
36 */
37 for (i = 0; i < CAA_ARRAY_SIZE(values); i++) {
38 struct mynode *node;
39
40 node = malloc(sizeof(*node));
41 if (!node) {
42 ret = -1;
43 goto end;
44 }
45
46 cds_wfs_node_init(&node->node);
47 node->value = values[i];
48 cds_wfs_push(&mystack, &node->node);
49 }
50
51 /*
52 * Pop all nodes from mystack into shead. The head can the be
53 * used for iteration.
54 */
55 shead = cds_wfs_pop_all_blocking(&mystack);
56
57 /*
58 * Show the stack content, iterate in reverse order of push,
bb641aa3
MD
59 * from newest to oldest. Use cds_wfs_for_each_blocking_safe()
60 * so we can free the nodes as we iterate.
53a77191
MD
61 */
62 printf("mystack content:");
bb641aa3 63 cds_wfs_for_each_blocking_safe(shead, snode, sn) {
53a77191
MD
64 struct mynode *node =
65 caa_container_of(snode, struct mynode, node);
66 printf(" %d", node->value);
bb641aa3 67 free(node);
53a77191
MD
68 }
69 printf("\n");
70end:
6acc6ad4 71 cds_wfs_destroy(&mystack);
53a77191
MD
72 return ret;
73}
This page took 0.04015 seconds and 5 git commands to generate.