From 89a7bc728d8b84b1ed67577a85eb94faa8ac2d0b Mon Sep 17 00:00:00 2001 From: Jes Sorensen <Jes.Sorensen@redhat.com> Date: Tue, 6 Jul 2010 11:44:48 -0300 Subject: [PATCH 1/2] Error checking RH-Author: Jes Sorensen <Jes.Sorensen@redhat.com> Message-id: <1278416689-30416-2-git-send-email-Jes.Sorensen@redhat.com> Patchwork-id: 10495 O-Subject: [PATCH 1/2] Error checking Bugzilla: 587049 RH-Acked-by: Christoph Hellwig <chellwig@redhat.com> RH-Acked-by: Kevin Wolf <kwolf@redhat.com> RH-Acked-by: Juan Quintela <quintela@redhat.com> From: Jes Sorensen <Jes.Sorensen@redhat.com> Backport of 8653c0158c23ec592f0041ab48b83d6cc6d152fe: >From: malc <malc@c046a42c-6fe2-441c-8c8c-71466251a162> Date: Sat, 21 Feb 2009 05:48:11 +0000 Subject: [PATCH 1/3] Error checking git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6630 c046a42c-6fe2-441c-8c8c-71466251a162 Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com> --- qemu/posix-aio-compat.c | 91 ++++++++++++++++++++++++++++++++++++----------- 1 files changed, 70 insertions(+), 21 deletions(-) Signed-off-by: Eduardo Habkost <ehabkost@redhat.com> --- qemu/posix-aio-compat.c | 91 ++++++++++++++++++++++++++++++++++++----------- 1 files changed, 70 insertions(+), 21 deletions(-) diff --git a/qemu/posix-aio-compat.c b/qemu/posix-aio-compat.c index 6a718c0..59f9d9a 100644 --- a/qemu/posix-aio-compat.c +++ b/qemu/posix-aio-compat.c @@ -15,7 +15,9 @@ #include <unistd.h> #include <errno.h> #include <sys/time.h> +#include <string.h> #include <stdlib.h> +#include <stdio.h> #include "osdep.h" #include "posix-aio-compat.h" @@ -32,20 +34,64 @@ static TAILQ_HEAD(, qemu_paiocb) request_list; #define QEMU_AIO_WRITE 0x0002 #define QEMU_AIO_FLUSH 0x0008 +static void die2(int err, const char *what) +{ + fprintf(stderr, "%s failed: %s\n", what, strerror(err)); + abort(); +} + +static void die(const char *what) +{ + die2(errno, what); +} + +static void mutex_lock(pthread_mutex_t *mutex) +{ + int ret = pthread_mutex_lock(mutex); + if (ret) die2(ret, "pthread_mutex_lock"); +} + +static void mutex_unlock(pthread_mutex_t *mutex) +{ + int ret = pthread_mutex_unlock(mutex); + if (ret) die2(ret, "pthread_mutex_unlock"); +} + +static int cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, + struct timespec *ts) +{ + int ret = pthread_cond_timedwait(cond, mutex, ts); + if (ret && ret != ETIMEDOUT) die2(ret, "pthread_cond_timedwait"); + return ret; +} + +static void cond_broadcast(pthread_cond_t *cond) +{ + int ret = pthread_cond_broadcast(cond); + if (ret) die2(ret, "pthread_cond_broadcast"); +} + +static void thread_create(pthread_t *thread, pthread_attr_t *attr, + void *(*start_routine)(void*), void *arg) +{ + int ret = pthread_create(thread, attr, start_routine, arg); + if (ret) die2(ret, "pthread_create"); +} + static void *aio_thread(void *unused) { sigset_t set; /* block all signals */ - sigfillset(&set); - sigprocmask(SIG_BLOCK, &set, NULL); + if (sigfillset(&set)) die("sigfillset"); + if (sigprocmask(SIG_BLOCK, &set, NULL)) die("sigprocmask"); while (1) { struct qemu_paiocb *aiocb; size_t offset; int ret = 0; - pthread_mutex_lock(&lock); + mutex_lock(&lock); while (TAILQ_EMPTY(&request_list) && !(ret == ETIMEDOUT)) { @@ -54,7 +100,7 @@ static void *aio_thread(void *unused) qemu_gettimeofday(&tv); ts.tv_sec = tv.tv_sec + 10; - ret = pthread_cond_timedwait(&cond, &lock, &ts); + ret = cond_timedwait(&cond, &lock, &ts); } if (TAILQ_EMPTY(&request_list)) @@ -67,7 +113,7 @@ static void *aio_thread(void *unused) aiocb->active = 1; idle_threads--; - pthread_mutex_unlock(&lock); + mutex_unlock(&lock); while (offset < aiocb->aio_nbytes) { ssize_t len; @@ -103,10 +149,10 @@ static void *aio_thread(void *unused) offset += len; } - pthread_mutex_lock(&lock); + mutex_lock(&lock); aiocb->ret = offset; idle_threads++; - pthread_mutex_unlock(&lock); + mutex_unlock(&lock); sigqueue(getpid(), aiocb->aio_sigevent.sigev_signo, @@ -115,23 +161,26 @@ static void *aio_thread(void *unused) idle_threads--; cur_threads--; - pthread_mutex_unlock(&lock); + mutex_unlock(&lock); return NULL; } -static int spawn_thread(void) +static void spawn_thread(void) { - pthread_attr_t attr; int ret; + pthread_attr_t attr; cur_threads++; idle_threads++; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - ret = pthread_create(&thread_id, &attr, aio_thread, NULL); - pthread_attr_destroy(&attr); + ret = pthread_attr_init(&attr); + if (ret) die2 (ret, "pthread_attr_init"); + ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + if (ret) die2 (ret, "pthread_attr_setdetachstate"); + thread_create(&thread_id, &attr, aio_thread, NULL); + ret = pthread_attr_destroy(&attr); + if (ret) die2 (ret, "pthread_attr_destroy"); return ret; } @@ -148,12 +197,12 @@ static int qemu_paio_submit(struct qemu_paiocb *aiocb, int aio_type) aiocb->aio_type = aio_type; aiocb->ret = -EINPROGRESS; aiocb->active = 0; - pthread_mutex_lock(&lock); + mutex_lock(&lock); if (idle_threads == 0 && cur_threads < max_threads) spawn_thread(); TAILQ_INSERT_TAIL(&request_list, aiocb, node); - pthread_mutex_unlock(&lock); - pthread_cond_broadcast(&cond); + mutex_unlock(&lock); + cond_broadcast(&cond); return 0; } @@ -177,9 +226,9 @@ ssize_t qemu_paio_return(struct qemu_paiocb *aiocb) { ssize_t ret; - pthread_mutex_lock(&lock); + mutex_lock(&lock); ret = aiocb->ret; - pthread_mutex_unlock(&lock); + mutex_unlock(&lock); return ret; } @@ -200,7 +249,7 @@ int qemu_paio_cancel(int fd, struct qemu_paiocb *aiocb) { int ret; - pthread_mutex_lock(&lock); + mutex_lock(&lock); if (!aiocb->active) { TAILQ_REMOVE(&request_list, aiocb, node); aiocb->ret = -ECANCELED; @@ -209,7 +258,7 @@ int qemu_paio_cancel(int fd, struct qemu_paiocb *aiocb) ret = QEMU_PAIO_NOTCANCELED; else ret = QEMU_PAIO_ALLDONE; - pthread_mutex_unlock(&lock); + mutex_unlock(&lock); return ret; } -- 1.7.0.3