Path: blob/main/sys/contrib/ck/include/ck_hp_stack.h
48254 views
/*1* Copyright 2010-2015 Samy Al Bahra.2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#ifndef CK_HP_STACK_H27#define CK_HP_STACK_H2829#include <ck_cc.h>30#include <ck_hp.h>31#include <ck_pr.h>32#include <ck_stack.h>33#include <ck_stddef.h>3435#define CK_HP_STACK_SLOTS_COUNT 136#define CK_HP_STACK_SLOTS_SIZE sizeof(void *)3738CK_CC_INLINE static void39ck_hp_stack_push_mpmc(struct ck_stack *target, struct ck_stack_entry *entry)40{4142ck_stack_push_upmc(target, entry);43return;44}4546CK_CC_INLINE static bool47ck_hp_stack_trypush_mpmc(struct ck_stack *target, struct ck_stack_entry *entry)48{4950return ck_stack_trypush_upmc(target, entry);51}5253CK_CC_INLINE static struct ck_stack_entry *54ck_hp_stack_pop_mpmc(ck_hp_record_t *record, struct ck_stack *target)55{56struct ck_stack_entry *entry, *update;5758do {59entry = ck_pr_load_ptr(&target->head);60if (entry == NULL)61return NULL;6263ck_hp_set_fence(record, 0, entry);64} while (entry != ck_pr_load_ptr(&target->head));6566while (ck_pr_cas_ptr_value(&target->head, entry, entry->next, &entry) == false) {67if (entry == NULL)68return NULL;6970ck_hp_set_fence(record, 0, entry);7172update = ck_pr_load_ptr(&target->head);73while (entry != update) {74ck_hp_set_fence(record, 0, update);75entry = update;76update = ck_pr_load_ptr(&target->head);77if (update == NULL)78return NULL;79}80}8182return entry;83}8485CK_CC_INLINE static bool86ck_hp_stack_trypop_mpmc(ck_hp_record_t *record, struct ck_stack *target, struct ck_stack_entry **r)87{88struct ck_stack_entry *entry;8990entry = ck_pr_load_ptr(&target->head);91if (entry == NULL)92return false;9394ck_hp_set_fence(record, 0, entry);95if (entry != ck_pr_load_ptr(&target->head))96goto leave;9798if (ck_pr_cas_ptr_value(&target->head, entry, entry->next, &entry) == false)99goto leave;100101*r = entry;102return true;103104leave:105ck_hp_set(record, 0, NULL);106return false;107}108109#endif /* CK_HP_STACK_H */110111112