[Spice-commits] 10 commits - .gitlab-ci.yml src/continuation.h src/coroutine_gthread.c src/coroutine.h src/coroutine_ucontext.c src/coroutine_winfibers.c src/spice-channel.c tests/coroutine.c

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Aug 12 15:17:47 UTC 2020


 .gitlab-ci.yml            |    2 ++
 src/continuation.h        |    6 ------
 src/coroutine.h           |    8 +++-----
 src/coroutine_gthread.c   |   11 ++++-------
 src/coroutine_ucontext.c  |   22 +++++-----------------
 src/coroutine_winfibers.c |   16 ++++------------
 src/spice-channel.c       |    1 -
 tests/coroutine.c         |   32 ++++++++++++++++++++++++++++++++
 8 files changed, 50 insertions(+), 48 deletions(-)

New commits:
commit 25cbf8b45f9fbfa033dea029ffbeafd66b40ec54
Author: Frediano Ziglio <freddy77 at gmail.com>
Date:   Wed Aug 12 09:36:17 2020 +0100

    ci: Test for Windows coroutines
    
    Signed-off-by: Frediano Ziglio <freddy77 at gmail.com>
    Acked-by: Marc-André Lureau <marcandre.lureau at redhat.com>

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 0faea7d..3d4f533 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -18,6 +18,7 @@ variables:
               mingw64-gtk3 mingw64-json-glib mingw64-opus
               mingw64-gstreamer1-plugins-base mingw64-gstreamer1-plugins-good
               mingw64-usbredir mingw32-usbredir
+              wine-core.x86_64 wine-core.i686
 
   GIT_SUBMODULE_STRATEGY: recursive
 
@@ -69,3 +70,4 @@ windows:
     - mkdir build-win64 && cd build-win64
     - mingw64-meson --buildtype=release -Dgtk_doc=disabled --werror
     - ninja install
+    - (cd tests && DISPLAY= WINEPATH=/usr/x86_64-w64-mingw32/sys-root/mingw/bin wine test-coroutine.exe)
commit aa8741d16bb796c4803eea74ee28847effc42ed6
Author: Frediano Ziglio <freddy77 at gmail.com>
Date:   Tue Aug 11 17:44:01 2020 +0100

    coroutine_ucontext: Reuse SPICE_CONTAINEROF
    
    container_of is already implemented using spice macros.
    
    Signed-off-by: Frediano Ziglio <freddy77 at gmail.com>
    Acked-by: Marc-André Lureau <marcandre.lureau at redhat.com>

diff --git a/src/continuation.h b/src/continuation.h
index bbde386..79b68c4 100644
--- a/src/continuation.h
+++ b/src/continuation.h
@@ -20,7 +20,6 @@
 
 #pragma once
 
-#include "spice-common.h"
 #include <stddef.h>
 #include <ucontext.h>
 #include <setjmp.h>
@@ -47,11 +46,6 @@ int cc_release(struct continuation *cc);
    the current continuation handy. */
 int cc_swap(struct continuation *from, struct continuation *to);
 
-#define offset_of(type, member) ((unsigned long)(&((type *)0)->member))
-#define container_of(obj, type, member)                                 \
-        SPICE_ALIGNED_CAST(type *,                                      \
-                           (((char *)obj) - offset_of(type, member)))
-
 /*
  * Local variables:
  *  c-indent-level: 8
diff --git a/src/coroutine_ucontext.c b/src/coroutine_ucontext.c
index faf94fa..9d16ada 100644
--- a/src/coroutine_ucontext.c
+++ b/src/coroutine_ucontext.c
@@ -29,6 +29,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <spice/macros.h>
 #include "coroutine.h"
 
 #ifndef MAP_ANONYMOUS
@@ -42,7 +43,7 @@ int coroutine_release(struct coroutine *co)
 
 static int _coroutine_release(struct continuation *cc)
 {
-	struct coroutine *co = container_of(cc, struct coroutine, cc);
+	struct coroutine *co = SPICE_CONTAINEROF(cc, struct coroutine, cc);
 
 	munmap(co->cc.stack, co->cc.stack_size);
 
@@ -53,7 +54,7 @@ static int _coroutine_release(struct continuation *cc)
 
 static void coroutine_trampoline(struct continuation *cc)
 {
-	struct coroutine *co = container_of(cc, struct coroutine, cc);
+	struct coroutine *co = SPICE_CONTAINEROF(cc, struct coroutine, cc);
 	co->data = co->entry(co->data);
 }
 
commit 9def63a7a402e491ba79a4aa98ef85f8142f11b4
Author: Frediano Ziglio <freddy77 at gmail.com>
Date:   Tue Aug 11 21:24:26 2020 +0100

    coroutine_gthread: Fix recursive yield
    
    Signed-off-by: Frediano Ziglio <freddy77 at gmail.com>
    Acked-by: Marc-André Lureau <marcandre.lureau at redhat.com>

diff --git a/src/coroutine_gthread.c b/src/coroutine_gthread.c
index 60063a7..d5c11e6 100644
--- a/src/coroutine_gthread.c
+++ b/src/coroutine_gthread.c
@@ -75,6 +75,7 @@ static gpointer coroutine_thread(gpointer opaque)
 	co->exited = 1;
 
 	co->caller->runnable = TRUE;
+	co->caller = NULL;
 	CO_DEBUG("BROADCAST");
 	g_cond_broadcast(&run_cond);
 	CO_DEBUG("UNLOCK");
@@ -104,7 +105,6 @@ static void *coroutine_swap(struct coroutine *from, struct coroutine *to, void *
 	from->runnable = FALSE;
 	to->runnable = TRUE;
 	to->data = arg;
-	to->caller = from;
 	CO_DEBUG("BROADCAST");
 	g_cond_broadcast(&run_cond);
 	CO_DEBUG("UNLOCK");
@@ -116,7 +116,6 @@ static void *coroutine_swap(struct coroutine *from, struct coroutine *to, void *
 		g_cond_wait(&run_cond, &run_lock);
 	}
 	current = from;
-	to->caller = NULL;
 
 	CO_DEBUG("SWAPPED");
 	return from->data;
@@ -134,6 +133,7 @@ void *coroutine_yieldto(struct coroutine *to, void *arg)
 	g_return_val_if_fail(!to->exited, NULL);
 
 	CO_DEBUG("SWAP");
+	to->caller = coroutine_self();
 	return coroutine_swap(coroutine_self(), to, arg);
 }
 
diff --git a/tests/coroutine.c b/tests/coroutine.c
index 7701277..a0bf490 100644
--- a/tests/coroutine.c
+++ b/tests/coroutine.c
@@ -74,19 +74,51 @@ static void test_coroutine_two(void)
     g_assert(self == coroutine_self());
 }
 
+static gpointer co_entry_yield2(gpointer data)
+{
+    struct coroutine *self = coroutine_self();
+    gpointer val;
+
+    g_assert(data == GINT_TO_POINTER(12));
+    val = coroutine_yield(GINT_TO_POINTER(23));
+    g_assert_cmpint(GPOINTER_TO_INT(val), ==, 34);
+
+    g_assert(self == coroutine_self());
+    g_assert(!coroutine_self_is_main());
+
+    return NULL;
+}
+
 static gpointer co_entry_yield(gpointer data)
 {
+    struct coroutine *self = coroutine_self();
+    struct coroutine co = {
+        .stack_size = 1 << 20,
+        .entry = co_entry_yield2,
+    };
     gpointer val;
 
+    coroutine_init(&co);
+    val = coroutine_yieldto(&co, GINT_TO_POINTER(12));
+
+    g_assert(self == coroutine_self());
+    g_assert_cmpint(GPOINTER_TO_INT(val), ==, 23);
+
+    coroutine_yieldto(&co, GINT_TO_POINTER(34));
+    g_assert(self == coroutine_self());
+
     g_assert(data == NULL);
     val = coroutine_yield(GINT_TO_POINTER(1));
     g_assert_cmpint(GPOINTER_TO_INT(val), ==, 2);
 
+    g_assert(self == coroutine_self());
     g_assert(!coroutine_self_is_main());
 
     val = coroutine_yield(GINT_TO_POINTER(3));
     g_assert_cmpint(GPOINTER_TO_INT(val), ==, 4);
 
+    g_assert(self == coroutine_self());
+
     return NULL;
 }
 
commit 12af86a5a52a730112ccf8edb6d0fd41b45f53dc
Author: Frediano Ziglio <freddy77 at gmail.com>
Date:   Tue Aug 11 17:52:11 2020 +0100

    coroutine_winfibers: Fix chaining multiple coroutines
    
    Using a single "caller" global does not allow to store multiple
    coroutines so use "caller" inside coroutine structure.
    Also correctly set the current coroutine returning in coroutine_swap.
    
    Signed-off-by: Frediano Ziglio <freddy77 at gmail.com>
    Acked-by: Marc-André Lureau <marcandre.lureau at redhat.com>

diff --git a/src/coroutine_winfibers.c b/src/coroutine_winfibers.c
index 1dabcaa..00fa578 100644
--- a/src/coroutine_winfibers.c
+++ b/src/coroutine_winfibers.c
@@ -26,7 +26,6 @@
 
 static struct coroutine leader = { 0, };
 static struct coroutine *current = &leader;
-static struct coroutine *caller = NULL;
 
 int coroutine_release(struct coroutine *co)
 {
@@ -37,6 +36,7 @@ int coroutine_release(struct coroutine *co)
 static void WINAPI coroutine_trampoline(LPVOID lpParameter)
 {
 	struct coroutine *co = (struct coroutine *)lpParameter;
+	struct coroutine *caller = co->caller;
 
 	co->data = co->entry(co->data);
 
@@ -72,13 +72,12 @@ static void *coroutine_swap(struct coroutine *from, struct coroutine *to, void *
 {
 	to->data = arg;
 	current = to;
-	caller = from;
 	SwitchToFiber(to->fiber);
 	if (to->ret == 0)
 		return from->data;
 	else if (to->ret == 1) {
 		coroutine_release(to);
-		current = &leader;
+		current = from;
 		to->exited = 1;
 		return to->data;
 	}
commit ca2e4ec3bb4f5ce590b8c35fe173247c71958f20
Author: Frediano Ziglio <freddy77 at gmail.com>
Date:   Tue Aug 11 17:49:53 2020 +0100

    coroutine: Remove "release" field
    
    Not used and implemented in different ways by the various
    coroutine implementations
    
    Signed-off-by: Frediano Ziglio <freddy77 at gmail.com>
    Acked-by: Marc-André Lureau <marcandre.lureau at redhat.com>

diff --git a/src/coroutine.h b/src/coroutine.h
index d2f367a..96d8593 100644
--- a/src/coroutine.h
+++ b/src/coroutine.h
@@ -34,7 +34,6 @@ struct coroutine
 {
 	size_t stack_size;
 	void *(*entry)(void *);
-	int (*release)(struct coroutine *);
 
 	/* read-only */
 	int exited;
diff --git a/src/coroutine_gthread.c b/src/coroutine_gthread.c
index 1862092..60063a7 100644
--- a/src/coroutine_gthread.c
+++ b/src/coroutine_gthread.c
@@ -49,7 +49,6 @@ static void coroutine_system_init(void)
 	/* The thread that creates the first coroutine is the system
 	 * coroutine so let's fill out a structure for it */
 	leader.entry = NULL;
-	leader.release = NULL;
 	leader.stack_size = 0;
 	leader.exited = 0;
 	leader.thread = g_thread_self();
diff --git a/src/coroutine_ucontext.c b/src/coroutine_ucontext.c
index 890acb1..faf94fa 100644
--- a/src/coroutine_ucontext.c
+++ b/src/coroutine_ucontext.c
@@ -44,12 +44,6 @@ static int _coroutine_release(struct continuation *cc)
 {
 	struct coroutine *co = container_of(cc, struct coroutine, cc);
 
-	if (co->release) {
-		int ret = co->release(co);
-		if (ret < 0)
-			return ret;
-	}
-
 	munmap(co->cc.stack, co->cc.stack_size);
 
 	co->caller = NULL;
diff --git a/src/coroutine_winfibers.c b/src/coroutine_winfibers.c
index 4108f13..1dabcaa 100644
--- a/src/coroutine_winfibers.c
+++ b/src/coroutine_winfibers.c
@@ -40,11 +40,6 @@ static void WINAPI coroutine_trampoline(LPVOID lpParameter)
 
 	co->data = co->entry(co->data);
 
-	if (co->release)
-		co->ret = co->release(co);
-	else
-		co->ret = 0;
-
 	co->caller = NULL;
 
 	// and switch back to caller
diff --git a/src/spice-channel.c b/src/spice-channel.c
index 217a83f..dd5d63b 100644
--- a/src/spice-channel.c
+++ b/src/spice-channel.c
@@ -2707,7 +2707,6 @@ static gboolean connect_delayed(gpointer data)
 
     co->stack_size = 16 << 20; /* 16Mb */
     co->entry = spice_channel_coroutine;
-    co->release = NULL;
 
     coroutine_init(co);
     coroutine_yieldto(co, channel);
commit 25875b2e26a54a08571376da31684eed316df6d5
Author: Frediano Ziglio <freddy77 at gmail.com>
Date:   Tue Aug 11 17:51:18 2020 +0100

    coroutine: Initialize "current" at link time
    
    Signed-off-by: Frediano Ziglio <freddy77 at gmail.com>
    Acked-by: Marc-André Lureau <marcandre.lureau at redhat.com>

diff --git a/src/coroutine_ucontext.c b/src/coroutine_ucontext.c
index 89258b4..890acb1 100644
--- a/src/coroutine_ucontext.c
+++ b/src/coroutine_ucontext.c
@@ -84,18 +84,11 @@ void coroutine_init(struct coroutine *co)
 	cc_init(&co->cc);
 }
 
-#if 0
-static __thread struct coroutine leader;
-static __thread struct coroutine *current;
-#else
 static struct coroutine leader;
-static struct coroutine *current;
-#endif
+static struct coroutine *current = &leader;
 
 struct coroutine *coroutine_self(void)
 {
-	if (current == NULL)
-		current = &leader;
 	return current;
 }
 
diff --git a/src/coroutine_winfibers.c b/src/coroutine_winfibers.c
index 809298b..4108f13 100644
--- a/src/coroutine_winfibers.c
+++ b/src/coroutine_winfibers.c
@@ -25,7 +25,7 @@
 #include "coroutine.h"
 
 static struct coroutine leader = { 0, };
-static struct coroutine *current = NULL;
+static struct coroutine *current = &leader;
 static struct coroutine *caller = NULL;
 
 int coroutine_release(struct coroutine *co)
@@ -70,8 +70,6 @@ void coroutine_init(struct coroutine *co)
 
 struct coroutine *coroutine_self(void)
 {
-	if (current == NULL)
-		current = &leader;
 	return current;
 }
 
commit 5f162843f96baf54c74cb9a5fe9b3b5801026d14
Author: Frediano Ziglio <freddy77 at gmail.com>
Date:   Tue Aug 11 17:45:40 2020 +0100

    coroutine: Make coroutine_swap static
    
    Only used by different coroutine implementations
    
    Signed-off-by: Frediano Ziglio <freddy77 at gmail.com>
    Acked-by: Marc-André Lureau <marcandre.lureau at redhat.com>

diff --git a/src/coroutine.h b/src/coroutine.h
index 95456b7..d2f367a 100644
--- a/src/coroutine.h
+++ b/src/coroutine.h
@@ -58,8 +58,6 @@ void coroutine_init(struct coroutine *co);
 
 int coroutine_release(struct coroutine *co);
 
-void *coroutine_swap(struct coroutine *from, struct coroutine *to, void *arg);
-
 struct coroutine *coroutine_self(void);
 
 void *coroutine_yieldto(struct coroutine *to, void *arg);
diff --git a/src/coroutine_gthread.c b/src/coroutine_gthread.c
index 5f02b45..1862092 100644
--- a/src/coroutine_gthread.c
+++ b/src/coroutine_gthread.c
@@ -100,7 +100,7 @@ int coroutine_release(struct coroutine *co G_GNUC_UNUSED)
 	return 0;
 }
 
-void *coroutine_swap(struct coroutine *from, struct coroutine *to, void *arg)
+static void *coroutine_swap(struct coroutine *from, struct coroutine *to, void *arg)
 {
 	from->runnable = FALSE;
 	to->runnable = TRUE;
diff --git a/src/coroutine_ucontext.c b/src/coroutine_ucontext.c
index d4e57e6..89258b4 100644
--- a/src/coroutine_ucontext.c
+++ b/src/coroutine_ucontext.c
@@ -99,7 +99,7 @@ struct coroutine *coroutine_self(void)
 	return current;
 }
 
-void *coroutine_swap(struct coroutine *from, struct coroutine *to, void *arg)
+static void *coroutine_swap(struct coroutine *from, struct coroutine *to, void *arg)
 {
 	int ret;
 	to->data = arg;
diff --git a/src/coroutine_winfibers.c b/src/coroutine_winfibers.c
index a56d33d..809298b 100644
--- a/src/coroutine_winfibers.c
+++ b/src/coroutine_winfibers.c
@@ -75,7 +75,7 @@ struct coroutine *coroutine_self(void)
 	return current;
 }
 
-void *coroutine_swap(struct coroutine *from, struct coroutine *to, void *arg)
+static void *coroutine_swap(struct coroutine *from, struct coroutine *to, void *arg)
 {
 	to->data = arg;
 	current = to;
commit afa6a89d5f9d45f119af1fcc5255822c4542243b
Author: Frediano Ziglio <freddy77 at gmail.com>
Date:   Tue Aug 11 17:46:16 2020 +0100

    coroutine: Do not check coroutine_self for NULL
    
    The function never returns a NULL value
    
    Signed-off-by: Frediano Ziglio <freddy77 at gmail.com>
    Acked-by: Marc-André Lureau <marcandre.lureau at redhat.com>

diff --git a/src/coroutine.h b/src/coroutine.h
index 9eb8e0c..95456b7 100644
--- a/src/coroutine.h
+++ b/src/coroutine.h
@@ -68,8 +68,9 @@ void *coroutine_yield(void *arg);
 
 gboolean coroutine_is_main(struct coroutine *co);
 
-static inline gboolean coroutine_self_is_main(void) {
-	return coroutine_self() == NULL || coroutine_is_main(coroutine_self());
+static inline gboolean coroutine_self_is_main(void)
+{
+	return coroutine_is_main(coroutine_self());
 }
 
 /*
commit 3b24b556fdef88e4582cffec01800d24bb65ae24
Author: Frediano Ziglio <freddy77 at gmail.com>
Date:   Tue Aug 11 21:23:09 2020 +0100

    coroutine_gthread: Do not call g_cond_init
    
    Not necessary from Glib 2.32 for static allocated conditions.
    
    Signed-off-by: Frediano Ziglio <freddy77 at gmail.com>
    Acked-by: Marc-André Lureau <marcandre.lureau at redhat.com>

diff --git a/src/coroutine_gthread.c b/src/coroutine_gthread.c
index 08b6f7b..5f02b45 100644
--- a/src/coroutine_gthread.c
+++ b/src/coroutine_gthread.c
@@ -43,8 +43,6 @@ static void coroutine_system_init(void)
 
 	CO_DEBUG("INIT");
 
-	g_cond_init(&run_cond);
-
 	CO_DEBUG("LOCK");
 	g_mutex_lock(&run_lock);
 
commit 87dc8edfd54a925f0f36cd1efd3b50137dc34a1a
Author: Frediano Ziglio <freddy77 at gmail.com>
Date:   Tue Aug 11 21:22:28 2020 +0100

    coroutine_gthread: Remove repeated semicolon
    
    Signed-off-by: Frediano Ziglio <freddy77 at gmail.com>
    Acked-by: Marc-André Lureau <marcandre.lureau at redhat.com>

diff --git a/src/coroutine_gthread.c b/src/coroutine_gthread.c
index 5eebc17..08b6f7b 100644
--- a/src/coroutine_gthread.c
+++ b/src/coroutine_gthread.c
@@ -25,7 +25,7 @@
 #include <stdlib.h>
 
 static GCond run_cond;
-static GMutex run_lock;;
+static GMutex run_lock;
 static struct coroutine *current;
 static struct coroutine leader;
 


More information about the Spice-commits mailing list