2001-08-14 22:13:14 +00:00
|
|
|
.\"
|
|
|
|
.\" Copyright (C) 2001 Jason Evans <jasone@FreeBSD.org>. All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice(s), this list of conditions and the following disclaimer as
|
|
|
|
.\" the first lines of this file unmodified other than the possible
|
|
|
|
.\" addition of one or more copyright notices.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice(s), this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
|
|
|
|
.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
.\" DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
|
|
|
|
.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
.\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
.\" DAMAGE.
|
|
|
|
.\"
|
|
|
|
.\" $FreeBSD$
|
|
|
|
.\"
|
|
|
|
.Dd August 13, 2001
|
|
|
|
.Dt SEMA 9
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
|
|
|
.Nm sema ,
|
|
|
|
.Nm sema_init ,
|
|
|
|
.Nm sema_destroy ,
|
|
|
|
.Nm sema_post ,
|
|
|
|
.Nm sema_wait ,
|
|
|
|
.Nm sema_timedwait ,
|
|
|
|
.Nm sema_trywait ,
|
|
|
|
.Nm sema_value
|
|
|
|
.Nd kernel counting semaphore
|
|
|
|
.Sh SYNOPSIS
|
2001-10-04 08:59:53 +00:00
|
|
|
.In sys/types.h
|
2001-12-26 23:14:04 +00:00
|
|
|
.In sys/lock.h
|
2001-08-15 11:40:44 +00:00
|
|
|
.In sys/sema.h
|
2001-08-14 22:13:14 +00:00
|
|
|
.Ft void
|
|
|
|
.Fn sema_init "struct sema *sema" "int value" "const char *description"
|
|
|
|
.Ft void
|
|
|
|
.Fn sema_destroy "struct sema *sema"
|
|
|
|
.Ft void
|
|
|
|
.Fn sema_post "struct sema *sema"
|
|
|
|
.Ft void
|
|
|
|
.Fn sema_wait "struct sema *sema"
|
|
|
|
.Ft int
|
|
|
|
.Fn sema_timedwait "struct sema *sema" "int timo"
|
|
|
|
.Ft int
|
|
|
|
.Fn sema_trywait "struct sema *sema"
|
|
|
|
.Ft int
|
|
|
|
.Fn sema_value "struct sema *sema"
|
|
|
|
.Sh DESCRIPTION
|
|
|
|
Counting semaphores provide a mechanism for synchronizing access to a pool of
|
|
|
|
resources.
|
|
|
|
Unlike mutexes, semaphores do not have the concept of an owner, so they can also
|
|
|
|
be useful in situations where one thread needs to acquire a resource, and
|
|
|
|
another thread needs to release it.
|
|
|
|
Each semaphore has an integer value associated with it.
|
|
|
|
Posting (incrementing) always succeeds, but waiting (decrementing) can only
|
|
|
|
successfully complete if the resulting value of the semaphore is greater than or
|
|
|
|
equal to zero.
|
|
|
|
.Pp
|
|
|
|
Semaphores should not be used where mutexes and condition variables
|
|
|
|
will suffice.
|
|
|
|
Semaphores are a more complex synchronization mechanism than mutexes and
|
2001-08-15 11:40:44 +00:00
|
|
|
condition variables, and are not as efficient.
|
2001-08-14 22:13:14 +00:00
|
|
|
.Pp
|
|
|
|
Semaphores are created with
|
|
|
|
.Fn sema_init ,
|
|
|
|
where
|
|
|
|
.Fa sema
|
|
|
|
is a pointer to space for a
|
2001-08-15 11:40:44 +00:00
|
|
|
.Vt "struct sema" ,
|
2001-08-14 22:13:14 +00:00
|
|
|
.Fa value
|
|
|
|
is the initial value of the semaphore, and
|
|
|
|
.Fa description
|
2001-08-15 11:40:44 +00:00
|
|
|
is a pointer to a null-terminated character string that describes the semaphore.
|
2001-08-14 22:13:14 +00:00
|
|
|
Semaphores are destroyed with
|
|
|
|
.Fn sema_destroy .
|
|
|
|
A semaphore is posted (incremented) with
|
|
|
|
.Fn sema_post .
|
|
|
|
A semaphore is waited on (decremented) with
|
|
|
|
.Fn sema_wait ,
|
|
|
|
.Fn sema_timedwait ,
|
|
|
|
or
|
|
|
|
.Fn sema_trywait .
|
|
|
|
The
|
|
|
|
.Fa timo
|
|
|
|
argument to
|
|
|
|
.Fn sema_timedwait
|
|
|
|
specifies the minimum time in ticks to wait before returning with failure.
|
|
|
|
.Fn sema_value
|
|
|
|
returns the current value of the semaphore.
|
|
|
|
.Pp
|
|
|
|
.Fn sema_timedwait
|
|
|
|
and
|
|
|
|
.Fn sema_trywait
|
|
|
|
will return 0 if waiting on the semaphore failed; otherwise a non-zero value
|
|
|
|
will be returned to indicate success.
|
|
|
|
.Sh SEE ALSO
|
2001-08-15 11:40:44 +00:00
|
|
|
.Xr condvar 9 ,
|
2002-03-28 12:51:06 +00:00
|
|
|
.Xr mtx_pool 9 ,
|
2001-09-17 07:24:48 +00:00
|
|
|
.Xr mutex 9 ,
|
|
|
|
.Xr sx 9
|