]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/htl/sem-timedwait.c
1 /* Wait on a semaphore with a timeout. Generic version.
2 Copyright (C) 2005-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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, see
17 <https://www.gnu.org/licenses/>. */
19 #include <semaphore.h>
24 #include <pt-internal.h>
27 __sem_timedwait_internal (sem_t
*restrict sem
,
28 const struct timespec
*restrict timeout
)
32 struct __pthread
*self
;
34 __pthread_spin_lock (&sem
->__lock
);
36 /* Successful down. */
39 __pthread_spin_unlock (&sem
->__lock
);
43 if (timeout
!= NULL
&& ! valid_nanoseconds (timeout
->tv_nsec
))
49 /* Add ourselves to the queue. */
50 self
= _pthread_self ();
52 __pthread_enqueue (&sem
->__queue
, self
);
53 __pthread_spin_unlock (&sem
->__lock
);
55 /* Block the thread. */
57 err
= __pthread_timedblock (self
, timeout
, CLOCK_REALTIME
);
61 __pthread_block (self
);
64 __pthread_spin_lock (&sem
->__lock
);
65 if (self
->prevp
== NULL
)
66 /* Another thread removed us from the queue, which means a wakeup message
67 has been sent. It was either consumed while we were blocking, or
68 queued after we timed out and before we acquired the semaphore lock, in
69 which case the message queue must be drained. */
73 /* We're still in the queue. Noone attempted to wake us up, i.e. we
75 __pthread_dequeue (self
);
78 __pthread_spin_unlock (&sem
->__lock
);
81 __pthread_block (self
);
85 assert (err
== ETIMEDOUT
);
94 __sem_timedwait (sem_t
*restrict sem
, const struct timespec
*restrict timeout
)
96 return __sem_timedwait_internal (sem
, timeout
);
99 weak_alias (__sem_timedwait
, sem_timedwait
);