LWN.net Logo

Toward generic atomic operations

Toward generic atomic operations

Posted Aug 3, 2012 1:59 UTC (Fri) by wahern (subscriber, #37304)
Parent article: Toward generic atomic operations

I realize that this is Linux Weekly News, but since when was Linux the only platform with pthreads support? Heck, even Windows has pthreads-win32. The only platform I know of with broken pthreads is OpenBSD, which is going through a messy transition from user threads to 1:1 pre-emptive threading. (I say broken because some signal handling is presently fubar.) The reason people roll their own threading library isn't because they want to be portable to non-Linux, it's because they want to be "portable" to Windows. That's a tremendous distinction. (Also, it's because rolling your own threading API is one of those low-level tasks that's enticing to people who still think of C as a high-level assembler, aching to be abused.)

And, unless I'm mistaken, both GCC and clang have the capability to support <stdatomic.h>, they just don't ship with that header. But all the way back to GCC 4.1'ish (an eternity ago), GCC has had atomic primitives, based on a proposed Intel specification and which bares more than a passing resemblance to the C11 and C++11 API. If you want <stdatomic.h> you can find an implementation based on GCC and clang intrinsics here,

http://svnweb.freebsd.org/base/head/include/stdatomic.h?v...

I'm not sure if it's 100% complete, but for all the basic stuff (increment, store, load, compare-and-swap), it should suffice.


(Log in to post comments)

Toward generic atomic operations

Posted Aug 3, 2012 3:34 UTC (Fri) by jcm (subscriber, #18262) [Link]

Like I said, there are implementations already that you can install (there's also been stuff in P99, and Google turns up others, like your example), and I noted that GCC has built-ins for atomics. I didn't go into the older implementation that wasn't universal for every architecture and wasn't used by end applications because that wasn't really its purpose. C11 by contrast isn't a hack, it's a standard that is supposed to be used universally.

Anyway, thanks for the feedback!

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds