1 /* Enqueue and list of read or write requests.
2 Copyright (C) 1997,1998,1999,2000,2001,2003,2005
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 /* We need this special structure to handle asynchronous I/O. */
38 struct sigevent sigev
;
39 struct waitlist list
[0];
44 lio_listio (mode
, list
, nent
, sig
)
46 struct aiocb
*const list
[];
50 struct sigevent defsigev
;
51 struct requestlist
*requests
[nent
];
53 volatile int total
= 0;
56 /* Check arguments. */
57 if (mode
!= LIO_WAIT
&& mode
!= LIO_NOWAIT
)
65 defsigev
.sigev_notify
= SIGEV_NONE
;
69 /* Request the mutex. */
70 pthread_mutex_lock (&__aio_requests_mutex
);
72 /* Now we can enqueue all requests. Since we already acquired the
73 mutex the enqueue function need not do this. */
74 for (cnt
= 0; cnt
< nent
; ++cnt
)
75 if (list
[cnt
] != NULL
&& list
[cnt
]->aio_lio_opcode
!= LIO_NOP
)
77 list
[cnt
]->aio_sigevent
.sigev_notify
= SIGEV_NONE
;
78 requests
[cnt
] = __aio_enqueue_request ((aiocb_union
*) list
[cnt
],
79 (list
[cnt
]->aio_lio_opcode
82 if (requests
[cnt
] != NULL
)
83 /* Successfully enqueued. */
86 /* Signal that we've seen an error. `errno' and the error code
87 of the aiocb will tell more. */
95 /* We don't have anything to do except signalling if we work
98 /* Release the mutex. We do this before raising a signal since the
99 signal handler might do a `siglongjmp' and then the mutex is
101 pthread_mutex_unlock (&__aio_requests_mutex
);
103 if (mode
== LIO_NOWAIT
)
105 #ifdef BROKEN_THREAD_SIGNALS
106 __aio_notify_only (sig
,
107 sig
->sigev_notify
== SIGEV_SIGNAL
? getpid () : 0);
109 __aio_notify_only (sig
);
115 else if (mode
== LIO_WAIT
)
117 pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
118 struct waitlist waitlist
[nent
];
122 for (cnt
= 0; cnt
< nent
; ++cnt
)
124 assert (requests
[cnt
] == NULL
|| list
[cnt
] != NULL
);
126 if (requests
[cnt
] != NULL
&& list
[cnt
]->aio_lio_opcode
!= LIO_NOP
)
128 waitlist
[cnt
].cond
= &cond
;
129 waitlist
[cnt
].next
= requests
[cnt
]->waiting
;
130 waitlist
[cnt
].counterp
= &total
;
131 waitlist
[cnt
].sigevp
= NULL
;
132 #ifdef BROKEN_THREAD_SIGNALS
133 waitlist
[cnt
].caller_pid
= 0; /* Not needed. */
135 requests
[cnt
]->waiting
= &waitlist
[cnt
];
140 /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation
141 points we must be careful. We added entries to the waiting lists
142 which we must remove. So defer cancelation for now. */
143 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE
, &oldstate
);
146 pthread_cond_wait (&cond
, &__aio_requests_mutex
);
148 /* Now it's time to restore the cancelation state. */
149 pthread_setcancelstate (oldstate
, NULL
);
151 /* Release the conditional variable. */
152 if (pthread_cond_destroy (&cond
) != 0)
153 /* This must never happen. */
158 struct async_waitlist
*waitlist
;
160 waitlist
= (struct async_waitlist
*)
161 malloc (sizeof (struct async_waitlist
)
162 + (nent
* sizeof (struct waitlist
)));
164 if (waitlist
== NULL
)
166 __set_errno (EAGAIN
);
171 #ifdef BROKEN_THREAD_SIGNALS
172 pid_t caller_pid
= sig
->sigev_notify
== SIGEV_SIGNAL
? getpid () : 0;
176 for (cnt
= 0; cnt
< nent
; ++cnt
)
178 assert (requests
[cnt
] == NULL
|| list
[cnt
] != NULL
);
180 if (requests
[cnt
] != NULL
181 && list
[cnt
]->aio_lio_opcode
!= LIO_NOP
)
183 waitlist
->list
[cnt
].cond
= NULL
;
184 waitlist
->list
[cnt
].next
= requests
[cnt
]->waiting
;
185 waitlist
->list
[cnt
].counterp
= &waitlist
->counter
;
186 waitlist
->list
[cnt
].sigevp
= &waitlist
->sigev
;
187 #ifdef BROKEN_THREAD_SIGNALS
188 waitlist
->list
[cnt
].caller_pid
= caller_pid
;
190 requests
[cnt
]->waiting
= &waitlist
->list
[cnt
];
195 waitlist
->counter
= total
;
196 waitlist
->sigev
= *sig
;
200 /* Release the mutex. */
201 pthread_mutex_unlock (&__aio_requests_mutex
);