1 #ifndef _URCU_WFSTACK_H
2 #define _URCU_WFSTACK_H
7 * Userspace RCU library - Stack with Wait-Free push, Blocking pop.
9 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <urcu/compiler.h>
34 #if (!defined(_GNU_SOURCE) && !defined(_LGPL_SOURCE))
35 #error "Dynamic loader LGPL wrappers not implemented yet"
38 #define WF_STACK_END ((void *)0x1UL)
39 #define WFS_ADAPT_ATTEMPTS 10 /* Retry if being set */
40 #define WFS_WAIT 10 /* Wait 10 ms if being set */
43 struct wfs_node
*next
;
47 struct wfs_node
*head
;
51 void wfs_node_init(struct wfs_node
*node
)
56 void wfs_init(struct wfs_stack
*s
)
60 s
->head
= WF_STACK_END
;
61 ret
= pthread_mutex_init(&s
->lock
, NULL
);
65 void wfs_push(struct wfs_stack
*s
, struct wfs_node
*node
)
67 struct wfs_node
*old_head
;
69 assert(node
->next
== NULL
);
71 * uatomic_xchg() implicit memory barrier orders earlier stores to node
72 * (setting it to NULL) before publication.
74 old_head
= uatomic_xchg(&s
->head
, node
);
76 * At this point, dequeuers see a NULL node->next, they should busy-wait
77 * until node->next is set to old_head.
79 STORE_SHARED(node
->next
, old_head
);
83 * Returns NULL if stack is empty.
86 __wfs_pop_blocking(struct wfs_stack
*s
)
88 struct wfs_node
*head
, *next
;
92 head
= LOAD_SHARED(s
->head
);
93 if (head
== WF_STACK_END
)
96 * Adaptative busy-looping waiting for push to complete.
98 while ((next
= LOAD_SHARED(head
->next
)) == NULL
) {
99 if (++attempt
>= WFS_ADAPT_ATTEMPTS
) {
100 poll(NULL
, 0, WFS_WAIT
); /* Wait for 10ms */
105 if (uatomic_cmpxchg(&s
->head
, head
, next
) == head
)
108 goto retry
; /* Concurrent modification. Retry. */
112 wfs_pop_blocking(struct wfs_stack
*s
)
114 struct wfs_node
*retnode
;
117 ret
= pthread_mutex_lock(&s
->lock
);
119 retnode
= __wfs_pop_blocking(s
);
120 ret
= pthread_mutex_unlock(&s
->lock
);
129 #endif /* _URCU_WFSTACK_H */
This page took 0.047911 seconds and 4 git commands to generate.