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