refcnt.h revision 252386
1249259Sdim/*-
2249259Sdim * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
3249259Sdim * All rights reserved.
4249259Sdim *
5249259Sdim * Redistribution and use in source and binary forms, with or without
6249259Sdim * modification, are permitted provided that the following conditions
7249259Sdim * are met:
8249259Sdim * 1. Redistributions of source code must retain the above copyright
9249259Sdim *    notice, this list of conditions and the following disclaimer.
10249259Sdim * 2. Redistributions in binary form must reproduce the above copyright
11249259Sdim *    notice, this list of conditions and the following disclaimer in the
12249259Sdim *    documentation and/or other materials provided with the distribution.
13249259Sdim * 3. Neither the name of the author nor the names of any co-contributors
14249259Sdim *    may be used to endorse or promote products derived from this software
15249259Sdim *    without specific prior written permission.
16249259Sdim *
17249259Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18249259Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19249259Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20249259Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21249259Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22249259Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23249259Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24249259Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25249259Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26249259Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27249259Sdim * SUCH DAMAGE.
28249259Sdim *
29249259Sdim * $FreeBSD: head/sbin/hastd/refcnt.h 252386 2013-06-29 20:13:39Z ed $
30249259Sdim */
31249259Sdim
32249259Sdim#ifndef __REFCNT_H__
33249259Sdim#define __REFCNT_H__
34249259Sdim
35249259Sdim#include <machine/atomic.h>
36249259Sdim
37249259Sdim#include "pjdlog.h"
38249259Sdim
39249259Sdimtypedef unsigned int refcnt_t;
40249259Sdim
41249259Sdimstatic __inline void
42249259Sdimrefcnt_init(refcnt_t *count, unsigned int v)
43249259Sdim{
44249259Sdim
45249259Sdim	*count = v;
46249259Sdim}
47263508Sdim
48263508Sdimstatic __inline void
49263508Sdimrefcnt_acquire(refcnt_t *count)
50249259Sdim{
51249259Sdim
52249259Sdim	atomic_add_acq_int(count, 1);
53249259Sdim}
54249259Sdim
55249259Sdimstatic __inline unsigned int
56249259Sdimrefcnt_release(refcnt_t *count)
57249259Sdim{
58249259Sdim	unsigned int old;
59249259Sdim
60249259Sdim	/* XXX: Should this have a rel membar? */
61249259Sdim	old = atomic_fetchadd_int(count, -1);
62249259Sdim	PJDLOG_ASSERT(old > 0);
63249259Sdim	return (old - 1);
64249259Sdim}
65249259Sdim
66249259Sdim#endif	/* ! __REFCNT_H__ */
67249259Sdim