xref: /freebsd/sbin/hastd/refcnt.h (revision 6e87c151)
1d6e636c9SPawel Jakub Dawidek /*-
2d6e636c9SPawel Jakub Dawidek  * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
3d6e636c9SPawel Jakub Dawidek  * All rights reserved.
4d6e636c9SPawel Jakub Dawidek  *
5d6e636c9SPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without
6d6e636c9SPawel Jakub Dawidek  * modification, are permitted provided that the following conditions
7d6e636c9SPawel Jakub Dawidek  * are met:
8d6e636c9SPawel Jakub Dawidek  * 1. Redistributions of source code must retain the above copyright
9d6e636c9SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer.
10d6e636c9SPawel Jakub Dawidek  * 2. Redistributions in binary form must reproduce the above copyright
11d6e636c9SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer in the
12d6e636c9SPawel Jakub Dawidek  *    documentation and/or other materials provided with the distribution.
13d6e636c9SPawel Jakub Dawidek  * 3. Neither the name of the author nor the names of any co-contributors
14d6e636c9SPawel Jakub Dawidek  *    may be used to endorse or promote products derived from this software
15d6e636c9SPawel Jakub Dawidek  *    without specific prior written permission.
16d6e636c9SPawel Jakub Dawidek  *
17d6e636c9SPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18d6e636c9SPawel Jakub Dawidek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19d6e636c9SPawel Jakub Dawidek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20d6e636c9SPawel Jakub Dawidek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21d6e636c9SPawel Jakub Dawidek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22d6e636c9SPawel Jakub Dawidek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23d6e636c9SPawel Jakub Dawidek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24d6e636c9SPawel Jakub Dawidek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25d6e636c9SPawel Jakub Dawidek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26d6e636c9SPawel Jakub Dawidek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27d6e636c9SPawel Jakub Dawidek  * SUCH DAMAGE.
28d6e636c9SPawel Jakub Dawidek  *
29d6e636c9SPawel Jakub Dawidek  * $FreeBSD$
30d6e636c9SPawel Jakub Dawidek  */
31d6e636c9SPawel Jakub Dawidek 
32d6e636c9SPawel Jakub Dawidek #ifndef __REFCNT_H__
33d6e636c9SPawel Jakub Dawidek #define __REFCNT_H__
34d6e636c9SPawel Jakub Dawidek 
356e87c151SEd Schouten #include <stdatomic.h>
36d6e636c9SPawel Jakub Dawidek 
37d6e636c9SPawel Jakub Dawidek #include "pjdlog.h"
38d6e636c9SPawel Jakub Dawidek 
396e87c151SEd Schouten typedef atomic_uint refcnt_t;
406e87c151SEd Schouten 
41d6e636c9SPawel Jakub Dawidek static __inline void
426e87c151SEd Schouten refcnt_init(refcnt_t *count, unsigned int v)
43d6e636c9SPawel Jakub Dawidek {
44d6e636c9SPawel Jakub Dawidek 
456e87c151SEd Schouten 	atomic_init(count, v);
466e87c151SEd Schouten }
476e87c151SEd Schouten 
486e87c151SEd Schouten static __inline void
496e87c151SEd Schouten refcnt_acquire(refcnt_t *count)
506e87c151SEd Schouten {
516e87c151SEd Schouten 
526e87c151SEd Schouten 	atomic_fetch_add_explicit(count, 1, memory_order_acquire);
53d6e636c9SPawel Jakub Dawidek }
54d6e636c9SPawel Jakub Dawidek 
55d6e636c9SPawel Jakub Dawidek static __inline unsigned int
566e87c151SEd Schouten refcnt_release(refcnt_t *count)
57d6e636c9SPawel Jakub Dawidek {
58d6e636c9SPawel Jakub Dawidek 	unsigned int old;
59d6e636c9SPawel Jakub Dawidek 
60d6e636c9SPawel Jakub Dawidek 	/* XXX: Should this have a rel membar? */
616e87c151SEd Schouten 	old = atomic_fetch_sub(count, 1);
62d6e636c9SPawel Jakub Dawidek 	PJDLOG_ASSERT(old > 0);
63d6e636c9SPawel Jakub Dawidek 	return (old - 1);
64d6e636c9SPawel Jakub Dawidek }
65d6e636c9SPawel Jakub Dawidek 
66d6e636c9SPawel Jakub Dawidek #endif	/* ! __REFCNT_H__ */
67