]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/nacl/lll_timedwait_tid.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / nacl / lll_timedwait_tid.c
1 /* Timed waiting for thread death. NaCl version.
2 Copyright (C) 2015-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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 <http://www.gnu.org/licenses/>. */
18
19 #include <assert.h>
20 #include <atomic.h>
21 #include <errno.h>
22 #include <lowlevellock.h>
23 #include <sys/time.h>
24
25 int
26 __lll_timedwait_tid (int *tidp, const struct timespec *abstime)
27 {
28 /* Reject invalid timeouts. */
29 if (__glibc_unlikely (abstime->tv_nsec < 0)
30 || __glibc_unlikely (abstime->tv_nsec >= 1000000000))
31 return EINVAL;
32
33 /* Repeat until thread terminated. */
34 int tid;
35 while ((tid = atomic_load_relaxed (tidp)) != 0)
36 {
37 /* See exit-thread.h for details. */
38 if (tid == NACL_EXITING_TID)
39 /* The thread should now be in the process of exiting, so it will
40 finish quick enough that the timeout doesn't matter. If any
41 thread ever stays in this state for long, there is something
42 catastrophically wrong. */
43 atomic_spin_nop ();
44 else
45 {
46 assert (tid > 0);
47
48 /* If *FUTEX == TID, wait until woken or timeout. */
49 int err = __nacl_irt_futex.futex_wait_abs ((volatile int *) tidp,
50 tid, abstime);
51 if (err != 0)
52 {
53 if (__glibc_likely (err == ETIMEDOUT))
54 return err;
55 assert (err == EAGAIN);
56 }
57 }
58 }
59
60 return 0;
61 }