From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Lakil-0000a1-NI for qemu-devel@nongnu.org; Sat, 21 Feb 2009 00:48:15 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Lakij-0000ZZ-BM for qemu-devel@nongnu.org; Sat, 21 Feb 2009 00:48:14 -0500 Received: from [199.232.76.173] (port=55310 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Lakij-0000ZW-89 for qemu-devel@nongnu.org; Sat, 21 Feb 2009 00:48:13 -0500 Received: from savannah.gnu.org ([199.232.41.3]:33723 helo=sv.gnu.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1Lakii-0006ow-SU for qemu-devel@nongnu.org; Sat, 21 Feb 2009 00:48:13 -0500 Received: from cvs.savannah.gnu.org ([199.232.41.69]) by sv.gnu.org with esmtp (Exim 4.63) (envelope-from ) id 1Lakii-0006MS-4s for qemu-devel@nongnu.org; Sat, 21 Feb 2009 05:48:12 +0000 Received: from malc by cvs.savannah.gnu.org with local (Exim 4.63) (envelope-from ) id 1Lakih-0006MO-RS for qemu-devel@nongnu.org; Sat, 21 Feb 2009 05:48:11 +0000 MIME-Version: 1.0 Errors-To: malc Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: malc Message-Id: Date: Sat, 21 Feb 2009 05:48:11 +0000 Subject: [Qemu-devel] [6630] Error checking Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Revision: 6630 http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=6630 Author: malc Date: 2009-02-21 05:48:11 +0000 (Sat, 21 Feb 2009) Log Message: ----------- Error checking Modified Paths: -------------- trunk/posix-aio-compat.c Modified: trunk/posix-aio-compat.c =================================================================== --- trunk/posix-aio-compat.c 2009-02-19 20:17:09 UTC (rev 6629) +++ trunk/posix-aio-compat.c 2009-02-21 05:48:11 UTC (rev 6630) @@ -15,6 +15,9 @@ #include #include #include +#include +#include +#include #include "osdep.h" #include "posix-aio-compat.h" @@ -27,20 +30,64 @@ static int idle_threads = 0; static TAILQ_HEAD(, qemu_paiocb) request_list; +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)) { @@ -49,7 +96,7 @@ qemu_gettimeofday(&tv); ts.tv_sec = tv.tv_sec + 10; - ret = pthread_cond_timedwait(&cond, &lock, &ts); + ret = cond_timedwait(&cond, &lock, &ts); } if (ret == ETIMEDOUT) @@ -62,7 +109,7 @@ aiocb->active = 1; idle_threads--; - pthread_mutex_unlock(&lock); + mutex_unlock(&lock); while (offset < aiocb->aio_nbytes) { ssize_t len; @@ -89,35 +136,36 @@ offset += len; } - pthread_mutex_lock(&lock); + mutex_lock(&lock); aiocb->ret = offset; idle_threads++; - pthread_mutex_unlock(&lock); + mutex_unlock(&lock); - kill(getpid(), aiocb->ev_signo); + if (kill(getpid(), aiocb->ev_signo)) die("kill failed"); } idle_threads--; cur_threads--; - pthread_mutex_unlock(&lock); + mutex_unlock(&lock); return NULL; } -static int spawn_thread(void) +static void spawn_thread(void) { + int ret; pthread_attr_t attr; - int ret; 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); - - return ret; + 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"); } int qemu_paio_init(struct qemu_paioinit *aioinit) @@ -132,12 +180,12 @@ aiocb->is_write = is_write; 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; } @@ -156,9 +204,9 @@ { ssize_t ret; - pthread_mutex_lock(&lock); + mutex_lock(&lock); ret = aiocb->ret; - pthread_mutex_unlock(&lock); + mutex_unlock(&lock); return ret; } @@ -179,7 +227,7 @@ { int ret; - pthread_mutex_lock(&lock); + mutex_lock(&lock); if (!aiocb->active) { TAILQ_REMOVE(&request_list, aiocb, node); aiocb->ret = -ECANCELED; @@ -188,7 +236,7 @@ ret = QEMU_PAIO_NOTCANCELED; else ret = QEMU_PAIO_ALLDONE; - pthread_mutex_unlock(&lock); + mutex_unlock(&lock); return ret; }