Path: blob/main/sys/contrib/ck/include/ck_brlock.h
48255 views
/*1* Copyright 2011-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_BRLOCK_H27#define CK_BRLOCK_H2829/*30* Big reader spinlocks provide cache-local contention-free read31* lock acquisition in the absence of writers. This comes at the32* cost of O(n) write lock acquisition. They were first implemented33* in the Linux kernel by Ingo Molnar and David S. Miller around the34* year 2000.35*36* This implementation is thread-agnostic which comes at the cost37* of larger reader objects due to necessary linkage overhead. In38* order to cut down on TLB pressure, it is recommended to allocate39* these objects on the same page.40*/4142#include <ck_pr.h>43#include <ck_stdbool.h>44#include <ck_stddef.h>4546struct ck_brlock_reader {47unsigned int n_readers;48struct ck_brlock_reader *previous;49struct ck_brlock_reader *next;50};51typedef struct ck_brlock_reader ck_brlock_reader_t;5253#define CK_BRLOCK_READER_INITIALIZER {0}5455struct ck_brlock {56struct ck_brlock_reader *readers;57unsigned int writer;58};59typedef struct ck_brlock ck_brlock_t;6061#define CK_BRLOCK_INITIALIZER {NULL, false}6263CK_CC_INLINE static void64ck_brlock_init(struct ck_brlock *br)65{6667br->readers = NULL;68br->writer = false;69ck_pr_barrier();70return;71}7273CK_CC_INLINE static void74ck_brlock_write_lock(struct ck_brlock *br)75{76struct ck_brlock_reader *cursor;7778/*79* As the frequency of write acquisitions should be low,80* there is no point to more advanced contention avoidance.81*/82while (ck_pr_fas_uint(&br->writer, true) == true)83ck_pr_stall();8485ck_pr_fence_atomic_load();8687/* The reader list is protected under the writer br. */88for (cursor = br->readers; cursor != NULL; cursor = cursor->next) {89while (ck_pr_load_uint(&cursor->n_readers) != 0)90ck_pr_stall();91}9293ck_pr_fence_lock();94return;95}9697CK_CC_INLINE static void98ck_brlock_write_unlock(struct ck_brlock *br)99{100101ck_pr_fence_unlock();102ck_pr_store_uint(&br->writer, false);103return;104}105106CK_CC_INLINE static bool107ck_brlock_write_trylock(struct ck_brlock *br, unsigned int factor)108{109struct ck_brlock_reader *cursor;110unsigned int steps = 0;111112while (ck_pr_fas_uint(&br->writer, true) == true) {113if (++steps >= factor)114return false;115116ck_pr_stall();117}118119/*120* We do not require a strict fence here as atomic RMW operations121* are serializing.122*/123ck_pr_fence_atomic_load();124125for (cursor = br->readers; cursor != NULL; cursor = cursor->next) {126while (ck_pr_load_uint(&cursor->n_readers) != 0) {127if (++steps >= factor) {128ck_brlock_write_unlock(br);129return false;130}131132ck_pr_stall();133}134}135136ck_pr_fence_lock();137return true;138}139140CK_CC_INLINE static void141ck_brlock_read_register(struct ck_brlock *br, struct ck_brlock_reader *reader)142{143144reader->n_readers = 0;145reader->previous = NULL;146147/* Implicit compiler barrier. */148ck_brlock_write_lock(br);149150reader->next = ck_pr_load_ptr(&br->readers);151if (reader->next != NULL)152reader->next->previous = reader;153ck_pr_store_ptr(&br->readers, reader);154155ck_brlock_write_unlock(br);156return;157}158159CK_CC_INLINE static void160ck_brlock_read_unregister(struct ck_brlock *br, struct ck_brlock_reader *reader)161{162163ck_brlock_write_lock(br);164165if (reader->next != NULL)166reader->next->previous = reader->previous;167168if (reader->previous != NULL)169reader->previous->next = reader->next;170else171br->readers = reader->next;172173ck_brlock_write_unlock(br);174return;175}176177CK_CC_INLINE static void178ck_brlock_read_lock(struct ck_brlock *br, struct ck_brlock_reader *reader)179{180181if (reader->n_readers >= 1) {182ck_pr_store_uint(&reader->n_readers, reader->n_readers + 1);183return;184}185186for (;;) {187while (ck_pr_load_uint(&br->writer) == true)188ck_pr_stall();189190#if defined(__x86__) || defined(__x86_64__)191ck_pr_fas_uint(&reader->n_readers, 1);192193/*194* Serialize reader counter update with respect to load of195* writer.196*/197ck_pr_fence_atomic_load();198#else199ck_pr_store_uint(&reader->n_readers, 1);200201/*202* Serialize reader counter update with respect to load of203* writer.204*/205ck_pr_fence_store_load();206#endif207208if (ck_pr_load_uint(&br->writer) == false)209break;210211ck_pr_store_uint(&reader->n_readers, 0);212}213214ck_pr_fence_lock();215return;216}217218CK_CC_INLINE static bool219ck_brlock_read_trylock(struct ck_brlock *br,220struct ck_brlock_reader *reader,221unsigned int factor)222{223unsigned int steps = 0;224225if (reader->n_readers >= 1) {226ck_pr_store_uint(&reader->n_readers, reader->n_readers + 1);227return true;228}229230for (;;) {231while (ck_pr_load_uint(&br->writer) == true) {232if (++steps >= factor)233return false;234235ck_pr_stall();236}237238#if defined(__x86__) || defined(__x86_64__)239ck_pr_fas_uint(&reader->n_readers, 1);240241/*242* Serialize reader counter update with respect to load of243* writer.244*/245ck_pr_fence_atomic_load();246#else247ck_pr_store_uint(&reader->n_readers, 1);248249/*250* Serialize reader counter update with respect to load of251* writer.252*/253ck_pr_fence_store_load();254#endif255256if (ck_pr_load_uint(&br->writer) == false)257break;258259ck_pr_store_uint(&reader->n_readers, 0);260261if (++steps >= factor)262return false;263}264265ck_pr_fence_lock();266return true;267}268269CK_CC_INLINE static void270ck_brlock_read_unlock(struct ck_brlock_reader *reader)271{272273ck_pr_fence_unlock();274ck_pr_store_uint(&reader->n_readers, reader->n_readers - 1);275return;276}277278#endif /* CK_BRLOCK_H */279280281