Re: making GStaticMutexes faster

Sebastian Wilhelmi <wilhelmi ira uka de> writes:

> recently I had an email discussion with Tim about GStaticMutexes,
> which now have a really big overhead, because before every access to
> them (locking, unlocking) another mutex has to be locked. 

Not on operating systems with POSIX thread support, right?

> This is super paranoid, as the only case it might fail without this
> additional lock is on an MP-machine without cache coherence, which
> should not only be very rare.

I think "cache coherence" is a topic, not a particular property...

The particular property you are proposing relying on is if processor
#1 writes location A and then location B, then another processor will
never see a write to location B then a write to location A.

(Since the writes are here separated by a function return it is
pretty unlikely that instruction reordering would cause problems,
so it probably is mostly a question of cache behavior, yes.)

I don't have any more information than what I had back when we
originally discussed this topic, which is that it is not safe
"in general" without an explicit memory barrier, and it is safe 
on the common ia32 processors. I still have no idea on what
set of processors / machines it will fail on.

> Additionally I doubt GLib has been
> ported to such a machine yet. IOW I would vote for a speed up, by
> just returning *mutex in g_static_mutex_get_mutex_impl, if it is
> non-zero.  This leaves the lock only for the initial calls to
> g_static_mutex_get_mutex_impl and there it of course has to be.
> So the change would be:
> Index: gthread.c
> ===================================================================
> RCS file: /cvs/gnome/glib/gthread.c,v
> retrieving revision 1.23
> diff -u -b -B -r1.23 gthread.c
> --- gthread.c	2001/05/09 12:51:21	1.23
> +++ gthread.c	2001/05/09 13:17:15
> @@ -182,6 +182,9 @@
>  GMutex *
>  g_static_mutex_get_mutex_impl (GMutex** mutex)
>  {
> +  if (*mutex)
> +    return *mutex;
> +
>    if (!g_thread_supported ())
>      return NULL;
> We could as well put that into g_static_mutex_get_mutex to also save the
> function call.
> And yes, this makes a difference, GStaticMutex suddenly becomes ~ 3 times
> faster on platforms without native static mutexes.

How common are such platforms?


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]