Path: blob/main/sys/contrib/ck/include/ck_queue.h
103790 views
/*1* Copyright 2012-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/*-27* Copyright (c) 1991, 199328* The Regents of the University of California. All rights reserved.29*30* Redistribution and use in source and binary forms, with or without31* modification, are permitted provided that the following conditions32* are met:33* 1. Redistributions of source code must retain the above copyright34* notice, this list of conditions and the following disclaimer.35* 2. Redistributions in binary form must reproduce the above copyright36* notice, this list of conditions and the following disclaimer in the37* documentation and/or other materials provided with the distribution.38* 4. Neither the name of the University nor the names of its contributors39* may be used to endorse or promote products derived from this software40* without specific prior written permission.41*42* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND43* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE44* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE45* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE46* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL47* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS48* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)49* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT50* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY51* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF52* SUCH DAMAGE.53*54* @(#)queue.h 8.5 (Berkeley) 8/20/9455* $FreeBSD: release/9.0.0/sys/sys/queue.h 221843 2011-05-13 15:49:23Z mdf $56*/5758#ifndef CK_QUEUE_H59#define CK_QUEUE_H6061#include <ck_pr.h>6263/*64* This file defines three types of data structures: singly-linked lists,65* singly-linked tail queues and lists.66*67* A singly-linked list is headed by a single forward pointer. The elements68* are singly linked for minimum space and pointer manipulation overhead at69* the expense of O(n) removal for arbitrary elements. New elements can be70* added to the list after an existing element or at the head of the list.71* Elements being removed from the head of the list should use the explicit72* macro for this purpose for optimum efficiency. A singly-linked list may73* only be traversed in the forward direction. Singly-linked lists are ideal74* for applications with large datasets and few or no removals or for75* implementing a LIFO queue.76*77* A singly-linked tail queue is headed by a pair of pointers, one to the78* head of the list and the other to the tail of the list. The elements are79* singly linked for minimum space and pointer manipulation overhead at the80* expense of O(n) removal for arbitrary elements. New elements can be added81* to the list after an existing element, at the head of the list, or at the82* end of the list. Elements being removed from the head of the tail queue83* should use the explicit macro for this purpose for optimum efficiency.84* A singly-linked tail queue may only be traversed in the forward direction.85* Singly-linked tail queues are ideal for applications with large datasets86* and few or no removals or for implementing a FIFO queue.87*88* A list is headed by a single forward pointer (or an array of forward89* pointers for a hash table header). The elements are doubly linked90* so that an arbitrary element can be removed without a need to91* traverse the list. New elements can be added to the list before92* or after an existing element or at the head of the list. A list93* may only be traversed in the forward direction.94*95* It is safe to use _FOREACH/_FOREACH_SAFE in the presence of concurrent96* modifications to the list. Writers to these lists must, on the other hand,97* implement writer-side synchronization. The _SWAP operations are not atomic.98* This facility is currently unsupported on architectures such as the Alpha99* which require load-depend memory fences.100*101* CK_SLIST CK_LIST CK_STAILQ102* _HEAD + + +103* _HEAD_INITIALIZER + + +104* _ENTRY + + +105* _INIT + + +106* _EMPTY + + +107* _FIRST + + +108* _NEXT + + +109* _FOREACH + + +110* _FOREACH_SAFE + + +111* _INSERT_HEAD + + +112* _INSERT_BEFORE - + -113* _INSERT_AFTER + + +114* _INSERT_TAIL - - +115* _REMOVE_AFTER + - +116* _REMOVE_HEAD + - +117* _REMOVE + + +118* _SWAP + + +119* _MOVE + + +120*/121122/*123* Singly-linked List declarations.124*/125#define CK_SLIST_HEAD(name, type) \126struct name { \127struct type *cslh_first; /* first element */ \128}129130#define CK_SLIST_HEAD_INITIALIZER(head) \131{ NULL }132133#define CK_SLIST_ENTRY(type) \134struct { \135struct type *csle_next; /* next element */ \136}137138/*139* Singly-linked List functions.140*/141#define CK_SLIST_EMPTY(head) \142(ck_pr_load_ptr(&(head)->cslh_first) == NULL)143144#define CK_SLIST_FIRST(head) \145(ck_pr_load_ptr(&(head)->cslh_first))146147#define CK_SLIST_NEXT(elm, field) \148ck_pr_load_ptr(&((elm)->field.csle_next))149150#define CK_SLIST_FOREACH(var, head, field) \151for ((var) = CK_SLIST_FIRST((head)); \152(var); \153(var) = CK_SLIST_NEXT((var), field))154155#define CK_SLIST_FOREACH_FROM(var, head, field) \156for ((var) = ((var) != NULL ? (var) : CK_SLIST_FIRST((head))); \157(var); \158(var) = CK_SLIST_NEXT((var), field))159160#define CK_SLIST_FOREACH_SAFE(var, head, field, tvar) \161for ((var) = CK_SLIST_FIRST(head); \162(var) && ((tvar) = CK_SLIST_NEXT(var, field), 1); \163(var) = (tvar))164165#define CK_SLIST_FOREACH_PREVPTR(var, varp, head, field) \166for ((varp) = &(head)->cslh_first; \167((var) = ck_pr_load_ptr(varp)) != NULL; \168(varp) = &(var)->field.csle_next)169170#define CK_SLIST_INIT(head) do { \171ck_pr_store_ptr(&(head)->cslh_first, NULL); \172ck_pr_fence_store(); \173} while (0)174175#define CK_SLIST_INSERT_AFTER(a, b, field) do { \176(b)->field.csle_next = (a)->field.csle_next; \177ck_pr_fence_store(); \178ck_pr_store_ptr(&(a)->field.csle_next, b); \179} while (0)180181#define CK_SLIST_INSERT_HEAD(head, elm, field) do { \182(elm)->field.csle_next = (head)->cslh_first; \183ck_pr_fence_store(); \184ck_pr_store_ptr(&(head)->cslh_first, elm); \185} while (0)186187#define CK_SLIST_INSERT_PREVPTR(prevp, slistelm, elm, field) do { \188(elm)->field.csle_next = (slistelm); \189ck_pr_fence_store(); \190ck_pr_store_ptr(prevp, elm); \191} while (0)192193#define CK_SLIST_REMOVE_AFTER(elm, field) do { \194ck_pr_store_ptr(&(elm)->field.csle_next, \195(elm)->field.csle_next->field.csle_next); \196} while (0)197198#define CK_SLIST_REMOVE(head, elm, type, field) do { \199if ((head)->cslh_first == (elm)) { \200CK_SLIST_REMOVE_HEAD((head), field); \201} else { \202struct type *curelm = (head)->cslh_first; \203while (curelm->field.csle_next != (elm)) \204curelm = curelm->field.csle_next; \205CK_SLIST_REMOVE_AFTER(curelm, field); \206} \207} while (0)208209#define CK_SLIST_REMOVE_HEAD(head, field) do { \210ck_pr_store_ptr(&(head)->cslh_first, \211(head)->cslh_first->field.csle_next); \212} while (0)213214#define CK_SLIST_REMOVE_PREVPTR(prevp, elm, field) do { \215ck_pr_store_ptr(prevptr, (elm)->field.csle_next); \216} while (0)217218#define CK_SLIST_MOVE(head1, head2, field) do { \219ck_pr_store_ptr(&(head1)->cslh_first, (head2)->cslh_first); \220} while (0)221222/*223* This operation is not applied atomically.224*/225#define CK_SLIST_SWAP(a, b, type) do { \226struct type *swap_first = (a)->cslh_first; \227(a)->cslh_first = (b)->cslh_first; \228(b)->cslh_first = swap_first; \229} while (0)230231/*232* Singly-linked Tail queue declarations.233*/234#define CK_STAILQ_HEAD(name, type) \235struct name { \236struct type *cstqh_first;/* first element */ \237struct type **cstqh_last;/* addr of last next element */ \238}239240#define CK_STAILQ_HEAD_INITIALIZER(head) \241{ NULL, &(head).cstqh_first }242243#define CK_STAILQ_ENTRY(type) \244struct { \245struct type *cstqe_next; /* next element */ \246}247248/*249* Singly-linked Tail queue functions.250*/251#define CK_STAILQ_CONCAT(head1, head2) do { \252if ((head2)->cstqh_first != NULL) { \253ck_pr_store_ptr((head1)->cstqh_last, (head2)->cstqh_first); \254ck_pr_fence_store(); \255(head1)->cstqh_last = (head2)->cstqh_last; \256CK_STAILQ_INIT((head2)); \257} \258} while (0)259260#define CK_STAILQ_EMPTY(head) (ck_pr_load_ptr(&(head)->cstqh_first) == NULL)261262#define CK_STAILQ_FIRST(head) (ck_pr_load_ptr(&(head)->cstqh_first))263264#define CK_STAILQ_FOREACH(var, head, field) \265for((var) = CK_STAILQ_FIRST((head)); \266(var); \267(var) = CK_STAILQ_NEXT((var), field))268269#define CK_STAILQ_FOREACH_FROM(var, head, field) \270for ((var) = ((var) != NULL ? (var) : CK_STAILQ_FIRST((head))); \271(var); \272(var) = CK_STAILQ_NEXT((var), field))273274#define CK_STAILQ_FOREACH_SAFE(var, head, field, tvar) \275for ((var) = CK_STAILQ_FIRST((head)); \276(var) && ((tvar) = \277CK_STAILQ_NEXT((var), field), 1); \278(var) = (tvar))279280#define CK_STAILQ_INIT(head) do { \281ck_pr_store_ptr(&(head)->cstqh_first, NULL); \282ck_pr_fence_store(); \283(head)->cstqh_last = &(head)->cstqh_first; \284} while (0)285286#define CK_STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \287(elm)->field.cstqe_next = (tqelm)->field.cstqe_next; \288ck_pr_fence_store(); \289ck_pr_store_ptr(&(tqelm)->field.cstqe_next, elm); \290if ((elm)->field.cstqe_next == NULL) \291(head)->cstqh_last = &(elm)->field.cstqe_next; \292} while (0)293294#define CK_STAILQ_INSERT_HEAD(head, elm, field) do { \295(elm)->field.cstqe_next = (head)->cstqh_first; \296ck_pr_fence_store(); \297ck_pr_store_ptr(&(head)->cstqh_first, elm); \298if ((elm)->field.cstqe_next == NULL) \299(head)->cstqh_last = &(elm)->field.cstqe_next; \300} while (0)301302#define CK_STAILQ_INSERT_TAIL(head, elm, field) do { \303(elm)->field.cstqe_next = NULL; \304ck_pr_fence_store(); \305ck_pr_store_ptr((head)->cstqh_last, (elm)); \306(head)->cstqh_last = &(elm)->field.cstqe_next; \307} while (0)308309#define CK_STAILQ_NEXT(elm, field) \310(ck_pr_load_ptr(&(elm)->field.cstqe_next))311312#define CK_STAILQ_REMOVE(head, elm, type, field) do { \313if ((head)->cstqh_first == (elm)) { \314CK_STAILQ_REMOVE_HEAD((head), field); \315} else { \316struct type *curelm = (head)->cstqh_first; \317while (curelm->field.cstqe_next != (elm)) \318curelm = curelm->field.cstqe_next; \319CK_STAILQ_REMOVE_AFTER(head, curelm, field); \320} \321} while (0)322323#define CK_STAILQ_REMOVE_AFTER(head, elm, field) do { \324ck_pr_store_ptr(&(elm)->field.cstqe_next, \325(elm)->field.cstqe_next->field.cstqe_next); \326if ((elm)->field.cstqe_next == NULL) \327(head)->cstqh_last = &(elm)->field.cstqe_next; \328} while (0)329330#define CK_STAILQ_REMOVE_HEAD(head, field) do { \331ck_pr_store_ptr(&(head)->cstqh_first, \332(head)->cstqh_first->field.cstqe_next); \333if ((head)->cstqh_first == NULL) \334(head)->cstqh_last = &(head)->cstqh_first; \335} while (0)336337#define CK_STAILQ_MOVE(head1, head2, field) do { \338ck_pr_store_ptr(&(head1)->cstqh_first, (head2)->cstqh_first); \339(head1)->cstqh_last = (head2)->cstqh_last; \340if ((head2)->cstqh_last == &(head2)->cstqh_first) \341(head1)->cstqh_last = &(head1)->cstqh_first; \342} while (0)343344/*345* This operation is not applied atomically.346*/347#define CK_STAILQ_SWAP(head1, head2, type) do { \348struct type *swap_first = CK_STAILQ_FIRST(head1); \349struct type **swap_last = (head1)->cstqh_last; \350CK_STAILQ_FIRST(head1) = CK_STAILQ_FIRST(head2); \351(head1)->cstqh_last = (head2)->cstqh_last; \352CK_STAILQ_FIRST(head2) = swap_first; \353(head2)->cstqh_last = swap_last; \354if (CK_STAILQ_EMPTY(head1)) \355(head1)->cstqh_last = &(head1)->cstqh_first; \356if (CK_STAILQ_EMPTY(head2)) \357(head2)->cstqh_last = &(head2)->cstqh_first; \358} while (0)359360/*361* List declarations.362*/363#define CK_LIST_HEAD(name, type) \364struct name { \365struct type *clh_first; /* first element */ \366}367368#define CK_LIST_HEAD_INITIALIZER(head) \369{ NULL }370371#define CK_LIST_ENTRY(type) \372struct { \373struct type *cle_next; /* next element */ \374struct type **cle_prev; /* address of previous next element */ \375}376377#define CK_LIST_FIRST(head) ck_pr_load_ptr(&(head)->clh_first)378#define CK_LIST_EMPTY(head) (CK_LIST_FIRST(head) == NULL)379#define CK_LIST_NEXT(elm, field) ck_pr_load_ptr(&(elm)->field.cle_next)380381#define CK_LIST_FOREACH(var, head, field) \382for ((var) = CK_LIST_FIRST((head)); \383(var); \384(var) = CK_LIST_NEXT((var), field))385386#define CK_LIST_FOREACH_FROM(var, head, field) \387for ((var) = ((var) != NULL ? (var) : CK_LIST_FIRST((head))); \388(var); \389(var) = CK_LIST_NEXT((var), field))390391#define CK_LIST_FOREACH_SAFE(var, head, field, tvar) \392for ((var) = CK_LIST_FIRST((head)); \393(var) && ((tvar) = CK_LIST_NEXT((var), field), 1); \394(var) = (tvar))395396#define CK_LIST_INIT(head) do { \397ck_pr_store_ptr(&(head)->clh_first, NULL); \398ck_pr_fence_store(); \399} while (0)400401#define CK_LIST_INSERT_AFTER(listelm, elm, field) do { \402(elm)->field.cle_next = (listelm)->field.cle_next; \403(elm)->field.cle_prev = &(listelm)->field.cle_next; \404ck_pr_fence_store(); \405if ((listelm)->field.cle_next != NULL) \406(listelm)->field.cle_next->field.cle_prev = &(elm)->field.cle_next;\407ck_pr_store_ptr(&(listelm)->field.cle_next, elm); \408} while (0)409410#define CK_LIST_INSERT_BEFORE(listelm, elm, field) do { \411(elm)->field.cle_prev = (listelm)->field.cle_prev; \412(elm)->field.cle_next = (listelm); \413ck_pr_fence_store(); \414ck_pr_store_ptr((listelm)->field.cle_prev, (elm)); \415(listelm)->field.cle_prev = &(elm)->field.cle_next; \416} while (0)417418#define CK_LIST_INSERT_HEAD(head, elm, field) do { \419(elm)->field.cle_next = (head)->clh_first; \420ck_pr_fence_store(); \421if ((elm)->field.cle_next != NULL) \422(head)->clh_first->field.cle_prev = &(elm)->field.cle_next; \423ck_pr_store_ptr(&(head)->clh_first, elm); \424(elm)->field.cle_prev = &(head)->clh_first; \425} while (0)426427#define CK_LIST_REMOVE(elm, field) do { \428ck_pr_store_ptr((elm)->field.cle_prev, (elm)->field.cle_next); \429if ((elm)->field.cle_next != NULL) \430(elm)->field.cle_next->field.cle_prev = (elm)->field.cle_prev; \431} while (0)432433#define CK_LIST_MOVE(head1, head2, field) do { \434ck_pr_store_ptr(&(head1)->clh_first, (head2)->clh_first); \435if ((head1)->clh_first != NULL) \436(head1)->clh_first->field.cle_prev = &(head1)->clh_first; \437} while (0)438439/*440* This operation is not applied atomically.441*/442#define CK_LIST_SWAP(head1, head2, type, field) do { \443struct type *swap_tmp = (head1)->clh_first; \444(head1)->clh_first = (head2)->clh_first; \445(head2)->clh_first = swap_tmp; \446if ((swap_tmp = (head1)->clh_first) != NULL) \447swap_tmp->field.cle_prev = &(head1)->clh_first; \448if ((swap_tmp = (head2)->clh_first) != NULL) \449swap_tmp->field.cle_prev = &(head2)->clh_first; \450} while (0)451452#endif /* CK_QUEUE_H */453454455