2 * Copyright 2009-2015 Samy Al Bahra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <ck_stdbool.h>
33 #include <ck_stddef.h>
35 struct ck_stack_entry {
36 struct ck_stack_entry *next;
38 typedef struct ck_stack_entry ck_stack_entry_t;
41 struct ck_stack_entry *head;
42 char *generation CK_CC_PACKED;
44 typedef struct ck_stack ck_stack_t;
46 #define CK_STACK_INITIALIZER { NULL, NULL }
48 #ifndef CK_F_STACK_PUSH_UPMC
49 #define CK_F_STACK_PUSH_UPMC
51 * Stack producer operation safe for multiple unique producers and multiple consumers.
53 CK_CC_INLINE static void
54 ck_stack_push_upmc(struct ck_stack *target, struct ck_stack_entry *entry)
56 struct ck_stack_entry *stack;
58 stack = ck_pr_load_ptr(&target->head);
62 while (ck_pr_cas_ptr_value(&target->head, stack, entry, &stack) == false) {
69 #endif /* CK_F_STACK_PUSH_UPMC */
71 #ifndef CK_F_STACK_TRYPUSH_UPMC
72 #define CK_F_STACK_TRYPUSH_UPMC
74 * Stack producer operation for multiple unique producers and multiple consumers.
75 * Returns true on success and false on failure.
77 CK_CC_INLINE static bool
78 ck_stack_trypush_upmc(struct ck_stack *target, struct ck_stack_entry *entry)
80 struct ck_stack_entry *stack;
82 stack = ck_pr_load_ptr(&target->head);
86 return ck_pr_cas_ptr(&target->head, stack, entry);
88 #endif /* CK_F_STACK_TRYPUSH_UPMC */
90 #ifndef CK_F_STACK_POP_UPMC
91 #define CK_F_STACK_POP_UPMC
93 * Stack consumer operation safe for multiple unique producers and multiple consumers.
95 CK_CC_INLINE static struct ck_stack_entry *
96 ck_stack_pop_upmc(struct ck_stack *target)
98 struct ck_stack_entry *entry, *next;
100 entry = ck_pr_load_ptr(&target->head);
106 while (ck_pr_cas_ptr_value(&target->head, entry, next, &entry) == false) {
118 #ifndef CK_F_STACK_TRYPOP_UPMC
119 #define CK_F_STACK_TRYPOP_UPMC
121 * Stack production operation for multiple unique producers and multiple consumers.
122 * Returns true on success and false on failure. The value pointed to by the second
123 * argument is set to a valid ck_stack_entry_t reference if true is returned. If
124 * false is returned, then the value pointed to by the second argument is undefined.
126 CK_CC_INLINE static bool
127 ck_stack_trypop_upmc(struct ck_stack *target, struct ck_stack_entry **r)
129 struct ck_stack_entry *entry;
131 entry = ck_pr_load_ptr(&target->head);
136 if (ck_pr_cas_ptr(&target->head, entry, entry->next) == true) {
143 #endif /* CK_F_STACK_TRYPOP_UPMC */
145 #ifndef CK_F_STACK_BATCH_POP_UPMC
146 #define CK_F_STACK_BATCH_POP_UPMC
148 * Pop all items off the stack.
150 CK_CC_INLINE static struct ck_stack_entry *
151 ck_stack_batch_pop_upmc(struct ck_stack *target)
153 struct ck_stack_entry *entry;
155 entry = ck_pr_fas_ptr(&target->head, NULL);
159 #endif /* CK_F_STACK_BATCH_POP_UPMC */
161 #ifndef CK_F_STACK_PUSH_MPMC
162 #define CK_F_STACK_PUSH_MPMC
164 * Stack producer operation safe for multiple producers and multiple consumers.
166 CK_CC_INLINE static void
167 ck_stack_push_mpmc(struct ck_stack *target, struct ck_stack_entry *entry)
170 ck_stack_push_upmc(target, entry);
173 #endif /* CK_F_STACK_PUSH_MPMC */
175 #ifndef CK_F_STACK_TRYPUSH_MPMC
176 #define CK_F_STACK_TRYPUSH_MPMC
178 * Stack producer operation safe for multiple producers and multiple consumers.
180 CK_CC_INLINE static bool
181 ck_stack_trypush_mpmc(struct ck_stack *target, struct ck_stack_entry *entry)
184 return ck_stack_trypush_upmc(target, entry);
186 #endif /* CK_F_STACK_TRYPUSH_MPMC */
188 #ifdef CK_F_PR_CAS_PTR_2_VALUE
189 #ifndef CK_F_STACK_POP_MPMC
190 #define CK_F_STACK_POP_MPMC
192 * Stack consumer operation safe for multiple producers and multiple consumers.
194 CK_CC_INLINE static struct ck_stack_entry *
195 ck_stack_pop_mpmc(struct ck_stack *target)
197 struct ck_stack original, update;
199 original.generation = ck_pr_load_ptr(&target->generation);
201 original.head = ck_pr_load_ptr(&target->head);
202 if (original.head == NULL)
205 /* Order with respect to next pointer. */
208 update.generation = original.generation + 1;
209 update.head = original.head->next;
211 while (ck_pr_cas_ptr_2_value(target, &original, &update, &original) == false) {
212 if (original.head == NULL)
215 update.generation = original.generation + 1;
217 /* Order with respect to next pointer. */
219 update.head = original.head->next;
222 return original.head;
224 #endif /* CK_F_STACK_POP_MPMC */
226 #ifndef CK_F_STACK_TRYPOP_MPMC
227 #define CK_F_STACK_TRYPOP_MPMC
228 CK_CC_INLINE static bool
229 ck_stack_trypop_mpmc(struct ck_stack *target, struct ck_stack_entry **r)
231 struct ck_stack original, update;
233 original.generation = ck_pr_load_ptr(&target->generation);
235 original.head = ck_pr_load_ptr(&target->head);
236 if (original.head == NULL)
239 update.generation = original.generation + 1;
241 update.head = original.head->next;
243 if (ck_pr_cas_ptr_2_value(target, &original, &update, &original) == true) {
250 #endif /* CK_F_STACK_TRYPOP_MPMC */
251 #endif /* CK_F_PR_CAS_PTR_2_VALUE */
253 #ifndef CK_F_STACK_BATCH_POP_MPMC
254 #define CK_F_STACK_BATCH_POP_MPMC
256 * This is equivalent to the UP/MC version as NULL does not need a
257 * a generation count.
259 CK_CC_INLINE static struct ck_stack_entry *
260 ck_stack_batch_pop_mpmc(struct ck_stack *target)
263 return ck_stack_batch_pop_upmc(target);
265 #endif /* CK_F_STACK_BATCH_POP_MPMC */
267 #ifndef CK_F_STACK_PUSH_MPNC
268 #define CK_F_STACK_PUSH_MPNC
270 * Stack producer operation safe with no concurrent consumers.
272 CK_CC_INLINE static void
273 ck_stack_push_mpnc(struct ck_stack *target, struct ck_stack_entry *entry)
275 struct ck_stack_entry *stack;
278 ck_pr_fence_store_atomic();
279 stack = ck_pr_fas_ptr(&target->head, entry);
280 ck_pr_store_ptr(&entry->next, stack);
285 #endif /* CK_F_STACK_PUSH_MPNC */
288 * Stack producer operation for single producer and no concurrent consumers.
290 CK_CC_INLINE static void
291 ck_stack_push_spnc(struct ck_stack *target, struct ck_stack_entry *entry)
294 entry->next = target->head;
295 target->head = entry;
300 * Stack consumer operation for no concurrent producers and single consumer.
302 CK_CC_INLINE static struct ck_stack_entry *
303 ck_stack_pop_npsc(struct ck_stack *target)
305 struct ck_stack_entry *n;
307 if (target->head == NULL)
311 target->head = n->next;
317 * Pop all items off a stack.
319 CK_CC_INLINE static struct ck_stack_entry *
320 ck_stack_batch_pop_npsc(struct ck_stack *target)
322 struct ck_stack_entry *n;
331 * Stack initialization function. Guarantees initialization across processors.
333 CK_CC_INLINE static void
334 ck_stack_init(struct ck_stack *stack)
338 stack->generation = NULL;
342 /* Defines a container_of functions for */
343 #define CK_STACK_CONTAINER(T, M, N) CK_CC_CONTAINER(ck_stack_entry_t, T, M, N)
345 #define CK_STACK_ISEMPTY(m) ((m)->head == NULL)
346 #define CK_STACK_FIRST(s) ((s)->head)
347 #define CK_STACK_NEXT(m) ((m)->next)
348 #define CK_STACK_FOREACH(stack, entry) \
349 for ((entry) = CK_STACK_FIRST(stack); \
351 (entry) = CK_STACK_NEXT(entry))
352 #define CK_STACK_FOREACH_SAFE(stack, entry, T) \
353 for ((entry) = CK_STACK_FIRST(stack); \
354 (entry) != NULL && ((T) = (entry)->next, 1); \
357 #endif /* CK_STACK_H */