1 .\"
   2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
   3 .\" permission to reproduce portions of its copyrighted documentation.
   4 .\" Original documentation from The Open Group can be obtained online at
   5 .\" http://www.opengroup.org/bookstore/.
   6 .\"
   7 .\" The Institute of Electrical and Electronics Engineers and The Open
   8 .\" Group, have given us permission to reprint portions of their
   9 .\" documentation.
  10 .\"
  11 .\" In the following statement, the phrase ``this text'' refers to portions
  12 .\" of the system documentation.
  13 .\"
  14 .\" Portions of this text are reprinted and reproduced in electronic form
  15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
  16 .\" Standard for Information Technology -- Portable Operating System
  17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
  18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
  19 .\" Engineers, Inc and The Open Group.  In the event of any discrepancy
  20 .\" between these versions and the original IEEE and The Open Group
  21 .\" Standard, the original IEEE and The Open Group Standard is the referee
  22 .\" document.  The original Standard can be obtained online at
  23 .\" http://www.opengroup.org/unix/online.html.
  24 .\"
  25 .\" This notice shall appear on any product containing this material.
  26 .\"
  27 .\" The contents of this file are subject to the terms of the
  28 .\" Common Development and Distribution License (the "License").
  29 .\" You may not use this file except in compliance with the License.
  30 .\"
  31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  32 .\" or http://www.opensolaris.org/os/licensing.
  33 .\" See the License for the specific language governing permissions
  34 .\" and limitations under the License.
  35 .\"
  36 .\" When distributing Covered Code, include this CDDL HEADER in each
  37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  38 .\" If applicable, add the following below this CDDL HEADER, with the
  39 .\" fields enclosed by brackets "[]" replaced with your own identifying
  40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
  41 .\"
  42 .\"
  43 .\" Copyright (c) 2001, The IEEE and The Open Group.  All Rights Reserved.
  44 .\" Portions Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved.
  45 .\"
  46 .TH SEM_TIMEDWAIT 3C "Feb 5, 2008"
  47 .SH NAME
  48 sem_timedwait, sem_reltimedwait_np \- lock a semaphore
  49 .SH SYNOPSIS
  50 .LP
  51 .nf
  52 #include <semaphore.h>
  53 #include <time.h>
  54 
  55 \fBint\fR \fBsem_timedwait\fR(\fBsem_t *restrict\fR \fIsem\fR,
  56      \fBconst struct timespec *restrict\fR \fIabs_timeout\fR);
  57 .fi
  58 
  59 .LP
  60 .nf
  61 \fBint\fR \fBsem_reltimedwait_np\fR(\fBsem_t *restrict\fR \fIsem\fR,
  62      \fBconst struct timespec *restrict\fR \fIrel_timeout\fR);
  63 .fi
  64 
  65 .SH DESCRIPTION
  66 .LP
  67 The \fBsem_timedwait()\fR function locks the semaphore referenced by \fIsem\fR
  68 as in the \fBsem_wait\fR(3C) function. However, if the semaphore cannot be
  69 locked without waiting for another process or thread to unlock the semaphore by
  70 performing a \fBsem_post\fR(3C) function, this wait is terminated when the
  71 specified timeout expires.
  72 .sp
  73 .LP
  74 The \fBsem_reltimedwait_np()\fR function is identical to the
  75 \fBsem_timedwait()\fR function, except that the timeout is specified as a
  76 relative time interval.
  77 .sp
  78 .LP
  79 For \fBsem_timedwait()\fR, the timeout expires when the absolute time specified
  80 by \fIabs_timeout\fR passes, as measured by the \fBCLOCK_REALTIME\fR clock
  81 (that is, when the value of that clock equals or exceeds \fIabs_timeout\fR), or
  82 if the absolute time specified by \fIabs_timeout\fR has already been passed at
  83 the time of the call.
  84 .sp
  85 .LP
  86 For \fBsem_reltimedwait_np()\fR, the timeout expires when the time interval
  87 specified by \fIrel_timeout\fR passes, as measured by the \fBCLOCK_REALTIME\fR
  88 clock, or if the time interval specified by \fIrel_timeout\fR is negative at
  89 the time of the call.
  90 .sp
  91 .LP
  92 The resolution of the timeout is the resolution of the \fBCLOCK_REALTIME\fR
  93 clock. The \fBtimespec\fR data type is defined as a structure in the
  94 <\fBtime.h\fR> header.
  95 .sp
  96 .LP
  97 Under no circumstance does the function fail with a timeout if the semaphore
  98 can be locked immediately. The validity of the \fIabs_timeout\fR need not be
  99 checked if the semaphore can be locked immediately.
 100 .SH RETURN VALUES
 101 .LP
 102 The \fBsem_timedwait()\fR and \fBsem_reltimedwait_np()\fR functions return 0 if
 103 the calling process successfully performed the semaphore lock operation on the
 104 semaphore designated by \fIsem\fR. If the call was unsuccessful, the state of
 105 the semaphore is be unchanged and the function returns -1 and sets \fBerrno\fR
 106 to indicate the error.
 107 .SH ERRORS
 108 .LP
 109 The \fBsem_timedwait()\fR and \fBsem_reltimedwait_np()\fR functions will fail
 110 if:
 111 .sp
 112 .ne 2
 113 .na
 114 \fB\fBEINVAL\fR\fR
 115 .ad
 116 .RS 13n
 117 The \fIsem\fR argument does not refer to a valid semaphore.
 118 .RE
 119 
 120 .sp
 121 .ne 2
 122 .na
 123 \fB\fBEINVAL\fR\fR
 124 .ad
 125 .RS 13n
 126 The process or thread would have blocked, and the timeout parameter specified a
 127 nanoseconds field value less than zero or greater than or equal to 1,000
 128 million.
 129 .RE
 130 
 131 .sp
 132 .ne 2
 133 .na
 134 \fB\fBETIMEDOUT\fR\fR
 135 .ad
 136 .RS 13n
 137 The semaphore could not be locked before the specified timeout expired.
 138 .RE
 139 
 140 .sp
 141 .LP
 142 The \fBsem_timedwait()\fR and \fBsem_reltimedwait_np()\fR functions may fail
 143 if:
 144 .sp
 145 .ne 2
 146 .na
 147 \fB\fBEDEADLK\fR\fR
 148 .ad
 149 .RS 11n
 150 A deadlock condition was detected.
 151 .RE
 152 
 153 .sp
 154 .ne 2
 155 .na
 156 \fB\fBEINTR\fR\fR
 157 .ad
 158 .RS 11n
 159 A signal interrupted this function.
 160 .RE
 161 
 162 .SH ATTRIBUTES
 163 .LP
 164 See \fBattributes\fR(5) for descriptions of the following attributes:
 165 .sp
 166 
 167 .sp
 168 .TS
 169 box;
 170 c | c
 171 l | l .
 172 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 173 _
 174 Interface Stability     Committed
 175 _
 176 MT-Level        MT-Safe
 177 _
 178 Standard        See below.
 179 .TE
 180 
 181 .sp
 182 .LP
 183 For \fBsem_timedwait()\fR, see \fBstandards\fR(5).
 184 .SH SEE ALSO
 185 .LP
 186 \fBsemctl\fR(2), \fBsemget\fR(2), \fBsemop\fR(2), \fBtime\fR(2),
 187 \fBsem_post\fR(3C), \fBsem_trywait\fR(3C), \fBsem_wait\fR(3C),
 188 \fBattributes\fR(5), \fBstandards\fR(5)