Any plans for glib to contain atomic compare/exchange?



Hi,

In Win32 there are a group of functions that allow you to perform swaps,
compares, compares and swaps and inc/dec in an atomic fashion. They are
called for instance InterlockedExchange, InterlockedCompareExchance,
InterlockedIncrement etc. 

Internally they are implemented in terms of a couple of assembly
instructions which use the bus assert instructions of the CPU (on x86
"lock") followed by a single instruction like inc, cmpxchg.

Because the instructions are atomic, obviously the scheduler cannot
interrupt the comparison/exchange/increment halfway through, and the bus
assert prevents other CPUs from interfering also.

These are very handy for implementing thread safe reference counts,
termination signals/variables, linked lists and so on without the use of
an explicit mutex. That makes them more convenient and higher
performance.

I've seen implementations for these functions for at least x86 and PPC.
On CPUs where they aren't supported of course a standard mutex can be
used (you take a slight performance hit though obviously).

It's this kind of simple throwaway function that GLib is great for. Are
there any plans to implement this sort of thing? Should I file it in
bugzilla, or is it too low-level? Should I post this to gtk-devel rather
than here? Possible API:

(inline) void g_interlocked_increment(guint *i);
(inline) void g_interlocked_exchange(guint *src, guint *dest);

Well, s/interlocked/atomic/ at your leisure of course. Thoughts?

thanks -mike

-- 
Mike Hearn <m hearn signal qinetiq com>
QinetiQ - Malvern Technology Center




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