This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Purpose of USE_ATOMIC_COMPILER_BUILTINS


On Mon, 2015-11-16 at 16:37 +0100, Florian Weimer wrote:
> What is the USE_ATOMIC_COMPILER_BUILTINS?  Is it still needed now that
> we require GCC 4.7 or later to compile glibc?

Joseph answered that already.  It is basically an implementation detail
that only matters when implementing atomics.

> I'm asking because the provided set of macros is fairly incomplete
> (there is fetch_add, but not sub_fetch), and there are some rather
> questionable mappings, for example:

https://sourceware.org/glibc/wiki/Concurrency

Maybe we need to clarify this on this wiki page, but if you are fixing
concurrent code, use the C11 atomics.  The interface for that isn't
exactly C11, but mimics it closely.  The semantic equivalence makes it
easy for programmers familiar with C11, while not using C11 atomics
directly makes us more independent of how C11 is actually implemented
(see Joseph's remarks re libatomic, for example).

The C11-like atomic ops we have so far are what seemed to be needed in
current code.  If you need more, please either make a request for which
ones you want, or send a patch.

In the particular case, you could as well use (fetch_add(-1)-1) instead
of the sub_fetch (it's all unsigned ints), unless you are concerned
about the extra subtraction being too slow.

> atomic_increment â atomic_add â atomic_exchange_and_add â
> atomic_exchange_and_add_acq
> 
> This suggests that atomic_increment only provides acquire memory
> ordering, which is quite surprising.

Well, that's what it was and so I didn't change that (unless a bug
slipped in somewhere, but I paid attention to not change the MOs of
old-style atomics and also checked for same semantics across archs).

If you think this mapping is bad, and you're surprised that this is only
acquire MO, this is just one more reason to use the new-style C11
atomics! :)

> I'm dealing with a few data races and deadlocks in malloc right now, and
> it would be nice to use the right atomics to solve them.  But based on
> the contents of the <atomic.h> header, I'm not sure if this is possible.

It definitely is possible.  You should use the C11 atomics.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]