refcnt.h revision 246922
1180740Sdes/*-
2180740Sdes * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
3180740Sdes * All rights reserved.
4180740Sdes *
5180740Sdes * Redistribution and use in source and binary forms, with or without
6180740Sdes * modification, are permitted provided that the following conditions
7180740Sdes * are met:
8180740Sdes * 1. Redistributions of source code must retain the above copyright
9180740Sdes *    notice, this list of conditions and the following disclaimer.
10180746Sdes * 2. Redistributions in binary form must reproduce the above copyright
11180746Sdes *    notice, this list of conditions and the following disclaimer in the
12180746Sdes *    documentation and/or other materials provided with the distribution.
13180740Sdes * 3. Neither the name of the author nor the names of any co-contributors
14180740Sdes *    may be used to endorse or promote products derived from this software
15180740Sdes *    without specific prior written permission.
16180740Sdes *
17180740Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18180740Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19180740Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20180740Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21180740Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22180740Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23180740Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24180740Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25180746Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26180746Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27180746Sdes * SUCH DAMAGE.
28180740Sdes *
29180740Sdes * $FreeBSD: head/sbin/hastd/refcnt.h 246922 2013-02-17 21:12:34Z pjd $
30180740Sdes */
31180740Sdes
32180740Sdes#ifndef __REFCNT_H__
33180740Sdes#define __REFCNT_H__
34180740Sdes
35180740Sdes#include <machine/atomic.h>
36180740Sdes
37180740Sdes#include "pjdlog.h"
38180740Sdes
39180740Sdesstatic __inline void
40180740Sdesrefcnt_acquire(volatile unsigned int *count)
41180750Sdes{
42180750Sdes
43180750Sdes	atomic_add_acq_int(count, 1);
44180740Sdes}
45180740Sdes
46180740Sdesstatic __inline unsigned int
47180740Sdesrefcnt_release(volatile unsigned int *count)
48180740Sdes{
49180740Sdes	unsigned int old;
50180740Sdes
51180740Sdes	/* XXX: Should this have a rel membar? */
52180740Sdes	old = atomic_fetchadd_int(count, -1);
53180740Sdes	PJDLOG_ASSERT(old > 0);
54180740Sdes	return (old - 1);
55180740Sdes}
56180740Sdes
57180740Sdes#endif	/* ! __REFCNT_H__ */
58180740Sdes