Path: blob/main/sys/compat/linuxkpi/common/include/asm/atomic-long.h
39604 views
/*-1* Copyright (c) 2010 Isilon Systems, Inc.2* Copyright (c) 2010 iX Systems, Inc.3* Copyright (c) 2010 Panasas, Inc.4* Copyright (c) 2013-2017 Mellanox Technologies, Ltd.5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice unmodified, this list of conditions, and the following12* disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR18* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES19* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.20* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,21* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT22* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF26* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/28#ifndef _LINUXKPI_ASM_ATOMIC_LONG_H_29#define _LINUXKPI_ASM_ATOMIC_LONG_H_3031#include <linux/compiler.h>32#include <sys/types.h>33#include <machine/atomic.h>34#define ATOMIC_LONG_INIT(x) { .counter = (x) }3536typedef struct {37volatile long counter;38} atomic_long_t;3940#define atomic_long_add(i, v) atomic_long_add_return((i), (v))41#define atomic_long_sub(i, v) atomic_long_sub_return((i), (v))42#define atomic_long_inc_return(v) atomic_long_add_return(1, (v))43#define atomic_long_inc_not_zero(v) atomic_long_add_unless((v), 1, 0)4445static inline long46atomic_long_add_return(long i, atomic_long_t *v)47{48return i + atomic_fetchadd_long(&v->counter, i);49}5051static inline long52atomic_long_sub_return(long i, atomic_long_t *v)53{54return atomic_fetchadd_long(&v->counter, -i) - i;55}5657static inline void58atomic_long_set(atomic_long_t *v, long i)59{60WRITE_ONCE(v->counter, i);61}6263static inline long64atomic_long_read(atomic_long_t *v)65{66return READ_ONCE(v->counter);67}6869static inline long70atomic_long_inc(atomic_long_t *v)71{72return atomic_fetchadd_long(&v->counter, 1) + 1;73}7475static inline long76atomic_long_dec(atomic_long_t *v)77{78return atomic_fetchadd_long(&v->counter, -1) - 1;79}8081static inline long82atomic_long_xchg(atomic_long_t *v, long val)83{84return atomic_swap_long(&v->counter, val);85}8687static inline long88atomic_long_cmpxchg(atomic_long_t *v, long old, long new)89{90long ret = old;9192for (;;) {93if (atomic_fcmpset_long(&v->counter, &ret, new))94break;95if (ret != old)96break;97}98return (ret);99}100101static inline int102atomic_long_add_unless(atomic_long_t *v, long a, long u)103{104long c = atomic_long_read(v);105106for (;;) {107if (unlikely(c == u))108break;109if (likely(atomic_fcmpset_long(&v->counter, &c, c + a)))110break;111}112return (c != u);113}114115static inline long116atomic_long_fetch_add_unless(atomic_long_t *v, long a, long u)117{118long c = atomic_long_read(v);119120for (;;) {121if (unlikely(c == u))122break;123if (likely(atomic_fcmpset_long(&v->counter, &c, c + a)))124break;125}126return (c);127}128129static inline long130atomic_long_dec_and_test(atomic_long_t *v)131{132long i = atomic_long_add(-1, v);133return i == 0 ;134}135136#endif /* _LINUXKPI_ASM_ATOMIC_LONG_H_ */137138139