]>
Commit | Line | Data |
---|---|---|
68c97aef | 1 | /* Timed waiting for thread death. Generic futex-using version. |
bfff8b1b | 2 | Copyright (C) 2003-2017 Free Software Foundation, Inc. |
68c97aef RM |
3 | This file is part of the GNU C Library. |
4 | Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003. | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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/>. */ | |
19 | ||
20 | #include <atomic.h> | |
21 | #include <errno.h> | |
22 | #include <lowlevellock.h> | |
23 | #include <sys/time.h> | |
24 | ||
25 | ||
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) | |
33 | { | |
34 | int tid; | |
35 | ||
36 | if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) | |
37 | return EINVAL; | |
38 | ||
39 | /* Repeat until thread terminated. */ | |
40 | while ((tid = *tidp) != 0) | |
41 | { | |
42 | struct timeval tv; | |
43 | struct timespec rt; | |
44 | ||
45 | /* Get the current time. */ | |
46 | (void) __gettimeofday (&tv, NULL); | |
47 | ||
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) | |
52 | { | |
53 | rt.tv_nsec += 1000000000; | |
54 | --rt.tv_sec; | |
55 | } | |
56 | ||
57 | /* Already timed out? */ | |
58 | if (rt.tv_sec < 0) | |
59 | return ETIMEDOUT; | |
60 | ||
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. | |
64 | */ | |
65 | if (lll_futex_timed_wait (tidp, tid, &rt, LLL_SHARED) == -ETIMEDOUT) | |
66 | return ETIMEDOUT; | |
67 | } | |
68 | ||
69 | return 0; | |
70 | } |