Commit | Line | Data |
---|---|---|
294d3396 MD |
1 | #ifndef _URCU_WFSTACK_STATIC_H |
2 | #define _URCU_WFSTACK_STATIC_H | |
3 | ||
4 | /* | |
5 | * wfstack-static.h | |
6 | * | |
7 | * Userspace RCU library - Stack with Wait-Free push, Blocking pop. | |
8 | * | |
9 | * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See wfstack.h for linking | |
10 | * dynamically with the userspace rcu library. | |
11 | * | |
12 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
13 | * | |
14 | * This library is free software; you can redistribute it and/or | |
15 | * modify it under the terms of the GNU Lesser General Public | |
16 | * License as published by the Free Software Foundation; either | |
17 | * version 2.1 of the License, or (at your option) any later version. | |
18 | * | |
19 | * This library is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
22 | * Lesser General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU Lesser General Public | |
25 | * License along with this library; if not, write to the Free Software | |
26 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
27 | */ | |
28 | ||
29 | #include <pthread.h> | |
30 | #include <assert.h> | |
b57aee66 | 31 | #include <poll.h> |
294d3396 | 32 | #include <urcu/compiler.h> |
a2e7bf9c | 33 | #include <urcu/uatomic.h> |
294d3396 MD |
34 | |
35 | #ifdef __cplusplus | |
36 | extern "C" { | |
37 | #endif | |
38 | ||
16aa9ee8 DG |
39 | #define CDS_WF_STACK_END ((void *)0x1UL) |
40 | #define CDS_WFS_ADAPT_ATTEMPTS 10 /* Retry if being set */ | |
41 | #define CDS_WFS_WAIT 10 /* Wait 10 ms if being set */ | |
294d3396 | 42 | |
756a0322 | 43 | static inline |
16aa9ee8 | 44 | void _cds_wfs_node_init(struct cds_wfs_node *node) |
294d3396 MD |
45 | { |
46 | node->next = NULL; | |
47 | } | |
48 | ||
756a0322 | 49 | static inline |
16aa9ee8 | 50 | void _cds_wfs_init(struct cds_wfs_stack *s) |
294d3396 MD |
51 | { |
52 | int ret; | |
53 | ||
16aa9ee8 | 54 | s->head = CDS_WF_STACK_END; |
294d3396 MD |
55 | ret = pthread_mutex_init(&s->lock, NULL); |
56 | assert(!ret); | |
57 | } | |
58 | ||
191098fc MD |
59 | /* |
60 | * Returns 0 if stack was empty, 1 otherwise. | |
61 | */ | |
756a0322 | 62 | static inline |
191098fc | 63 | int _cds_wfs_push(struct cds_wfs_stack *s, struct cds_wfs_node *node) |
294d3396 | 64 | { |
16aa9ee8 | 65 | struct cds_wfs_node *old_head; |
294d3396 MD |
66 | |
67 | assert(node->next == NULL); | |
68 | /* | |
69 | * uatomic_xchg() implicit memory barrier orders earlier stores to node | |
70 | * (setting it to NULL) before publication. | |
71 | */ | |
72 | old_head = uatomic_xchg(&s->head, node); | |
73 | /* | |
74 | * At this point, dequeuers see a NULL node->next, they should busy-wait | |
75 | * until node->next is set to old_head. | |
76 | */ | |
6cf3827c | 77 | CMM_STORE_SHARED(node->next, old_head); |
191098fc | 78 | return (old_head != CDS_WF_STACK_END); |
294d3396 MD |
79 | } |
80 | ||
81 | /* | |
82 | * Returns NULL if stack is empty. | |
83 | */ | |
756a0322 | 84 | static inline |
16aa9ee8 DG |
85 | struct cds_wfs_node * |
86 | ___cds_wfs_pop_blocking(struct cds_wfs_stack *s) | |
294d3396 | 87 | { |
16aa9ee8 | 88 | struct cds_wfs_node *head, *next; |
294d3396 MD |
89 | int attempt = 0; |
90 | ||
91 | retry: | |
6cf3827c | 92 | head = CMM_LOAD_SHARED(s->head); |
16aa9ee8 | 93 | if (head == CDS_WF_STACK_END) |
294d3396 MD |
94 | return NULL; |
95 | /* | |
96 | * Adaptative busy-looping waiting for push to complete. | |
97 | */ | |
6cf3827c | 98 | while ((next = CMM_LOAD_SHARED(head->next)) == NULL) { |
16aa9ee8 DG |
99 | if (++attempt >= CDS_WFS_ADAPT_ATTEMPTS) { |
100 | poll(NULL, 0, CDS_WFS_WAIT); /* Wait for 10ms */ | |
294d3396 MD |
101 | attempt = 0; |
102 | } else | |
06f22bdb | 103 | caa_cpu_relax(); |
294d3396 MD |
104 | } |
105 | if (uatomic_cmpxchg(&s->head, head, next) == head) | |
106 | return head; | |
107 | else | |
108 | goto retry; /* Concurrent modification. Retry. */ | |
109 | } | |
110 | ||
756a0322 | 111 | static inline |
16aa9ee8 DG |
112 | struct cds_wfs_node * |
113 | _cds_wfs_pop_blocking(struct cds_wfs_stack *s) | |
294d3396 | 114 | { |
16aa9ee8 | 115 | struct cds_wfs_node *retnode; |
294d3396 MD |
116 | int ret; |
117 | ||
118 | ret = pthread_mutex_lock(&s->lock); | |
119 | assert(!ret); | |
16aa9ee8 | 120 | retnode = ___cds_wfs_pop_blocking(s); |
294d3396 MD |
121 | ret = pthread_mutex_unlock(&s->lock); |
122 | assert(!ret); | |
123 | return retnode; | |
124 | } | |
125 | ||
126 | #ifdef __cplusplus | |
127 | } | |
128 | #endif | |
129 | ||
130 | #endif /* _URCU_WFSTACK_STATIC_H */ |