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 * 14246922Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15246922Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16246922Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17246922Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18246922Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19246922Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20246922Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21246922Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22246922Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23246922Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24246922Spjd * SUCH DAMAGE. 25246922Spjd * 26246922Spjd * $FreeBSD$ 27246922Spjd */ 28246922Spjd 29246922Spjd#ifndef __REFCNT_H__ 30246922Spjd#define __REFCNT_H__ 31246922Spjd 32252386Sed#include <machine/atomic.h> 33246922Spjd 34246922Spjd#include "pjdlog.h" 35246922Spjd 36252386Sedtypedef unsigned int refcnt_t; 37249969Sed 38246922Spjdstatic __inline void 39249969Sedrefcnt_init(refcnt_t *count, unsigned int v) 40246922Spjd{ 41246922Spjd 42252386Sed *count = v; 43246922Spjd} 44246922Spjd 45249969Sedstatic __inline void 46249969Sedrefcnt_acquire(refcnt_t *count) 47249969Sed{ 48249969Sed 49252386Sed atomic_add_acq_int(count, 1); 50249969Sed} 51249969Sed 52246922Spjdstatic __inline unsigned int 53249969Sedrefcnt_release(refcnt_t *count) 54246922Spjd{ 55246922Spjd unsigned int old; 56246922Spjd 57246922Spjd /* XXX: Should this have a rel membar? */ 58252386Sed old = atomic_fetchadd_int(count, -1); 59246922Spjd PJDLOG_ASSERT(old > 0); 60246922Spjd return (old - 1); 61246922Spjd} 62246922Spjd 63246922Spjd#endif /* ! __REFCNT_H__ */ 64