From 5c6ae58d4bdca71f0b63e854ceb6a8ee67cbddd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Date: Mon, 10 May 2021 22:07:57 +0200 Subject: bitops.h: Improve find_xxx_bit() documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Document the following functions return the bitmap size if no matching bit is found: - find_first_bit - find_next_bit - find_last_bit - find_first_zero_bit - find_next_zero_bit Reviewed-by: Richard Henderson Signed-off-by: Philippe Mathieu-Daudé Reviewed-by: Stefan Hajnoczi Message-id: 20210510200758.2623154-2-philmd@redhat.com Signed-off-by: Stefan Hajnoczi --- include/qemu/bitops.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/qemu/bitops.h b/include/qemu/bitops.h index 3acbf3384c..a72f69fea8 100644 --- a/include/qemu/bitops.h +++ b/include/qemu/bitops.h @@ -140,7 +140,8 @@ static inline int test_bit(long nr, const unsigned long *addr) * @addr: The address to start the search at * @size: The maximum size to search * - * Returns the bit number of the first set bit, or size. + * Returns the bit number of the last set bit, + * or @size if there is no set bit in the bitmap. */ unsigned long find_last_bit(const unsigned long *addr, unsigned long size); @@ -150,6 +151,9 @@ unsigned long find_last_bit(const unsigned long *addr, * @addr: The address to base the search on * @offset: The bitnumber to start searching at * @size: The bitmap size in bits + * + * Returns the bit number of the next set bit, + * or @size if there are no further set bits in the bitmap. */ unsigned long find_next_bit(const unsigned long *addr, unsigned long size, @@ -160,6 +164,9 @@ unsigned long find_next_bit(const unsigned long *addr, * @addr: The address to base the search on * @offset: The bitnumber to start searching at * @size: The bitmap size in bits + * + * Returns the bit number of the next cleared bit, + * or @size if there are no further clear bits in the bitmap. */ unsigned long find_next_zero_bit(const unsigned long *addr, @@ -171,7 +178,8 @@ unsigned long find_next_zero_bit(const unsigned long *addr, * @addr: The address to start the search at * @size: The maximum size to search * - * Returns the bit number of the first set bit. + * Returns the bit number of the first set bit, + * or @size if there is no set bit in the bitmap. */ static inline unsigned long find_first_bit(const unsigned long *addr, unsigned long size) @@ -194,7 +202,8 @@ static inline unsigned long find_first_bit(const unsigned long *addr, * @addr: The address to start the search at * @size: The maximum size to search * - * Returns the bit number of the first cleared bit. + * Returns the bit number of the first cleared bit, + * or @size if there is no clear bit in the bitmap. */ static inline unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size) -- cgit v1.2.3 From fb74a286feaa4ec2cdcda61ba570244464581ca7 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Mon, 17 May 2021 12:05:44 +0200 Subject: coroutine-sleep: disallow NULL QemuCoSleepState** argument Simplify the code by removing conditionals. qemu_co_sleep_ns can simply point the argument to an on-stack temporary. Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Paolo Bonzini Message-id: 20210517100548.28806-3-pbonzini@redhat.com Signed-off-by: Stefan Hajnoczi --- include/qemu/coroutine.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index ce5b9c6851..c5d7742989 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -295,7 +295,7 @@ typedef struct QemuCoSleepState QemuCoSleepState; /** * Yield the coroutine for a given duration. During this yield, @sleep_state - * (if not NULL) is set to an opaque pointer, which may be used for + * is set to an opaque pointer, which may be used for * qemu_co_sleep_wake(). Be careful, the pointer is set back to zero when the * timer fires. Don't save the obtained value to other variables and don't call * qemu_co_sleep_wake from another aio context. @@ -304,7 +304,8 @@ void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, QemuCoSleepState **sleep_state); static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) { - qemu_co_sleep_ns_wakeable(type, ns, NULL); + QemuCoSleepState *unused = NULL; + qemu_co_sleep_ns_wakeable(type, ns, &unused); } /** -- cgit v1.2.3 From 29a6ea24eb85d8400df1607a9e11d0ef9ec5e88d Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Mon, 17 May 2021 12:05:47 +0200 Subject: coroutine-sleep: replace QemuCoSleepState pointer with struct in the API Right now, users of qemu_co_sleep_ns_wakeable are simply passing a pointer to QemuCoSleepState by reference to the function. But QemuCoSleepState really is just a Coroutine*; making the content of the struct public is just as efficient and lets us skip the user_state_pointer indirection. Since the usage is changed, take the occasion to rename the struct to QemuCoSleep. Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Paolo Bonzini Message-id: 20210517100548.28806-6-pbonzini@redhat.com Signed-off-by: Stefan Hajnoczi --- include/qemu/coroutine.h | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'include') diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index c5d7742989..82c0671f80 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -291,21 +291,22 @@ void qemu_co_rwlock_wrlock(CoRwlock *lock); */ void qemu_co_rwlock_unlock(CoRwlock *lock); -typedef struct QemuCoSleepState QemuCoSleepState; +typedef struct QemuCoSleep { + Coroutine *to_wake; +} QemuCoSleep; /** - * Yield the coroutine for a given duration. During this yield, @sleep_state - * is set to an opaque pointer, which may be used for - * qemu_co_sleep_wake(). Be careful, the pointer is set back to zero when the - * timer fires. Don't save the obtained value to other variables and don't call - * qemu_co_sleep_wake from another aio context. + * Yield the coroutine for a given duration. Initializes @w so that, + * during this yield, it can be passed to qemu_co_sleep_wake() to + * terminate the sleep. */ -void coroutine_fn qemu_co_sleep_ns_wakeable(QEMUClockType type, int64_t ns, - QemuCoSleepState **sleep_state); +void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, + QEMUClockType type, int64_t ns); + static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) { - QemuCoSleepState *unused = NULL; - qemu_co_sleep_ns_wakeable(type, ns, &unused); + QemuCoSleep w = { 0 }; + qemu_co_sleep_ns_wakeable(&w, type, ns); } /** @@ -314,7 +315,7 @@ static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) * qemu_co_sleep_ns() and should be checked to be non-NULL before calling * qemu_co_sleep_wake(). */ -void qemu_co_sleep_wake(QemuCoSleepState *sleep_state); +void qemu_co_sleep_wake(QemuCoSleep *w); /** * Yield until a file descriptor becomes readable -- cgit v1.2.3 From 0a6f0c76a030710780ce10d6347a70f098024d21 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Mon, 17 May 2021 12:05:48 +0200 Subject: coroutine-sleep: introduce qemu_co_sleep Allow using QemuCoSleep to sleep forever until woken by qemu_co_sleep_wake. This makes the logic of qemu_co_sleep_ns_wakeable easy to understand. In the future we will introduce an API that can work even if the sleep and wake happen from different threads. For now, initializing w->to_wake after timer_mod is fine because the timer can only fire in the same AioContext. Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Paolo Bonzini Message-id: 20210517100548.28806-7-pbonzini@redhat.com Signed-off-by: Stefan Hajnoczi --- include/qemu/coroutine.h | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'include') diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index 82c0671f80..292e61aef0 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -303,6 +303,11 @@ typedef struct QemuCoSleep { void coroutine_fn qemu_co_sleep_ns_wakeable(QemuCoSleep *w, QEMUClockType type, int64_t ns); +/** + * Yield the coroutine until the next call to qemu_co_sleep_wake. + */ +void coroutine_fn qemu_co_sleep(QemuCoSleep *w); + static inline void coroutine_fn qemu_co_sleep_ns(QEMUClockType type, int64_t ns) { QemuCoSleep w = { 0 }; -- cgit v1.2.3