initial commit
[glibc.git] / sysdeps / unix / bsd / bsd4.4 / kfreebsd / fbtl / gai_misc.h
1 /* Copyright (C) 2006-2013 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 <http://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 <pthreadP.h>
26 #include <lowlevellock.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 lll_futex_wake ((unsigned int *) waitlist->counterp, 1, LLL_PRIVATE); \
34 } while (0)
35
36 #warning have to check sign of return values from futex wait calls
37
38 #define GAI_MISC_WAIT(result, futex, timeout, cancel) \
39 do { \
40 volatile int *futexaddr = &futex; \
41 int oldval = futex; \
42 \
43 if (oldval != 0) \
44 { \
45 pthread_mutex_unlock (&__gai_requests_mutex); \
46 \
47 int oldtype; \
48 if (cancel) \
49 oldtype = LIBC_CANCEL_ASYNC (); \
50 \
51 int status; \
52 do \
53 { \
54 status = lll_futex_timed_wait ((unsigned int *) futexaddr, oldval,\
55 timeout, LLL_PRIVATE); \
56 if (status != EWOULDBLOCK) \
57 break; \
58 \
59 oldval = *futexaddr; \
60 } \
61 while (oldval != 0); \
62 \
63 if (cancel) \
64 LIBC_CANCEL_RESET (oldtype); \
65 \
66 if (status == EINTR) \
67 result = EINTR; \
68 else if (status == ETIMEDOUT) \
69 result = EAGAIN; \
70 else \
71 assert (status == 0 || status == EWOULDBLOCK); \
72 \
73 pthread_mutex_lock (&__gai_requests_mutex); \
74 } \
75 } while (0)
76
77
78 #define gai_start_notify_thread __gai_start_notify_thread
79 #define gai_create_helper_thread __gai_create_helper_thread
80
81 extern inline void
82 __gai_start_notify_thread (void)
83 {
84 sigset_t ss;
85 sigemptyset (&ss);
86 INLINE_SYSCALL (sigprocmask, 3, SIG_SETMASK, &ss, NULL);
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. */
100 (void) pthread_attr_setstacksize (&attr,
101 __pthread_get_minstack (&attr)
102 + 4 * PTHREAD_STACK_MIN);
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);
109 INLINE_SYSCALL (sigprocmask, 3, SIG_SETMASK, &ss, &oss);
110
111 int ret = pthread_create (threadp, &attr, tf, arg);
112
113 /* Restore the signal mask. */
114 INLINE_SYSCALL (sigprocmask, 3, SIG_SETMASK, &oss, NULL);
115
116 (void) pthread_attr_destroy (&attr);
117 return ret;
118 }
119
120 #include <resolv/gai_misc.h>