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 <jfons...@vmware.com>
Signed-off-by: Emil Velikov <emil.l.veli...@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 <jfons...@vmware.com>
_______________________________________________
waffle mailing list
waffle@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/waffle

Reply via email to