10 #include "pthread_impl.h"
12 /* The following is a threads-based implementation of AIO with minimal
13 * dependence on implementation details. Most synchronization is
14 * performed with pthread primitives, but atomics and futex operations
15 * are used for notification in a couple places where the pthread
16 * primitives would be inefficient or impractical.
18 * For each fd with outstanding aio operations, an aio_queue structure
19 * is maintained. These are reference-counted and destroyed by the last
20 * aio worker thread to exit. Accessing any member of the aio_queue
21 * structure requires a lock on the aio_queue. Adding and removing aio
22 * queues themselves requires a write lock on the global map object,
23 * a 4-level table mapping file descriptor numbers to aio queues. A
24 * read lock on the map is used to obtain locks on existing queues by
25 * excluding destruction of the queue by a different thread while it is
28 * Each aio queue has a list of active threads/operations. Presently there
29 * is a one to one relationship between threads and operations. The only
30 * members of the aio_thread structure which are accessed by other threads
31 * are the linked list pointers, op (which is immutable), running (which
32 * is updated atomically), and err (which is synchronized via running),
33 * so no locking is necessary. Most of the other other members are used
34 * for sharing data between the main flow of execution and cancellation
37 * Taking any aio locks requires having all signals blocked. This is
38 * necessary because aio_cancel is needed by close, and close is required
39 * to be async-signal safe. All aio worker threads run with all signals
40 * blocked permanently.
53 struct aio_thread *next, *prev;
61 int fd, seekable, append, ref, init;
64 struct aio_thread *head;
67 static pthread_rwlock_t maplock = PTHREAD_RWLOCK_INITIALIZER;
68 static struct aio_queue *****map;
69 static volatile int aio_fd_cnt;
70 volatile int __aio_fut;
72 static struct aio_queue *__aio_get_queue(int fd, int need)
76 unsigned char b=fd>>16, c=fd>>8, d=fd;
77 struct aio_queue *q = 0;
78 pthread_rwlock_rdlock(&maplock);
79 if ((!map || !map[a] || !map[a][b] || !map[a][b][c] || !(q=map[a][b][c][d])) && need) {
80 pthread_rwlock_unlock(&maplock);
81 pthread_rwlock_wrlock(&maplock);
82 if (!map) map = calloc(sizeof *map, (-1U/2+1)>>24);
84 if (!map[a]) map[a] = calloc(sizeof **map, 256);
85 if (!map[a]) goto out;
86 if (!map[a][b]) map[a][b] = calloc(sizeof ***map, 256);
87 if (!map[a][b]) goto out;
88 if (!map[a][b][c]) map[a][b][c] = calloc(sizeof ****map, 256);
89 if (!map[a][b][c]) goto out;
90 if (!(q = map[a][b][c][d])) {
91 map[a][b][c][d] = q = calloc(sizeof *****map, 1);
94 pthread_mutex_init(&q->lock, 0);
95 pthread_cond_init(&q->cond, 0);
100 if (q) pthread_mutex_lock(&q->lock);
102 pthread_rwlock_unlock(&maplock);
106 static void __aio_unref_queue(struct aio_queue *q)
110 pthread_mutex_unlock(&q->lock);
114 /* This is potentially the last reference, but a new reference
115 * may arrive since we cannot free the queue object without first
116 * taking the maplock, which requires releasing the queue lock. */
117 pthread_mutex_unlock(&q->lock);
118 pthread_rwlock_wrlock(&maplock);
119 pthread_mutex_lock(&q->lock);
123 unsigned char b=fd>>16, c=fd>>8, d=fd;
126 pthread_rwlock_unlock(&maplock);
127 pthread_mutex_unlock(&q->lock);
131 pthread_rwlock_unlock(&maplock);
132 pthread_mutex_unlock(&q->lock);
136 static void cleanup(void *ctx)
138 struct aio_thread *at = ctx;
139 struct aio_queue *q = at->q;
140 struct aiocb *cb = at->cb;
141 struct sigevent sev = cb->aio_sigevent;
143 /* There are four potential types of waiters we could need to wake:
144 * 1. Callers of aio_cancel/close.
145 * 2. Callers of aio_suspend with a single aiocb.
146 * 3. Callers of aio_suspend with a list.
147 * 4. AIO worker threads waiting for sequenced operations.
148 * Types 1-3 are notified via atomics/futexes, mainly for AS-safety
149 * considerations. Type 4 is notified later via a cond var. */
152 if (a_swap(&at->running, 0) < 0)
153 __wake(&at->running, -1, 1);
154 if (a_swap(&cb->__err, at->err) != EINPROGRESS)
155 __wake(&cb->__err, -1, 1);
156 if (a_swap(&__aio_fut, 0))
157 __wake(&__aio_fut, -1, 1);
159 pthread_mutex_lock(&q->lock);
161 if (at->next) at->next->prev = at->prev;
162 if (at->prev) at->prev->next = at->next;
163 else q->head = at->next;
165 /* Signal aio worker threads waiting for sequenced operations. */
166 pthread_cond_broadcast(&q->cond);
168 __aio_unref_queue(q);
170 if (sev.sigev_notify == SIGEV_SIGNAL) {
172 .si_signo = sev.sigev_signo,
173 .si_value = sev.sigev_value,
174 .si_code = SI_ASYNCIO,
178 __syscall(SYS_rt_sigqueueinfo, si.si_pid, si.si_signo, &si);
180 if (sev.sigev_notify == SIGEV_THREAD) {
181 a_store(&__pthread_self()->cancel, 0);
182 sev.sigev_notify_function(sev.sigev_value);
186 static void *io_thread_func(void *ctx)
188 struct aio_thread at, *p;
190 struct aio_args *args = ctx;
191 struct aiocb *cb = args->cb;
192 int fd = cb->aio_fildes;
194 void *buf = (void *)cb->aio_buf;
195 size_t len = cb->aio_nbytes;
196 off_t off = cb->aio_offset;
198 struct aio_queue *q = __aio_get_queue(fd, 1);
201 args->err = q ? 0 : EAGAIN;
202 sem_post(&args->sem);
210 at.td = __pthread_self();
213 if ((at.next = q->head)) at.next->prev = &at;
218 int seekable = lseek(fd, 0, SEEK_CUR) >= 0;
219 q->seekable = seekable;
220 q->append = !seekable || (fcntl(fd, F_GETFL) & O_APPEND);
224 pthread_cleanup_push(cleanup, &at);
226 /* Wait for sequenced operations. */
227 if (op!=LIO_READ && (op!=LIO_WRITE || q->append)) {
229 for (p=at.next; p && p->op!=LIO_WRITE; p=p->next);
231 pthread_cond_wait(&q->cond, &q->lock);
235 pthread_mutex_unlock(&q->lock);
239 ret = q->append ? write(fd, buf, len) : pwrite(fd, buf, len, off);
242 ret = !q->seekable ? read(fd, buf, len) : pread(fd, buf, len, off);
252 at.err = ret<0 ? errno : 0;
254 pthread_cleanup_pop(1);
259 static int submit(struct aiocb *cb, int op)
263 sigset_t allmask, origmask;
265 struct aio_args args = { .cb = cb, .op = op };
266 sem_init(&args.sem, 0, 0);
268 if (cb->aio_sigevent.sigev_notify == SIGEV_THREAD) {
269 if (cb->aio_sigevent.sigev_notify_attributes)
270 a = *cb->aio_sigevent.sigev_notify_attributes;
272 pthread_attr_init(&a);
274 pthread_attr_init(&a);
275 pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN);
276 pthread_attr_setguardsize(&a, 0);
278 pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
279 sigfillset(&allmask);
280 pthread_sigmask(SIG_BLOCK, &allmask, &origmask);
281 cb->__err = EINPROGRESS;
282 if (pthread_create(&td, &a, io_thread_func, &args)) {
286 pthread_sigmask(SIG_SETMASK, &origmask, 0);
289 while (sem_wait(&args.sem));
299 int aio_read(struct aiocb *cb)
301 return submit(cb, LIO_READ);
304 int aio_write(struct aiocb *cb)
306 return submit(cb, LIO_WRITE);
309 int aio_fsync(int op, struct aiocb *cb)
311 if (op != O_SYNC && op != O_DSYNC) {
315 return submit(cb, op);
318 ssize_t aio_return(struct aiocb *cb)
323 int aio_error(const struct aiocb *cb)
326 return cb->__err & 0x7fffffff;
329 int aio_cancel(int fd, struct aiocb *cb)
331 sigset_t allmask, origmask;
332 int ret = AIO_ALLDONE;
333 struct aio_thread *p;
336 /* Unspecified behavior case. Report an error. */
337 if (cb && fd != cb->aio_fildes) {
342 sigfillset(&allmask);
343 pthread_sigmask(SIG_BLOCK, &allmask, &origmask);
345 if (!(q = __aio_get_queue(fd, 0))) {
346 if (fcntl(fd, F_GETFD) < 0) ret = -1;
350 for (p = q->head; p; p = p->next) {
351 if (cb && cb != p->cb) continue;
352 /* Transition target from running to running-with-waiters */
353 if (a_cas(&p->running, 1, -1)) {
354 pthread_cancel(p->td);
355 __wait(&p->running, 0, -1, 1);
356 if (p->err == ECANCELED) ret = AIO_CANCELED;
360 pthread_mutex_unlock(&q->lock);
362 pthread_sigmask(SIG_SETMASK, &origmask, 0);
366 int __aio_close(int fd)
369 if (aio_fd_cnt) aio_cancel(fd, 0);