[Spice-devel] [PATCH spice-gtk] coroutine: abort on OOM or impossible conditions

Marc-André Lureau marcandre.lureau at gmail.com
Tue Nov 19 08:30:55 PST 2013


Make the coroutine code more glib-like, by aborting on OOM or unhandled
conditions.
---
 gtk/continuation.c        |  8 +++-----
 gtk/continuation.h        |  2 +-
 gtk/coroutine.h           |  3 ++-
 gtk/coroutine_gthread.c   |  7 ++-----
 gtk/coroutine_ucontext.c  | 18 ++++--------------
 gtk/coroutine_winfibers.c | 11 ++++-------
 gtk/spice-channel.c       |  9 +--------
 7 files changed, 17 insertions(+), 41 deletions(-)

diff --git a/gtk/continuation.c b/gtk/continuation.c
index 2891a25..95faf03 100644
--- a/gtk/continuation.c
+++ b/gtk/continuation.c
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
  */
 #include <config.h>
+#include <glib.h>
 
 #undef _FORTIFY_SOURCE
 
@@ -49,12 +50,11 @@ static void continuation_trampoline(int i0, int i1)
 	cc->entry(cc);
 }
 
-int cc_init(struct continuation *cc)
+void cc_init(struct continuation *cc)
 {
 	volatile union cc_arg arg;
 	arg.p = cc;
-	if (getcontext(&cc->uc) == -1)
-		return -1;
+	g_assert(getcontext(&cc->uc) != -1);
 
 	cc->uc.uc_link = &cc->last;
 	cc->uc.uc_stack.ss_sp = cc->stack;
@@ -63,8 +63,6 @@ int cc_init(struct continuation *cc)
 
 	makecontext(&cc->uc, (void *)continuation_trampoline, 2, arg.i[0], arg.i[1]);
 	swapcontext(&cc->last, &cc->uc);
-
-	return 0;
 }
 
 int cc_release(struct continuation *cc)
diff --git a/gtk/continuation.h b/gtk/continuation.h
index 1247337..675a257 100644
--- a/gtk/continuation.h
+++ b/gtk/continuation.h
@@ -39,7 +39,7 @@ struct continuation
 	jmp_buf jmp;
 };
 
-int cc_init(struct continuation *cc);
+void cc_init(struct continuation *cc);
 
 int cc_release(struct continuation *cc);
 
diff --git a/gtk/coroutine.h b/gtk/coroutine.h
index ef6f3db..adfc338 100644
--- a/gtk/coroutine.h
+++ b/gtk/coroutine.h
@@ -56,7 +56,8 @@ struct coroutine
 };
 
 #define IN_MAIN_CONTEXT (coroutine_self() == NULL || coroutine_is_main_context(coroutine_self()))
-int coroutine_init(struct coroutine *co) G_GNUC_WARN_UNUSED_RESULT;
+
+void coroutine_init(struct coroutine *co);
 
 int coroutine_release(struct coroutine *co);
 
diff --git a/gtk/coroutine_gthread.c b/gtk/coroutine_gthread.c
index ab30631..4e96ee5 100644
--- a/gtk/coroutine_gthread.c
+++ b/gtk/coroutine_gthread.c
@@ -86,7 +86,7 @@ static gpointer coroutine_thread(gpointer opaque)
 	return NULL;
 }
 
-int coroutine_init(struct coroutine *co)
+void coroutine_init(struct coroutine *co)
 {
 	if (run_cond == NULL)
 		coroutine_system_init();
@@ -96,14 +96,11 @@ int coroutine_init(struct coroutine *co)
 					  FALSE, TRUE,
 					  G_THREAD_PRIORITY_NORMAL,
 					  NULL);
-	if (co->thread == NULL)
-		return -1;
+	g_assert(co->thread != NULL);
 
 	co->exited = 0;
 	co->runnable = FALSE;
 	co->caller = NULL;
-
-	return 0;
 }
 
 int coroutine_release(struct coroutine *co G_GNUC_UNUSED)
diff --git a/gtk/coroutine_ucontext.c b/gtk/coroutine_ucontext.c
index 4689166..b42fb84 100644
--- a/gtk/coroutine_ucontext.c
+++ b/gtk/coroutine_ucontext.c
@@ -63,10 +63,8 @@ static void coroutine_trampoline(struct continuation *cc)
 	co->data = co->entry(co->data);
 }
 
-int coroutine_init(struct coroutine *co)
+void coroutine_init(struct coroutine *co)
 {
-	int inited;
-
 	if (co->stack_size == 0)
 		co->stack_size = 16 << 20;
 
@@ -75,21 +73,13 @@ int coroutine_init(struct coroutine *co)
 			    PROT_READ | PROT_WRITE,
 			    MAP_PRIVATE | MAP_ANONYMOUS,
 			    -1, 0);
-	if (co->cc.stack == MAP_FAILED)
-		g_error("Failed to allocate %u bytes for coroutine stack: %s",
-			(unsigned)co->stack_size, strerror(errno));
+	g_assert(co->cc.stack != MAP_FAILED);
+
 	co->cc.entry = coroutine_trampoline;
 	co->cc.release = _coroutine_release;
 	co->exited = 0;
 
-	inited = cc_init(&co->cc);
-	if (inited != 0) {
-		munmap(co->cc.stack, co->cc.stack_size);
-		co->cc.stack = NULL;
-		co->exited = 1;
-	}
-
-	return inited;
+	cc_init(&co->cc);
 }
 
 #if 0
diff --git a/gtk/coroutine_winfibers.c b/gtk/coroutine_winfibers.c
index 266b1de..a5b7e87 100644
--- a/gtk/coroutine_winfibers.c
+++ b/gtk/coroutine_winfibers.c
@@ -51,20 +51,17 @@ static void WINAPI coroutine_trampoline(LPVOID lpParameter)
 	SwitchToFiber(caller->fiber);
 }
 
-int coroutine_init(struct coroutine *co)
+void coroutine_init(struct coroutine *co)
 {
 	if (leader.fiber == NULL) {
 		leader.fiber = ConvertThreadToFiber(&leader);
-		if (leader.fiber == NULL)
-			return -1;
+		g_assert(leader.fiber != NULL);
 	}
 
 	co->fiber = CreateFiber(0, &coroutine_trampoline, co);
-	co->ret = 0;
-	if (co->fiber == NULL)
-		return -1;
+	g_assert(co->fiber != NULL);
 
-	return 0;
+	co->ret = 0;
 }
 
 struct coroutine *coroutine_self(void)
diff --git a/gtk/spice-channel.c b/gtk/spice-channel.c
index 2c8c1b9..a045767 100644
--- a/gtk/spice-channel.c
+++ b/gtk/spice-channel.c
@@ -2366,7 +2366,6 @@ static gboolean connect_delayed(gpointer data)
     SpiceChannel *channel = data;
     SpiceChannelPrivate *c = channel->priv;
     struct coroutine *co;
-    int inited;
 
     CHANNEL_DEBUG(channel, "Open coroutine starting %p", channel);
     c->connect_delayed_id = 0;
@@ -2377,13 +2376,7 @@ static gboolean connect_delayed(gpointer data)
     co->entry = spice_channel_coroutine;
     co->release = NULL;
 
-    inited = coroutine_init(co);
-    if (inited != 0) {
-        g_warning("Failed to initialize channel coroutine");
-        CHANNEL_DEBUG(channel, "coroutine_init failed");
-        g_object_unref(G_OBJECT(channel));
-        return FALSE;
-    }
+    coroutine_init(co);
     coroutine_yieldto(co, channel);
 
     return FALSE;
-- 
1.8.3.1



More information about the Spice-devel mailing list