[Spice-devel] [PATCH v2] RedChannel: Add FOREACH_CHANNEL and use it to iterate

Frediano Ziglio fziglio at redhat.com
Wed May 25 11:09:24 UTC 2016


Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/red-channel.c | 40 ++++++++++++++++++----------------------
 1 file changed, 18 insertions(+), 22 deletions(-)

Changes since v1:
- fix a typo in subject;
- use a single macro that is safe instead of two.

diff --git a/server/red-channel.c b/server/red-channel.c
index 661001a..f45dc2e 100644
--- a/server/red-channel.c
+++ b/server/red-channel.c
@@ -59,6 +59,13 @@ typedef struct MarkerPipeItem {
 
 #define CHANNEL_BLOCKED_SLEEP_DURATION 10000 //micro
 
+#define FOREACH_CHANNEL(client, _link, _next, _data)                   \
+    for (_link = (client ? (client)->channels : NULL); \
+         (_data = (_link ? _link->data : NULL), \
+         _next = (_link ? _link->next : NULL), \
+         _link) != NULL; \
+         _link = _next)
+
 enum QosPingState {
     PING_STATE_NONE,
     PING_STATE_TIMER,
@@ -2052,14 +2059,16 @@ static gboolean red_channel_client_set_migration_seamless(RedChannelClient *rcc)
 
 void red_client_set_migration_seamless(RedClient *client) // dest
 {
-    GList *link;
+    GList *link, *next;
+    RedChannelClient *rcc;
+
     spice_assert(client->during_target_migrate);
     pthread_mutex_lock(&client->lock);
     client->seamless_migrate = TRUE;
     /* update channel clients that got connected before the migration
      * type was set. red_client_add_channel will handle newer channel clients */
-    for (link = client->channels; link != NULL; link = link->next) {
-        if (red_channel_client_set_migration_seamless(link->data))
+    FOREACH_CHANNEL(client, link, next, rcc) {
+        if (red_channel_client_set_migration_seamless(rcc))
             client->num_migrated_channels++;
     }
     pthread_mutex_unlock(&client->lock);
@@ -2077,14 +2086,10 @@ void red_client_migrate(RedClient *client)
                       " this might be a BUG",
                       client->thread_id, pthread_self());
     }
-    link = client->channels;
-    while (link) {
-        next = link->next;
-        rcc = link->data;
+    FOREACH_CHANNEL(client, link, next, rcc) {
         if (red_channel_client_is_connected(rcc)) {
             rcc->channel->client_cbs.migrate(rcc);
         }
-        link = next;
     }
 }
 
@@ -2101,12 +2106,9 @@ void red_client_destroy(RedClient *client)
                       client->thread_id,
                       pthread_self());
     }
-    link = client->channels;
-    while (link) {
-        next = link->next;
+    FOREACH_CHANNEL(client, link, next, rcc) {
         // some channels may be in other threads, so disconnection
         // is not synchronous.
-        rcc = link->data;
         rcc->destroying = 1;
         // some channels may be in other threads. However we currently
         // assume disconnect is synchronous (we changed the dispatcher
@@ -2118,7 +2120,6 @@ void red_client_destroy(RedClient *client)
         spice_assert(rcc->pipe_size == 0);
         spice_assert(rcc->send_data.size == 0);
         red_channel_client_destroy(rcc);
-        link = next;
     }
     red_client_unref(client);
 }
@@ -2126,12 +2127,11 @@ void red_client_destroy(RedClient *client)
 /* client->lock should be locked */
 static RedChannelClient *red_client_get_channel(RedClient *client, int type, int id)
 {
-    GList *link;
+    GList *link, *next;
     RedChannelClient *rcc;
     RedChannelClient *ret = NULL;
 
-    for (link = client->channels; link != NULL; link = link->next) {
-        rcc = link->data;
+    FOREACH_CHANNEL(client, link, next, rcc) {
         if (rcc->channel->type == type && rcc->channel->id == id) {
             ret = rcc;
             break;
@@ -2162,6 +2162,7 @@ void red_client_set_main(RedClient *client, MainChannelClient *mcc) {
 void red_client_semi_seamless_migrate_complete(RedClient *client)
 {
     GList *link, *next;
+    RedChannelClient *rcc;
 
     pthread_mutex_lock(&client->lock);
     if (!client->during_target_migrate || client->seamless_migrate) {
@@ -2170,15 +2171,10 @@ void red_client_semi_seamless_migrate_complete(RedClient *client)
         return;
     }
     client->during_target_migrate = FALSE;
-    link = client->channels;
-    while (link) {
-        next = link->next;
-        RedChannelClient *rcc = link->data;
-
+    FOREACH_CHANNEL(client, link, next, rcc) {
         if (rcc->latency_monitor.timer) {
             red_channel_client_start_ping_timer(rcc, PING_TEST_IDLE_NET_TIMEOUT_MS);
         }
-        link = next;
     }
     pthread_mutex_unlock(&client->lock);
     reds_on_client_semi_seamless_migrate_complete(client->reds, client);
-- 
2.7.4



More information about the Spice-devel mailing list