qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Jan Kiszka <jan.kiszka@siemens.com>
To: Kevin Wolf <kwolf@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Anthony Liguori <aliguori@us.ibm.com>,
	"qemu-devel@nongnu.org" <qemu-devel@nongnu.org>
Subject: Re: [Qemu-devel] [PATCH 3/6] Switch POSIX compat AIO to QEMU abstractions
Date: Wed, 21 Sep 2011 16:02:26 +0200	[thread overview]
Message-ID: <4E79EE72.3090509@siemens.com> (raw)
In-Reply-To: <4E79ED2D.5000008@redhat.com>

On 2011-09-21 15:57, Kevin Wolf wrote:
> Am 20.09.2011 18:53, schrieb Jan Kiszka:
>> Although there is nothing to wrap for non-POSIX here, redirecting thread
>> and synchronization services to our core simplifies managements jobs
>> like scheduling parameter adjustment. It also frees compat AIO from some
>> duplicate code (/wrt qemu-thread).
>>
>> CC: Kevin Wolf <kwolf@redhat.com>
>> Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
>> ---
>>  posix-aio-compat.c |  115 ++++++++++++++-------------------------------------
>>  1 files changed, 32 insertions(+), 83 deletions(-)
>>
>> diff --git a/posix-aio-compat.c b/posix-aio-compat.c
>> index d3c1174..0715aba 100644
>> --- a/posix-aio-compat.c
>> +++ b/posix-aio-compat.c
>> @@ -13,7 +13,6 @@
>>  
>>  #include <sys/ioctl.h>
>>  #include <sys/types.h>
>> -#include <pthread.h>
>>  #include <unistd.h>
>>  #include <errno.h>
>>  #include <time.h>
>> @@ -27,9 +26,12 @@
>>  #include "qemu-common.h"
>>  #include "trace.h"
>>  #include "block_int.h"
>> +#include "qemu-thread.h"
>>  
>>  #include "block/raw-posix-aio.h"
>>  
>> +#define AIO_THREAD_IDLE_TIMEOUT 10000 /* 10 s */
>> +
>>  static void do_spawn_thread(void);
>>  
>>  struct qemu_paiocb {
>> @@ -57,10 +59,9 @@ typedef struct PosixAioState {
>>  } PosixAioState;
>>  
>>  
>> -static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
>> -static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
>> -static pthread_t thread_id;
>> -static pthread_attr_t attr;
>> +static QemuMutex lock;
>> +static QemuCond cond;
>> +static QemuThread thread;
>>  static int max_threads = 64;
>>  static int cur_threads = 0;
>>  static int idle_threads = 0;
>> @@ -86,39 +87,6 @@ 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_signal(pthread_cond_t *cond)
>> -{
>> -    int ret = pthread_cond_signal(cond);
>> -    if (ret) die2(ret, "pthread_cond_signal");
>> -}
>> -
>> -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 ssize_t handle_aiocb_ioctl(struct qemu_paiocb *aiocb)
>>  {
>>      int ret;
>> @@ -311,27 +279,22 @@ static void posix_aio_notify_event(void);
>>  
>>  static void *aio_thread(void *unused)
>>  {
>> -    mutex_lock(&lock);
>> +    qemu_mutex_lock(&lock);
>>      pending_threads--;
>> -    mutex_unlock(&lock);
>> +    qemu_mutex_unlock(&lock);
>>      do_spawn_thread();
>>  
>>      while (1) {
>>          struct qemu_paiocb *aiocb;
>> -        ssize_t ret = 0;
>> -        qemu_timeval tv;
>> -        struct timespec ts;
>> -
>> -        qemu_gettimeofday(&tv);
>> -        ts.tv_sec = tv.tv_sec + 10;
>> -        ts.tv_nsec = 0;
>> +        bool timed_out = false;
>> +        ssize_t ret;
>>  
>> -        mutex_lock(&lock);
>> +        qemu_mutex_lock(&lock);
>>  
>> -        while (QTAILQ_EMPTY(&request_list) &&
>> -               !(ret == ETIMEDOUT)) {
>> +        while (QTAILQ_EMPTY(&request_list) && !timed_out) {
>>              idle_threads++;
>> -            ret = cond_timedwait(&cond, &lock, &ts);
>> +            timed_out = qemu_cond_timedwait(&cond, &lock,
>> +                                            AIO_THREAD_IDLE_TIMEOUT) != 0;
> 
> Maybe I'm confused by too many negations, but isn't this the wrong way
> round?

You mean design-wise? Maybe. In any case, I think this code would also
win if we just do

	if (timed_out)
		break;

in the loop instead of testing the inverse on entry.

> 
> +    err = pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts);
> +    if (err && err != ETIMEDOUT) {
> +        error_exit(err, __func__);
> +    }
> +    return err == 0;
> 
> So if there was an timeout, qemu_cond_timedwait returns 0 (should it
> return a bool? Also documenting the return value wouldn't hurt) and
> timed_out becomes false (0 != 0).

Will switch to a bool return code (and document it).

Thanks,
Jan

-- 
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux

  reply	other threads:[~2011-09-21 14:02 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-20 16:53 [Qemu-devel] [PATCH 0/6] Spread the use of QEMU threading & locking API Jan Kiszka
2011-09-20 16:53 ` [Qemu-devel] [PATCH 1/6] Enable joinable POSIX threads Jan Kiszka
2011-09-21  7:16   ` Paolo Bonzini
2011-09-21 13:40     ` Kevin Wolf
2011-09-21 13:38       ` Paolo Bonzini
2011-09-20 16:53 ` [Qemu-devel] [PATCH 2/6] Introduce qemu_cond_timedwait Jan Kiszka
2011-09-20 18:22   ` Paolo Bonzini
2011-09-20 19:02     ` [Qemu-devel] [PATCH v2 2/6] Introduce qemu_cond_timedwait for POSIX Jan Kiszka
2011-09-20 16:53 ` [Qemu-devel] [PATCH 3/6] Switch POSIX compat AIO to QEMU abstractions Jan Kiszka
2011-09-21 13:57   ` Kevin Wolf
2011-09-21 14:02     ` Jan Kiszka [this message]
2011-09-21 14:11       ` Kevin Wolf
2011-09-20 16:53 ` [Qemu-devel] [PATCH 4/6] Switch compatfd to QEMU thread Jan Kiszka
2011-09-20 16:53 ` [Qemu-devel] [PATCH 5/6] audio: Use QEMU threads & synchronization Jan Kiszka
2011-09-20 16:53 ` [Qemu-devel] [PATCH 6/6] audio: Switch coreaudio to QemuMutex Jan Kiszka
2011-09-26  7:58   ` Andreas Färber
2011-09-26  8:06     ` Jan Kiszka

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4E79EE72.3090509@siemens.com \
    --to=jan.kiszka@siemens.com \
    --cc=aliguori@us.ibm.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).