xref: /freebsd/sys/sys/blockcount.h (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
5  * Copyright (c) 2020 The FreeBSD Foundation
6  *
7  * Portions of this software were developed by Mark Johnston under
8  * sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef __SYS_BLOCKCOUNT_H__
33 #define __SYS_BLOCKCOUNT_H__
34 
35 #ifdef _KERNEL
36 
37 #include <sys/systm.h>
38 #include <sys/_blockcount.h>
39 
40 struct lock_object;
41 
42 int _blockcount_sleep(blockcount_t *bc, struct lock_object *, const char *wmesg,
43     int prio);
44 void _blockcount_wakeup(blockcount_t *bc, u_int old);
45 
46 static __inline void
47 blockcount_init(blockcount_t *bc)
48 {
49 	atomic_store_int(&bc->__count, 0);
50 }
51 
52 static __inline void
53 blockcount_acquire(blockcount_t *bc, u_int n)
54 {
55 #ifdef INVARIANTS
56 	u_int old;
57 
58 	old = atomic_fetchadd_int(&bc->__count, n);
59 	KASSERT(old + n > old, ("%s: counter overflow %p", __func__, bc));
60 #else
61 	atomic_add_int(&bc->__count, n);
62 #endif
63 }
64 
65 static __inline void
66 blockcount_release(blockcount_t *bc, u_int n)
67 {
68 	u_int old;
69 
70 	atomic_thread_fence_rel();
71 	old = atomic_fetchadd_int(&bc->__count, -n);
72 	KASSERT(old >= n, ("%s: counter underflow %p", __func__, bc));
73 	if (_BLOCKCOUNT_COUNT(old) == n && _BLOCKCOUNT_WAITERS(old))
74 		_blockcount_wakeup(bc, old);
75 }
76 
77 static __inline void
78 _blockcount_wait(blockcount_t *bc, struct lock_object *lo, const char *wmesg,
79     int prio)
80 {
81 	KASSERT((prio & ~PRIMASK) == 0, ("%s: invalid prio %x", __func__, prio));
82 
83 	while (_blockcount_sleep(bc, lo, wmesg, prio) == EAGAIN)
84 		;
85 }
86 
87 #define	blockcount_sleep(bc, lo, wmesg, prio)	\
88 	_blockcount_sleep((bc), (struct lock_object *)(lo), (wmesg), (prio))
89 #define	blockcount_wait(bc, lo, wmesg, prio)	\
90 	_blockcount_wait((bc), (struct lock_object *)(lo), (wmesg), (prio))
91 
92 #endif /* _KERNEL */
93 #endif /* !__SYS_BLOCKCOUNT_H__ */
94