1 | /* sem_wait -- wait on a semaphore. Generic futex-using version. |
2 | Copyright (C) 2003-2024 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 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <lowlevellock.h> /* lll_futex* used by the old code. */ |
20 | #include "semaphoreP.h" |
21 | #include "sem_waitcommon.c" |
22 | |
23 | int |
24 | __new_sem_wait (sem_t *sem) |
25 | { |
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. |
33 | |
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 |
36 | */ |
37 | __pthread_testcancel (); |
38 | |
39 | if (__new_sem_wait_fast (sem: (struct new_sem *) sem, definitive_result: 0) == 0) |
40 | return 0; |
41 | else |
42 | return __new_sem_wait_slow64 (sem: (struct new_sem *) sem, |
43 | CLOCK_REALTIME, NULL); |
44 | } |
45 | versioned_symbol (libc, __new_sem_wait, sem_wait, GLIBC_2_34); |
46 | |
47 | #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_1, GLIBC_2_34) |
48 | compat_symbol (libpthread, __new_sem_wait, sem_wait, GLIBC_2_1); |
49 | #endif |
50 | |
51 | #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_1) |
52 | int |
53 | attribute_compat_text_section |
54 | __old_sem_wait (sem_t *sem) |
55 | { |
56 | int *futex = (int *) sem; |
57 | int err; |
58 | |
59 | do |
60 | { |
61 | if (atomic_decrement_if_positive (futex) > 0) |
62 | return 0; |
63 | |
64 | /* Always assume the semaphore is shared. */ |
65 | err = lll_futex_wait_cancel (futex, 0, LLL_SHARED); |
66 | } |
67 | while (err == 0 || err == -EWOULDBLOCK); |
68 | |
69 | __set_errno (-err); |
70 | return -1; |
71 | } |
72 | |
73 | compat_symbol (libpthread, __old_sem_wait, sem_wait, GLIBC_2_0); |
74 | #endif |
75 | |
76 | int |
77 | __new_sem_trywait (sem_t *sem) |
78 | { |
79 | /* We must not fail spuriously, so require a definitive result even if this |
80 | may lead to a long execution time. */ |
81 | if (__new_sem_wait_fast (sem: (struct new_sem *) sem, definitive_result: 1) == 0) |
82 | return 0; |
83 | __set_errno (EAGAIN); |
84 | return -1; |
85 | } |
86 | versioned_symbol (libc, __new_sem_trywait, sem_trywait, GLIBC_2_34); |
87 | |
88 | #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_1, GLIBC_2_34) |
89 | compat_symbol (libpthread, __new_sem_trywait, sem_trywait, GLIBC_2_1); |
90 | #endif |
91 | |
92 | #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_1) |
93 | int |
94 | __old_sem_trywait (sem_t *sem) |
95 | { |
96 | int *futex = (int *) sem; |
97 | int val; |
98 | |
99 | if (*futex > 0) |
100 | { |
101 | val = atomic_decrement_if_positive (futex); |
102 | if (val > 0) |
103 | return 0; |
104 | } |
105 | |
106 | __set_errno (EAGAIN); |
107 | return -1; |
108 | } |
109 | compat_symbol (libpthread, __old_sem_trywait, sem_trywait, GLIBC_2_0); |
110 | #endif |
111 | |