2 .\" Copyright (C) 2001 Jason Evans <jasone@FreeBSD.org>. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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.
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
41 .Nd kernel counting semaphore
47 .Fn sema_init "struct sema *sema" "int value" "const char *description"
49 .Fn sema_destroy "struct sema *sema"
51 .Fn sema_post "struct sema *sema"
53 .Fn sema_wait "struct sema *sema"
55 .Fn sema_timedwait "struct sema *sema" "int timo"
57 .Fn sema_trywait "struct sema *sema"
59 .Fn sema_value "struct sema *sema"
61 Counting semaphores provide a mechanism for synchronizing access to a pool of
63 Unlike mutexes, semaphores do not have the concept of an owner, so they can also
64 be useful in situations where one thread needs to acquire a resource, and
65 another thread needs to release it.
66 Each semaphore has an integer value associated with it.
67 Posting (incrementing) always succeeds, but waiting (decrementing) can only
68 successfully complete if the resulting value of the semaphore is greater than or
71 Semaphores should not be used where mutexes and condition variables
73 Semaphores are a more complex synchronization mechanism than mutexes and
74 condition variables, and are not as efficient.
76 Semaphores are created with
80 is a pointer to space for a
83 is the initial value of the semaphore, and
85 is a pointer to a null-terminated character string that describes the semaphore.
86 Semaphores are destroyed with
88 A semaphore is posted (incremented) with
90 A semaphore is waited on (decremented) with
99 specifies the minimum time in ticks to wait before returning with failure.
101 is used to read the current value of the semaphore.
106 returns the current value of the semaphore.
108 If decrementing the semaphore would result in its value being negative,
110 returns 0 to indicate failure.
111 Otherwise, a non-zero value is returned to indicate success.
116 returns 0 if waiting on the semaphore succeeded; otherwise a
117 non-zero error code is returned.
121 function will fail if:
123 .It Bq Er EWOULDBLOCK