1/* Copyright (C) 2006-2020 Free Software Foundation, Inc.
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
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
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>
25#include <nptl/pthreadP.h>
26#include <futex-internal.h>
27
28#define DONT_NEED_GAI_MISC_COND 1
29
30#define GAI_MISC_NOTIFY(waitlist) \
31 do { \
32 if (*waitlist->counterp > 0 && --*waitlist->counterp == 0) \
33 futex_wake ((unsigned int *) waitlist->counterp, 1, FUTEX_PRIVATE); \
34 } while (0)
35
36#define GAI_MISC_WAIT(result, futex, timeout, cancel) \
37 do { \
38 volatile unsigned int *futexaddr = &futex; \
39 unsigned int oldval = futex; \
40 \
41 if (oldval != 0) \
42 { \
43 pthread_mutex_unlock (&__gai_requests_mutex); \
44 \
45 int status; \
46 do \
47 { \
48 if (cancel) \
49 status = futex_reltimed_wait_cancelable ( \
50 (unsigned int *) futexaddr, oldval, timeout, FUTEX_PRIVATE); \
51 else \
52 status = futex_reltimed_wait ((unsigned int *) futexaddr, \
53 oldval, timeout, FUTEX_PRIVATE); \
54 if (status != EAGAIN) \
55 break; \
56 \
57 oldval = *futexaddr; \
58 } \
59 while (oldval != 0); \
60 \
61 if (status == EINTR) \
62 result = EINTR; \
63 else if (status == ETIMEDOUT) \
64 result = EAGAIN; \
65 else \
66 assert (status == 0 || status == EAGAIN); \
67 \
68 pthread_mutex_lock (&__gai_requests_mutex); \
69 } \
70 } while (0)
71
72
73#define gai_start_notify_thread __gai_start_notify_thread
74#define gai_create_helper_thread __gai_create_helper_thread
75
76extern inline void
77__gai_start_notify_thread (void)
78{
79 sigset_t ss;
80 sigemptyset (&ss);
81 int sigerr __attribute__ ((unused));
82 sigerr = pthread_sigmask (SIG_SETMASK, &ss, NULL);
83 assert_perror (sigerr);
84}
85
86extern inline int
87__gai_create_helper_thread (pthread_t *threadp, void *(*tf) (void *),
88 void *arg)
89{
90 pthread_attr_t attr;
91
92 /* Make sure the thread is created detached. */
93 pthread_attr_init (&attr);
94 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
95
96 /* The helper thread needs only very little resources. */
97 (void) pthread_attr_setstacksize (&attr,
98 __pthread_get_minstack (&attr)
99 + 4 * PTHREAD_STACK_MIN);
100
101 /* Block all signals in the helper thread. To do this thoroughly we
102 temporarily have to block all signals here. */
103 sigset_t ss;
104 sigset_t oss;
105 sigfillset (&ss);
106 int sigerr __attribute__ ((unused));
107 sigerr = pthread_sigmask (SIG_SETMASK, &ss, &oss);
108 assert_perror (sigerr);
109
110 int ret = pthread_create (threadp, &attr, tf, arg);
111
112 /* Restore the signal mask. */
113 sigerr = pthread_sigmask (SIG_SETMASK, &oss, NULL);
114 assert_perror (sigerr);
115
116 (void) pthread_attr_destroy (&attr);
117 return ret;
118}
119
120#include_next <gai_misc.h>
121