Add bug 18604 to the correct section
[glibc.git] / nptl / lll_timedwait_tid.c
blob68b48570950d0df68b5625adfd25e15706aabf73
1 /* Timed waiting for thread death. Generic futex-using version.
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <atomic.h>
21 #include <errno.h>
22 #include <lowlevellock.h>
23 #include <sys/time.h>
26 /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex
27 wake-up when the clone terminates. The memory location contains the
28 thread ID while the clone is running and is reset to zero by the kernel
29 afterwards. The kernel up to version 3.16.3 does not use the private futex
30 operations for futex wake-up when the clone terminates. */
31 int
32 __lll_timedwait_tid (int *tidp, const struct timespec *abstime)
34 int tid;
36 if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
37 return EINVAL;
39 /* Repeat until thread terminated. */
40 while ((tid = *tidp) != 0)
42 struct timeval tv;
43 struct timespec rt;
45 /* Get the current time. */
46 (void) __gettimeofday (&tv, NULL);
48 /* Compute relative timeout. */
49 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
50 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
51 if (rt.tv_nsec < 0)
53 rt.tv_nsec += 1000000000;
54 --rt.tv_sec;
57 /* Already timed out? */
58 if (rt.tv_sec < 0)
59 return ETIMEDOUT;
61 /* If *tidp == tid, wait until thread terminates or the wait times out.
62 The kernel up to version 3.16.3 does not use the private futex
63 operations for futex wake-up when the clone terminates.
65 if (lll_futex_timed_wait (tidp, tid, &rt, LLL_SHARED) == -ETIMEDOUT)
66 return ETIMEDOUT;
69 return 0;