X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Faio%2Faio.c;h=6d34fa869340810fc9abddfd5b8412df324aece7;hb=a882841baf42e6a8b74cc33a239b84a9a79493db;hp=a5f6443299d4a5c1298bda57fc4fc8f21c414aaa;hpb=5ce3737931bb411a8d167356d4d0287b53b0cbdc;p=musl diff --git a/src/aio/aio.c b/src/aio/aio.c index a5f64432..6d34fa86 100644 --- a/src/aio/aio.c +++ b/src/aio/aio.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "syscall.h" #include "atomic.h" #include "pthread_impl.h" @@ -40,13 +41,6 @@ * blocked permanently. */ -struct aio_args { - struct aiocb *cb; - int op; - int err; - sem_t sem; -}; - struct aio_thread { pthread_t td; struct aiocb *cb; @@ -64,6 +58,13 @@ struct aio_queue { struct aio_thread *head; }; +struct aio_args { + struct aiocb *cb; + struct aio_queue *q; + int op; + sem_t sem; +}; + static pthread_rwlock_t maplock = PTHREAD_RWLOCK_INITIALIZER; static struct aio_queue *****map; static volatile int aio_fd_cnt; @@ -71,13 +72,17 @@ volatile int __aio_fut; static struct aio_queue *__aio_get_queue(int fd, int need) { - if (fd < 0) return 0; + if (fd < 0) { + errno = EBADF; + return 0; + } int a=fd>>24; unsigned char b=fd>>16, c=fd>>8, d=fd; struct aio_queue *q = 0; pthread_rwlock_rdlock(&maplock); if ((!map || !map[a] || !map[a][b] || !map[a][b][c] || !(q=map[a][b][c][d])) && need) { pthread_rwlock_unlock(&maplock); + if (fcntl(fd, F_GETFD) < 0) return 0; pthread_rwlock_wrlock(&maplock); if (!map) map = calloc(sizeof *map, (-1U/2+1)>>24); if (!map) goto out; @@ -195,12 +200,11 @@ static void *io_thread_func(void *ctx) size_t len = cb->aio_nbytes; off_t off = cb->aio_offset; - struct aio_queue *q = __aio_get_queue(fd, 1); + struct aio_queue *q = args->q; ssize_t ret; - args->err = q ? 0 : EAGAIN; + pthread_mutex_lock(&q->lock); sem_post(&args->sem); - if (!q) return 0; at.op = op; at.running = 1; @@ -212,7 +216,6 @@ static void *io_thread_func(void *ctx) at.prev = 0; if ((at.next = q->head)) at.next->prev = &at; q->head = &at; - q->ref++; if (!q->init) { int seekable = lseek(fd, 0, SEEK_CUR) >= 0; @@ -256,23 +259,43 @@ static void *io_thread_func(void *ctx) return 0; } +static size_t io_thread_stack_size = MINSIGSTKSZ+2048; +static pthread_once_t init_stack_size_once; + +static void init_stack_size() +{ + unsigned long val = __getauxval(AT_MINSIGSTKSZ); + if (val > MINSIGSTKSZ) io_thread_stack_size = val + 512; +} + static int submit(struct aiocb *cb, int op) { int ret = 0; pthread_attr_t a; sigset_t allmask, origmask; pthread_t td; - struct aio_args args = { .cb = cb, .op = op }; + struct aio_queue *q = __aio_get_queue(cb->aio_fildes, 1); + struct aio_args args = { .cb = cb, .op = op, .q = q }; sem_init(&args.sem, 0, 0); + if (!q) { + if (errno != EBADF) errno = EAGAIN; + cb->__ret = -1; + cb->__err = errno; + return -1; + } + q->ref++; + pthread_mutex_unlock(&q->lock); + if (cb->aio_sigevent.sigev_notify == SIGEV_THREAD) { if (cb->aio_sigevent.sigev_notify_attributes) a = *cb->aio_sigevent.sigev_notify_attributes; else pthread_attr_init(&a); } else { + pthread_once(&init_stack_size_once, init_stack_size); pthread_attr_init(&a); - pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN); + pthread_attr_setstacksize(&a, io_thread_stack_size); pthread_attr_setguardsize(&a, 0); } pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED); @@ -280,17 +303,15 @@ static int submit(struct aiocb *cb, int op) pthread_sigmask(SIG_BLOCK, &allmask, &origmask); cb->__err = EINPROGRESS; if (pthread_create(&td, &a, io_thread_func, &args)) { - errno = EAGAIN; - ret = -1; + pthread_mutex_lock(&q->lock); + __aio_unref_queue(q); + cb->__err = errno = EAGAIN; + cb->__ret = ret = -1; } pthread_sigmask(SIG_SETMASK, &origmask, 0); if (!ret) { while (sem_wait(&args.sem)); - if (args.err) { - errno = args.err; - ret = -1; - } } return ret; @@ -342,8 +363,9 @@ int aio_cancel(int fd, struct aiocb *cb) sigfillset(&allmask); pthread_sigmask(SIG_BLOCK, &allmask, &origmask); + errno = ENOENT; if (!(q = __aio_get_queue(fd, 0))) { - if (fcntl(fd, F_GETFD) < 0) ret = -1; + if (errno == EBADF) ret = -1; goto done; } @@ -370,9 +392,9 @@ int __aio_close(int fd) return fd; } -LFS64(aio_cancel); -LFS64(aio_error); -LFS64(aio_fsync); -LFS64(aio_read); -LFS64(aio_write); -LFS64(aio_return); +weak_alias(aio_cancel, aio_cancel64); +weak_alias(aio_error, aio_error64); +weak_alias(aio_fsync, aio_fsync64); +weak_alias(aio_read, aio_read64); +weak_alias(aio_write, aio_write64); +weak_alias(aio_return, aio_return64);