xref: /netbsd-src/lib/librt/sem_wait.3 (revision 9c18a264d9de532928a573b321043d76afa3f6aa)
1.\" $NetBSD: sem_wait.3,v 1.3 2012/03/08 22:13:32 wiz Exp $
2.\"
3.\" Copyright (C) 2000 Jason Evans <jasone@FreeBSD.org>.
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice(s), this list of conditions and the following disclaimer as
11.\"    the first lines of this file unmodified other than the possible
12.\"    addition of one or more copyright notices.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice(s), this list of conditions and the following disclaimer in
15.\"    the documentation and/or other materials provided with the
16.\"    distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
19.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
22.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd February 29, 2012
31.Dt SEM_WAIT 3
32.Os
33.Sh NAME
34.Nm sem_wait ,
35.Nm sem_timedwait ,
36.Nm sem_trywait
37.Nd decrement (lock) a semaphore
38.Sh LIBRARY
39.Lb librt
40.Sh SYNOPSIS
41.In semaphore.h
42.Ft int
43.Fn sem_wait "sem_t *sem"
44.Ft int
45.Fn sem_timedwait "sem_t *sem" "const struct timespec * restrict abstime"
46.Ft int
47.Fn sem_trywait "sem_t *sem"
48.Sh DESCRIPTION
49The
50.Fn sem_wait
51function decrements (locks) the semaphore pointed to by
52.Fa sem ,
53but blocks if the value of
54.Fa sem
55is zero, until the value is non-zero and the value can be decremented.
56.Pp
57The
58.Fn sem_timedwait
59function performs the same action, but will not wait beyond
60.Fa abstime
61for the value of
62.Fa sem
63to be non-zero.
64.Pp
65The
66.Fn sem_trywait
67function decrements (locks) the semaphore pointed to by
68.Fa sem
69only if the value is non-zero.
70Otherwise, the semaphore is not decremented and an error is returned.
71.Sh RETURN VALUES
72.Rv -std sem_wait
73.Sh ERRORS
74.Fn sem_wait ,
75.Fn sem_timedwait ,
76and
77.Fn sem_trywait
78will fail if:
79.Bl -tag -width Er
80.It Bq Er EINVAL
81.Fa sem
82points to an invalid semaphore.
83.El
84.Pp
85Additionally,
86.Fn sem_trywait
87will fail if:
88.Bl -tag -width Er
89.It Bq Er EAGAIN
90The semaphore value was zero, and thus could not be decremented.
91.El
92.Pp
93.Fn sem_timedwait
94will also fail if:
95.Bl -tag -width Er
96.It Bq Er ETIMEDOUT
97The semaphore value was zero and
98.Fa abstime
99was reached.
100.El
101.Sh SEE ALSO
102.Xr sem_post 3
103.Sh STANDARDS
104.Fn sem_wait
105and
106.Fn sem_trywait
107conform to
108.St -p1003.1-96 .
109