refcount.h revision 238828
1150629Sjhb/*- 2150629Sjhb * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org> 3150629Sjhb * All rights reserved. 4150629Sjhb * 5150629Sjhb * Redistribution and use in source and binary forms, with or without 6150629Sjhb * modification, are permitted provided that the following conditions 7150629Sjhb * are met: 8150629Sjhb * 1. Redistributions of source code must retain the above copyright 9150629Sjhb * notice, this list of conditions and the following disclaimer. 10150629Sjhb * 2. Redistributions in binary form must reproduce the above copyright 11150629Sjhb * notice, this list of conditions and the following disclaimer in the 12150629Sjhb * documentation and/or other materials provided with the distribution. 13150629Sjhb * 3. Neither the name of the author nor the names of any co-contributors 14150629Sjhb * may be used to endorse or promote products derived from this software 15150629Sjhb * without specific prior written permission. 16150629Sjhb * 17150629Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18150629Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19150629Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20150629Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21150629Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22150629Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23150629Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24150629Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25150629Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26150629Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27150629Sjhb * SUCH DAMAGE. 28150629Sjhb * 29150629Sjhb * $FreeBSD: head/sys/sys/refcount.h 238828 2012-07-27 09:16:48Z glebius $ 30150629Sjhb */ 31150629Sjhb 32150629Sjhb#ifndef __SYS_REFCOUNT_H__ 33150629Sjhb#define __SYS_REFCOUNT_H__ 34150629Sjhb 35238828Sglebius#include <sys/limits.h> 36180762Sdes#include <machine/atomic.h> 37180762Sdes 38180762Sdes#ifdef _KERNEL 39180762Sdes#include <sys/systm.h> 40180762Sdes#else 41180762Sdes#define KASSERT(exp, msg) /* */ 42180762Sdes#endif 43180762Sdes 44150629Sjhbstatic __inline void 45150629Sjhbrefcount_init(volatile u_int *count, u_int value) 46150629Sjhb{ 47150629Sjhb 48150629Sjhb *count = value; 49150629Sjhb} 50150629Sjhb 51150629Sjhbstatic __inline void 52150629Sjhbrefcount_acquire(volatile u_int *count) 53150629Sjhb{ 54150629Sjhb 55238828Sglebius KASSERT(*count < UINT_MAX, ("refcount %p overflowed", count)); 56150629Sjhb atomic_add_acq_int(count, 1); 57150629Sjhb} 58150629Sjhb 59150629Sjhbstatic __inline int 60150629Sjhbrefcount_release(volatile u_int *count) 61150629Sjhb{ 62180762Sdes u_int old; 63150629Sjhb 64180762Sdes /* XXX: Should this have a rel membar? */ 65180762Sdes old = atomic_fetchadd_int(count, -1); 66180762Sdes KASSERT(old > 0, ("negative refcount %p", count)); 67180762Sdes return (old == 1); 68150629Sjhb} 69150629Sjhb 70150629Sjhb#endif /* ! __SYS_REFCOUNT_H__ */ 71