[waffle] [PATCH 2/3] wcore: rework non-compatible mutex initialization

Jose Fonseca jfonseca at vmware.com
Fri Mar 20 04:46:00 PDT 2015


On 19/03/15 22:26, Emil Velikov wrote:
> C11 does not specify a static initializer, based on the idea that the
> a mutex will be platform and/or implementation dependent. As such the
> alternative solution is to initialize the mutex with mtx_init().
> This will allow us to remove the transition hack, and in due time move
> to the system's C11 threads implementation.
>
> Signed-off-by: Emil Velikov <emil.l.velikov at gmail.com>
> ---
>   src/waffle/core/wcore_display.c | 6 +++---
>   src/waffle/core/wcore_display.h | 3 ++-
>   2 files changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/src/waffle/core/wcore_display.c b/src/waffle/core/wcore_display.c
> index 2feeeba..49aeae6 100644
> --- a/src/waffle/core/wcore_display.c
> +++ b/src/waffle/core/wcore_display.c
> @@ -34,14 +34,14 @@ wcore_display_init(struct wcore_display *self,
>                      struct wcore_platform *platform)
>   {
>       static size_t id_counter = 0;
> -    static mtx_t mutex = _MTX_INITIALIZER_NP;

IIUC, this mutex protecting the static id_counter above, not the 
wcore_display object, or anything, so moving mtx to display seems incorrect.

The ideal solution here would be to use C11 atomics, but that's a bunch 
of extra portibility code to maintain...  The portable way of fixing 
this with C11 threads is using once_flag.

Jose

>
>       assert(self);
>       assert(platform);
>
> -    mtx_lock(&mutex);
> +    mtx_init(&self->mutex, mtx_plain);
> +    mtx_lock(&self->mutex);
>       self->api.display_id = ++id_counter;
> -    mtx_unlock(&mutex);
> +    mtx_unlock(&self->mutex);
>
>       self->platform = platform;
>
> diff --git a/src/waffle/core/wcore_display.h b/src/waffle/core/wcore_display.h
> index de6ca5e..c4348c4 100644
> --- a/src/waffle/core/wcore_display.h
> +++ b/src/waffle/core/wcore_display.h
> @@ -39,6 +39,7 @@ union waffle_native_display;
>   struct wcore_display {
>       struct api_object api;
>       struct wcore_platform *platform;
> +    mtx_t mutex;
>   };
>
>   static inline struct waffle_display*
> @@ -59,7 +60,7 @@ wcore_display_init(struct wcore_display *self,
>   static inline bool
>   wcore_display_teardown(struct wcore_display *self)
>   {
> -    (void) self;
>       assert(self);
> +    mtx_destroy(&self->mutex);
>       return true;
>   }
>



More information about the waffle mailing list