1246922Spjd/*- 2246922Spjd * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org> 3246922Spjd * All rights reserved. 4246922Spjd * 5246922Spjd * Redistribution and use in source and binary forms, with or without 6246922Spjd * modification, are permitted provided that the following conditions 7246922Spjd * are met: 8246922Spjd * 1. Redistributions of source code must retain the above copyright 9246922Spjd * notice, this list of conditions and the following disclaimer. 10246922Spjd * 2. Redistributions in binary form must reproduce the above copyright 11246922Spjd * notice, this list of conditions and the following disclaimer in the 12246922Spjd * documentation and/or other materials provided with the distribution. 13246922Spjd * 3. Neither the name of the author nor the names of any co-contributors 14246922Spjd * may be used to endorse or promote products derived from this software 15246922Spjd * without specific prior written permission. 16246922Spjd * 17246922Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18246922Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19246922Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20246922Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21246922Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22246922Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23246922Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24246922Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25246922Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26246922Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27246922Spjd * SUCH DAMAGE. 28246922Spjd * 29246922Spjd * $FreeBSD$ 30246922Spjd */ 31246922Spjd 32246922Spjd#ifndef __REFCNT_H__ 33246922Spjd#define __REFCNT_H__ 34246922Spjd 35252386Sed#include <machine/atomic.h> 36246922Spjd 37246922Spjd#include "pjdlog.h" 38246922Spjd 39252386Sedtypedef unsigned int refcnt_t; 40249969Sed 41246922Spjdstatic __inline void 42249969Sedrefcnt_init(refcnt_t *count, unsigned int v) 43246922Spjd{ 44246922Spjd 45252386Sed *count = v; 46246922Spjd} 47246922Spjd 48249969Sedstatic __inline void 49249969Sedrefcnt_acquire(refcnt_t *count) 50249969Sed{ 51249969Sed 52252386Sed atomic_add_acq_int(count, 1); 53249969Sed} 54249969Sed 55246922Spjdstatic __inline unsigned int 56249969Sedrefcnt_release(refcnt_t *count) 57246922Spjd{ 58246922Spjd unsigned int old; 59246922Spjd 60246922Spjd /* XXX: Should this have a rel membar? */ 61252386Sed old = atomic_fetchadd_int(count, -1); 62246922Spjd PJDLOG_ASSERT(old > 0); 63246922Spjd return (old - 1); 64246922Spjd} 65246922Spjd 66246922Spjd#endif /* ! __REFCNT_H__ */ 67