1285SN/A/*-
2462SN/A * SPDX-License-Identifier: BSD-2-Clause
3285SN/A *
4285SN/A * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.  All rights reserved.
5285SN/A *
6285SN/A * Redistribution and use in source and binary forms, with or without
7285SN/A * modification, are permitted provided that the following conditions
8285SN/A * are met:
9285SN/A * 1. Redistributions of source code must retain the above copyright
10285SN/A *    notice(s), this list of conditions and the following disclaimer as
11285SN/A *    the first lines of this file unmodified other than the possible
12285SN/A *    addition of one or more copyright notices.
13285SN/A * 2. Redistributions in binary form must reproduce the above copyright
14285SN/A *    notice(s), this list of conditions and the following disclaimer in the
15285SN/A *    documentation and/or other materials provided with the distribution.
16285SN/A *
17285SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
18285SN/A * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19285SN/A * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20285SN/A * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
21285SN/A * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22285SN/A * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23285SN/A * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24285SN/A * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25285SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26285SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27285SN/A * DAMAGE.
28285SN/A */
29285SN/A
30285SN/A/*
31285SN/A * Counting semaphores.
32285SN/A *
33285SN/A * Priority propagation will not generally raise the priority of semaphore
34285SN/A * "owners" (a misnomer in the context of semaphores), so should not be relied
35285SN/A * upon in combination with semaphores.
36285SN/A */
37285SN/A
38285SN/A#include <sys/param.h>
39285SN/A#include <sys/systm.h>
40285SN/A#include <sys/ktr.h>
41285SN/A#include <sys/condvar.h>
42285SN/A#include <sys/lock.h>
43285SN/A#include <sys/mutex.h>
44285SN/A#include <sys/sema.h>
45285SN/A
46285SN/Avoid
47285SN/Asema_init(struct sema *sema, int value, const char *description)
48285SN/A{
49285SN/A
50285SN/A	KASSERT((value >= 0), ("%s(): negative value\n", __func__));
51285SN/A
52285SN/A	bzero(sema, sizeof(*sema));
53285SN/A	mtx_init(&sema->sema_mtx, description, "sema backing lock",
54285SN/A	    MTX_DEF | MTX_NOWITNESS | MTX_QUIET);
55285SN/A	cv_init(&sema->sema_cv, description);
56285SN/A	sema->sema_value = value;
57285SN/A
58285SN/A	CTR4(KTR_LOCK, "%s(%p, %d, \"%s\")", __func__, sema, value, description);
59285SN/A}
60285SN/A
61285SN/Avoid
62285SN/Asema_destroy(struct sema *sema)
63285SN/A{
64285SN/A
65285SN/A	CTR3(KTR_LOCK, "%s(%p) \"%s\"", __func__, sema,
66285SN/A	    cv_wmesg(&sema->sema_cv));
67285SN/A
68285SN/A	KASSERT((sema->sema_waiters == 0), ("%s(): waiters\n", __func__));
69285SN/A
70285SN/A	mtx_destroy(&sema->sema_mtx);
71285SN/A	cv_destroy(&sema->sema_cv);
72285SN/A}
73285SN/A
74285SN/Avoid
75285SN/A_sema_post(struct sema *sema, const char *file, int line)
76285SN/A{
77285SN/A
78285SN/A	mtx_lock(&sema->sema_mtx);
79285SN/A	sema->sema_value++;
80285SN/A	if (sema->sema_waiters && sema->sema_value > 0)
81285SN/A		cv_signal(&sema->sema_cv);
82285SN/A
83285SN/A	CTR6(KTR_LOCK, "%s(%p) \"%s\" v = %d at %s:%d", __func__, sema,
84285SN/A	    cv_wmesg(&sema->sema_cv), sema->sema_value, file, line);
85285SN/A
86285SN/A	mtx_unlock(&sema->sema_mtx);
87285SN/A}
88285SN/A
89285SN/Avoid
90285SN/A_sema_wait(struct sema *sema, const char *file, int line)
91285SN/A{
92285SN/A
93285SN/A	mtx_lock(&sema->sema_mtx);
94285SN/A	while (sema->sema_value == 0) {
95285SN/A		sema->sema_waiters++;
96285SN/A		cv_wait(&sema->sema_cv, &sema->sema_mtx);
97285SN/A		sema->sema_waiters--;
98285SN/A	}
99285SN/A	sema->sema_value--;
100285SN/A
101285SN/A	CTR6(KTR_LOCK, "%s(%p) \"%s\" v = %d at %s:%d", __func__, sema,
102285SN/A	    cv_wmesg(&sema->sema_cv), sema->sema_value, file, line);
103285SN/A
104285SN/A	mtx_unlock(&sema->sema_mtx);
105285SN/A}
106285SN/A
107285SN/Aint
108285SN/A_sema_timedwait(struct sema *sema, int timo, const char *file, int line)
109285SN/A{
110285SN/A	int error;
111285SN/A
112285SN/A	mtx_lock(&sema->sema_mtx);
113285SN/A
114285SN/A	/*
115285SN/A	 * A spurious wakeup will cause the timeout interval to start over.
116285SN/A	 * This isn't a big deal as long as spurious wakeups don't occur
117285SN/A	 * continuously, since the timeout period is merely a lower bound on how
118285SN/A	 * long to wait.
119285SN/A	 */
120285SN/A	for (error = 0; sema->sema_value == 0 && error == 0;) {
121285SN/A		sema->sema_waiters++;
122285SN/A		error = cv_timedwait(&sema->sema_cv, &sema->sema_mtx, timo);
123285SN/A		sema->sema_waiters--;
124285SN/A	}
125285SN/A	if (sema->sema_value > 0) {
126285SN/A		/* Success. */
127285SN/A		sema->sema_value--;
128285SN/A		error = 0;
129285SN/A
130285SN/A		CTR6(KTR_LOCK, "%s(%p) \"%s\" v = %d at %s:%d", __func__, sema,
131285SN/A		    cv_wmesg(&sema->sema_cv), sema->sema_value, file, line);
132285SN/A	} else {
133285SN/A		CTR5(KTR_LOCK, "%s(%p) \"%s\" fail at %s:%d", __func__, sema,
134285SN/A		    cv_wmesg(&sema->sema_cv), file, line);
135285SN/A	}
136285SN/A
137285SN/A	mtx_unlock(&sema->sema_mtx);
138285SN/A	return (error);
139285SN/A}
140285SN/A
141285SN/Aint
142285SN/A_sema_trywait(struct sema *sema, const char *file, int line)
143367SN/A{
144367SN/A	int ret;
145367SN/A
146367SN/A	mtx_lock(&sema->sema_mtx);
147367SN/A
148367SN/A	if (sema->sema_value > 0) {
149367SN/A		/* Success. */
150367SN/A		sema->sema_value--;
151367SN/A		ret = 1;
152367SN/A
153367SN/A		CTR6(KTR_LOCK, "%s(%p) \"%s\" v = %d at %s:%d", __func__, sema,
154285SN/A		    cv_wmesg(&sema->sema_cv), sema->sema_value, file, line);
155285SN/A	} else {
156367SN/A		ret = 0;
157285SN/A
158285SN/A		CTR5(KTR_LOCK, "%s(%p) \"%s\" fail at %s:%d", __func__, sema,
159285SN/A		    cv_wmesg(&sema->sema_cv), file, line);
160285SN/A	}
161285SN/A
162285SN/A	mtx_unlock(&sema->sema_mtx);
163285SN/A	return (ret);
164285SN/A}
165367SN/A
166367SN/Aint
167367SN/Asema_value(struct sema *sema)
168367SN/A{
169367SN/A	int ret;
170367SN/A
171367SN/A	mtx_lock(&sema->sema_mtx);
172367SN/A	ret = sema->sema_value;
173367SN/A	mtx_unlock(&sema->sema_mtx);
174367SN/A	return (ret);
175367SN/A}
176367SN/A