[Spice-devel] [PATCH spice-server 02/16] test-display-base: Protect command ring with a mutex

Frediano Ziglio fziglio at redhat.com
Mon Sep 4 10:57:10 UTC 2017


The ring is accessed by multiple thread.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/tests/test-display-base.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/server/tests/test-display-base.c b/server/tests/test-display-base.c
index 69e0b8d2..ac1cffca 100644
--- a/server/tests/test-display-base.c
+++ b/server/tests/test-display-base.c
@@ -26,6 +26,7 @@
 #include <sys/select.h>
 #include <sys/types.h>
 #include <getopt.h>
+#include <pthread.h>
 
 #include "spice.h"
 #include <spice/qxl_dev.h>
@@ -467,27 +468,35 @@ static void get_init_info(SPICE_GNUC_UNUSED QXLInstance *qin,
 static unsigned int commands_end = 0;
 static unsigned int commands_start = 0;
 static struct QXLCommandExt* commands[1024];
+static pthread_mutex_t command_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 #define COMMANDS_SIZE G_N_ELEMENTS(commands)
 
 static void push_command(QXLCommandExt *ext)
 {
-    spice_assert(commands_end - commands_start < (int) COMMANDS_SIZE);
+    pthread_mutex_lock(&command_mutex);
+    spice_assert(commands_end - commands_start < COMMANDS_SIZE);
     commands[commands_end % COMMANDS_SIZE] = ext;
     commands_end++;
+    pthread_mutex_unlock(&command_mutex);
 }
 
 static struct QXLCommandExt *get_simple_command(void)
 {
+    pthread_mutex_lock(&command_mutex);
     struct QXLCommandExt *ret = commands[commands_start % COMMANDS_SIZE];
     spice_assert(commands_start < commands_end);
     commands_start++;
+    pthread_mutex_unlock(&command_mutex);
     return ret;
 }
 
 static int get_num_commands(void)
 {
-    return commands_end - commands_start;
+    pthread_mutex_lock(&command_mutex);
+    int ret = commands_end - commands_start;
+    pthread_mutex_unlock(&command_mutex);
+    return ret;
 }
 
 // called from spice_server thread (i.e. red_worker thread)
-- 
2.13.5



More information about the Spice-devel mailing list