(sem_timedwait): Fix a typo.
[glibc/pb-stable.git] / nptl / sysdeps / unix / sysv / linux / s390 / sem_timedwait.c
blobb1c57beff92a09d37171994a9cb24182dcac4ccf
1 /* Copyright (C) 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <sysdep.h>
22 #include <lowlevellock.h>
23 #include <internaltypes.h>
24 #include <semaphore.h>
26 #include <pthreadP.h>
27 #include <shlib-compat.h>
30 int
31 sem_timedwait (sem, abstime)
32 sem_t *sem;
33 const struct timespec *abstime;
35 /* First check for cancellation. */
36 CANCELLATION_P (THREAD_SELF);
38 int *futex = (int *) sem;
39 int oldval;
40 int newval;
41 int err;
45 /* Atomically decrement semaphore counter if it is > 0. */
46 lll_compare_and_swap (futex, oldval, newval,
47 "ltr %2,%1; jnp 1f; ahi %2,-1");
48 /* oldval != newval if the semaphore count has been decremented. */
49 if (oldval != newval)
50 return 0;
52 /* Check for invalid timeout values. */
53 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
55 __set_errno (EINVAL);
56 return -1;
59 /* Get the current time. */
60 struct timeval tv;
61 (void) __gettimeofday (&tv, NULL);
63 /* Compute the relative timeout. */
64 struct timespec rt;
65 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
66 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
67 if (rt.tv_nsec < 0)
69 rt.tv_nsec += 1000000000;
70 --rt.tv_sec;
72 /* Already timed out. */
73 if (rt.tv_sec < 0)
75 __set_errno (ETIMEDOUT);
76 return -1;
79 /* Enable asynchronous cancellation. Required by the standard. */
80 int oldtype = __pthread_enable_asynccancel ();
82 /* Do wait. */
83 err = lll_futex_timed_wait (futex, 0, &rt);
85 /* Disable asynchronous cancellation. */
86 __pthread_disable_asynccancel (oldtype);
88 /* Returned after timing out? */
89 if (err == -ETIMEDOUT)
91 __set_errno (ETIMEDOUT);
92 return -1;
95 while (err == 0 || err == -EWOULDBLOCK);
97 __set_errno (-err);
98 return -1;