refcnt.h revision 246922
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: head/sbin/hastd/refcnt.h 246922 2013-02-17 21:12:34Z pjd $ 30246922Spjd */ 31246922Spjd 32246922Spjd#ifndef __REFCNT_H__ 33246922Spjd#define __REFCNT_H__ 34246922Spjd 35246922Spjd#include <machine/atomic.h> 36246922Spjd 37246922Spjd#include "pjdlog.h" 38246922Spjd 39246922Spjdstatic __inline void 40246922Spjdrefcnt_acquire(volatile unsigned int *count) 41246922Spjd{ 42246922Spjd 43246922Spjd atomic_add_acq_int(count, 1); 44246922Spjd} 45246922Spjd 46246922Spjdstatic __inline unsigned int 47246922Spjdrefcnt_release(volatile unsigned int *count) 48246922Spjd{ 49246922Spjd unsigned int old; 50246922Spjd 51246922Spjd /* XXX: Should this have a rel membar? */ 52246922Spjd old = atomic_fetchadd_int(count, -1); 53246922Spjd PJDLOG_ASSERT(old > 0); 54246922Spjd return (old - 1); 55246922Spjd} 56246922Spjd 57246922Spjd#endif /* ! __REFCNT_H__ */ 58