Path: blob/main/sys/contrib/ck/include/ck_tflock.h
48254 views
/*1* Copyright 2014 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_TFLOCK_TICKET_H27#define CK_TFLOCK_TICKET_H2829/*30* This is an implementation of task-fair locks derived from the work31* described in:32* John M. Mellor-Crummey and Michael L. Scott. 1991.33* Scalable reader-writer synchronization for shared-memory34* multiprocessors. SIGPLAN Not. 26, 7 (April 1991), 106-113.35*/3637#include <ck_cc.h>38#include <ck_pr.h>3940struct ck_tflock_ticket {41uint32_t request;42uint32_t completion;43};44typedef struct ck_tflock_ticket ck_tflock_ticket_t;4546#define CK_TFLOCK_TICKET_INITIALIZER { 0, 0 }4748#define CK_TFLOCK_TICKET_RC_INCR 0x10000U /* Read-side increment. */49#define CK_TFLOCK_TICKET_WC_INCR 0x1U /* Write-side increment. */50#define CK_TFLOCK_TICKET_W_MASK 0xffffU /* Write-side mask. */51#define CK_TFLOCK_TICKET_WC_TOPMSK 0x8000U /* Write clear mask for overflow. */52#define CK_TFLOCK_TICKET_RC_TOPMSK 0x80000000U /* Read clear mask for overflow. */5354CK_CC_INLINE static uint32_t55ck_tflock_ticket_fca_32(uint32_t *target, uint32_t mask, uint32_t delta)56{57uint32_t snapshot = ck_pr_load_32(target);58uint32_t goal;5960for (;;) {61goal = (snapshot & ~mask) + delta;62if (ck_pr_cas_32_value(target, snapshot, goal, &snapshot) == true)63break;6465ck_pr_stall();66}6768return snapshot;69}7071CK_CC_INLINE static void72ck_tflock_ticket_init(struct ck_tflock_ticket *pf)73{7475pf->request = pf->completion = 0;76ck_pr_barrier();77return;78}7980CK_CC_INLINE static void81ck_tflock_ticket_write_lock(struct ck_tflock_ticket *lock)82{83uint32_t previous;8485previous = ck_tflock_ticket_fca_32(&lock->request, CK_TFLOCK_TICKET_WC_TOPMSK,86CK_TFLOCK_TICKET_WC_INCR);87ck_pr_fence_atomic_load();88while (ck_pr_load_32(&lock->completion) != previous)89ck_pr_stall();9091ck_pr_fence_lock();92return;93}9495CK_CC_INLINE static void96ck_tflock_ticket_write_unlock(struct ck_tflock_ticket *lock)97{9899ck_pr_fence_unlock();100ck_tflock_ticket_fca_32(&lock->completion, CK_TFLOCK_TICKET_WC_TOPMSK,101CK_TFLOCK_TICKET_WC_INCR);102return;103}104105CK_CC_INLINE static void106ck_tflock_ticket_read_lock(struct ck_tflock_ticket *lock)107{108uint32_t previous;109110previous = ck_tflock_ticket_fca_32(&lock->request,111CK_TFLOCK_TICKET_RC_TOPMSK, CK_TFLOCK_TICKET_RC_INCR) &112CK_TFLOCK_TICKET_W_MASK;113114ck_pr_fence_atomic_load();115116while ((ck_pr_load_32(&lock->completion) &117CK_TFLOCK_TICKET_W_MASK) != previous) {118ck_pr_stall();119}120121ck_pr_fence_lock();122return;123}124125CK_CC_INLINE static void126ck_tflock_ticket_read_unlock(struct ck_tflock_ticket *lock)127{128129ck_pr_fence_unlock();130ck_tflock_ticket_fca_32(&lock->completion, CK_TFLOCK_TICKET_RC_TOPMSK,131CK_TFLOCK_TICKET_RC_INCR);132return;133}134135#endif /* CK_TFLOCK_TICKET_H */136137138