Path: blob/main/sys/contrib/ck/include/ck_sequence.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_SEQUENCE_H27#define CK_SEQUENCE_H2829#include <ck_cc.h>30#include <ck_pr.h>31#include <ck_stdbool.h>3233struct ck_sequence {34unsigned int sequence;35};36typedef struct ck_sequence ck_sequence_t;3738#define CK_SEQUENCE_INITIALIZER { .sequence = 0 }3940CK_CC_INLINE static void41ck_sequence_init(struct ck_sequence *sq)42{4344ck_pr_store_uint(&sq->sequence, 0);45return;46}4748CK_CC_INLINE static unsigned int49ck_sequence_read_begin(const struct ck_sequence *sq)50{51unsigned int version;5253for (;;) {54version = ck_pr_load_uint(&sq->sequence);5556/*57* If a sequence is even then associated data may be in a58* consistent state.59*/60if (CK_CC_LIKELY((version & 1) == 0))61break;6263/*64* If a sequence is odd then a thread is in the middle of an65* update. Retry the read to avoid operating on inconsistent66* data.67*/68ck_pr_stall();69}7071ck_pr_fence_load();72return version;73}7475CK_CC_INLINE static bool76ck_sequence_read_retry(const struct ck_sequence *sq, unsigned int version)77{7879/*80* If the sequence number was updated then a read should be81* re-attempted.82*/83ck_pr_fence_load();84return ck_pr_load_uint(&sq->sequence) != version;85}8687#define CK_SEQUENCE_READ(seqlock, version) \88for (*(version) = 1; \89(*(version) != 0) && (*(version) = ck_sequence_read_begin(seqlock), 1); \90*(version) = ck_sequence_read_retry(seqlock, *(version)))9192/*93* This must be called after a successful mutex acquisition.94*/95CK_CC_INLINE static void96ck_sequence_write_begin(struct ck_sequence *sq)97{9899/*100* Increment the sequence to an odd number to indicate101* the beginning of a write update.102*/103ck_pr_store_uint(&sq->sequence, sq->sequence + 1);104ck_pr_fence_store();105return;106}107108/*109* This must be called before mutex ownership is relinquished.110*/111CK_CC_INLINE static void112ck_sequence_write_end(struct ck_sequence *sq)113{114115/*116* Increment the sequence to an even number to indicate117* completion of a write update.118*/119ck_pr_fence_store();120ck_pr_store_uint(&sq->sequence, sq->sequence + 1);121return;122}123124#endif /* CK_SEQUENCE_H */125126127