qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Nicolas Saenz Julienne <nsaenzju@redhat.com>, qemu-devel@nongnu.org
Cc: stefanha@redhat.com, "Lukáš Doktor" <ldoktor@redhat.com>
Subject: Re: [PATCH v2 2/3] thread-pool: replace semaphore with condition variable
Date: Fri, 13 May 2022 14:38:21 +0200	[thread overview]
Message-ID: <e95b8018-f729-958c-91ee-5c7ec88d046f@redhat.com> (raw)
In-Reply-To: <dd8c888aac88b0fd70ee5a73cf9dca3406efe606.camel@redhat.com>

On 5/13/22 13:56, Nicolas Saenz Julienne wrote:
>>               pool->idle_threads++;
>> -            qemu_mutex_unlock(&pool->lock);
>> -            ret = qemu_sem_timedwait(&pool->sem, 10000);
>> -            qemu_mutex_lock(&pool->lock);
>> +            ret = qemu_cond_timedwait(&pool->request_cond, &pool-
>>> lock, 10000);
>>               pool->idle_threads--;
>> -        } while (back_to_sleep(pool, ret));
>> -        if (ret == -1 || pool->stopping ||
>
> I think, you need to check for 'pool->stopping' upon exiting wait_cond().
> Otherwise it'll blindly try to dequeue a request from a list that is otherwise
> empty.

Good point, thanks.

>> -    if (elem->state == THREAD_QUEUED &&
>> -        /* No thread has yet started working on elem. we can try to
>> "steal"
>> -         * the item from the worker if we can get a signal from the
>> -         * semaphore.  Because this is non-blocking, we can do it
>> with
>> -         * the lock taken and ensure that elem will remain
>> THREAD_QUEUED.
>> -         */
>> -        qemu_sem_timedwait(&pool->sem, 0) == 0) {
>> +    if (elem->state == THREAD_QUEUED) {
>>           QTAILQ_REMOVE(&pool->request_list, elem, reqs);
>>           qemu_bh_schedule(pool->completion_bh);
> 
> The 'thread-pool cancel' unit test fails.
> 
> I think it's because there is an assumption in worker_thread() that if you get
> woken up, you'll have a pending request. And you're now 'stealing' work
> requests, without 'stealing' a wakeup (what qemu_sem_timedwait(sem, 0) achieved
> in the past).

You don't need to steal a wakeup because cond_wait does not "count", but 
yeah it's essentially the same issue that you mentioned above.

Paolo



  reply	other threads:[~2022-05-13 12:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-12 10:43 [PATCH v2 0/3] thread-pool: fix performance regression Paolo Bonzini
2022-05-12 10:43 ` [PATCH v2 1/3] thread-pool: optimize scheduling of completion bottom half Paolo Bonzini
2022-05-12 10:43 ` [PATCH v2 2/3] thread-pool: replace semaphore with condition variable Paolo Bonzini
2022-05-13 11:56   ` Nicolas Saenz Julienne
2022-05-13 12:38     ` Paolo Bonzini [this message]
2022-05-12 10:43 ` [PATCH v2 3/3] thread-pool: remove stopping variable Paolo Bonzini

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=e95b8018-f729-958c-91ee-5c7ec88d046f@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=ldoktor@redhat.com \
    --cc=nsaenzju@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /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).