]>
Commit | Line | Data |
---|---|---|
2b778ceb | 1 | /* Copyright (C) 2006-2021 Free Software Foundation, Inc. |
6eab4ee5 UD |
2 | This file is part of the GNU C Library. |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU Lesser General Public | |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
8 | ||
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | Lesser General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
6eab4ee5 UD |
17 | |
18 | /* We define a special synchronization primitive for AIO. POSIX | |
19 | conditional variables would be ideal but the pthread_cond_*wait | |
20 | operations do not return on EINTR. This is a requirement for | |
21 | correct aio_suspend and lio_listio implementations. */ | |
22 | ||
23 | #include <assert.h> | |
24 | #include <signal.h> | |
f083450f | 25 | #include <nptl/pthreadP.h> |
a2f0363f | 26 | #include <futex-internal.h> |
6eab4ee5 UD |
27 | |
28 | #define DONT_NEED_GAI_MISC_COND 1 | |
29 | ||
30 | #define GAI_MISC_NOTIFY(waitlist) \ | |
31 | do { \ | |
d052233c | 32 | if (*waitlist->counterp > 0 && --*waitlist->counterp == 0) \ |
a2f0363f | 33 | futex_wake ((unsigned int *) waitlist->counterp, 1, FUTEX_PRIVATE); \ |
6eab4ee5 UD |
34 | } while (0) |
35 | ||
36 | #define GAI_MISC_WAIT(result, futex, timeout, cancel) \ | |
37 | do { \ | |
e185d57e RM |
38 | volatile unsigned int *futexaddr = &futex; \ |
39 | unsigned int oldval = futex; \ | |
6eab4ee5 UD |
40 | \ |
41 | if (oldval != 0) \ | |
42 | { \ | |
43 | pthread_mutex_unlock (&__gai_requests_mutex); \ | |
44 | \ | |
6eab4ee5 UD |
45 | int status; \ |
46 | do \ | |
47 | { \ | |
ce7eb0e9 | 48 | if (cancel) \ |
47f78f36 LM |
49 | status = __futex_abstimed_wait_cancelable64 ( \ |
50 | (unsigned int *) futexaddr, oldval, CLOCK_MONOTONIC, timeout, \ | |
51 | FUTEX_PRIVATE); \ | |
ce7eb0e9 | 52 | else \ |
47f78f36 LM |
53 | status = __futex_abstimed_wait64 ((unsigned int *) futexaddr, \ |
54 | oldval, CLOCK_REALTIME, timeout, FUTEX_PRIVATE); \ | |
a2f0363f | 55 | if (status != EAGAIN) \ |
6eab4ee5 UD |
56 | break; \ |
57 | \ | |
58 | oldval = *futexaddr; \ | |
59 | } \ | |
60 | while (oldval != 0); \ | |
61 | \ | |
a2f0363f | 62 | if (status == EINTR) \ |
6eab4ee5 | 63 | result = EINTR; \ |
a2f0363f | 64 | else if (status == ETIMEDOUT) \ |
6eab4ee5 | 65 | result = EAGAIN; \ |
47f78f36 LM |
66 | else if (status == EOVERFLOW) \ |
67 | result = EOVERFLOW; \ | |
6eab4ee5 | 68 | else \ |
a2f0363f | 69 | assert (status == 0 || status == EAGAIN); \ |
6eab4ee5 UD |
70 | \ |
71 | pthread_mutex_lock (&__gai_requests_mutex); \ | |
72 | } \ | |
73 | } while (0) | |
74 | ||
75 | ||
76 | #define gai_start_notify_thread __gai_start_notify_thread | |
77 | #define gai_create_helper_thread __gai_create_helper_thread | |
78 | ||
79 | extern inline void | |
80 | __gai_start_notify_thread (void) | |
81 | { | |
82 | sigset_t ss; | |
83 | sigemptyset (&ss); | |
ad37480c MS |
84 | int sigerr __attribute__ ((unused)); |
85 | sigerr = pthread_sigmask (SIG_SETMASK, &ss, NULL); | |
74eb826c | 86 | assert_perror (sigerr); |
6eab4ee5 UD |
87 | } |
88 | ||
89 | extern inline int | |
90 | __gai_create_helper_thread (pthread_t *threadp, void *(*tf) (void *), | |
91 | void *arg) | |
92 | { | |
93 | pthread_attr_t attr; | |
94 | ||
95 | /* Make sure the thread is created detached. */ | |
96 | pthread_attr_init (&attr); | |
97 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); | |
98 | ||
99 | /* The helper thread needs only very little resources. */ | |
ee9e0640 UD |
100 | (void) pthread_attr_setstacksize (&attr, |
101 | __pthread_get_minstack (&attr) | |
102 | + 4 * PTHREAD_STACK_MIN); | |
6eab4ee5 UD |
103 | |
104 | /* Block all signals in the helper thread. To do this thoroughly we | |
105 | temporarily have to block all signals here. */ | |
106 | sigset_t ss; | |
107 | sigset_t oss; | |
108 | sigfillset (&ss); | |
ad37480c MS |
109 | int sigerr __attribute__ ((unused)); |
110 | sigerr = pthread_sigmask (SIG_SETMASK, &ss, &oss); | |
74eb826c | 111 | assert_perror (sigerr); |
6eab4ee5 UD |
112 | |
113 | int ret = pthread_create (threadp, &attr, tf, arg); | |
114 | ||
115 | /* Restore the signal mask. */ | |
74eb826c RM |
116 | sigerr = pthread_sigmask (SIG_SETMASK, &oss, NULL); |
117 | assert_perror (sigerr); | |
6eab4ee5 UD |
118 | |
119 | (void) pthread_attr_destroy (&attr); | |
120 | return ret; | |
121 | } | |
122 | ||
123 | #include_next <gai_misc.h> |