Always define XSTAT_IS_XSTAT64
[glibc.git] / nptl / sem_wait.c
blobb242c08d2ecdc2c6d9a719b1852e1e3c02d9e8f4
1 /* sem_wait -- wait on a semaphore. Generic futex-using version.
2 Copyright (C) 2003-2016 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 <lowlevellock.h> /* lll_futex* used by the old code. */
21 #include "sem_waitcommon.c"
23 int
24 __new_sem_wait (sem_t *sem)
26 /* We need to check whether we need to act upon a cancellation request here
27 because POSIX specifies that cancellation points "shall occur" in
28 sem_wait and sem_timedwait, which also means that they need to check
29 this regardless whether they block or not (unlike "may occur"
30 functions). See the POSIX Rationale for this requirement: Section
31 "Thread Cancellation Overview" [1] and austin group issue #1076 [2]
32 for thoughs on why this may be a suboptimal design.
34 [1] http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xsh_chap02.html
35 [2] http://austingroupbugs.net/view.php?id=1076 for thoughts on why this
37 __pthread_testcancel ();
39 if (__new_sem_wait_fast ((struct new_sem *) sem, 0) == 0)
40 return 0;
41 else
42 return __new_sem_wait_slow((struct new_sem *) sem, NULL);
44 versioned_symbol (libpthread, __new_sem_wait, sem_wait, GLIBC_2_1);
46 #if SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_1)
47 int
48 attribute_compat_text_section
49 __old_sem_wait (sem_t *sem)
51 int *futex = (int *) sem;
52 int err;
56 if (atomic_decrement_if_positive (futex) > 0)
57 return 0;
59 /* Enable asynchronous cancellation. Required by the standard. */
60 int oldtype = __pthread_enable_asynccancel ();
62 /* Always assume the semaphore is shared. */
63 err = lll_futex_wait (futex, 0, LLL_SHARED);
65 /* Disable asynchronous cancellation. */
66 __pthread_disable_asynccancel (oldtype);
68 while (err == 0 || err == -EWOULDBLOCK);
70 __set_errno (-err);
71 return -1;
74 compat_symbol (libpthread, __old_sem_wait, sem_wait, GLIBC_2_0);
75 #endif
77 int
78 __new_sem_trywait (sem_t *sem)
80 /* We must not fail spuriously, so require a definitive result even if this
81 may lead to a long execution time. */
82 if (__new_sem_wait_fast ((struct new_sem *) sem, 1) == 0)
83 return 0;
84 __set_errno (EAGAIN);
85 return -1;
87 versioned_symbol (libpthread, __new_sem_trywait, sem_trywait, GLIBC_2_1);
88 #if SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_1)
89 int
90 __old_sem_trywait (sem_t *sem)
92 int *futex = (int *) sem;
93 int val;
95 if (*futex > 0)
97 val = atomic_decrement_if_positive (futex);
98 if (val > 0)
99 return 0;
102 __set_errno (EAGAIN);
103 return -1;
105 compat_symbol (libpthread, __old_sem_trywait, sem_trywait, GLIBC_2_0);
106 #endif