[Spice-commits] 2 commits - server/event-loop.c server/event-loop.tmpl.c server/Makefile.am server/red-common.h server/tests

Frediano Ziglio fziglio at kemper.freedesktop.org
Thu Jan 21 06:21:22 PST 2016


 server/Makefile.am              |    2 
 server/event-loop.c             |  180 +++++++++++++++++++++++++++++++++++++
 server/event-loop.tmpl.c        |  190 ----------------------------------------
 server/red-common.h             |    4 
 server/tests/Makefile.am        |    6 -
 server/tests/basic_event_loop.c |   15 +--
 6 files changed, 192 insertions(+), 205 deletions(-)

New commits:
commit 6e407a81df330490412cc8c480f7db4e4e85a969
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Thu Jan 21 14:14:57 2016 +0100

    event-loop: Remove template
    
    Since SpiceCoreInterfaceInternal is a private data structure, we can
    extend it as we see fit without breaking ABI. In particular, adding a
    GMainContext member to it allows us to remove the need for
    the event loop template which is currently included in the
    basic_event_loop.c test file.
    
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/server/Makefile.am b/server/Makefile.am
index bef999d..92b716f 100644
--- a/server/Makefile.am
+++ b/server/Makefile.am
@@ -70,6 +70,7 @@ libserver_la_SOURCES =				\
 	char-device.c				\
 	char-device.h				\
 	demarshallers.h				\
+	event-loop.c				\
 	glz-encoder.c				\
 	glz-encoder.h				\
 	glz-encoder-dict.c		\
@@ -159,7 +160,6 @@ EXTRA_DIST =					\
 	cache-item.tmpl.c			\
 	glz-encode-match.tmpl.c			\
 	glz-encode.tmpl.c			\
-	event-loop.tmpl.c			\
 	spice-server.syms			\
 	$(NULL)
 
diff --git a/server/event-loop.c b/server/event-loop.c
new file mode 100644
index 0000000..4738ed9
--- /dev/null
+++ b/server/event-loop.c
@@ -0,0 +1,180 @@
+/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+   Copyright (C) 2009-2015 Red Hat, Inc.
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+/*
+ *This file export a global variable:
+ *
+ * SpiceCoreInterfaceInternal event_loop_core;
+ */
+
+#include "red-common.h"
+
+struct SpiceTimer {
+    GMainContext *context;
+    SpiceTimerFunc func;
+    void *opaque;
+    GSource *source;
+};
+
+static SpiceTimer* timer_add(const SpiceCoreInterfaceInternal *iface,
+                             SpiceTimerFunc func, void *opaque)
+{
+    SpiceTimer *timer = spice_malloc0(sizeof(SpiceTimer));
+
+    timer->context = iface->main_context;
+    timer->func = func;
+    timer->opaque = opaque;
+
+    return timer;
+}
+
+static gboolean timer_func(gpointer user_data)
+{
+    SpiceTimer *timer = user_data;
+
+    timer->func(timer->opaque);
+    /* timer might be free after func(), don't touch */
+
+    return FALSE;
+}
+
+static void timer_cancel(SpiceTimer *timer)
+{
+    if (timer->source) {
+        g_source_destroy(timer->source);
+        g_source_unref(timer->source);
+        timer->source = NULL;
+    }
+}
+
+static void timer_start(SpiceTimer *timer, uint32_t ms)
+{
+    timer_cancel(timer);
+
+    timer->source = g_timeout_source_new(ms);
+    spice_assert(timer->source != NULL);
+
+    g_source_set_callback(timer->source, timer_func, timer, NULL);
+
+    g_source_attach(timer->source, timer->context);
+}
+
+static void timer_remove(SpiceTimer *timer)
+{
+    timer_cancel(timer);
+    spice_assert(timer->source == NULL);
+    free(timer);
+}
+
+struct SpiceWatch {
+    GMainContext *context;
+    void *opaque;
+    GSource *source;
+    GIOChannel *channel;
+    SpiceWatchFunc func;
+};
+
+static GIOCondition spice_event_to_giocondition(int event_mask)
+{
+    GIOCondition condition = 0;
+
+    if (event_mask & SPICE_WATCH_EVENT_READ)
+        condition |= G_IO_IN;
+    if (event_mask & SPICE_WATCH_EVENT_WRITE)
+        condition |= G_IO_OUT;
+
+    return condition;
+}
+
+static int giocondition_to_spice_event(GIOCondition condition)
+{
+    int event = 0;
+
+    if (condition & G_IO_IN)
+        event |= SPICE_WATCH_EVENT_READ;
+    if (condition & G_IO_OUT)
+        event |= SPICE_WATCH_EVENT_WRITE;
+
+    return event;
+}
+
+static gboolean watch_func(GIOChannel *source, GIOCondition condition,
+                           gpointer data)
+{
+    SpiceWatch *watch = data;
+    int fd = g_io_channel_unix_get_fd(source);
+
+    watch->func(fd, giocondition_to_spice_event(condition), watch->opaque);
+
+    return TRUE;
+}
+
+static void watch_update_mask(SpiceWatch *watch, int event_mask)
+{
+    if (watch->source) {
+        g_source_destroy(watch->source);
+        g_source_unref(watch->source);
+        watch->source = NULL;
+    }
+
+    if (!event_mask)
+        return;
+
+    watch->source = g_io_create_watch(watch->channel, spice_event_to_giocondition(event_mask));
+    g_source_set_callback(watch->source, (GSourceFunc)watch_func, watch, NULL);
+    g_source_attach(watch->source, watch->context);
+}
+
+static SpiceWatch *watch_add(const SpiceCoreInterfaceInternal *iface,
+                             int fd, int event_mask, SpiceWatchFunc func, void *opaque)
+{
+    SpiceWatch *watch;
+
+    spice_return_val_if_fail(fd != -1, NULL);
+    spice_return_val_if_fail(func != NULL, NULL);
+
+    watch = spice_malloc0(sizeof(SpiceWatch));
+    watch->context = iface->main_context;
+    watch->channel = g_io_channel_unix_new(fd);
+    watch->func = func;
+    watch->opaque = opaque;
+
+    watch_update_mask(watch, event_mask);
+
+    return watch;
+}
+
+static void watch_remove(SpiceWatch *watch)
+{
+    watch_update_mask(watch, 0);
+    spice_assert(watch->source == NULL);
+
+    g_io_channel_unref(watch->channel);
+    free(watch);
+}
+
+SpiceCoreInterfaceInternal event_loop_core = {
+    .timer_add = timer_add,
+    .timer_start = timer_start,
+    .timer_cancel = timer_cancel,
+    .timer_remove = timer_remove,
+
+    .watch_add = watch_add,
+    .watch_update_mask = watch_update_mask,
+    .watch_remove = watch_remove,
+};
diff --git a/server/event-loop.tmpl.c b/server/event-loop.tmpl.c
deleted file mode 100644
index 9d253d9..0000000
--- a/server/event-loop.tmpl.c
+++ /dev/null
@@ -1,190 +0,0 @@
-/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
-/*
-   Copyright (C) 2009-2015 Red Hat, Inc.
-
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   This library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, see <http://www.gnu.org/licenses/>.
-*/
-
-/* This is a template file to implement event loop using GLib one.
- *
- * Is implemented as a template file to avoid some linker problem.
- *
- * This file export a variable:
- *
- * SpiceCoreInterfaceInternal event_loop_core;
- *
- * You should also define some functions like:
- *
- * GMainContext *event_loop_context_from_iface(const SpiceCoreInterfaceInternal *opaque);
- * void event_loop_channel_event(int event, SpiceChannelEventInfo *info);
- */
-
-#include "red-common.h"
-
-struct SpiceTimer {
-    GMainContext *context;
-    SpiceTimerFunc func;
-    void *opaque;
-    GSource *source;
-};
-
-static SpiceTimer* timer_add(const SpiceCoreInterfaceInternal *iface,
-                             SpiceTimerFunc func, void *opaque)
-{
-    SpiceTimer *timer = spice_malloc0(sizeof(SpiceTimer));
-
-    timer->context = event_loop_context_from_iface(iface);
-    timer->func = func;
-    timer->opaque = opaque;
-
-    return timer;
-}
-
-static gboolean timer_func(gpointer user_data)
-{
-    SpiceTimer *timer = user_data;
-
-    timer->func(timer->opaque);
-    /* timer might be free after func(), don't touch */
-
-    return FALSE;
-}
-
-static void timer_cancel(SpiceTimer *timer)
-{
-    if (timer->source) {
-        g_source_destroy(timer->source);
-        g_source_unref(timer->source);
-        timer->source = NULL;
-    }
-}
-
-static void timer_start(SpiceTimer *timer, uint32_t ms)
-{
-    timer_cancel(timer);
-
-    timer->source = g_timeout_source_new(ms);
-    spice_assert(timer->source != NULL);
-
-    g_source_set_callback(timer->source, timer_func, timer, NULL);
-
-    g_source_attach(timer->source, timer->context);
-}
-
-static void timer_remove(SpiceTimer *timer)
-{
-    timer_cancel(timer);
-    spice_assert(timer->source == NULL);
-    free(timer);
-}
-
-struct SpiceWatch {
-    GMainContext *context;
-    void *opaque;
-    GSource *source;
-    GIOChannel *channel;
-    SpiceWatchFunc func;
-};
-
-static GIOCondition spice_event_to_giocondition(int event_mask)
-{
-    GIOCondition condition = 0;
-
-    if (event_mask & SPICE_WATCH_EVENT_READ)
-        condition |= G_IO_IN;
-    if (event_mask & SPICE_WATCH_EVENT_WRITE)
-        condition |= G_IO_OUT;
-
-    return condition;
-}
-
-static int giocondition_to_spice_event(GIOCondition condition)
-{
-    int event = 0;
-
-    if (condition & G_IO_IN)
-        event |= SPICE_WATCH_EVENT_READ;
-    if (condition & G_IO_OUT)
-        event |= SPICE_WATCH_EVENT_WRITE;
-
-    return event;
-}
-
-static gboolean watch_func(GIOChannel *source, GIOCondition condition,
-                           gpointer data)
-{
-    SpiceWatch *watch = data;
-    int fd = g_io_channel_unix_get_fd(source);
-
-    watch->func(fd, giocondition_to_spice_event(condition), watch->opaque);
-
-    return TRUE;
-}
-
-static void watch_update_mask(SpiceWatch *watch, int event_mask)
-{
-    if (watch->source) {
-        g_source_destroy(watch->source);
-        g_source_unref(watch->source);
-        watch->source = NULL;
-    }
-
-    if (!event_mask)
-        return;
-
-    watch->source = g_io_create_watch(watch->channel, spice_event_to_giocondition(event_mask));
-    g_source_set_callback(watch->source, (GSourceFunc)watch_func, watch, NULL);
-    g_source_attach(watch->source, watch->context);
-}
-
-static SpiceWatch *watch_add(const SpiceCoreInterfaceInternal *iface,
-                             int fd, int event_mask, SpiceWatchFunc func, void *opaque)
-{
-    SpiceWatch *watch;
-
-    spice_return_val_if_fail(fd != -1, NULL);
-    spice_return_val_if_fail(func != NULL, NULL);
-
-    watch = spice_malloc0(sizeof(SpiceWatch));
-    watch->context = event_loop_context_from_iface(iface);
-    watch->channel = g_io_channel_unix_new(fd);
-    watch->func = func;
-    watch->opaque = opaque;
-
-    watch_update_mask(watch, event_mask);
-
-    return watch;
-}
-
-static void watch_remove(SpiceWatch *watch)
-{
-    watch_update_mask(watch, 0);
-    spice_assert(watch->source == NULL);
-
-    g_io_channel_unref(watch->channel);
-    free(watch);
-}
-
-static SpiceCoreInterfaceInternal event_loop_core = {
-    .timer_add = timer_add,
-    .timer_start = timer_start,
-    .timer_cancel = timer_cancel,
-    .timer_remove = timer_remove,
-
-    .watch_add = watch_add,
-    .watch_update_mask = watch_update_mask,
-    .watch_remove = watch_remove,
-
-    .channel_event = event_loop_channel_event
-};
diff --git a/server/red-common.h b/server/red-common.h
index 253dc45..90a7d20 100644
--- a/server/red-common.h
+++ b/server/red-common.h
@@ -52,6 +52,10 @@ struct SpiceCoreInterfaceInternal {
     void (*watch_remove)(SpiceWatch *watch);
 
     void (*channel_event)(int event, SpiceChannelEventInfo *info);
+
+    GMainContext *main_context;
 };
 
+extern SpiceCoreInterfaceInternal event_loop_core;
+
 #endif
diff --git a/server/tests/Makefile.am b/server/tests/Makefile.am
index 6f02c99..fea2181 100644
--- a/server/tests/Makefile.am
+++ b/server/tests/Makefile.am
@@ -30,9 +30,9 @@ libtest_a_SOURCES =				\
 LDADD =								\
 	libtest.a						\
 	$(top_builddir)/spice-common/common/libspice-common.la	\
-	$(top_builddir)/server/libspice-server.la		\
+	$(top_builddir)/server/libserver.la			\
 	$(GLIB2_LIBS)						\
-	$(SPICE_NONPKGCONFIG_LIBS)		                \
+	$(SPICE_NONPKGCONFIG_LIBS)				\
 	$(NULL)
 
 noinst_PROGRAMS =				\
@@ -70,8 +70,6 @@ noinst_LIBRARIES += \
 
 spice_server_replay_SOURCES = replay.c
 
-stream_test_LDADD = ../libserver.la $(LDADD)
-
 stat_test_SOURCES = stat-main.c
 stat_test_LDADD = \
 	libstat_test1.a \
diff --git a/server/tests/basic_event_loop.c b/server/tests/basic_event_loop.c
index 81e70b7..3f1bc71 100644
--- a/server/tests/basic_event_loop.c
+++ b/server/tests/basic_event_loop.c
@@ -43,19 +43,12 @@ GMainContext *basic_event_loop_get_context(void)
     return main_context;
 }
 
-static inline GMainContext *event_loop_context_from_iface(const SpiceCoreInterfaceInternal *iface)
-{
-    return main_context;
-}
-
 static void event_loop_channel_event(int event, SpiceChannelEventInfo *info)
 {
     DPRINTF(0, "channel event con, type, id, event: %d, %d, %d, %d",
             info->connection_id, info->type, info->id, event);
 }
 
-#include "../event-loop.tmpl.c"
-
 void basic_event_loop_mainloop(void)
 {
     GMainLoop *loop = g_main_loop_new(main_context, FALSE);
@@ -91,7 +84,6 @@ static SpiceCoreInterface core = {
     },
     .timer_add = base_timer_add,
     .watch_add = base_watch_add,
-    .channel_event = event_loop_channel_event,
 };
 
 SpiceCoreInterface *basic_event_loop_init(void)
@@ -104,6 +96,9 @@ SpiceCoreInterface *basic_event_loop_init(void)
     core.timer_remove = event_loop_core.timer_remove;
     core.watch_update_mask = event_loop_core.watch_update_mask;
     core.watch_remove = event_loop_core.watch_remove;
+    event_loop_core.channel_event = core.channel_event = event_loop_channel_event;
+    event_loop_core.main_context = main_context;
+
     return &core;
 }
 
commit 4b923347b7aae3120c5b31168ccb7d5c88c07462
Author: Christophe Fergeau <cfergeau at redhat.com>
Date:   Thu Jan 21 14:14:56 2016 +0100

    test-loop: Improve basic_event_loop base_{timer, watch}_add
    
    They call the functions provided by event_loop_core, but with a NULL
    SpiceCoreInterfaceInternal parameter. It makes more sense to pass
    event_loop_core rather than NULL.
    This will allow to pass the GMainContext to be used through
    SpiceCoreInterfaceInternal rather than through a template parameter.
    
    Acked-by: Frediano Ziglio <fziglio at redhat.com>

diff --git a/server/tests/basic_event_loop.c b/server/tests/basic_event_loop.c
index 997d251..81e70b7 100644
--- a/server/tests/basic_event_loop.c
+++ b/server/tests/basic_event_loop.c
@@ -76,12 +76,12 @@ static void ignore_sigpipe(void)
 
 static SpiceTimer* base_timer_add(SpiceTimerFunc func, void *opaque)
 {
-    return event_loop_core.timer_add(NULL, func, opaque);
+    return event_loop_core.timer_add(&event_loop_core, func, opaque);
 }
 
 static SpiceWatch *base_watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
 {
-    return event_loop_core.watch_add(NULL, fd, event_mask, func, opaque);
+    return event_loop_core.watch_add(&event_loop_core, fd, event_mask, func, opaque);
 }
 
 static SpiceCoreInterface core = {


More information about the Spice-commits mailing list