From: Denis V. Lunev <[email protected]>

Getting or putting nothing cannot change the pool, yet both calls take
the mutex, and co_put_to_shres() also wakes every waiter to find the
pool unchanged. Return early when n is zero.

block-copy is about to reserve nothing for write-zeroes tasks the target
serves without a buffer, which makes such calls the common case for a
mostly-zero image.

Suggested-by: Vladimir Sementsov-Ogievskiy <[email protected]>
Signed-off-by: Denis V. Lunev <[email protected]>
CC: Vladimir Sementsov-Ogievskiy <[email protected]>
CC: John Snow <[email protected]>
CC: Andrey Drobyshev <[email protected]>
---
 util/qemu-co-shared-resource.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/util/qemu-co-shared-resource.c b/util/qemu-co-shared-resource.c
index 752eb5a1c5..f9b6af0072 100644
--- a/util/qemu-co-shared-resource.c
+++ b/util/qemu-co-shared-resource.c
@@ -69,6 +69,10 @@ static bool co_try_get_from_shres_locked(SharedResource *s, 
uint64_t n)
 void coroutine_fn co_get_from_shres(SharedResource *s, uint64_t n)
 {
     assert(n <= s->total);
+    if (!n) {
+        return;
+    }
+
     QEMU_LOCK_GUARD(&s->lock);
     while (!co_try_get_from_shres_locked(s, n)) {
         qemu_co_queue_wait(&s->queue, &s->lock);
@@ -77,6 +81,10 @@ void coroutine_fn co_get_from_shres(SharedResource *s, 
uint64_t n)
 
 void coroutine_fn co_put_to_shres(SharedResource *s, uint64_t n)
 {
+    if (!n) {
+        return;
+    }
+
     QEMU_LOCK_GUARD(&s->lock);
     assert(s->total - s->available >= n);
     s->available += n;
-- 
2.53.0


Reply via email to