refcount.h revision 234010
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 180763 2008-07-23 16:44:20Z des $ 30150629Sjhb */ 31150629Sjhb 32150629Sjhb#ifndef __SYS_REFCOUNT_H__ 33150629Sjhb#define __SYS_REFCOUNT_H__ 34150629Sjhb 35180762Sdes#include <machine/atomic.h> 36180762Sdes 37180762Sdes#ifdef _KERNEL 38180762Sdes#include <sys/systm.h> 39180762Sdes#else 40180762Sdes#define KASSERT(exp, msg) /* */ 41180762Sdes#endif 42180762Sdes 43150629Sjhbstatic __inline void 44150629Sjhbrefcount_init(volatile u_int *count, u_int value) 45150629Sjhb{ 46150629Sjhb 47150629Sjhb *count = value; 48150629Sjhb} 49150629Sjhb 50150629Sjhbstatic __inline void 51150629Sjhbrefcount_acquire(volatile u_int *count) 52150629Sjhb{ 53150629Sjhb 54150629Sjhb atomic_add_acq_int(count, 1); 55150629Sjhb} 56150629Sjhb 57150629Sjhbstatic __inline int 58150629Sjhbrefcount_release(volatile u_int *count) 59150629Sjhb{ 60180762Sdes u_int old; 61150629Sjhb 62180762Sdes /* XXX: Should this have a rel membar? */ 63180762Sdes old = atomic_fetchadd_int(count, -1); 64180762Sdes KASSERT(old > 0, ("negative refcount %p", count)); 65180762Sdes return (old == 1); 66150629Sjhb} 67150629Sjhb 68150629Sjhb#endif /* ! __SYS_REFCOUNT_H__ */ 69