GMutex *
g_static_mutex_get_mutex_impl (GMutex **mutex)
{
  if (*mutex)
    return *mutex;

  if (!g_thread_supported ())
    return NULL;

  g_assert (g_mutex_protect_static_mutex_allocation);

  g_mutex_lock (g_mutex_protect_static_mutex_allocation);

  if (!(*mutex))
    {
      GMutex *new_mutex = g_mutex_new (); 
      
      /* The following is a memory barrier to avoid the write 
       * to *new_mutex being reordered to after writing *mutex, 
       * which of course is only theoretically possible */
      g_mutex_lock (new_mutex);
      g_mutex_unlock (new_mutex);

      *mutex = new_mutex;
    }

  g_mutex_unlock (g_mutex_protect_static_mutex_allocation);
  
  return *mutex;
}
