qemu-queue cleanups
cover qemu-queue cleanups
These patches simplify the jungle of lists provided by qemu-queue from 4 to 3. QCIRCLEQ is dropped, since it provides no real advantage over QTAILQ. QSIMPLEQ is simplified to no longer permit insertion at the tail, with the advantage that it is more suited for simple free lists.
Stefan, these are a bit borderline for qemu-trivial. Let me know if they're fine.
Paolo Bonzini (8):
notifier: switch to QLIST
block-migration: switch to QTAILQ
qed: switch to QTAILQ
ccid: switch to QTAILQ
qemu-queue: really simplify QSIMPLEQ
qemu-queue: drop QCIRCLEQ
coroutine: switch to QSIMPLEQ
block: use QSIMPLEQ for the AIO free list
block-migration.c | 44 ++++++------ block.c | 9 +-- block/qed.c | 20 +++--- block/qed.h | 4 +- block_int.h | 4 +- coroutine-ucontext.c | 10 ++-- hw/ccid-card-emulated.c | 24 +++--- input.c | 2 +- migration.c | 2 +- notify.c | 10 ++-- notify.h | 8 +- qemu-coroutine-int.h | 2 +- qemu-queue.h | 175 +++-------------------------------------------- qemu-timer.c | 2 +- vl.c | 2 +- 15 files changed, 81 insertions(+), 237 deletions(-)
Signature · 1 line
1.7.7.1
PATCH 1/8 notifier: switch to QLIST
Notifiers do not need to access both ends of the list, and using a QLIST also simplifies the API.
---
I do plan to exploit the simplified API in a future patch...
input.c | 2 +- migration.c | 2 +- notify.c | 10 +++++----- notify.h | 8 ++++---- qemu-timer.c | 2 +- vl.c | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-)
input.c +1 -1
@@ -268,5 +268,5 @@ void qemu_add_mouse_mode_change_notifier(Notifier *notify)void qemu_remove_mouse_mode_change_notifier (Notifier *notify) { -notifier_list_remove (&mouse_mode_notifiers, notify); +notifier_remove (notify); }
migration.c +1 -1
@@ -333,7 +333,7 @@ void add_migration_state_change_notifier(Notifier *notify)void remove_migration_state_change_notifier (Notifier *notify) { -notifier_list_remove (&migration_state_notifiers, notify); +notifier_remove (notify); }bool migration_is_active (MigrationState *s)
notify.c +5 -5
@@ -16,24 +16,24 @@void notifier_list_init (NotifierList *list) { -QTAILQ_INIT (&list->notifiers); +QLIST_INIT (&list->notifiers); }void notifier_list_add (NotifierList *list,Notifier *notifier) { -QTAILQ_INSERT_HEAD (&list->notifiers, notifier, node); +QLIST_INSERT_HEAD (&list->notifiers, notifier, node); } -void notifier_list_remove (NotifierList *list,Notifier *notifier) +void notifier_remove (Notifier *notifier) { -QTAILQ_REMOVE (&list->notifiers, notifier, node); +QLIST_REMOVE (notifier, node); }void notifier_list_notify (NotifierList *list,void *data) {Notifier *notifier, *next; - QTAILQ_FOREACH_SAFE(notifier, &list->notifiers, node, next) { + QLIST_FOREACH_SAFE(notifier, &list->notifiers, node, next) { notifier->notify (notifier, data); } }
notify.h +4 -4
@@ -21,22 +21,22 @@ typedef struct Notifier Notifier;struct Notifier {void (*notify)(Notifier *notifier,void *data); -QTAILQ_ENTRY (Notifier) node; +QLIST_ENTRY (Notifier) node; };typedef struct NotifierList { - QTAILQ_HEAD(, Notifier) notifiers; + QLIST_HEAD(, Notifier) notifiers; }NotifierList ;#define NOTIFIER_LIST_INITIALIZER (head) \ - { QTAILQ_HEAD_INITIALIZER((head).notifiers) } + { QLIST_HEAD_INITIALIZER((head).notifiers) }void notifier_list_init (NotifierList *list);void notifier_list_add (NotifierList *list,Notifier *notifier); -void notifier_list_remove (NotifierList *list,Notifier *notifier); +void notifier_remove (Notifier *notifier);void notifier_list_notify (NotifierList *list,void *data);
qemu-timer.c +1 -1
@@ -453,7 +453,7 @@ void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)void qemu_unregister_clock_reset_notifier (QEMUClock *clock,Notifier *notifier) { -notifier_list_remove (&clock->reset_notifiers, notifier); +notifier_remove (notifier); }void init_clocks (void )
vl.c +1 -1
@@ -2058,7 +2058,7 @@ void qemu_add_exit_notifier(Notifier *notify)void qemu_remove_exit_notifier (Notifier *notify) { -notifier_list_remove (&exit_notifiers, notify); +notifier_remove (notify); }static void qemu_run_exit_notifiers (void )
Signature · 1 line
1.7.7.1
Re: [Qemu-devel] [PATCH 1/8] notifier: switch to QLIST
notify.c line 27
Quoted 8 lines · Paolo Bonzini
void
notifier_list_add(NotifierList *list, Notifier *notifier){notify.c +2
-2-QTAILQ_INSERT_HEAD (&list->notifiers, notifier, node); +QLIST_INSERT_HEAD (&list->notifiers, notifier, node); } -void notifier_list_remove (NotifierList *list,Notifier *notifier) +void notifier_remove (Notifier *notifier)
Why introduce this asymmetry with notifier_list_add() and notifier_remove()? Please make the function names consistent.
Stefan
Re: [Qemu-devel] [PATCH 1/8] notifier: switch to QLIST
Quoted 7 lines · Stefan Hajnoczi
Quoted 6 lines · Paolo Bonzini
void notifier_list_add(NotifierList *list, Notifier *notifier) {notify.c +1
-1-QTAILQ_INSERT_HEAD (&list->notifiers, notifier, node); +QLIST_INSERT_HEAD (&list->notifiers, notifier, node); }
Quoted 5 lines
notify.c line 27
Quoted 2 lines
notify.c +1
-1-void notifier_list_remove (NotifierList *list,Notifier *notifier) +void notifier_remove (Notifier *notifier)Why introduce this asymmetry with
notifier_list_add()andnotifier_remove()? Please make the function names consistent.
Because notifier_list_add adds the notifier to a specific NotifierList; notifier_remove removes the notifier from whatever list it is in.
Normally whoever implements notifiers does not have access to the NotifierList, so there are wrappers for both notifier_list_add and notifier_list_remove. This patch changes things so that the wrappers for notifier_remove are not needed anymore (though this series was already big enough, so I left the wrappers in).
Paolo
Re: [Qemu-devel] [PATCH 1/8] notifier: switch to QLIST
Quoted 18 lines · Paolo Bonzini
Quoted 9 lines · Stefan Hajnoczi
Quoted 7 lines · Paolo Bonzini
void
notifier_list_add(NotifierList *list, Notifier *notifier){
- QTAILQ_INSERT_HEAD(&list->notifiers, notifier, node);
+ QLIST_INSERT_HEAD(&list->notifiers, notifier, node); }Quoted 6 lines
notify.c line 27
Quoted 2 lines
notify.c +1
-1-void notifier_list_remove (NotifierList *list,Notifier *notifier) +void notifier_remove (Notifier *notifier)Why introduce this asymmetry with
notifier_list_add()andnotifier_remove()? Please make the function names consistent.
Quoted 8 lines
Because
notifier_list_addadds the notifier to a specific NotifierList;notifier_removeremoves the notifier from whatever list it is in.Normally whoever implements notifiers does not have access to the NotifierList, so there are wrappers for both
notifier_list_addandnotifier_list_remove. This patch changes things so that the wrappers fornotifier_removeare not needed anymore (though this series was already big enough, so I left the wrappers in).
I see.
Stefan
PATCH 2/8 block-migration: switch to QTAILQ
In reply to Paolo BonziniBlock migration needs to insert at tail. Use a QTAILQ, even though the double links are strictly not necessary.
--- block-migration.c | 44 ++++++++++++++++++++++---------------------- 1 files changed, 22 insertions(+), 22 deletions(-)
block-migration.c +22 -22
@@ -49,7 +49,7 @@ typedef struct BlkMigDevState {int64_t completed_sectors;int64_t total_sectors;int64_t dirty; -QSIMPLEQ_ENTRY (BlkMigDevState) entry; +QTAILQ_ENTRY (BlkMigDevState) entry;unsigned long *aio_bitmap; } BlkMigDevState;
@@ -62,14 +62,14 @@ typedef struct BlkMigBlock {QEMUIOVector qiov;BlockDriverAIOCB *aiocb;int ret; -QSIMPLEQ_ENTRY (BlkMigBlock) entry; +QTAILQ_ENTRY (BlkMigBlock) entry; } BlkMigBlock; typedef struct BlkMigState { int blk_enable; int shared_base; - QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list; - QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list; + QTAILQ_HEAD(bmds_list, BlkMigDevState) bmds_list; + QTAILQ_HEAD(blk_list, BlkMigBlock) blk_list; int submitted; int read_done; int transferred;
@@ -101,7 +101,7 @@ static void blk_send(QEMUFile *f, BlkMigBlock * blk)int blk_mig_active (void ) { -return !QSIMPLEQ_EMPTY (&block_mig_state.bmds_list); +return !QTAILQ_EMPTY (&block_mig_state.bmds_list); }uint64_t blk_mig_bytes_transferred (void )
@@ -109,7 +109,7 @@ uint64_t blk_mig_bytes_transferred(void)BlkMigDevState *bmds;uint64_t sum =0 ; - QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { + QTAILQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { sum += bmds->completed_sectors; }return sum <<BDRV_SECTOR_BITS ;
@@ -125,7 +125,7 @@ uint64_t blk_mig_bytes_total(void)BlkMigDevState *bmds;uint64_t sum =0 ; - QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { + QTAILQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { sum += bmds->total_sectors; }return sum <<BDRV_SECTOR_BITS ;
@@ -194,7 +194,7 @@ static void blk_mig_read_cb(void *opaque, int ret) block_mig_state.total_time += (curr_time - block_mig_state.prev_time_offset); block_mig_state.prev_time_offset = curr_time; -QSIMPLEQ_INSERT_TAIL (&block_mig_state.blk_list, blk, entry); +QTAILQ_INSERT_TAIL (&block_mig_state.blk_list, blk, entry);bmds_set_aio_inflight (blk->bmds, blk->sector, blk->nr_sectors,0 ); block_mig_state.submitted--;
@@ -263,7 +263,7 @@ static void set_dirty_tracking(int enable) {BlkMigDevState *bmds; - QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { + QTAILQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {bdrv_set_dirty_tracking (bmds->bs, enable); } }
@@ -301,7 +301,7 @@ static void init_blk_migration_it(void *opaque, BlockDriverState *bs) bs->device_name); } -QSIMPLEQ_INSERT_TAIL (&block_mig_state.bmds_list, bmds, entry); +QTAILQ_INSERT_TAIL (&block_mig_state.bmds_list, bmds, entry); } }
@@ -326,7 +326,7 @@ static int blk_mig_save_bulked_block(Monitor *mon, QEMUFile *f)int progress;int ret =0 ; - QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { + QTAILQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {if (bmds->bulk_completed ==0 ) {if (mig_save_device_bulk (mon, f, bmds) ==1 ) {/* completed bulk section for this device */
@@ -361,7 +361,7 @@ static void blk_mig_reset_dirty_cursor(void) {BlkMigDevState *bmds; - QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { + QTAILQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { bmds->cur_dirty =0 ; } }
@@ -438,7 +438,7 @@ static int blk_mig_save_dirty_block(Monitor *mon, QEMUFile *f, int is_async)BlkMigDevState *bmds;int ret =0 ; - QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { + QTAILQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {if (mig_save_device_dirty (mon, f, bmds, is_async) ==0 ) { ret =1 ;break ;
@@ -456,7 +456,7 @@ static void flush_blks(QEMUFile* f) __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done, block_mig_state.transferred); -while ((blk =QSIMPLEQ_FIRST (&block_mig_state.blk_list)) !=NULL ) { +while ((blk =QTAILQ_FIRST (&block_mig_state.blk_list)) !=NULL ) {if (qemu_file_rate_limit (f)) {break ; }
@@ -466,7 +466,7 @@ static void flush_blks(QEMUFile* f) }blk_send (f, blk); -QSIMPLEQ_REMOVE_HEAD (&block_mig_state.blk_list, entry); +QTAILQ_REMOVE (&block_mig_state.blk_list, blk, entry);g_free (blk->buf);g_free (blk);
@@ -485,7 +485,7 @@ static int64_t get_remaining_dirty(void)BlkMigDevState *bmds;int64_t dirty =0 ; - QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { + QTAILQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) { dirty +=bdrv_get_dirty_count (bmds->bs); }
@@ -525,16 +525,16 @@ static void blk_mig_cleanup(Monitor *mon)set_dirty_tracking (0 ); -while ((bmds =QSIMPLEQ_FIRST (&block_mig_state.bmds_list)) !=NULL ) { -QSIMPLEQ_REMOVE_HEAD (&block_mig_state.bmds_list, entry); +while ((bmds =QTAILQ_FIRST (&block_mig_state.bmds_list)) !=NULL ) { +QTAILQ_REMOVE (&block_mig_state.bmds_list, bmds, entry);bdrv_set_in_use (bmds->bs,0 );drive_put_ref (drive_get_by_blockdev (bmds->bs));g_free (bmds->aio_bitmap);g_free (bmds); } -while ((blk =QSIMPLEQ_FIRST (&block_mig_state.blk_list)) !=NULL ) { -QSIMPLEQ_REMOVE_HEAD (&block_mig_state.blk_list, entry); +while ((blk =QTAILQ_FIRST (&block_mig_state.blk_list)) !=NULL ) { +QTAILQ_REMOVE (&block_mig_state.blk_list, blk, entry);g_free (blk->buf);g_free (blk); }
@@ -717,8 +717,8 @@ static void block_set_params(int blk_enable, int shared_base, void *opaque)void blk_mig_init (void ) { -QSIMPLEQ_INIT (&block_mig_state.bmds_list); -QSIMPLEQ_INIT (&block_mig_state.blk_list); +QTAILQ_INIT (&block_mig_state.bmds_list); +QTAILQ_INIT (&block_mig_state.blk_list);register_savevm_live (NULL ,"block" ,0 ,1 , block_set_params, block_save_live,NULL , block_load, &block_mig_state);
Signature · 1 line
1.7.7.1
PATCH 3/8 qed: switch to QTAILQ
In reply to Paolo BonziniQED needs to insert at tail. Use a QTAILQ, even though the double links are strictly not necessary.
--- block/qed.c | 20 ++++++++++---------- block/qed.h | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-)
block/qed.c +10 -10
@@ -300,7 +300,7 @@ static void qed_unplug_allocating_write_reqs(BDRVQEDState *s) s->allocating_write_reqs_plugged = false; - acb =QSIMPLEQ_FIRST (&s->allocating_write_reqs); + acb =QTAILQ_FIRST (&s->allocating_write_reqs);if (acb) {qed_aio_next_io (acb,0 ); }
@@ -339,7 +339,7 @@ static void qed_need_check_timer_cb(void *opaque)BDRVQEDState *s = opaque;/* The timer should only fire when allocating writes have drained */ -assert (!QSIMPLEQ_FIRST (&s->allocating_write_reqs)); +assert (!QTAILQ_FIRST (&s->allocating_write_reqs));trace_qed_need_check_timer_cb (s);
@@ -375,7 +375,7 @@ static int bdrv_qed_open(BlockDriverState *bs, int flags)int ret; s->bs = bs; -QSIMPLEQ_INIT (&s->allocating_write_reqs); +QTAILQ_INIT (&s->allocating_write_reqs); ret =bdrv_pread (bs->file,0 , &le_header,sizeof (le_header));if (ret <0 ) {
@@ -886,9 +886,9 @@ static void qed_aio_complete(QEDAIOCB *acb, int ret)* next request in the queue. This ensures that we don't cycle through * requests multiple times but rather finish one at a time completely. */ -if (acb ==QSIMPLEQ_FIRST (&s->allocating_write_reqs)) { -QSIMPLEQ_REMOVE_HEAD (&s->allocating_write_reqs, next); - acb =QSIMPLEQ_FIRST (&s->allocating_write_reqs); +if (acb ==QTAILQ_FIRST (&s->allocating_write_reqs)) { +QTAILQ_REMOVE (&s->allocating_write_reqs, acb, next); + acb =QTAILQ_FIRST (&s->allocating_write_reqs);if (acb) {qed_aio_next_io (acb,0 ); }else if (s->header.features &QED_F_NEED_CHECK ) {
@@ -1094,15 +1094,15 @@ static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len)BDRVQEDState *s =acb_to_s (acb);/* Cancel timer when the first allocating request comes in */ -if (QSIMPLEQ_EMPTY (&s->allocating_write_reqs)) { +if (QTAILQ_EMPTY (&s->allocating_write_reqs)) {qed_cancel_need_check_timer (s); }/* Freeze this request if another allocating write is in progress */ -if (acb !=QSIMPLEQ_FIRST (&s->allocating_write_reqs)) { -QSIMPLEQ_INSERT_TAIL (&s->allocating_write_reqs, acb, next); +if (acb !=QTAILQ_FIRST (&s->allocating_write_reqs)) { +QTAILQ_INSERT_TAIL (&s->allocating_write_reqs, acb, next); } -if (acb !=QSIMPLEQ_FIRST (&s->allocating_write_reqs) || +if (acb !=QTAILQ_FIRST (&s->allocating_write_reqs) || s->allocating_write_reqs_plugged) {return ;/* wait for existing request to finish */ }
block/qed.h +2 -2
@@ -127,7 +127,7 @@ typedef struct QEDAIOCB {BlockDriverAIOCB common;QEMUBH *bh;int bh_ret;/* final return status for completion bh */ -QSIMPLEQ_ENTRY (QEDAIOCB) next;/* next request */ +QTAILQ_ENTRY (QEDAIOCB) next;/* next request */ bool is_write;/* false - read, true - write */ bool *finished;/* signal for cancel completion */ uint64_t end_pos;/* request end on block device, in bytes */
@@ -159,7 +159,7 @@ typedef struct {uint32_t l2_mask;/* Allocating write request queue */ - QSIMPLEQ_HEAD(, QEDAIOCB) allocating_write_reqs; + QTAILQ_HEAD(, QEDAIOCB) allocating_write_reqs;bool allocating_write_reqs_plugged;/* Periodic flush and clear need check flag */
Signature · 1 line
1.7.7.1
PATCH 4/8 ccid: switch to QTAILQ
In reply to Paolo BonziniCCID emulation needs to insert at tail. Use a QTAILQ, even though the double links are strictly not necessary.
--- hw/ccid-card-emulated.c | 24 ++++++++++++------------ 1 files changed, 12 insertions(+), 12 deletions(-)
hw/ccid-card-emulated.c +12 -12
@@ -89,7 +89,7 @@ static const char *emul_event_to_string(uint32_t emul_event) } typedef struct EmulEvent { - QSIMPLEQ_ENTRY(EmulEvent) entry; + QTAILQ_ENTRY(EmulEvent) entry; union { struct { uint32_t type;
@@ -118,11 +118,11 @@ struct EmulatedState {char *db;uint8_t atr[MAX_ATR_SIZE ];uint8_t atr_length; - QSIMPLEQ_HEAD(event_list, EmulEvent) event_list; + QTAILQ_HEAD(event_list, EmulEvent) event_list;QemuMutex event_list_mutex;QemuThread event_thread_id;VReader *reader; - QSIMPLEQ_HEAD(guest_apdu_list, EmulEvent) guest_apdu_list; + QTAILQ_HEAD(guest_apdu_list, EmulEvent) guest_apdu_list;QemuMutex vreader_mutex;/* and guest_apdu_list mutex */ QemuMutex handle_apdu_mutex;QemuCond handle_apdu_cond;
@@ -142,7 +142,7 @@ static void emulated_apdu_from_guest(CCIDCardState *base, event->p.data.len = len;memcpy (event->p.data.data, apdu, len);qemu_mutex_lock (&card->vreader_mutex); -QSIMPLEQ_INSERT_TAIL (&card->guest_apdu_list, event, entry); +QTAILQ_INSERT_TAIL (&card->guest_apdu_list, event, entry);qemu_mutex_unlock (&card->vreader_mutex);qemu_mutex_lock (&card->handle_apdu_mutex);qemu_cond_signal (&card->handle_apdu_cond);
@@ -160,7 +160,7 @@ static const uint8_t *emulated_get_atr(CCIDCardState *base, uint32_t *len)static void emulated_push_event (EmulatedState *card,EmulEvent *event) {qemu_mutex_lock (&card->event_list_mutex); -QSIMPLEQ_INSERT_TAIL (&(card->event_list), event, entry); +QTAILQ_INSERT_TAIL (&(card->event_list), event, entry);qemu_mutex_unlock (&card->event_list_mutex);if (write (card->pipe[1 ], card,1 ) !=1 ) {DPRINTF (card,1 ,"write to pipe failed\n" );
@@ -243,10 +243,10 @@ static void *handle_apdu_thread(void* arg)break ; }qemu_mutex_lock (&card->vreader_mutex); -while (!QSIMPLEQ_EMPTY (&card->guest_apdu_list)) { - event =QSIMPLEQ_FIRST (&card->guest_apdu_list); +while (!QTAILQ_EMPTY (&card->guest_apdu_list)) { + event =QTAILQ_FIRST (&card->guest_apdu_list);assert ((unsigned long )event >1000 ); -QSIMPLEQ_REMOVE_HEAD (&card->guest_apdu_list, entry); +QTAILQ_REMOVE (&card->guest_apdu_list, event, entry);if (event->p.data.type !=EMUL_GUEST_APDU ) {DPRINTF (card,1 ,"unexpected message in handle_apdu_thread\n" );g_free (event);
@@ -369,7 +369,7 @@ static void pipe_read(void *opaque) len =read (card->pipe[0 ], &dummy,sizeof (dummy)); }while (len ==sizeof (dummy));qemu_mutex_lock (&card->event_list_mutex); - QSIMPLEQ_FOREACH_SAFE(event, &card->event_list, entry, next) { + QTAILQ_FOREACH_SAFE(event, &card->event_list, entry, next) {DPRINTF (card,2 ,"event %s\n" ,emul_event_to_string (event->p.gen.type));switch (event->p.gen.type) {case EMUL_RESPONSE_APDU :
@@ -400,7 +400,7 @@ static void pipe_read(void *opaque) }g_free (event); } -QSIMPLEQ_INIT (&card->event_list); +QTAILQ_INIT (&card->event_list);qemu_mutex_unlock (&card->event_list_mutex); }
@@ -489,8 +489,8 @@ static int emulated_initfn(CCIDCardState *base)VCardEmulError ret;EnumTable *ptable; -QSIMPLEQ_INIT (&card->event_list); -QSIMPLEQ_INIT (&card->guest_apdu_list); +QTAILQ_INIT (&card->event_list); +QTAILQ_INIT (&card->guest_apdu_list);qemu_mutex_init (&card->event_list_mutex);qemu_mutex_init (&card->vreader_mutex);qemu_mutex_init (&card->handle_apdu_mutex);
Signature · 1 line
1.7.7.1
PATCH 5/8 qemu-queue: really simplify QSIMPLEQ
In reply to Paolo BonziniQSIMPLEQ is still relatively heavyweight when used as a free list, compared to a simple singly-linked list. One disadvantage is that it requires an initializer macro, unlike for example QLIST.
The patch removes the double links so that there is a "really really simple" queue type.
--- qemu-queue.h | 57 +++++++++------------------------------------------------ 1 files changed, 9 insertions(+), 48 deletions(-)
qemu-queue.h +9 -48
@@ -51,12 +51,11 @@* or after an existing element or at the head of the list. A list * may only be traversed in the forward direction. * -* A simple queue is headed by a pair of pointers, one the head of the -* list and the other to the tail of the list. The elements are singly -* linked to save space, so elements can only be removed from the -* head of the list. New elements can be added to the list after -* an existing element, at the head of the list, or at the end of the -* list. A simple queue may only be traversed in the forward direction. +* A simple queue is headed by a single forward pointer, and the elements +* are also singly linked to save space. Elements can only be removed +* from the head of the list. New elements can be added to the list after +* an existing element or at the head of the list. A simple queue may +* only be traversed in the forward direction. * * A tail queue is headed by a pair of pointers, one to the head of the * list and the other to the tail of the list. The elements are doubly
@@ -166,11 +165,10 @@ struct { \#define QSIMPLEQ_HEAD (name, type) \ struct name { \ struct type *sqh_first;/* first element */ \ -struct type **sqh_last;/* addr of last next element */ \ }#define QSIMPLEQ_HEAD_INITIALIZER (head) \ - { NULL, &(head).sqh_first } + { NULL }#define QSIMPLEQ_ENTRY (type) \ struct { \
@@ -182,43 +180,20 @@ struct { \*/ #define QSIMPLEQ_INIT(head) do { \ (head)->sqh_first = NULL; \ - (head)->sqh_last = &(head)->sqh_first; \ } while (/*CONSTCOND*/0) #define QSIMPLEQ_INSERT_HEAD(head, elm, field) do { \ - if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \ - (head)->sqh_last = &(elm)->field.sqe_next; \ + (elm)->field.sqe_next = (head)->sqh_first; \ (head)->sqh_first = (elm); \ } while (/*CONSTCOND*/0) -#define QSIMPLEQ_INSERT_TAIL(head, elm, field) do { \ - (elm)->field.sqe_next = NULL; \ - *(head)->sqh_last = (elm); \ - (head)->sqh_last = &(elm)->field.sqe_next; \ -} while (/*CONSTCOND*/0) - #define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ - if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL) \ - (head)->sqh_last = &(elm)->field.sqe_next; \ + (elm)->field.sqe_next = (listelm)->field.sqe_next; \ (listelm)->field.sqe_next = (elm); \ } while (/*CONSTCOND*/0) #define QSIMPLEQ_REMOVE_HEAD(head, field) do { \ - if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\ - (head)->sqh_last = &(head)->sqh_first; \ -} while (/*CONSTCOND*/0) - -#define QSIMPLEQ_REMOVE(head, elm, type, field) do { \ - if ((head)->sqh_first == (elm)) { \ - QSIMPLEQ_REMOVE_HEAD((head), field); \ - } else { \ - struct type *curelm = (head)->sqh_first; \ - while (curelm->field.sqe_next != (elm)) \ - curelm = curelm->field.sqe_next; \ - if ((curelm->field.sqe_next = \ - curelm->field.sqe_next->field.sqe_next) == NULL) \ - (head)->sqh_last = &(curelm)->field.sqe_next; \ - } \ + (head)->sqh_first = (head)->sqh_first->field.sqe_next; \ } while (/*CONSTCOND*/0)#define QSIMPLEQ_FOREACH (var, head, field) \
@@ -231,20 +206,6 @@ struct { \ (var) && ((next = ((var)->field.sqe_next)),1 ); \ (var) = (next)) -#define QSIMPLEQ_CONCAT(head1, head2) do { \ - if (!QSIMPLEQ_EMPTY((head2))) { \ - *(head1)->sqh_last = (head2)->sqh_first; \ - (head1)->sqh_last = (head2)->sqh_last; \ - QSIMPLEQ_INIT((head2)); \ - } \ -} while (/*CONSTCOND*/0) - -#define QSIMPLEQ_LAST (head, type, field) \ - (QSIMPLEQ_EMPTY((head)) ? \ - NULL : \ - ((struct type *)(void *) \ - ((char *)((head)->sqh_last) - offsetof(struct type, field)))) -/* * Simple queue access methods. */
Signature · 1 line
1.7.7.1
Re: [Qemu-devel] [Qemu-trivial] [PATCH 5/8] qemu-queue: really simplify QSIMPLEQ
Quoted 3 lines · Paolo Bonzini
QSIMPLEQ is still relatively heavyweight when used as a free list, compared to a simple singly-linked list. One disadvantage is that it requires an initializer macro, unlike for example QLIST.
<pedantic>QSIMPLEQ is no longer a queue if you cannot easily use it in a FIFO manner. A queue can efficiently push elements on one end and pop them from the other end.</pedantic>
Stefan
Re: [Qemu-devel] [PATCH 5/8] qemu-queue: really simplify QSIMPLEQ
In reply to Paolo BonziniQuoted 6 lines · Paolo Bonzini
QSIMPLEQ is still relatively heavyweight when used as a free list, compared to a simple singly-linked list. One disadvantage is that it requires an initializer macro, unlike for example QLIST.
The patch removes the double links so that there is a "really really simple" queue type.
I don't think we should have macros that follow the standard BSD queue.h naming pattern but don't work the same way... And as Stefan says, without the INSERT_TAIL operation it isn't actually a queue any more.
If you actually want a singly linked list I think you should probably pull in the SLIST macros from BSD queue.h.
(http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/sys/queue.h?rev=1.53)
I think we'll then have the full set of data structures back again (having added back simple-queues in commit c616bbe1).
-- PMM
PATCH 6/8 qemu-queue: drop QCIRCLEQ
In reply to Paolo BonziniThe main advantage of circular lists (the fact that the head node has the same memory layout as any other node, so you do not need conditionals) is completely negated by the implementation in qemu-queue.h. Not surprisingly, nobody uses QCIRCLEQ. While this might change if RCU is ever adopted by QEMU, the QLIST is also RCU-friendly and in fact it is used in a RCU-like manner by 9pfs already. So, just kill QCIRCLEQ.
--- qemu-queue.h | 118 +--------------------------------------------------------- 1 files changed, 1 insertions(+), 117 deletions(-)
qemu-queue.h +1 -117
@@ -42,7 +42,7 @@/* * This file defines four types of data structures: -* lists, simple queues, tail queues, and circular queues. +* lists, simple queues, tail queues. * * A list is headed by a single forward pointer (or an array of forward * pointers for a hash table header). The elements are doubly linked
@@ -64,14 +64,6 @@* after an existing element, at the head of the list, or at the end of * the list. A tail queue may be traversed in either direction. * -* A circle queue is headed by a pair of pointers, one to the head of the -* list and the other to the tail of the list. The elements are doubly -* linked so that an arbitrary element can be removed without a need to -* traverse the list. New elements can be added to the list before or after -* an existing element, at the head of the list, or at the end of the list. -* A circle queue may be traversed in either direction, but has a more -* complex end of list detection. -* * For details on the use of these macros, see the queue(3) manual page. */
@@ -312,112 +304,4 @@ struct { \#define QTAILQ_PREV (elm, headname, field) \ (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) - -/* -* Circular queue definitions. -*/ -#define QCIRCLEQ_HEAD (name, type) \ -struct name { \ - struct type *cqh_first;/* first element */ \ -struct type *cqh_last;/* last element */ \ -} - -#define QCIRCLEQ_HEAD_INITIALIZER (head) \ - { (void *)&head, (void *)&head } - -#define QCIRCLEQ_ENTRY (type) \ -struct { \ - struct type *cqe_next;/* next element */ \ - struct type *cqe_prev; /* previous element */ \ -} - -/* -* Circular queue functions. -*/ -#define QCIRCLEQ_INIT(head) do { \ - (head)->cqh_first = (void *)(head); \ - (head)->cqh_last = (void *)(head); \ -} while (/*CONSTCOND*/0) - -#define QCIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ - (elm)->field.cqe_next = (listelm)->field.cqe_next; \ - (elm)->field.cqe_prev = (listelm); \ - if ((listelm)->field.cqe_next == (void *)(head)) \ - (head)->cqh_last = (elm); \ - else \ - (listelm)->field.cqe_next->field.cqe_prev = (elm); \ - (listelm)->field.cqe_next = (elm); \ -} while (/*CONSTCOND*/0) - -#define QCIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \ - (elm)->field.cqe_next = (listelm); \ - (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ - if ((listelm)->field.cqe_prev == (void *)(head)) \ - (head)->cqh_first = (elm); \ - else \ - (listelm)->field.cqe_prev->field.cqe_next = (elm); \ - (listelm)->field.cqe_prev = (elm); \ -} while (/*CONSTCOND*/0) - -#define QCIRCLEQ_INSERT_HEAD(head, elm, field) do { \ - (elm)->field.cqe_next = (head)->cqh_first; \ - (elm)->field.cqe_prev = (void *)(head); \ - if ((head)->cqh_last == (void *)(head)) \ - (head)->cqh_last = (elm); \ - else \ - (head)->cqh_first->field.cqe_prev = (elm); \ - (head)->cqh_first = (elm); \ -} while (/*CONSTCOND*/0) - -#define QCIRCLEQ_INSERT_TAIL(head, elm, field) do { \ - (elm)->field.cqe_next = (void *)(head); \ - (elm)->field.cqe_prev = (head)->cqh_last; \ - if ((head)->cqh_first == (void *)(head)) \ - (head)->cqh_first = (elm); \ - else \ - (head)->cqh_last->field.cqe_next = (elm); \ - (head)->cqh_last = (elm); \ -} while (/*CONSTCOND*/0) - -#define QCIRCLEQ_REMOVE(head, elm, field) do { \ - if ((elm)->field.cqe_next == (void *)(head)) \ - (head)->cqh_last = (elm)->field.cqe_prev; \ - else \ - (elm)->field.cqe_next->field.cqe_prev = \ - (elm)->field.cqe_prev; \ - if ((elm)->field.cqe_prev == (void *)(head)) \ - (head)->cqh_first = (elm)->field.cqe_next; \ - else \ - (elm)->field.cqe_prev->field.cqe_next = \ - (elm)->field.cqe_next; \ -} while (/*CONSTCOND*/0) - -#define QCIRCLEQ_FOREACH (var, head, field) \ - for ((var) = ((head)->cqh_first); \ - (var) != (const void *)(head); \ - (var) = ((var)->field.cqe_next)) - -#define QCIRCLEQ_FOREACH_REVERSE (var, head, field) \ - for ((var) = ((head)->cqh_last); \ - (var) != (const void *)(head); \ - (var) = ((var)->field.cqe_prev)) - -/* -* Circular queue access methods. -*/ -#define QCIRCLEQ_EMPTY (head) ((head)->cqh_first == (void *)(head)) -#define QCIRCLEQ_FIRST (head) ((head)->cqh_first) -#define QCIRCLEQ_LAST (head) ((head)->cqh_last) -#define QCIRCLEQ_NEXT (elm, field) ((elm)->field.cqe_next) -#define QCIRCLEQ_PREV (elm, field) ((elm)->field.cqe_prev) - -#define QCIRCLEQ_LOOP_NEXT (head, elm, field) \ - (((elm)->field.cqe_next == (void *)(head)) \ - ? ((head)->cqh_first) \ - : (elm->field.cqe_next)) -#define QCIRCLEQ_LOOP_PREV (head, elm, field) \ - (((elm)->field.cqe_prev == (void *)(head)) \ - ? ((head)->cqh_last) \ - : (elm->field.cqe_prev)) -#endif /* !QEMU_SYS_QUEUE_H_ */
Signature · 1 line
1.7.7.1
PATCH 7/8 coroutine: switch to QSIMPLEQ
In reply to Paolo BonziniQSIMPLEQ is now the best candidate for a free list, use it.
--- coroutine-ucontext.c | 10 +++++----- qemu-coroutine-int.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-)
coroutine-ucontext.c +5 -5
@@ -36,7 +36,7 @@ enum { };/** Free list to speed up creation */ -static QLIST_HEAD(, Coroutine) pool = QLIST_HEAD_INITIALIZER(pool); +static QSIMPLEQ_HEAD(, Coroutine) pool = QSIMPLEQ_HEAD_INITIALIZER(pool);static unsigned int pool_size; typedef struct {
@@ -92,7 +92,7 @@ static void __attribute__((destructor)) coroutine_cleanup(void)Coroutine *co;Coroutine *tmp; - QLIST_FOREACH_SAFE(co, &pool, pool_next, tmp) { + QSIMPLEQ_FOREACH_SAFE(co, &pool, pool_next, tmp) {g_free (DO_UPCAST (CoroutineUContext, base, co)->stack);g_free (co); }
@@ -175,9 +175,9 @@ Coroutine *qemu_coroutine_new(void) {Coroutine *co; - co =QLIST_FIRST (&pool); + co =QSIMPLEQ_FIRST (&pool);if (co) { -QLIST_REMOVE (co, pool_next); +QSIMPLEQ_REMOVE_HEAD (&pool, pool_next); pool_size--; }else { co =coroutine_new ();
@@ -190,7 +190,7 @@ void qemu_coroutine_delete(Coroutine *co_)CoroutineUContext *co =DO_UPCAST (CoroutineUContext, base, co_);if (pool_size <POOL_MAX_SIZE ) { -QLIST_INSERT_HEAD (&pool, &co->base, pool_next); +QSIMPLEQ_INSERT_HEAD (&pool, &co->base, pool_next); co->base.caller =NULL ; pool_size++;return ;
qemu-coroutine-int.h +1 -1
@@ -37,7 +37,7 @@ struct Coroutine {CoroutineEntry *entry;void *entry_arg;Coroutine *caller; -QLIST_ENTRY (Coroutine) pool_next; +QSIMPLEQ_ENTRY (Coroutine) pool_next;QTAILQ_ENTRY (Coroutine) co_queue_next; };
Signature · 1 line
1.7.7.1
PATCH 8/8 block: use QSIMPLEQ for the AIO free list
In reply to Paolo BonziniQSIMPLEQ is equivalent to an open-coded free list, use it.
--- block.c | 9 ++++----- block_int.h | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-)
block.c +4 -5
@@ -3242,9 +3242,9 @@ void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, {BlockDriverAIOCB *acb; -if (pool->free_aiocb) { - acb = pool->free_aiocb; - pool->free_aiocb = acb->next; +if (!QSIMPLEQ_EMPTY (pool->free_aiocb)) { + acb =QSIMPLEQ_FIRST (pool->free_aiocb); +QSIMPLEQ_REMOVE_HEAD (pool->free_aiocb, next); }else { acb =g_malloc0 (pool->aiocb_size); acb->pool = pool;
@@ -3259,8 +3259,7 @@ void qemu_aio_release(void *p) {BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;AIOPool *pool = acb->pool; - acb->next = pool->free_aiocb; - pool->free_aiocb = acb; +QSIMPLEQ_INSERT_HEAD (pool->free_aiocb, acb, next); }/**************************************************************/
block_int.h +2 -2
@@ -56,7 +56,7 @@ typedef struct BdrvTrackedRequest BdrvTrackedRequest;typedef struct AIOPool {void (*cancel)(BlockDriverAIOCB *acb);int aiocb_size; -BlockDriverAIOCB *free_aiocb; + QSIMPLEQ_HEAD(, BlockDriverAIOCB) *free_aiocb; }AIOPool ; typedef struct BlockIOLimit {
@@ -268,7 +268,7 @@ struct BlockDriverAIOCB {BlockDriverState *bs;BlockDriverCompletionFunc *cb;void *opaque; -BlockDriverAIOCB *next; +QSIMPLEQ_ENTRY (BlockDriverAIOCB) next; };void get_tmp_filename (char *filename,int size);
Signature · 1 line
1.7.7.1
Quoted 7 lines · Paolo Bonzini
These patches simplify the jungle of lists provided by qemu-queue from 4 to 3. QCIRCLEQ is dropped, since it provides no real advantage over QTAILQ. QSIMPLEQ is simplified to no longer permit insertion at the tail, with the advantage that it is more suited for simple free lists.
Stefan, these are a bit borderline for qemu-trivial. Let me know if they're fine.
Please have them merged by a qemu.git committer.
The changes seem fine. No list implementation is perfect for all use cases and I see why you want the new QSIMPLEQ.
Quoted 18 lines · Paolo Bonzini
These patches simplify the jungle of lists provided by qemu-queue from 4 to 3. QCIRCLEQ is dropped, since it provides no real advantage over QTAILQ. QSIMPLEQ is simplified to no longer permit insertion at the tail, with the advantage that it is more suited for simple free lists.
Stefan, these are a bit borderline for qemu-trivial. Let me know if they're fine.
Paolo Bonzini (8):
notifier: switch to QLIST
block-migration: switch to QTAILQ
qed: switch to QTAILQ
ccid: switch to QTAILQ
qemu-queue: really simplify QSIMPLEQ
qemu-queue: drop QCIRCLEQ
coroutine: switch to QSIMPLEQ
block: use QSIMPLEQ for the AIO free list
Quoted 17 lines
block-migration.c | 44 ++++++------ block.c | 9 +-- block/qed.c | 20 +++--- block/qed.h | 4 +- block_int.h | 4 +- coroutine-ucontext.c | 10 ++-- hw/ccid-card-emulated.c | 24 +++--- input.c | 2 +- migration.c | 2 +- notify.c | 10 ++-- notify.h | 8 +- qemu-coroutine-int.h | 2 +- qemu-queue.h | 175 +++-------------------------------------------- qemu-timer.c | 2 +- vl.c | 2 +- 15 files changed, 81 insertions(+), 237 deletions(-)
Ping; Stefan asked to get these in through a qemu.git committer rather than qemu-trivial.
Paolo