1 /* Enqueue and list of read or write requests.
2 Copyright (C) 1997,1998,1999,2000,2001,2003,2005,2006
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
31 #define LIO_OPCODE_BASE 0
34 #include <shlib-compat.h>
37 /* We need this special structure to handle asynchronous I/O. */
41 struct sigevent sigev
;
42 struct waitlist list
[0];
46 /* The code in glibc 2.1 to glibc 2.4 issued only one event when all
47 requests submitted with lio_listio finished. The existing practice
48 is to issue events for the individual requests as well. This is
49 what the new code does. */
50 #if SHLIB_COMPAT (librt, GLIBC_2_1, GLIBC_2_4)
51 # define LIO_MODE(mode) ((mode) & 127)
52 # define NO_INDIVIDUAL_EVENT_P(mode) ((mode) & 128)
54 # define LIO_MODE(mode) mode
55 # define NO_INDIVIDUAL_EVENT_P(mode) 0
60 lio_listio_internal (int mode
, struct aiocb
*const list
[], int nent
,
63 struct sigevent defsigev
;
64 struct requestlist
*requests
[nent
];
66 volatile int total
= 0;
71 defsigev
.sigev_notify
= SIGEV_NONE
;
75 /* Request the mutex. */
76 pthread_mutex_lock (&__aio_requests_mutex
);
78 /* Now we can enqueue all requests. Since we already acquired the
79 mutex the enqueue function need not do this. */
80 for (cnt
= 0; cnt
< nent
; ++cnt
)
81 if (list
[cnt
] != NULL
&& list
[cnt
]->aio_lio_opcode
!= LIO_NOP
)
83 if (NO_INDIVIDUAL_EVENT_P (mode
))
84 list
[cnt
]->aio_sigevent
.sigev_notify
= SIGEV_NONE
;
86 requests
[cnt
] = __aio_enqueue_request ((aiocb_union
*) list
[cnt
],
87 (list
[cnt
]->aio_lio_opcode
90 if (requests
[cnt
] != NULL
)
91 /* Successfully enqueued. */
94 /* Signal that we've seen an error. `errno' and the error code
95 of the aiocb will tell more. */
103 /* We don't have anything to do except signalling if we work
106 /* Release the mutex. We do this before raising a signal since the
107 signal handler might do a `siglongjmp' and then the mutex is
109 pthread_mutex_unlock (&__aio_requests_mutex
);
111 if (LIO_MODE (mode
) == LIO_NOWAIT
)
113 #ifdef BROKEN_THREAD_SIGNALS
114 __aio_notify_only (sig
,
115 sig
->sigev_notify
== SIGEV_SIGNAL
? getpid () : 0);
117 __aio_notify_only (sig
);
123 else if (LIO_MODE (mode
) == LIO_WAIT
)
125 #ifndef DONT_NEED_AIO_MISC_COND
126 pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
129 struct waitlist waitlist
[nent
];
132 for (cnt
= 0; cnt
< nent
; ++cnt
)
134 assert (requests
[cnt
] == NULL
|| list
[cnt
] != NULL
);
136 if (requests
[cnt
] != NULL
&& list
[cnt
]->aio_lio_opcode
!= LIO_NOP
)
138 #ifndef DONT_NEED_AIO_MISC_COND
139 waitlist
[cnt
].cond
= &cond
;
141 waitlist
[cnt
].result
= &result
;
142 waitlist
[cnt
].next
= requests
[cnt
]->waiting
;
143 waitlist
[cnt
].counterp
= &total
;
144 waitlist
[cnt
].sigevp
= NULL
;
145 #ifdef BROKEN_THREAD_SIGNALS
146 waitlist
[cnt
].caller_pid
= 0; /* Not needed. */
148 requests
[cnt
]->waiting
= &waitlist
[cnt
];
153 #ifdef DONT_NEED_AIO_MISC_COND
154 AIO_MISC_WAIT (result
, total
, NULL
, 0);
156 /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancellation
157 points we must be careful. We added entries to the waiting lists
158 which we must remove. So defer cancellation for now. */
159 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE
, &oldstate
);
162 pthread_cond_wait (&cond
, &__aio_requests_mutex
);
164 /* Now it's time to restore the cancellation state. */
165 pthread_setcancelstate (oldstate
, NULL
);
167 /* Release the conditional variable. */
168 if (pthread_cond_destroy (&cond
) != 0)
169 /* This must never happen. */
173 /* If any of the I/O requests failed, return -1 and set errno. */
176 __set_errno (result
== EINTR
? EINTR
: EIO
);
182 struct async_waitlist
*waitlist
;
184 waitlist
= (struct async_waitlist
*)
185 malloc (sizeof (struct async_waitlist
)
186 + (nent
* sizeof (struct waitlist
)));
188 if (waitlist
== NULL
)
190 __set_errno (EAGAIN
);
195 #ifdef BROKEN_THREAD_SIGNALS
196 pid_t caller_pid
= sig
->sigev_notify
== SIGEV_SIGNAL
? getpid () : 0;
200 for (cnt
= 0; cnt
< nent
; ++cnt
)
202 assert (requests
[cnt
] == NULL
|| list
[cnt
] != NULL
);
204 if (requests
[cnt
] != NULL
205 && list
[cnt
]->aio_lio_opcode
!= LIO_NOP
)
207 #ifndef DONT_NEED_AIO_MISC_COND
208 waitlist
->list
[cnt
].cond
= NULL
;
210 waitlist
->list
[cnt
].result
= NULL
;
211 waitlist
->list
[cnt
].next
= requests
[cnt
]->waiting
;
212 waitlist
->list
[cnt
].counterp
= &waitlist
->counter
;
213 waitlist
->list
[cnt
].sigevp
= &waitlist
->sigev
;
214 #ifdef BROKEN_THREAD_SIGNALS
215 waitlist
->list
[cnt
].caller_pid
= caller_pid
;
217 requests
[cnt
]->waiting
= &waitlist
->list
[cnt
];
222 waitlist
->counter
= total
;
223 waitlist
->sigev
= *sig
;
227 /* Release the mutex. */
228 pthread_mutex_unlock (&__aio_requests_mutex
);
234 #if SHLIB_COMPAT (librt, GLIBC_2_1, GLIBC_2_4)
236 attribute_compat_text_section
237 __lio_listio_21 (int mode
, struct aiocb
*const list
[], int nent
,
238 struct sigevent
*sig
)
240 /* Check arguments. */
241 if (mode
!= LIO_WAIT
&& mode
!= LIO_NOWAIT
)
243 __set_errno (EINVAL
);
247 return lio_listio_internal (mode
| LIO_NO_INDIVIDUAL_EVENT
, list
, nent
, sig
);
249 compat_symbol (librt
, __lio_listio_21
, lio_listio
, GLIBC_2_1
);
254 __lio_listio_item_notify (int mode
, struct aiocb
*const list
[], int nent
,
255 struct sigevent
*sig
)
257 /* Check arguments. */
258 if (mode
!= LIO_WAIT
&& mode
!= LIO_NOWAIT
)
260 __set_errno (EINVAL
);
264 return lio_listio_internal (mode
, list
, nent
, sig
);
266 versioned_symbol (librt
, __lio_listio_item_notify
, lio_listio
, GLIBC_2_4
);