xref: /freebsd/sys/contrib/ck/include/spinlock/fas.h (revision 74e9b5f29ad0056bbe11a30c91dfa0705fa19cd5)
11fb62fb0SOlivier Houchard /*
21fb62fb0SOlivier Houchard  * Copyright 2010-2015 Samy Al Bahra.
31fb62fb0SOlivier Houchard  * All rights reserved.
41fb62fb0SOlivier Houchard  *
51fb62fb0SOlivier Houchard  * Redistribution and use in source and binary forms, with or without
61fb62fb0SOlivier Houchard  * modification, are permitted provided that the following conditions
71fb62fb0SOlivier Houchard  * are met:
81fb62fb0SOlivier Houchard  * 1. Redistributions of source code must retain the above copyright
91fb62fb0SOlivier Houchard  *    notice, this list of conditions and the following disclaimer.
101fb62fb0SOlivier Houchard  * 2. Redistributions in binary form must reproduce the above copyright
111fb62fb0SOlivier Houchard  *    notice, this list of conditions and the following disclaimer in the
121fb62fb0SOlivier Houchard  *    documentation and/or other materials provided with the distribution.
131fb62fb0SOlivier Houchard  *
141fb62fb0SOlivier Houchard  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151fb62fb0SOlivier Houchard  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161fb62fb0SOlivier Houchard  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171fb62fb0SOlivier Houchard  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181fb62fb0SOlivier Houchard  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191fb62fb0SOlivier Houchard  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201fb62fb0SOlivier Houchard  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211fb62fb0SOlivier Houchard  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221fb62fb0SOlivier Houchard  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231fb62fb0SOlivier Houchard  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241fb62fb0SOlivier Houchard  * SUCH DAMAGE.
251fb62fb0SOlivier Houchard  */
261fb62fb0SOlivier Houchard 
271fb62fb0SOlivier Houchard #ifndef CK_SPINLOCK_FAS_H
281fb62fb0SOlivier Houchard #define CK_SPINLOCK_FAS_H
291fb62fb0SOlivier Houchard 
301fb62fb0SOlivier Houchard #include <ck_backoff.h>
311fb62fb0SOlivier Houchard #include <ck_cc.h>
321fb62fb0SOlivier Houchard #include <ck_elide.h>
331fb62fb0SOlivier Houchard #include <ck_pr.h>
341fb62fb0SOlivier Houchard #include <ck_stdbool.h>
351fb62fb0SOlivier Houchard 
361fb62fb0SOlivier Houchard #ifndef CK_F_SPINLOCK_FAS
371fb62fb0SOlivier Houchard #define CK_F_SPINLOCK_FAS
381fb62fb0SOlivier Houchard 
391fb62fb0SOlivier Houchard struct ck_spinlock_fas {
401fb62fb0SOlivier Houchard 	unsigned int value;
411fb62fb0SOlivier Houchard };
421fb62fb0SOlivier Houchard typedef struct ck_spinlock_fas ck_spinlock_fas_t;
431fb62fb0SOlivier Houchard 
441fb62fb0SOlivier Houchard #define CK_SPINLOCK_FAS_INITIALIZER {false}
451fb62fb0SOlivier Houchard 
461fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_spinlock_fas_init(struct ck_spinlock_fas * lock)471fb62fb0SOlivier Houchard ck_spinlock_fas_init(struct ck_spinlock_fas *lock)
481fb62fb0SOlivier Houchard {
491fb62fb0SOlivier Houchard 
501fb62fb0SOlivier Houchard 	lock->value = false;
511fb62fb0SOlivier Houchard 	ck_pr_barrier();
521fb62fb0SOlivier Houchard 	return;
531fb62fb0SOlivier Houchard }
541fb62fb0SOlivier Houchard 
551fb62fb0SOlivier Houchard CK_CC_INLINE static bool
ck_spinlock_fas_trylock(struct ck_spinlock_fas * lock)561fb62fb0SOlivier Houchard ck_spinlock_fas_trylock(struct ck_spinlock_fas *lock)
571fb62fb0SOlivier Houchard {
581fb62fb0SOlivier Houchard 	bool value;
591fb62fb0SOlivier Houchard 
601fb62fb0SOlivier Houchard 	value = ck_pr_fas_uint(&lock->value, true);
611fb62fb0SOlivier Houchard 	ck_pr_fence_lock();
621fb62fb0SOlivier Houchard 
631fb62fb0SOlivier Houchard 	return !value;
641fb62fb0SOlivier Houchard }
651fb62fb0SOlivier Houchard 
661fb62fb0SOlivier Houchard CK_CC_INLINE static bool
ck_spinlock_fas_locked(struct ck_spinlock_fas * lock)671fb62fb0SOlivier Houchard ck_spinlock_fas_locked(struct ck_spinlock_fas *lock)
681fb62fb0SOlivier Houchard {
691fb62fb0SOlivier Houchard 	bool r;
701fb62fb0SOlivier Houchard 
711fb62fb0SOlivier Houchard 	r = ck_pr_load_uint(&lock->value);
721fb62fb0SOlivier Houchard 	ck_pr_fence_acquire();
731fb62fb0SOlivier Houchard 	return r;
741fb62fb0SOlivier Houchard }
751fb62fb0SOlivier Houchard 
761fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_spinlock_fas_lock(struct ck_spinlock_fas * lock)771fb62fb0SOlivier Houchard ck_spinlock_fas_lock(struct ck_spinlock_fas *lock)
781fb62fb0SOlivier Houchard {
791fb62fb0SOlivier Houchard 
80*74e9b5f2SOlivier Houchard         while (CK_CC_UNLIKELY(ck_pr_fas_uint(&lock->value, true) == true)) {
81*74e9b5f2SOlivier Houchard                 do {
821fb62fb0SOlivier Houchard                         ck_pr_stall();
83*74e9b5f2SOlivier Houchard                 } while (ck_pr_load_uint(&lock->value) == true);
841fb62fb0SOlivier Houchard         }
851fb62fb0SOlivier Houchard 
861fb62fb0SOlivier Houchard 	ck_pr_fence_lock();
871fb62fb0SOlivier Houchard 	return;
881fb62fb0SOlivier Houchard }
891fb62fb0SOlivier Houchard 
901fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_spinlock_fas_lock_eb(struct ck_spinlock_fas * lock)911fb62fb0SOlivier Houchard ck_spinlock_fas_lock_eb(struct ck_spinlock_fas *lock)
921fb62fb0SOlivier Houchard {
931fb62fb0SOlivier Houchard 	ck_backoff_t backoff = CK_BACKOFF_INITIALIZER;
941fb62fb0SOlivier Houchard 
951fb62fb0SOlivier Houchard 	while (ck_pr_fas_uint(&lock->value, true) == true)
961fb62fb0SOlivier Houchard 		ck_backoff_eb(&backoff);
971fb62fb0SOlivier Houchard 
981fb62fb0SOlivier Houchard 	ck_pr_fence_lock();
991fb62fb0SOlivier Houchard 	return;
1001fb62fb0SOlivier Houchard }
1011fb62fb0SOlivier Houchard 
1021fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_spinlock_fas_unlock(struct ck_spinlock_fas * lock)1031fb62fb0SOlivier Houchard ck_spinlock_fas_unlock(struct ck_spinlock_fas *lock)
1041fb62fb0SOlivier Houchard {
1051fb62fb0SOlivier Houchard 
1061fb62fb0SOlivier Houchard 	ck_pr_fence_unlock();
1071fb62fb0SOlivier Houchard 	ck_pr_store_uint(&lock->value, false);
1081fb62fb0SOlivier Houchard 	return;
1091fb62fb0SOlivier Houchard }
1101fb62fb0SOlivier Houchard 
1111fb62fb0SOlivier Houchard CK_ELIDE_PROTOTYPE(ck_spinlock_fas, ck_spinlock_fas_t,
1121fb62fb0SOlivier Houchard     ck_spinlock_fas_locked, ck_spinlock_fas_lock,
1131fb62fb0SOlivier Houchard     ck_spinlock_fas_locked, ck_spinlock_fas_unlock)
1141fb62fb0SOlivier Houchard 
1151fb62fb0SOlivier Houchard CK_ELIDE_TRYLOCK_PROTOTYPE(ck_spinlock_fas, ck_spinlock_fas_t,
1161fb62fb0SOlivier Houchard     ck_spinlock_fas_locked, ck_spinlock_fas_trylock)
1171fb62fb0SOlivier Houchard 
1181fb62fb0SOlivier Houchard #endif /* CK_F_SPINLOCK_FAS */
1191fb62fb0SOlivier Houchard #endif /* CK_SPINLOCK_FAS_H */
120