xref: /illumos-gate/usr/src/man/man3c/sem_timedwait.3c (revision 8b6b46dcb073dba71917d6a7309f0df7bad798a2)
166492cf0SYuri Pankov.\"
266492cf0SYuri Pankov.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
366492cf0SYuri Pankov.\" permission to reproduce portions of its copyrighted documentation.
466492cf0SYuri Pankov.\" Original documentation from The Open Group can be obtained online at
5c10c16deSRichard Lowe.\" http://www.opengroup.org/bookstore/.
666492cf0SYuri Pankov.\"
766492cf0SYuri Pankov.\" The Institute of Electrical and Electronics Engineers and The Open
866492cf0SYuri Pankov.\" Group, have given us permission to reprint portions of their
966492cf0SYuri Pankov.\" documentation.
1066492cf0SYuri Pankov.\"
1166492cf0SYuri Pankov.\" In the following statement, the phrase ``this text'' refers to portions
1266492cf0SYuri Pankov.\" of the system documentation.
1366492cf0SYuri Pankov.\"
1466492cf0SYuri Pankov.\" Portions of this text are reprinted and reproduced in electronic form
1566492cf0SYuri Pankov.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
1666492cf0SYuri Pankov.\" Standard for Information Technology -- Portable Operating System
1766492cf0SYuri Pankov.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
1866492cf0SYuri Pankov.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
1966492cf0SYuri Pankov.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
2066492cf0SYuri Pankov.\" between these versions and the original IEEE and The Open Group
2166492cf0SYuri Pankov.\" Standard, the original IEEE and The Open Group Standard is the referee
2266492cf0SYuri Pankov.\" document.  The original Standard can be obtained online at
2366492cf0SYuri Pankov.\" http://www.opengroup.org/unix/online.html.
2466492cf0SYuri Pankov.\"
25c10c16deSRichard Lowe.\" This notice shall appear on any product containing this material.
2666492cf0SYuri Pankov.\"
2766492cf0SYuri Pankov.\" The contents of this file are subject to the terms of the
2866492cf0SYuri Pankov.\" Common Development and Distribution License (the "License").
2966492cf0SYuri Pankov.\" You may not use this file except in compliance with the License.
3066492cf0SYuri Pankov.\"
3166492cf0SYuri Pankov.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
3266492cf0SYuri Pankov.\" or http://www.opensolaris.org/os/licensing.
3366492cf0SYuri Pankov.\" See the License for the specific language governing permissions
3466492cf0SYuri Pankov.\" and limitations under the License.
3566492cf0SYuri Pankov.\"
3666492cf0SYuri Pankov.\" When distributing Covered Code, include this CDDL HEADER in each
3766492cf0SYuri Pankov.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
3866492cf0SYuri Pankov.\" If applicable, add the following below this CDDL HEADER, with the
3966492cf0SYuri Pankov.\" fields enclosed by brackets "[]" replaced with your own identifying
4066492cf0SYuri Pankov.\" information: Portions Copyright [yyyy] [name of copyright owner]
4166492cf0SYuri Pankov.\"
4266492cf0SYuri Pankov.\"
4366492cf0SYuri Pankov.\" Copyright (c) 2001, The IEEE and The Open Group.  All Rights Reserved.
4466492cf0SYuri Pankov.\" Portions Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved.
45*8b6b46dcSRobert Mustacchi.\" Copyright 2024 Oxide Computer Company
4666492cf0SYuri Pankov.\"
47*8b6b46dcSRobert Mustacchi.Dd June 23, 2024
48*8b6b46dcSRobert Mustacchi.Dt SEM_TIMEDWAIT 3C
49*8b6b46dcSRobert Mustacchi.Os
50*8b6b46dcSRobert Mustacchi.Sh NAME
51*8b6b46dcSRobert Mustacchi.Nm sem_clockwait ,
52*8b6b46dcSRobert Mustacchi.Nm sem_timedwait ,
53*8b6b46dcSRobert Mustacchi.Nm sem_relclockwait_np ,
54*8b6b46dcSRobert Mustacchi.Nm sem_reltimedwait_np
55*8b6b46dcSRobert Mustacchi.Nd lock a semaphore with timeout
56*8b6b46dcSRobert Mustacchi.Sh SYNOPSIS
57*8b6b46dcSRobert Mustacchi.In sempahore.h
58*8b6b46dcSRobert Mustacchi.In time.h
59*8b6b46dcSRobert Mustacchi.Ft int
60*8b6b46dcSRobert Mustacchi.Fo sem_clockwait
61*8b6b46dcSRobert Mustacchi.Fa "sem_t *restrict sem"
62*8b6b46dcSRobert Mustacchi.Fa "clockid_t clock"
63*8b6b46dcSRobert Mustacchi.Fa "const struct timespec *abstime"
64*8b6b46dcSRobert Mustacchi.Fc
65*8b6b46dcSRobert Mustacchi.Ft int
66*8b6b46dcSRobert Mustacchi.Fo sem_timedwait
67*8b6b46dcSRobert Mustacchi.Fa "sem_t *restrict sem"
68*8b6b46dcSRobert Mustacchi.Fa "const struct timespec *abstime"
69*8b6b46dcSRobert Mustacchi.Fc
70*8b6b46dcSRobert Mustacchi.Ft int
71*8b6b46dcSRobert Mustacchi.Fo sem_relclockwait_np
72*8b6b46dcSRobert Mustacchi.Fa "sem_t *restrict sem"
73*8b6b46dcSRobert Mustacchi.Fa "clockid_t clock"
74*8b6b46dcSRobert Mustacchi.Fa "const struct timespec *reltime"
75*8b6b46dcSRobert Mustacchi.Fc
76*8b6b46dcSRobert Mustacchi.Ft int
77*8b6b46dcSRobert Mustacchi.Fo sem_reltimedwait_np
78*8b6b46dcSRobert Mustacchi.Fa "sem_t *restrict sem"
79*8b6b46dcSRobert Mustacchi.Fa "const struct timespec *reltime"
80*8b6b46dcSRobert Mustacchi.Fc
81*8b6b46dcSRobert Mustacchi.Sh DESCRIPTION
82*8b6b46dcSRobert MustacchiThe
83*8b6b46dcSRobert Mustacchi.Fn sem_clockwait ,
84*8b6b46dcSRobert Mustacchi.Fn sem_timedwait ,
85*8b6b46dcSRobert Mustacchi.Fn sem_relclockwait_np ,
86*8b6b46dcSRobert Mustacchiand
87*8b6b46dcSRobert Mustacchi.Fn sem_reltimewait_np
88*8b6b46dcSRobert Mustacchifunctions lock the sempahore referenced by
89*8b6b46dcSRobert Mustacchi.Fa sem
90*8b6b46dcSRobert Mustacchias in the
91*8b6b46dcSRobert Mustacchi.Xr sem_wait 3C
92*8b6b46dcSRobert Mustacchifunction.
93*8b6b46dcSRobert MustacchiIf the semaphore is locked, the calling thread will block until it
94*8b6b46dcSRobert Mustacchibecomes available by another process or thread unlocking the semaphore
95*8b6b46dcSRobert Mustacchiby calling
96*8b6b46dcSRobert Mustacchi.Xr sem_post 3C .
97*8b6b46dcSRobert MustacchiHowever, if the semaphore does not become available within a specified
98*8b6b46dcSRobert Mustacchitimeout, then the function call will terminate without acquiring the
99*8b6b46dcSRobert Mustacchisemaphore and return the
100*8b6b46dcSRobert Mustacchi.Er ETIMEDOUT
101*8b6b46dcSRobert Mustacchierror.
102*8b6b46dcSRobert MustacchiThese functions all differ in terms of how the timeout is specified and
103*8b6b46dcSRobert Mustacchithe clock that is used to determine when the timeout has elapsed.
104*8b6b46dcSRobert Mustacchi.Pp
105*8b6b46dcSRobert MustacchiIn general, the system provides the ability to program timeouts against
106*8b6b46dcSRobert Mustacchieither the realtime clock,
107*8b6b46dcSRobert Mustacchi.Dv CLOCK_REALTIME ,
108*8b6b46dcSRobert Mustacchiwhich measures the wall clock and is subject to changes due to time
109*8b6b46dcSRobert Mustacchisynchronization daemons such as NTP and PTP, or the high-resolution clock,
110*8b6b46dcSRobert Mustacchi.Dv CLOCK_HIGHRES ,
111*8b6b46dcSRobert Mustacchiwhich is a non-adjustable high-resolution clock that is provided by
112*8b6b46dcSRobert Mustacchisystem hardware.
113*8b6b46dcSRobert MustacchiThe specified timeout may either be specified as an absolute time in the
114*8b6b46dcSRobert Mustacchifuture or as a relative amount of time that should elapse.
115*8b6b46dcSRobert MustacchiEach clock has its own resolution, which can be determined by
116*8b6b46dcSRobert Mustacchi.Xr clock_getres 3C .
117*8b6b46dcSRobert MustacchiTimeouts may be rounded up to a given clock's resolution.
118*8b6b46dcSRobert MustacchiDue to scheduling effects, it is possible that more time may elapse than
119*8b6b46dcSRobert Mustacchiwas specified in the timeout when the caller does not successfully
120*8b6b46dcSRobert Mustacchiacquire the lock.
121*8b6b46dcSRobert Mustacchi.Pp
122*8b6b46dcSRobert MustacchiThe
123*8b6b46dcSRobert Mustacchi.Fn sem_clockwait
124*8b6b46dcSRobert Mustacchiand
125*8b6b46dcSRobert Mustacchi.Fn sem_relclocklock_np
126*8b6b46dcSRobert Mustacchifunctions allow the clock source to be used to be specified by the
127*8b6b46dcSRobert Mustacchi.Fa clock
128*8b6b46dcSRobert Mustacchiargument.
129*8b6b46dcSRobert MustacchiWhile there are additional clocks in the system, only
130*8b6b46dcSRobert Mustacchi.Dv CLOCK_REALTIME
131*8b6b46dcSRobert Mustacchior
132*8b6b46dcSRobert Mustacchi.Dv CLOCK_HIGHRES
133*8b6b46dcSRobert Mustacchimay be specified.
134*8b6b46dcSRobert MustacchiThe thread and process-specific CPU time clocks cannot be used.
135*8b6b46dcSRobert MustacchiConversely, the
136*8b6b46dcSRobert Mustacchi.Fn sem_timedwait
137*8b6b46dcSRobert Mustacchiand
138*8b6b46dcSRobert Mustacchi.Fn sem_reltimedwait_np
139*8b6b46dcSRobert Mustacchifunctions will always utilize the realtime clock,
140*8b6b46dcSRobert Mustacchi.Dv CLOCK_REALTIME .
141*8b6b46dcSRobert Mustacchi.Pp
142*8b6b46dcSRobert MustacchiThe
143*8b6b46dcSRobert Mustacchi.Fn sem_clockwait
144*8b6b46dcSRobert Mustacchiand
145*8b6b46dcSRobert Mustacchi.Fn sem_timedwait
146*8b6b46dcSRobert Mustacchifunctions treat the timeout value,
147*8b6b46dcSRobert Mustacchi.Fa abstime ,
148*8b6b46dcSRobert Mustacchias the absolute time in the future when the timeout should expire.
149*8b6b46dcSRobert MustacchiConversely, the
150*8b6b46dcSRobert Mustacchiand
151*8b6b46dcSRobert Mustacchi.Fn sem_reltimedwait_np
152*8b6b46dcSRobert Mustacchifunctions operate in terms of a relative time.
153*8b6b46dcSRobert MustacchiThe timer will expire when a specified amount of time passes on the
154*8b6b46dcSRobert Mustacchiclock specified as indicated by
155*8b6b46dcSRobert Mustacchi.Fa reltime .
156*8b6b46dcSRobert Mustacchi.Pp
157*8b6b46dcSRobert MustacchiIf the semaphore,
158*8b6b46dcSRobert Mustacchi.Fa sem ,
159*8b6b46dcSRobert Mustacchican be immediately locked, then the timeout value is ignored
160*8b6b46dcSRobert Mustacchientirely, even if the timeout had already expired or represented a
161*8b6b46dcSRobert Mustacchivalue that didn't make sense.
162*8b6b46dcSRobert MustacchiBoth are only checked if the thread would block on the semaphore itself.
163*8b6b46dcSRobert Mustacchi.Sh RETURN VALUES
164*8b6b46dcSRobert MustacchiUpon successful completion, the
165*8b6b46dcSRobert Mustacchi.Fn sem_clockwait ,
166*8b6b46dcSRobert Mustacchi.Fn sem_timedwait ,
167*8b6b46dcSRobert Mustacchi.Fn sem_relclockwait_np ,
168*8b6b46dcSRobert Mustacchiand
169*8b6b46dcSRobert Mustacchi.Fn sem_reltimewait_np
170*8b6b46dcSRobert Mustacchifunctions return
171*8b6b46dcSRobert Mustacchi.Sy 0
172*8b6b46dcSRobert Mustacchiand the thread will have successfully locked the semaphore.
173*8b6b46dcSRobert MustacchiOtherwise
174*8b6b46dcSRobert Mustacchi.Sy -1
175*8b6b46dcSRobert Mustacchiis returned and
176*8b6b46dcSRobert Mustacchi.Va errno
177*8b6b46dcSRobert Mustacchiis set to indicate the error.
178*8b6b46dcSRobert Mustacchi.Sh ERRORS
179*8b6b46dcSRobert MustacchiThe
180*8b6b46dcSRobert Mustacchi.Fn sem_clockwait ,
181*8b6b46dcSRobert Mustacchi.Fn sem_timedwait ,
182*8b6b46dcSRobert Mustacchi.Fn sem_relclockwait_np ,
183*8b6b46dcSRobert Mustacchiand
184*8b6b46dcSRobert Mustacchi.Fn sem_reltimewait_np
185*8b6b46dcSRobert Mustacchifunctions will fail if:
186*8b6b46dcSRobert Mustacchi.Bl -tag -width Er
187*8b6b46dcSRobert Mustacchi.It Er EINVAL
188*8b6b46dcSRobert MustacchiThe
189*8b6b46dcSRobert Mustacchi.Fa sem
190*8b6b46dcSRobert Mustacchiargument does not refer to a valid semaphore or the process or thread
191*8b6b46dcSRobert Mustacchiwould have blocked, and the timeout parameter specified a nanoseconds
192*8b6b46dcSRobert Mustacchifield value less than zero or greater than or equal to 1,000 million.
193*8b6b46dcSRobert Mustacchi.Pp
194*8b6b46dcSRobert MustacchiFor
195*8b6b46dcSRobert Mustacchi.Fn sem_clockwait
196*8b6b46dcSRobert Mustacchior
197*8b6b46dcSRobert Mustacchi.Fn sem_relclockwait_np
198*8b6b46dcSRobert Mustacchithe value of
199*8b6b46dcSRobert Mustacchi.Fa clock
200*8b6b46dcSRobert Mustacchiis either unsupported for locking or unknown to the system.
201*8b6b46dcSRobert Mustacchi.It Er EDEADLK
202c10c16deSRichard LoweA deadlock condition was detected.
203*8b6b46dcSRobert Mustacchi.It Er EINTR
204c10c16deSRichard LoweA signal interrupted this function.
205*8b6b46dcSRobert Mustacchi.It Er ETIMEDOUT
206*8b6b46dcSRobert MustacchiThe semaphore could not be locked before the specified timeout expired.
207*8b6b46dcSRobert Mustacchi.El
208*8b6b46dcSRobert Mustacchi.Sh INTERFACE STABILITY
209*8b6b46dcSRobert Mustacchi.Sy Committed
210*8b6b46dcSRobert Mustacchi.Sh MT-LEVEL
211*8b6b46dcSRobert Mustacchi.Sy MT-Safe
212*8b6b46dcSRobert Mustacchi.Sh SEE ALSO
213*8b6b46dcSRobert Mustacchi.Xr semctl 2 ,
214*8b6b46dcSRobert Mustacchi.Xr semget 2 ,
215*8b6b46dcSRobert Mustacchi.Xr semop 2 ,
216*8b6b46dcSRobert Mustacchi.Xr time 2 ,
217*8b6b46dcSRobert Mustacchi.Xr clock_getres 3C ,
218*8b6b46dcSRobert Mustacchi.Xr sem_post 3C ,
219*8b6b46dcSRobert Mustacchi.Xr sem_trywait 3C ,
220*8b6b46dcSRobert Mustacchi.Xr sem_wait 3C ,
221*8b6b46dcSRobert Mustacchi.Xr attributes 7 ,
222*8b6b46dcSRobert Mustacchi.Xr standards 7
223