xref: /freebsd/sbin/hastd/refcnt.h (revision b3e76948)
1d6e636c9SPawel Jakub Dawidek /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4d6e636c9SPawel Jakub Dawidek  * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
5d6e636c9SPawel Jakub Dawidek  *
6d6e636c9SPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without
7d6e636c9SPawel Jakub Dawidek  * modification, are permitted provided that the following conditions
8d6e636c9SPawel Jakub Dawidek  * are met:
9d6e636c9SPawel Jakub Dawidek  * 1. Redistributions of source code must retain the above copyright
10d6e636c9SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer.
11d6e636c9SPawel Jakub Dawidek  * 2. Redistributions in binary form must reproduce the above copyright
12d6e636c9SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer in the
13d6e636c9SPawel Jakub Dawidek  *    documentation and/or other materials provided with the distribution.
14d6e636c9SPawel Jakub Dawidek  *
15d6e636c9SPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16d6e636c9SPawel Jakub Dawidek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17d6e636c9SPawel Jakub Dawidek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18d6e636c9SPawel Jakub Dawidek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19d6e636c9SPawel Jakub Dawidek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20d6e636c9SPawel Jakub Dawidek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21d6e636c9SPawel Jakub Dawidek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22d6e636c9SPawel Jakub Dawidek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23d6e636c9SPawel Jakub Dawidek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24d6e636c9SPawel Jakub Dawidek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25d6e636c9SPawel Jakub Dawidek  * SUCH DAMAGE.
26d6e636c9SPawel Jakub Dawidek  */
27d6e636c9SPawel Jakub Dawidek 
28d6e636c9SPawel Jakub Dawidek #ifndef __REFCNT_H__
29d6e636c9SPawel Jakub Dawidek #define __REFCNT_H__
30d6e636c9SPawel Jakub Dawidek 
31f7586480SEd Schouten #include <machine/atomic.h>
32d6e636c9SPawel Jakub Dawidek 
33d6e636c9SPawel Jakub Dawidek #include "pjdlog.h"
34d6e636c9SPawel Jakub Dawidek 
35f7586480SEd Schouten typedef unsigned int refcnt_t;
366e87c151SEd Schouten 
37d6e636c9SPawel Jakub Dawidek static __inline void
refcnt_init(refcnt_t * count,unsigned int v)386e87c151SEd Schouten refcnt_init(refcnt_t *count, unsigned int v)
39d6e636c9SPawel Jakub Dawidek {
40d6e636c9SPawel Jakub Dawidek 
41f7586480SEd Schouten 	*count = v;
426e87c151SEd Schouten }
436e87c151SEd Schouten 
446e87c151SEd Schouten static __inline void
refcnt_acquire(refcnt_t * count)456e87c151SEd Schouten refcnt_acquire(refcnt_t *count)
466e87c151SEd Schouten {
476e87c151SEd Schouten 
48f7586480SEd Schouten 	atomic_add_acq_int(count, 1);
49d6e636c9SPawel Jakub Dawidek }
50d6e636c9SPawel Jakub Dawidek 
51d6e636c9SPawel Jakub Dawidek static __inline unsigned int
refcnt_release(refcnt_t * count)526e87c151SEd Schouten refcnt_release(refcnt_t *count)
53d6e636c9SPawel Jakub Dawidek {
54d6e636c9SPawel Jakub Dawidek 	unsigned int old;
55d6e636c9SPawel Jakub Dawidek 
56d6e636c9SPawel Jakub Dawidek 	/* XXX: Should this have a rel membar? */
57f7586480SEd Schouten 	old = atomic_fetchadd_int(count, -1);
58d6e636c9SPawel Jakub Dawidek 	PJDLOG_ASSERT(old > 0);
59d6e636c9SPawel Jakub Dawidek 	return (old - 1);
60d6e636c9SPawel Jakub Dawidek }
61d6e636c9SPawel Jakub Dawidek 
62d6e636c9SPawel Jakub Dawidek #endif	/* ! __REFCNT_H__ */
63