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.Dd February 1, 2006 28.Dt SEMA 9 29.Os 30.Sh NAME 31.Nm sema , 32.Nm sema_init , 33.Nm sema_destroy , 34.Nm sema_post , 35.Nm sema_wait , 36.Nm sema_timedwait , 37.Nm sema_trywait , 38.Nm sema_value 39.Nd kernel counting semaphore 40.Sh SYNOPSIS 41.In sys/types.h 42.In sys/lock.h 43.In sys/sema.h 44.Ft void 45.Fn sema_init "struct sema *sema" "int value" "const char *description" 46.Ft void 47.Fn sema_destroy "struct sema *sema" 48.Ft void 49.Fn sema_post "struct sema *sema" 50.Ft void 51.Fn sema_wait "struct sema *sema" 52.Ft int 53.Fn sema_timedwait "struct sema *sema" "int timo" 54.Ft int 55.Fn sema_trywait "struct sema *sema" 56.Ft int 57.Fn sema_value "struct sema *sema" 58.Sh DESCRIPTION 59Counting semaphores provide a mechanism for synchronizing access to a pool of 60resources. 61Unlike mutexes, semaphores do not have the concept of an owner, so they can also 62be useful in situations where one thread needs to acquire a resource, and 63another thread needs to release it. 64Each semaphore has an integer value associated with it. 65Posting (incrementing) always succeeds, but waiting (decrementing) can only 66successfully complete if the resulting value of the semaphore is greater than or 67equal to zero. 68.Pp 69Semaphores should not be used where mutexes and condition variables 70will suffice. 71Semaphores are a more complex synchronization mechanism than mutexes and 72condition variables, and are not as efficient. 73.Pp 74Semaphores are created with 75.Fn sema_init , 76where 77.Fa sema 78is a pointer to space for a 79.Vt "struct sema" , 80.Fa value 81is the initial value of the semaphore, and 82.Fa description 83is a pointer to a null-terminated character string that describes the semaphore. 84Semaphores are destroyed with 85.Fn sema_destroy . 86A semaphore is posted (incremented) with 87.Fn sema_post . 88A semaphore is waited on (decremented) with 89.Fn sema_wait , 90.Fn sema_timedwait , 91or 92.Fn sema_trywait . 93The 94.Fa timo 95argument to 96.Fn sema_timedwait 97specifies the minimum time in ticks to wait before returning with failure. 98.Fn sema_value 99is used to read the current value of the semaphore. 100.Sh RETURN VALUES 101The 102.Fn sema_value 103function 104returns the current value of the semaphore. 105.Pp 106If decrementing the semaphore would result in its value being negative, 107.Fn sema_trywait 108returns 0 to indicate failure. 109Otherwise, a non-zero value is returned to indicate success. 110.Pp 111The 112.Fn sema_timedwait 113function 114returns 0 if waiting on the semaphore succeeded; otherwise a 115non-zero error code is returned. 116.Sh ERRORS 117The 118.Fn sema_timedwait 119function will fail if: 120.Bl -tag -width Er 121.It Bq Er EWOULDBLOCK 122Timeout expired. 123.El 124.Sh SEE ALSO 125.Xr condvar 9 , 126.Xr locking 9 , 127.Xr mtx_pool 9 , 128.Xr mutex 9 , 129.Xr rwlock 9 , 130.Xr sx 9 131