Path: blob/main/sys/contrib/ck/include/spinlock/clh.h
48375 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_SPINLOCK_CLH_H27#define CK_SPINLOCK_CLH_H2829#include <ck_cc.h>30#include <ck_limits.h>31#include <ck_pr.h>32#include <ck_stdbool.h>33#include <ck_stddef.h>3435#ifndef CK_F_SPINLOCK_CLH36#define CK_F_SPINLOCK_CLH3738struct ck_spinlock_clh {39unsigned int wait;40struct ck_spinlock_clh *previous;41};42typedef struct ck_spinlock_clh ck_spinlock_clh_t;4344CK_CC_INLINE static void45ck_spinlock_clh_init(struct ck_spinlock_clh **lock, struct ck_spinlock_clh *unowned)46{4748unowned->previous = NULL;49unowned->wait = false;50*lock = unowned;51ck_pr_barrier();52return;53}5455CK_CC_INLINE static bool56ck_spinlock_clh_locked(struct ck_spinlock_clh **queue)57{58struct ck_spinlock_clh *head;59bool r;6061head = ck_pr_load_ptr(queue);62r = ck_pr_load_uint(&head->wait);63ck_pr_fence_acquire();64return r;65}6667CK_CC_INLINE static void68ck_spinlock_clh_lock(struct ck_spinlock_clh **queue, struct ck_spinlock_clh *thread)69{70struct ck_spinlock_clh *previous;7172/* Indicate to the next thread on queue that they will have to block. */73thread->wait = true;74ck_pr_fence_store_atomic();7576/*77* Mark current request as last request. Save reference to previous78* request.79*/80previous = ck_pr_fas_ptr(queue, thread);81thread->previous = previous;8283/* Wait until previous thread is done with lock. */84ck_pr_fence_load();85while (ck_pr_load_uint(&previous->wait) == true)86ck_pr_stall();8788ck_pr_fence_lock();89return;90}9192CK_CC_INLINE static void93ck_spinlock_clh_unlock(struct ck_spinlock_clh **thread)94{95struct ck_spinlock_clh *previous;9697/*98* If there are waiters, they are spinning on the current node wait99* flag. The flag is cleared so that the successor may complete an100* acquisition. If the caller is pre-empted then the predecessor field101* may be updated by a successor's lock operation. In order to avoid102* this, save a copy of the predecessor before setting the flag.103*/104previous = thread[0]->previous;105106/*107* We have to pay this cost anyways, use it as a compiler barrier too.108*/109ck_pr_fence_unlock();110ck_pr_store_uint(&(*thread)->wait, false);111112/*113* Predecessor is guaranteed not to be spinning on previous request,114* so update caller to use previous structure. This allows successor115* all the time in the world to successfully read updated wait flag.116*/117*thread = previous;118return;119}120#endif /* CK_F_SPINLOCK_CLH */121#endif /* CK_SPINLOCK_CLH_H */122123124