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