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 .sp 67 .LP 68 The \fBsem_timedwait()\fR function locks the semaphore referenced by \fIsem\fR 69 as in the \fBsem_wait\fR(3C) function. However, if the semaphore cannot be 70 locked without waiting for another process or thread to unlock the semaphore by 71 performing a \fBsem_post\fR(3C) function, this wait is terminated when the 72 specified timeout expires. 73 .sp 74 .LP 75 The \fBsem_reltimedwait_np()\fR function is identical to the 76 \fBsem_timedwait()\fR function, except that the timeout is specified as a 77 relative time interval. 78 .sp 79 .LP 80 For \fBsem_timedwait()\fR, the timeout expires when the absolute time specified 81 by \fIabs_timeout\fR passes, as measured by the \fBCLOCK_REALTIME\fR clock 82 (that is, when the value of that clock equals or exceeds \fIabs_timeout\fR), or 83 if the absolute time specified by \fIabs_timeout\fR has already been passed at 84 the time of the call. 85 .sp 86 .LP 87 For \fBsem_reltimedwait_np()\fR, the timeout expires when the time interval 88 specified by \fIrel_timeout\fR passes, as measured by the \fBCLOCK_REALTIME\fR 89 clock, or if the time interval specified by \fIrel_timeout\fR is negative at 90 the time of the call. 91 .sp 92 .LP 93 The resolution of the timeout is the resolution of the \fBCLOCK_REALTIME\fR 94 clock. The \fBtimespec\fR data type is defined as a structure in the 95 <\fBtime.h\fR> header. 96 .sp 97 .LP 98 Under no circumstance does the function fail with a timeout if the semaphore 99 can be locked immediately. The validity of the \fIabs_timeout\fR need not be 100 checked if the semaphore can be locked immediately. 101 .SH RETURN VALUES 102 .sp 103 .LP 104 The \fBsem_timedwait()\fR and \fBsem_reltimedwait_np()\fR functions return 0 if 105 the calling process successfully performed the semaphore lock operation on the 106 semaphore designated by \fIsem\fR. If the call was unsuccessful, the state of 107 the semaphore is be unchanged and the function returns -1 and sets \fBerrno\fR 108 to indicate the error. 109 .SH ERRORS 110 .sp 111 .LP 112 The \fBsem_timedwait()\fR and \fBsem_reltimedwait_np()\fR functions will fail 113 if: 114 .sp 115 .ne 2 116 .na 117 \fB\fBEINVAL\fR\fR 118 .ad 119 .RS 13n 120 The \fIsem\fR argument does not refer to a valid semaphore. 121 .RE 122 123 .sp 124 .ne 2 125 .na 126 \fB\fBEINVAL\fR\fR 127 .ad 128 .RS 13n 129 The process or thread would have blocked, and the timeout parameter specified a 130 nanoseconds field value less than zero or greater than or equal to 1,000 131 million. 132 .RE 133 134 .sp 135 .ne 2 136 .na 137 \fB\fBETIMEDOUT\fR\fR 138 .ad 139 .RS 13n 140 The semaphore could not be locked before the specified timeout expired. 141 .RE 142 143 .sp 144 .LP 145 The \fBsem_timedwait()\fR and \fBsem_reltimedwait_np()\fR functions may fail 146 if: 147 .sp 148 .ne 2 149 .na 150 \fB\fBEDEADLK\fR\fR 151 .ad 152 .RS 11n 153 A deadlock condition was detected. 154 .RE 155 156 .sp 157 .ne 2 158 .na 159 \fB\fBEINTR\fR\fR 160 .ad 161 .RS 11n 162 A signal interrupted this function. 163 .RE 164 165 .SH ATTRIBUTES 166 .sp 167 .LP 168 See \fBattributes\fR(5) for descriptions of the following attributes: 169 .sp 170 171 .sp 172 .TS 173 box; 174 c | c 175 l | l . 176 ATTRIBUTE TYPE ATTRIBUTE VALUE 177 _ 178 Interface Stability Commmitted 179 _ 180 MT-Level MT-Safe 181 _ 182 Standard See below. 183 .TE 184 185 .sp 186 .LP 187 For \fBsem_timedwait()\fR, see \fBstandards\fR(5). 188 .SH SEE ALSO 189 .sp 190 .LP 191 \fBsemctl\fR(2), \fBsemget\fR(2), \fBsemop\fR(2), \fBtime\fR(2), 192 \fBsem_post\fR(3C), \fBsem_trywait\fR(3C), \fBsem_wait\fR(3C), 193 \fBattributes\fR(5), \fBstandards\fR(5)