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

Christophe Fergeau cfergeau at redhat.com
Wed Sep 6 15:36:36 UTC 2017


Acked-by: Christophe Fergeau <cfergeau at redhat.com>

On Mon, Sep 04, 2017 at 11:57:10AM +0100, Frediano Ziglio wrote:
> 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
> 
> _______________________________________________
> Spice-devel mailing list
> Spice-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/spice-devel


More information about the Spice-devel mailing list