Adjust use of lll_futex_* macros.
[kopensolaris-gnu/glibc.git] / nptl / sysdeps / pthread / gai_misc.h
1 /* Copyright (C) 2006, 2007 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, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 /* We define a special synchronization primitive for AIO.  POSIX
20    conditional variables would be ideal but the pthread_cond_*wait
21    operations do not return on EINTR.  This is a requirement for
22    correct aio_suspend and lio_listio implementations.  */
23
24 #include <assert.h>
25 #include <signal.h>
26 #include <pthreadP.h>
27 #include <lowlevellock.h>
28
29 #define DONT_NEED_GAI_MISC_COND 1
30
31 #define GAI_MISC_NOTIFY(waitlist) \
32   do {                                                                        \
33     if (*waitlist->counterp > 0 && --*waitlist->counterp == 0)                \
34       lll_private_futex_wake (waitlist->counterp, 1);                         \
35   } while (0)
36
37 #define GAI_MISC_WAIT(result, futex, timeout, cancel) \
38   do {                                                                        \
39     volatile int *futexaddr = &futex;                                         \
40     int oldval = futex;                                                       \
41                                                                               \
42     if (oldval != 0)                                                          \
43       {                                                                       \
44         pthread_mutex_unlock (&__gai_requests_mutex);                         \
45                                                                               \
46         int oldtype;                                                          \
47         if (cancel)                                                           \
48           oldtype = LIBC_CANCEL_ASYNC ();                                     \
49                                                                               \
50         int status;                                                           \
51         do                                                                    \
52           {                                                                   \
53             status = lll_private_futex_timed_wait (futexaddr, oldval,         \
54                                                    timeout);                  \
55             if (status != -EWOULDBLOCK)                                       \
56               break;                                                          \
57                                                                               \
58             oldval = *futexaddr;                                              \
59           }                                                                   \
60         while (oldval != 0);                                                  \
61                                                                               \
62         if (cancel)                                                           \
63           LIBC_CANCEL_RESET (oldtype);                                        \
64                                                                               \
65         if (status == -EINTR)                                                 \
66           result = EINTR;                                                     \
67         else if (status == -ETIMEDOUT)                                        \
68           result = EAGAIN;                                                    \
69         else                                                                  \
70           assert (status == 0 || status == -EWOULDBLOCK);                     \
71                                                                               \
72         pthread_mutex_lock (&__gai_requests_mutex);                           \
73       }                                                                       \
74   } while (0)
75
76
77 #define gai_start_notify_thread __gai_start_notify_thread
78 #define gai_create_helper_thread __gai_create_helper_thread
79
80 extern inline void
81 __gai_start_notify_thread (void)
82 {
83   sigset_t ss;
84   sigemptyset (&ss);
85   INTERNAL_SYSCALL_DECL (err);
86   INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, NULL, _NSIG / 8);
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, PTHREAD_STACK_MIN);
101
102   /* Block all signals in the helper thread.  To do this thoroughly we
103      temporarily have to block all signals here.  */
104   sigset_t ss;
105   sigset_t oss;
106   sigfillset (&ss);
107   INTERNAL_SYSCALL_DECL (err);
108   INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, &oss, _NSIG / 8);
109
110   int ret = pthread_create (threadp, &attr, tf, arg);
111
112   /* Restore the signal mask.  */
113   INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &oss, NULL,
114                     _NSIG / 8);
115
116   (void) pthread_attr_destroy (&attr);
117   return ret;
118 }
119
120 #include_next <gai_misc.h>