xref: /freebsd/sys/kern/kern_sema.c (revision c6989859ae9388eeb46a24fe88f9b8d07101c710)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice(s), this list of conditions and the following disclaimer as
11  *    the first lines of this file unmodified other than the possible
12  *    addition of one or more copyright notices.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice(s), this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27  * DAMAGE.
28  */
29 
30 /*
31  * Counting semaphores.
32  *
33  * Priority propagation will not generally raise the priority of semaphore
34  * "owners" (a misnomer in the context of semaphores), so should not be relied
35  * upon in combination with semaphores.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/ktr.h>
44 #include <sys/condvar.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/sema.h>
48 
49 void
50 sema_init(struct sema *sema, int value, const char *description)
51 {
52 
53 	KASSERT((value >= 0), ("%s(): negative value\n", __func__));
54 
55 	bzero(sema, sizeof(*sema));
56 	mtx_init(&sema->sema_mtx, description, "sema backing lock",
57 	    MTX_DEF | MTX_NOWITNESS | MTX_QUIET);
58 	cv_init(&sema->sema_cv, description);
59 	sema->sema_value = value;
60 
61 	CTR4(KTR_LOCK, "%s(%p, %d, \"%s\")", __func__, sema, value, description);
62 }
63 
64 void
65 sema_destroy(struct sema *sema)
66 {
67 
68 	CTR3(KTR_LOCK, "%s(%p) \"%s\"", __func__, sema,
69 	    cv_wmesg(&sema->sema_cv));
70 
71 	KASSERT((sema->sema_waiters == 0), ("%s(): waiters\n", __func__));
72 
73 	mtx_destroy(&sema->sema_mtx);
74 	cv_destroy(&sema->sema_cv);
75 }
76 
77 void
78 _sema_post(struct sema *sema, const char *file, int line)
79 {
80 
81 	mtx_lock(&sema->sema_mtx);
82 	sema->sema_value++;
83 	if (sema->sema_waiters && sema->sema_value > 0)
84 		cv_signal(&sema->sema_cv);
85 
86 	CTR6(KTR_LOCK, "%s(%p) \"%s\" v = %d at %s:%d", __func__, sema,
87 	    cv_wmesg(&sema->sema_cv), sema->sema_value, file, line);
88 
89 	mtx_unlock(&sema->sema_mtx);
90 }
91 
92 void
93 _sema_wait(struct sema *sema, const char *file, int line)
94 {
95 
96 	mtx_lock(&sema->sema_mtx);
97 	while (sema->sema_value == 0) {
98 		sema->sema_waiters++;
99 		cv_wait(&sema->sema_cv, &sema->sema_mtx);
100 		sema->sema_waiters--;
101 	}
102 	sema->sema_value--;
103 
104 	CTR6(KTR_LOCK, "%s(%p) \"%s\" v = %d at %s:%d", __func__, sema,
105 	    cv_wmesg(&sema->sema_cv), sema->sema_value, file, line);
106 
107 	mtx_unlock(&sema->sema_mtx);
108 }
109 
110 int
111 _sema_timedwait(struct sema *sema, int timo, const char *file, int line)
112 {
113 	int error;
114 
115 	mtx_lock(&sema->sema_mtx);
116 
117 	/*
118 	 * A spurious wakeup will cause the timeout interval to start over.
119 	 * This isn't a big deal as long as spurious wakeups don't occur
120 	 * continuously, since the timeout period is merely a lower bound on how
121 	 * long to wait.
122 	 */
123 	for (error = 0; sema->sema_value == 0 && error == 0;) {
124 		sema->sema_waiters++;
125 		error = cv_timedwait(&sema->sema_cv, &sema->sema_mtx, timo);
126 		sema->sema_waiters--;
127 	}
128 	if (sema->sema_value > 0) {
129 		/* Success. */
130 		sema->sema_value--;
131 		error = 0;
132 
133 		CTR6(KTR_LOCK, "%s(%p) \"%s\" v = %d at %s:%d", __func__, sema,
134 		    cv_wmesg(&sema->sema_cv), sema->sema_value, file, line);
135 	} else {
136 		CTR5(KTR_LOCK, "%s(%p) \"%s\" fail at %s:%d", __func__, sema,
137 		    cv_wmesg(&sema->sema_cv), file, line);
138 	}
139 
140 	mtx_unlock(&sema->sema_mtx);
141 	return (error);
142 }
143 
144 int
145 _sema_trywait(struct sema *sema, const char *file, int line)
146 {
147 	int ret;
148 
149 	mtx_lock(&sema->sema_mtx);
150 
151 	if (sema->sema_value > 0) {
152 		/* Success. */
153 		sema->sema_value--;
154 		ret = 1;
155 
156 		CTR6(KTR_LOCK, "%s(%p) \"%s\" v = %d at %s:%d", __func__, sema,
157 		    cv_wmesg(&sema->sema_cv), sema->sema_value, file, line);
158 	} else {
159 		ret = 0;
160 
161 		CTR5(KTR_LOCK, "%s(%p) \"%s\" fail at %s:%d", __func__, sema,
162 		    cv_wmesg(&sema->sema_cv), file, line);
163 	}
164 
165 	mtx_unlock(&sema->sema_mtx);
166 	return (ret);
167 }
168 
169 int
170 sema_value(struct sema *sema)
171 {
172 	int ret;
173 
174 	mtx_lock(&sema->sema_mtx);
175 	ret = sema->sema_value;
176 	mtx_unlock(&sema->sema_mtx);
177 	return (ret);
178 }
179