xref: /freebsd/sys/compat/linuxkpi/common/include/linux/refcount.h (revision abb1a1340e3f0dcdbb81b5c42ad304f02c812e20)
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>
334efd5dd7SEmmanuel Vadot 
34*abb1a134SEmmanuel Vadot typedef atomic_t refcount_t;
354efd5dd7SEmmanuel Vadot 
364efd5dd7SEmmanuel Vadot static inline void
374efd5dd7SEmmanuel Vadot refcount_set(refcount_t *ref, unsigned int i)
384efd5dd7SEmmanuel Vadot {
39*abb1a134SEmmanuel Vadot 	atomic_set(ref, i);
404efd5dd7SEmmanuel Vadot }
414efd5dd7SEmmanuel Vadot 
424efd5dd7SEmmanuel Vadot static inline void
434efd5dd7SEmmanuel Vadot refcount_inc(refcount_t *ref)
444efd5dd7SEmmanuel Vadot {
45*abb1a134SEmmanuel Vadot 	atomic_inc(ref);
464efd5dd7SEmmanuel Vadot }
474efd5dd7SEmmanuel Vadot 
484efd5dd7SEmmanuel Vadot static inline bool
494efd5dd7SEmmanuel Vadot refcount_inc_not_zero(refcount_t *ref)
504efd5dd7SEmmanuel Vadot {
51*abb1a134SEmmanuel Vadot 	return (atomic_inc_not_zero(ref));
524efd5dd7SEmmanuel Vadot }
534efd5dd7SEmmanuel Vadot 
544efd5dd7SEmmanuel Vadot static inline void
554efd5dd7SEmmanuel Vadot refcount_dec(refcount_t *ref)
564efd5dd7SEmmanuel Vadot {
57*abb1a134SEmmanuel Vadot 	atomic_dec(ref);
584efd5dd7SEmmanuel Vadot }
594efd5dd7SEmmanuel Vadot 
604efd5dd7SEmmanuel Vadot static inline unsigned int
614efd5dd7SEmmanuel Vadot refcount_read(refcount_t *ref)
624efd5dd7SEmmanuel Vadot {
63*abb1a134SEmmanuel Vadot 	return atomic_read(ref);
644efd5dd7SEmmanuel Vadot }
654efd5dd7SEmmanuel Vadot 
664efd5dd7SEmmanuel Vadot static inline bool
674efd5dd7SEmmanuel Vadot refcount_dec_and_lock_irqsave(refcount_t *ref, spinlock_t *lock,
684efd5dd7SEmmanuel Vadot     unsigned long *flags)
694efd5dd7SEmmanuel Vadot {
70*abb1a134SEmmanuel Vadot 	if (atomic_dec_and_test(ref) == true) {
714efd5dd7SEmmanuel Vadot 		spin_lock_irqsave(lock, flags);
724efd5dd7SEmmanuel Vadot 		return (true);
734efd5dd7SEmmanuel Vadot 	}
744efd5dd7SEmmanuel Vadot 	return (false);
754efd5dd7SEmmanuel Vadot }
764efd5dd7SEmmanuel Vadot 
771a6874e3SEmmanuel Vadot static inline bool
78*abb1a134SEmmanuel Vadot refcount_dec_and_test(refcount_t *r)
791a6874e3SEmmanuel Vadot {
801a6874e3SEmmanuel Vadot 
811a6874e3SEmmanuel Vadot 	return (atomic_dec_and_test(r));
821a6874e3SEmmanuel Vadot }
831a6874e3SEmmanuel Vadot 
84307f78f3SVladimir Kondratyev #endif /* __LINUXKPI_LINUX_REFCOUNT_H__ */
85