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

Jose Fonseca jfonseca at vmware.com
Mon Mar 30 03:22:47 PDT 2015


On 27/03/15 23:28, 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 call_once/mtx_init.
> This will allow us to remove the transition hack, and in due time move
> to the system's C11 threads implementation.
>
> v2: Actually use call_once() to prevent the possibility of multiple
> threads hitting the mtx_init() at the same time. Suggested by Jose.
>
> v3: Do not destroy the mutex. Suggested by Chad.
>
> Cc: Jose Fonseca <jfonseca at vmware.com>
> Signed-off-by: Emil Velikov <emil.l.velikov at gmail.com>
> ---
>   src/waffle/core/wcore_display.c | 11 ++++++++++-
>   1 file changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/src/waffle/core/wcore_display.c b/src/waffle/core/wcore_display.c
> index 2feeeba..40f98cf 100644
> --- a/src/waffle/core/wcore_display.c
> +++ b/src/waffle/core/wcore_display.c
> @@ -29,16 +29,25 @@
>
>   #include "wcore_display.h"
>
> +static mtx_t mutex;
> +
> +static void
> +wcore_display_init_once(void)
> +{
> +    mtx_init(&mutex, mtx_plain);
> +}
> +
>   bool
>   wcore_display_init(struct wcore_display *self,
>                      struct wcore_platform *platform)
>   {
>       static size_t id_counter = 0;
> -    static mtx_t mutex = _MTX_INITIALIZER_NP;
> +    static once_flag flag = ONCE_FLAG_INIT;
>
>       assert(self);
>       assert(platform);
>
> +    call_once(&flag, wcore_display_init_once);
>       mtx_lock(&mutex);
>       self->api.display_id = ++id_counter;
>       mtx_unlock(&mutex);
>

Reviewed-by:  Jose Fonseca <jfonseca at vmware.com>


More information about the waffle mailing list