[Spice-commits] 4 commits - server/event-loop.tmpl.c server/Makefile.am server/tests
Frediano Ziglio
fziglio at kemper.freedesktop.org
Tue Jan 19 15:59:57 PST 2016
server/Makefile.am | 1
server/event-loop.tmpl.c | 190 ++++++++++++++++++++++++++++++++++++++++
server/tests/Makefile.am | 2
server/tests/basic_event_loop.c | 162 +++++++---------------------------
server/tests/basic_event_loop.h | 3
server/tests/replay.c | 18 ++-
server/tests/test-loop.c | 155 ++++++++++++++++++++++++++++++++
7 files changed, 400 insertions(+), 131 deletions(-)
New commits:
commit a5a0d4a290a7fdea731fbfd8c7d44f3a0709bd6c
Author: Frediano Ziglio <fziglio at redhat.com>
Date: Mon Jan 18 23:02:44 2016 +0000
tests: test removed triggered timers are not called
This could happen for instance if a given timer remove all clients
which have associated timers.
Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
Acked-by: Jonathon Jongsma <jjongsma at redhat.com>
diff --git a/server/tests/test-loop.c b/server/tests/test-loop.c
index 1dc3923..1c2f496 100644
--- a/server/tests/test-loop.c
+++ b/server/tests/test-loop.c
@@ -74,6 +74,19 @@ static void *loop_func(void *arg)
return NULL;
}
+static SpiceTimer *twice_timers[2] = { NULL, NULL };
+static int twice_called = 0;
+static void timer_not_twice(void *opaque)
+{
+ spice_assert(++twice_called == 1);
+
+ /* delete timers, should not have another call */
+ core->timer_remove(twice_timers[0]);
+ core->timer_remove(twice_timers[1]);
+ twice_timers[0] = NULL;
+ twice_timers[1] = NULL;
+}
+
int main(int argc, char **argv)
{
SpiceTimer *timer, *timers[10];
@@ -107,6 +120,14 @@ int main(int argc, char **argv)
timer = timers[i++] = core->timer_add(timer_exit, NULL);
core->timer_start(timer, 10);
+ /* test events are not called when freed */
+ timer = twice_timers[0] = core->timer_add(timer_not_twice, NULL);
+ spice_assert(timer != NULL);
+ core->timer_start(timer, 2);
+ timer = twice_timers[1] = core->timer_add(timer_not_twice, NULL);
+ spice_assert(timer != NULL);
+ core->timer_start(timer, 2);
+
/* run the loop */
loop = g_main_loop_new(basic_event_loop_get_context(), FALSE);
alarm(1);
commit 323dc4679485d667208d0336c98f499af29e12bd
Author: Frediano Ziglio <fziglio at redhat.com>
Date: Tue Dec 15 13:59:58 2015 +0000
tests: add a test for event loop
Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
Acked-by: Jonathon Jongsma <jjongsma at redhat.com>
diff --git a/server/tests/Makefile.am b/server/tests/Makefile.am
index 8caff04..6f02c99 100644
--- a/server/tests/Makefile.am
+++ b/server/tests/Makefile.am
@@ -49,12 +49,14 @@ noinst_PROGRAMS = \
spice-server-replay \
stream-test \
stat_test \
+ test-loop \
$(NULL)
TESTS = \
stat_test \
test-qxl-parsing \
stream-test \
+ test-loop \
$(NULL)
check_PROGRAMS = $(TESTS)
diff --git a/server/tests/basic_event_loop.c b/server/tests/basic_event_loop.c
index c3dabf4..997d251 100644
--- a/server/tests/basic_event_loop.c
+++ b/server/tests/basic_event_loop.c
@@ -106,3 +106,10 @@ SpiceCoreInterface *basic_event_loop_init(void)
core.watch_remove = event_loop_core.watch_remove;
return &core;
}
+
+void basic_event_loop_destroy(void)
+{
+ spice_assert(main_context != NULL);
+ g_main_context_unref(main_context);
+ main_context = NULL;
+}
diff --git a/server/tests/basic_event_loop.h b/server/tests/basic_event_loop.h
index 2ec9446..593532b 100644
--- a/server/tests/basic_event_loop.h
+++ b/server/tests/basic_event_loop.h
@@ -23,6 +23,7 @@
GMainContext *basic_event_loop_get_context(void);
SpiceCoreInterface *basic_event_loop_init(void);
+void basic_event_loop_destroy(void);
void basic_event_loop_mainloop(void);
#endif // __BASIC_EVENT_LOOP_H__
diff --git a/server/tests/test-loop.c b/server/tests/test-loop.c
new file mode 100644
index 0000000..1dc3923
--- /dev/null
+++ b/server/tests/test-loop.c
@@ -0,0 +1,134 @@
+/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
+/*
+ Copyright (C) 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/>.
+*/
+
+/* Test event loop
+ */
+
+#include <config.h>
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <signal.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <glib.h>
+
+#include <spice/macros.h>
+#include <common/log.h>
+#include "basic_event_loop.h"
+
+static SpiceCoreInterface *core = NULL;
+static GMainLoop *loop = NULL;
+static pthread_t loop_thread;
+
+static void timer_err(void *opaque)
+{
+ spice_assert(0);
+}
+
+static SpiceTimer *to_delete_timer = NULL;
+static void timer_del(void *opaque)
+{
+ spice_assert(core);
+ spice_assert(to_delete_timer);
+
+ spice_assert(pthread_equal(loop_thread, pthread_self()));
+
+ core->timer_remove(to_delete_timer);
+ to_delete_timer = NULL;
+}
+
+static void timer_exit(void *opaque)
+{
+ spice_assert(loop);
+
+ spice_assert(pthread_equal(loop_thread, pthread_self()));
+
+ g_main_loop_quit(loop);
+}
+
+static void *loop_func(void *arg)
+{
+ loop_thread = pthread_self();
+
+ spice_assert(loop);
+
+ g_main_loop_run(loop);
+
+ return NULL;
+}
+
+int main(int argc, char **argv)
+{
+ SpiceTimer *timer, *timers[10];
+ int i, rc;
+
+ memset(timers, 0, sizeof(timers));
+
+ core = basic_event_loop_init();
+
+ i = 0;
+
+ /* add a timer and delete to check is correctly deleted */
+ timer = core->timer_add(timer_err, NULL);
+ core->timer_start(timer, 1);
+ core->timer_remove(timer);
+
+ /* create timer, should not be executed */
+ timer = timers[i++] = core->timer_add(timer_err, NULL);
+
+ /* add a timer and cancel to check is not executed */
+ timer = timers[i++] = core->timer_add(timer_err, NULL);
+ core->timer_start(timer, 1);
+ core->timer_cancel(timer);
+
+ /* check we can remove timer inside a timer */
+ timer = to_delete_timer = core->timer_add(timer_del, NULL);
+ spice_assert(to_delete_timer != NULL);
+ core->timer_start(timer, 1);
+
+ /* create a timer that does something */
+ timer = timers[i++] = core->timer_add(timer_exit, NULL);
+ core->timer_start(timer, 10);
+
+ /* run the loop */
+ loop = g_main_loop_new(basic_event_loop_get_context(), FALSE);
+ alarm(1);
+ rc = pthread_create(&loop_thread, NULL, loop_func, NULL);
+ spice_assert(rc == 0);
+ rc = pthread_join(loop_thread, NULL);
+ spice_assert(rc == 0);
+ alarm(0);
+ g_main_loop_unref(loop);
+
+ /* delete executed ? */
+ spice_assert(to_delete_timer == NULL);
+
+ /* cleanup */
+ for (i = 0; i < G_N_ELEMENTS(timers); ++i) {
+ if (timers[i]) {
+ core->timer_remove(timers[i]);
+ timers[i] = NULL;
+ }
+ }
+
+ basic_event_loop_destroy();
+
+ return 0;
+}
commit f3a7befafe6632210a8554535f8196c568717691
Author: Frediano Ziglio <fziglio at redhat.com>
Date: Tue Dec 15 11:58:54 2015 +0000
tests: extract code for event loop
This code will be reused for main loop
Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
Acked-by: Jonathon Jongsma <jjongsma at redhat.com>
diff --git a/server/Makefile.am b/server/Makefile.am
index b20beec..bef999d 100644
--- a/server/Makefile.am
+++ b/server/Makefile.am
@@ -159,6 +159,7 @@ 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.tmpl.c b/server/event-loop.tmpl.c
new file mode 100644
index 0000000..9d253d9
--- /dev/null
+++ b/server/event-loop.tmpl.c
@@ -0,0 +1,190 @@
+/* -*- 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/tests/basic_event_loop.c b/server/tests/basic_event_loop.c
index 15fba65..c3dabf4 100644
--- a/server/tests/basic_event_loop.c
+++ b/server/tests/basic_event_loop.c
@@ -22,6 +22,7 @@
#include <signal.h>
#include <string.h>
+#include "red-common.h"
#include "spice/macros.h"
#include "common/ring.h"
#include "common/mem.h"
@@ -42,150 +43,19 @@ GMainContext *basic_event_loop_get_context(void)
return main_context;
}
-struct SpiceTimer {
- SpiceTimerFunc func;
- void *opaque;
- GSource *source;
-};
-
-static SpiceTimer* timer_add(SpiceTimerFunc func, void *opaque)
-{
- SpiceTimer *timer = spice_malloc0(sizeof(SpiceTimer));
-
- 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, main_context);
-}
-
-static void timer_remove(SpiceTimer *timer)
-{
- timer_cancel(timer);
- spice_assert(timer->source == NULL);
- free(timer);
-}
-
-struct SpiceWatch {
- 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, main_context);
-}
-
-static SpiceWatch *watch_add(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->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)
+static inline GMainContext *event_loop_context_from_iface(const SpiceCoreInterfaceInternal *iface)
{
- watch_update_mask(watch, 0);
- spice_assert(watch->source == NULL);
-
- g_io_channel_unref(watch->channel);
- free(watch);
+ return main_context;
}
-static void channel_event(int event, SpiceChannelEventInfo *info)
+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);
@@ -204,19 +74,24 @@ static void ignore_sigpipe(void)
sigaction(SIGPIPE, &act, NULL);
}
+static SpiceTimer* base_timer_add(SpiceTimerFunc func, void *opaque)
+{
+ return event_loop_core.timer_add(NULL, 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);
+}
+
static SpiceCoreInterface core = {
.base = {
.major_version = SPICE_INTERFACE_CORE_MAJOR,
.minor_version = SPICE_INTERFACE_CORE_MINOR,
},
- .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 = channel_event,
+ .timer_add = base_timer_add,
+ .watch_add = base_watch_add,
+ .channel_event = event_loop_channel_event,
};
SpiceCoreInterface *basic_event_loop_init(void)
@@ -224,5 +99,10 @@ SpiceCoreInterface *basic_event_loop_init(void)
ignore_sigpipe();
spice_assert(main_context == NULL);
main_context = g_main_context_new();
+ core.timer_start = event_loop_core.timer_start;
+ core.timer_cancel = event_loop_core.timer_cancel;
+ 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;
return &core;
}
commit 3686132923e99bed0eb4a6de1a1f6822a9bc6590
Author: Frediano Ziglio <fziglio at redhat.com>
Date: Mon Dec 14 17:54:32 2015 +0000
tests: do not use default loop context
Make sure we don't handle event reserved to other loop contexts.
Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
Acked-by: Jonathon Jongsma <jjongsma at redhat.com>
diff --git a/server/tests/basic_event_loop.c b/server/tests/basic_event_loop.c
index 84652a4..15fba65 100644
--- a/server/tests/basic_event_loop.c
+++ b/server/tests/basic_event_loop.c
@@ -21,7 +21,6 @@
#include <sys/time.h>
#include <signal.h>
#include <string.h>
-#include <glib.h>
#include "spice/macros.h"
#include "common/ring.h"
@@ -36,10 +35,17 @@ int debug = 0;
} \
}
+static GMainContext *main_context = NULL;
+
+GMainContext *basic_event_loop_get_context(void)
+{
+ return main_context;
+}
+
struct SpiceTimer {
SpiceTimerFunc func;
void *opaque;
- guint source_id;
+ GSource *source;
};
static SpiceTimer* timer_add(SpiceTimerFunc func, void *opaque)
@@ -56,7 +62,6 @@ static gboolean timer_func(gpointer user_data)
{
SpiceTimer *timer = user_data;
- timer->source_id = 0;
timer->func(timer->opaque);
/* timer might be free after func(), don't touch */
@@ -65,34 +70,40 @@ static gboolean timer_func(gpointer user_data)
static void timer_cancel(SpiceTimer *timer)
{
- if (timer->source_id == 0)
- return;
-
- g_source_remove(timer->source_id);
- timer->source_id = 0;
+ 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_id = g_timeout_add(ms, timer_func, 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, main_context);
}
static void timer_remove(SpiceTimer *timer)
{
timer_cancel(timer);
+ spice_assert(timer->source == NULL);
free(timer);
}
struct SpiceWatch {
void *opaque;
- guint source_id;
+ GSource *source;
GIOChannel *channel;
SpiceWatchFunc func;
};
-static GIOCondition spice_event_to_condition(int event_mask)
+static GIOCondition spice_event_to_giocondition(int event_mask)
{
GIOCondition condition = 0;
@@ -104,7 +115,7 @@ static GIOCondition spice_event_to_condition(int event_mask)
return condition;
}
-static int condition_to_spice_event(GIOCondition condition)
+static int giocondition_to_spice_event(GIOCondition condition)
{
int event = 0;
@@ -122,37 +133,49 @@ static gboolean watch_func(GIOChannel *source, GIOCondition condition,
SpiceWatch *watch = data;
int fd = g_io_channel_unix_get_fd(source);
- watch->func(fd, condition_to_spice_event(condition), watch->opaque);
+ 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, main_context);
+}
+
static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
{
SpiceWatch *watch;
- GIOCondition condition = spice_event_to_condition(event_mask);
+
+ spice_return_val_if_fail(fd != -1, NULL);
+ spice_return_val_if_fail(func != NULL, NULL);
watch = spice_malloc0(sizeof(SpiceWatch));
watch->channel = g_io_channel_unix_new(fd);
- watch->source_id = g_io_add_watch(watch->channel, condition, watch_func, watch);
watch->func = func;
watch->opaque = opaque;
- return watch;
-}
+ watch_update_mask(watch, event_mask);
-static void watch_update_mask(SpiceWatch *watch, int event_mask)
-{
- GIOCondition condition = spice_event_to_condition(event_mask);
-
- g_source_remove(watch->source_id);
- if (condition != 0)
- watch->source_id = g_io_add_watch(watch->channel, condition, watch_func, watch);
+ return watch;
}
static void watch_remove(SpiceWatch *watch)
{
- g_source_remove(watch->source_id);
+ watch_update_mask(watch, 0);
+ spice_assert(watch->source == NULL);
+
g_io_channel_unref(watch->channel);
free(watch);
}
@@ -165,7 +188,7 @@ static void channel_event(int event, SpiceChannelEventInfo *info)
void basic_event_loop_mainloop(void)
{
- GMainLoop *loop = g_main_loop_new(NULL, FALSE);
+ GMainLoop *loop = g_main_loop_new(main_context, FALSE);
g_main_loop_run(loop);
g_main_loop_unref(loop);
@@ -199,5 +222,7 @@ static SpiceCoreInterface core = {
SpiceCoreInterface *basic_event_loop_init(void)
{
ignore_sigpipe();
+ spice_assert(main_context == NULL);
+ main_context = g_main_context_new();
return &core;
}
diff --git a/server/tests/basic_event_loop.h b/server/tests/basic_event_loop.h
index 8220893..2ec9446 100644
--- a/server/tests/basic_event_loop.h
+++ b/server/tests/basic_event_loop.h
@@ -19,7 +19,9 @@
#define __BASIC_EVENT_LOOP_H__
#include <spice.h>
+#include <glib.h>
+GMainContext *basic_event_loop_get_context(void);
SpiceCoreInterface *basic_event_loop_init(void);
void basic_event_loop_mainloop(void);
diff --git a/server/tests/replay.c b/server/tests/replay.c
index f0ad6ed..2af9481 100644
--- a/server/tests/replay.c
+++ b/server/tests/replay.c
@@ -53,7 +53,7 @@ static GAsyncQueue *aqueue = NULL;
static long total_size;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-static guint fill_source_id = 0;
+static GSource *fill_source = NULL;
#define MEM_SLOT_GROUP_ID 0
@@ -125,7 +125,11 @@ static gboolean fill_queue_idle(gpointer user_data)
end:
if (!keep) {
pthread_mutex_lock(&mutex);
- fill_source_id = 0;
+ if (fill_source) {
+ g_source_destroy(fill_source);
+ g_source_unref(fill_source);
+ fill_source = NULL;
+ }
pthread_mutex_unlock(&mutex);
}
spice_qxl_wakeup(&display_sin);
@@ -140,10 +144,12 @@ static void fill_queue(void)
if (!started)
goto end;
- if (fill_source_id != 0)
+ if (fill_source)
goto end;
- fill_source_id = g_idle_add(fill_queue_idle, NULL);
+ fill_source = g_idle_source_new();
+ g_source_set_callback(fill_source, fill_queue_idle, NULL, NULL);
+ g_source_attach(fill_source, basic_event_loop_get_context());
end:
pthread_mutex_unlock(&mutex);
@@ -178,7 +184,7 @@ static int req_cmd_notification(QXLInstance *qin)
return TRUE;
g_printerr("id: %d, queue length: %d",
- fill_source_id, g_async_queue_length(aqueue));
+ g_source_get_id(fill_source), g_async_queue_length(aqueue));
return TRUE;
}
@@ -392,7 +398,7 @@ int main(int argc, char **argv)
fill_queue();
}
- loop = g_main_loop_new(NULL, FALSE);
+ loop = g_main_loop_new(basic_event_loop_get_context(), FALSE);
g_main_loop_run(loop);
end_replay();
More information about the Spice-commits
mailing list