xref: /src/sys/compat/linuxkpi/common/include/linux/refcount.h (revision d17bfb237065587c72db05a5bafc6bcab1206cb9)
14efd5dd7SEmmanuel Vadot /*-
24efd5dd7SEmmanuel Vadot  * Copyright (c) 2020 The FreeBSD Foundation
34efd5dd7SEmmanuel Vadot  *
44efd5dd7SEmmanuel Vadot  * This software was developed by Emmanuel Vadot under sponsorship
54efd5dd7SEmmanuel Vadot  * from the FreeBSD Foundation.
64efd5dd7SEmmanuel Vadot  *
74efd5dd7SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
84efd5dd7SEmmanuel Vadot  * modification, are permitted provided that the following conditions
94efd5dd7SEmmanuel Vadot  * are met:
104efd5dd7SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
114efd5dd7SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
124efd5dd7SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
134efd5dd7SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
144efd5dd7SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
154efd5dd7SEmmanuel Vadot  *
164efd5dd7SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174efd5dd7SEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184efd5dd7SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194efd5dd7SEmmanuel Vadot  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
204efd5dd7SEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214efd5dd7SEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224efd5dd7SEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234efd5dd7SEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244efd5dd7SEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254efd5dd7SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264efd5dd7SEmmanuel Vadot  * SUCH DAMAGE.
274efd5dd7SEmmanuel Vadot  */
284efd5dd7SEmmanuel Vadot 
29307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_REFCOUNT_H
30307f78f3SVladimir Kondratyev #define _LINUXKPI_LINUX_REFCOUNT_H
314efd5dd7SEmmanuel Vadot 
324efd5dd7SEmmanuel Vadot #include <linux/atomic.h>
33d17bfb23SJean-Sébastien Pédron #include <linux/spinlock.h>
344efd5dd7SEmmanuel Vadot 
35abb1a134SEmmanuel Vadot typedef atomic_t refcount_t;
364efd5dd7SEmmanuel Vadot 
374efd5dd7SEmmanuel Vadot static inline void
refcount_set(refcount_t * ref,unsigned int i)384efd5dd7SEmmanuel Vadot refcount_set(refcount_t *ref, unsigned int i)
394efd5dd7SEmmanuel Vadot {
40abb1a134SEmmanuel Vadot 	atomic_set(ref, i);
414efd5dd7SEmmanuel Vadot }
424efd5dd7SEmmanuel Vadot 
434efd5dd7SEmmanuel Vadot static inline void
refcount_inc(refcount_t * ref)444efd5dd7SEmmanuel Vadot refcount_inc(refcount_t *ref)
454efd5dd7SEmmanuel Vadot {
46abb1a134SEmmanuel Vadot 	atomic_inc(ref);
474efd5dd7SEmmanuel Vadot }
484efd5dd7SEmmanuel Vadot 
494efd5dd7SEmmanuel Vadot static inline bool
refcount_inc_not_zero(refcount_t * ref)504efd5dd7SEmmanuel Vadot refcount_inc_not_zero(refcount_t *ref)
514efd5dd7SEmmanuel Vadot {
52abb1a134SEmmanuel Vadot 	return (atomic_inc_not_zero(ref));
534efd5dd7SEmmanuel Vadot }
544efd5dd7SEmmanuel Vadot 
554efd5dd7SEmmanuel Vadot static inline void
refcount_dec(refcount_t * ref)564efd5dd7SEmmanuel Vadot refcount_dec(refcount_t *ref)
574efd5dd7SEmmanuel Vadot {
58abb1a134SEmmanuel Vadot 	atomic_dec(ref);
594efd5dd7SEmmanuel Vadot }
604efd5dd7SEmmanuel Vadot 
614efd5dd7SEmmanuel Vadot static inline unsigned int
refcount_read(refcount_t * ref)624efd5dd7SEmmanuel Vadot refcount_read(refcount_t *ref)
634efd5dd7SEmmanuel Vadot {
64abb1a134SEmmanuel Vadot 	return atomic_read(ref);
654efd5dd7SEmmanuel Vadot }
664efd5dd7SEmmanuel Vadot 
674efd5dd7SEmmanuel Vadot static inline bool
refcount_dec_and_lock_irqsave(refcount_t * ref,spinlock_t * lock,unsigned long * flags)684efd5dd7SEmmanuel Vadot refcount_dec_and_lock_irqsave(refcount_t *ref, spinlock_t *lock,
694efd5dd7SEmmanuel Vadot     unsigned long *flags)
704efd5dd7SEmmanuel Vadot {
71abb1a134SEmmanuel Vadot 	if (atomic_dec_and_test(ref) == true) {
724efd5dd7SEmmanuel Vadot 		spin_lock_irqsave(lock, flags);
734efd5dd7SEmmanuel Vadot 		return (true);
744efd5dd7SEmmanuel Vadot 	}
754efd5dd7SEmmanuel Vadot 	return (false);
764efd5dd7SEmmanuel Vadot }
774efd5dd7SEmmanuel Vadot 
781a6874e3SEmmanuel Vadot static inline bool
refcount_dec_and_test(refcount_t * r)79abb1a134SEmmanuel Vadot refcount_dec_and_test(refcount_t *r)
801a6874e3SEmmanuel Vadot {
811a6874e3SEmmanuel Vadot 
821a6874e3SEmmanuel Vadot 	return (atomic_dec_and_test(r));
831a6874e3SEmmanuel Vadot }
841a6874e3SEmmanuel Vadot 
85307f78f3SVladimir Kondratyev #endif /* __LINUXKPI_LINUX_REFCOUNT_H__ */
86