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