]>
Commit | Line | Data |
---|---|---|
de4471dd | 1 | /* sem_wait -- wait on a semaphore. Generic futex-using version. |
2b778ceb | 2 | Copyright (C) 2003-2021 Free Software Foundation, Inc. |
de4471dd 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 | |
59ba27a6 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
de4471dd | 19 | |
a2f0363f | 20 | #include <lowlevellock.h> /* lll_futex* used by the old code. */ |
b8d3e8fb | 21 | #include "semaphoreP.h" |
042e1521 | 22 | #include "sem_waitcommon.c" |
3d2dd6ca | 23 | |
de4471dd RM |
24 | int |
25 | __new_sem_wait (sem_t *sem) | |
3d2dd6ca | 26 | { |
47677f2e AZ |
27 | /* We need to check whether we need to act upon a cancellation request here |
28 | because POSIX specifies that cancellation points "shall occur" in | |
29 | sem_wait and sem_timedwait, which also means that they need to check | |
30 | this regardless whether they block or not (unlike "may occur" | |
31 | functions). See the POSIX Rationale for this requirement: Section | |
32 | "Thread Cancellation Overview" [1] and austin group issue #1076 [2] | |
33 | for thoughs on why this may be a suboptimal design. | |
34 | ||
35 | [1] http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xsh_chap02.html | |
36 | [2] http://austingroupbugs.net/view.php?id=1076 for thoughts on why this | |
37 | */ | |
38 | __pthread_testcancel (); | |
39 | ||
042e1521 | 40 | if (__new_sem_wait_fast ((struct new_sem *) sem, 0) == 0) |
3d2dd6ca | 41 | return 0; |
042e1521 | 42 | else |
b8d3e8fb LM |
43 | return __new_sem_wait_slow64 ((struct new_sem *) sem, |
44 | CLOCK_REALTIME, NULL); | |
3d2dd6ca UD |
45 | } |
46 | versioned_symbol (libpthread, __new_sem_wait, sem_wait, GLIBC_2_1); | |
47 | ||
3d2dd6ca UD |
48 | #if SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_1) |
49 | int | |
50 | attribute_compat_text_section | |
51 | __old_sem_wait (sem_t *sem) | |
de4471dd RM |
52 | { |
53 | int *futex = (int *) sem; | |
de4471dd RM |
54 | int err; |
55 | ||
56 | do | |
57 | { | |
ff48874d UD |
58 | if (atomic_decrement_if_positive (futex) > 0) |
59 | return 0; | |
de4471dd | 60 | |
835abc5c | 61 | /* Always assume the semaphore is shared. */ |
ce7eb0e9 | 62 | err = lll_futex_wait_cancel (futex, 0, LLL_SHARED); |
de4471dd RM |
63 | } |
64 | while (err == 0 || err == -EWOULDBLOCK); | |
65 | ||
66 | __set_errno (-err); | |
67 | return -1; | |
68 | } | |
69 | ||
de4471dd RM |
70 | compat_symbol (libpthread, __old_sem_wait, sem_wait, GLIBC_2_0); |
71 | #endif | |
042e1521 CD |
72 | |
73 | int | |
74 | __new_sem_trywait (sem_t *sem) | |
75 | { | |
76 | /* We must not fail spuriously, so require a definitive result even if this | |
77 | may lead to a long execution time. */ | |
78 | if (__new_sem_wait_fast ((struct new_sem *) sem, 1) == 0) | |
79 | return 0; | |
80 | __set_errno (EAGAIN); | |
81 | return -1; | |
82 | } | |
83 | versioned_symbol (libpthread, __new_sem_trywait, sem_trywait, GLIBC_2_1); | |
84 | #if SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_1) | |
85 | int | |
86 | __old_sem_trywait (sem_t *sem) | |
87 | { | |
88 | int *futex = (int *) sem; | |
89 | int val; | |
90 | ||
91 | if (*futex > 0) | |
92 | { | |
93 | val = atomic_decrement_if_positive (futex); | |
94 | if (val > 0) | |
95 | return 0; | |
96 | } | |
97 | ||
98 | __set_errno (EAGAIN); | |
99 | return -1; | |
100 | } | |
101 | compat_symbol (libpthread, __old_sem_trywait, sem_trywait, GLIBC_2_0); | |
102 | #endif |