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
22 #include <lowlevellock.h>
23 #include <internaltypes.h>
24 #include <semaphore.h>
27 #include <shlib-compat.h>
31 sem_timedwait (sem
, abstime
)
33 const struct timespec
*abstime
;
35 /* First check for cancellation. */
36 CANCELLATION_P (THREAD_SELF
);
38 int *futex
= (int *) sem
;
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. */
52 /* Check for invalid timeout values. */
53 if (abstime
->tv_nsec
< 0 || abstime
->tv_nsec
>= 1000000000)
59 /* Get the current time. */
61 (void) __gettimeofday (&tv
, NULL
);
63 /* Compute the relative timeout. */
65 rt
.tv_sec
= abstime
->tv_sec
- tv
.tv_sec
;
66 rt
.tv_nsec
= abstime
->tv_nsec
- tv
.tv_usec
* 1000;
69 rt
.tv_nsec
+= 1000000000;
72 /* Already timed out. */
75 __set_errno (ETIMEDOUT
);
79 /* Enable asynchronous cancellation. Required by the standard. */
80 int oldtype
= __pthread_enable_asynccancel ();
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
);
95 while (err
== 0 || err
== -EWOULDBLOCK
);