1139825Simp/*-
281645Sjasone * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.  All rights reserved.
381645Sjasone *
481645Sjasone * Redistribution and use in source and binary forms, with or without
581645Sjasone * modification, are permitted provided that the following conditions
681645Sjasone * are met:
781645Sjasone * 1. Redistributions of source code must retain the above copyright
881645Sjasone *    notice(s), this list of conditions and the following disclaimer as
981645Sjasone *    the first lines of this file unmodified other than the possible
1081645Sjasone *    addition of one or more copyright notices.
1181645Sjasone * 2. Redistributions in binary form must reproduce the above copyright
1281645Sjasone *    notice(s), this list of conditions and the following disclaimer in the
1381645Sjasone *    documentation and/or other materials provided with the distribution.
1481645Sjasone *
1581645Sjasone * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
1681645Sjasone * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1781645Sjasone * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1881645Sjasone * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
1981645Sjasone * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2081645Sjasone * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2181645Sjasone * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
2281645Sjasone * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2381645Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2481645Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
2581645Sjasone * DAMAGE.
2681645Sjasone *
2781645Sjasone * $FreeBSD$
2881645Sjasone */
2981645Sjasone
3081645Sjasone#ifndef	_SYS_SEMA_H_
3181645Sjasone#define	_SYS_SEMA_H_
3281645Sjasone
3384753Sbde#include <sys/queue.h>
3484753Sbde#include <sys/_lock.h>
3581645Sjasone#include <sys/_mutex.h>
3681645Sjasone#include <sys/condvar.h>
3781645Sjasone
3881645Sjasonestruct sema {
3981645Sjasone	struct mtx	sema_mtx;	/* General protection lock. */
4081645Sjasone	struct cv	sema_cv;	/* Waiters. */
4181645Sjasone	int		sema_waiters;	/* Number of waiters. */
4281645Sjasone	int		sema_value;	/* Semaphore value. */
4381645Sjasone};
4481645Sjasone
4581645Sjasone#ifdef _KERNEL
4681645Sjasonevoid	sema_init(struct sema *sema, int value, const char *description);
4781645Sjasonevoid	sema_destroy(struct sema *sema);
4881645Sjasonevoid	_sema_post(struct sema *sema, const char *file, int line);
4981645Sjasonevoid	_sema_wait(struct sema *sema, const char *file, int line);
5081645Sjasoneint	_sema_timedwait(struct sema *sema, int timo, const char *file, int
5181645Sjasone    line);
5281645Sjasoneint	_sema_trywait(struct sema *sema, const char *file, int line);
5381645Sjasoneint	sema_value(struct sema *sema);
5481645Sjasone
5583593Sjhb#define	sema_post(sema)		_sema_post((sema), LOCK_FILE, LOCK_LINE)
5683593Sjhb#define	sema_wait(sema)		_sema_wait((sema), LOCK_FILE, LOCK_LINE)
5781645Sjasone#define	sema_timedwait(sema, timo)					\
5883593Sjhb	_sema_timedwait((sema), (timo), LOCK_FILE, LOCK_LINE)
5983593Sjhb#define	sema_trywait(sema)	_sema_trywait((sema), LOCK_FILE, LOCK_LINE)
6081645Sjasone
6181645Sjasone#endif	/* _KERNEL */
6281645Sjasone#endif	/* _SYS_SEMA_H_ */
63