Reputation:
Is there any small library, that wrapps various processors' CAS-like operations into macros or functions, that are portable across multiple compilers?
PS. The atomic.hpp library is inside boost::interprocess::detail namespace. The author refuses to make it a public, well maintained library.
Lets reopen the question, and see if there are any other options?
Upvotes: 31
Views: 32959
Reputation: 2875
What the author said (in the link you provided) was "I think you can use them safely until some official Boost library comes". Deferring the interface change until "when atomic functions are going to be present in C++0x".
Whatever you use today, you're likely going to want to migrate to new std::
functionality when it's available anyway.
The boost stuff is generally pretty good, looks like it's used in the implementation of a released Boost library. I've also been tempted to use that implementation a few times.
I'd go for it.
Upvotes: 2
Reputation: 16554
There is a proposed C++0x-compatible Boost atomics library: http://www.chaoticmind.net/~hcb/projects/boost.atomic/
The purpose of this library is to provide an implementation of atomic operations for boost, based on the interface specified by the C++0x draft standard. It aims to make transitioning to std::atomic easy, as well as providing a means to make code using this C++0x feature compilable on older systems.
It's obviously not part of Boost yet, but you can check out the review thread here: http://lists.boost.org/Archives/boost/2009/12/160195.php
Boost.Atomic is now in a form that I consider calling it a release. It has support for "true" atomic variables on:
- gcc/x86, 32-bit (tested on Linux, FreeBSD)
- gcc/x86, 64-bit (tested on Linux)
- gcc/powerpc32 (tested on Linux, Mac OS X)
- gcc/powerpc64 (untested)
- generic Win32 (tested with Visual Studio Express on Win XP)
For all others it falls back gracefully to locked operation. There is proper quickbook documentation, including a hopefully illustrative example section.
Upvotes: 3
Reputation: 2153
OPA (Open Portable Atomics) could be a good fit for your needs. https://trac.mcs.anl.gov/projects/openpa/
It provides a consistent C API to common atomic operations across multiple platforms under an MIT-style license. The library is small and certainly meets your size requirements. The current platform list is:
I've never used it in a C++ program, although it ought to work with little or no changes. I'd be happy to tweak it if you run into trouble (just mail [email protected]).
Upvotes: 30
Reputation: 5001
You could also look at libsync for inspiration from http://www.ioremap.net/node/224 , which is quite new (maybe too new), but it is being used in the Elliptics Network so it does get (some?) testing.
It also gives you higher level primitives next to CAS: RCU (Read Copy Update) for lockless synchronisation between threads.
But it depends on what you mean by 'portable': it supports archtectures x86 and PPC, OSes Linux, FreeBSD, OpenBSD, Solaris and MacOSX but ... no Windows.
And the license is GPL, which you can hate or love.
Upvotes: 0
Reputation: 31053
There is the library of the atomic_ops project by Boehm. Dunno about the license, though.
Upvotes: 6
Reputation: 6477
Intel Threading Building Blocks has a nice portable atomic<T>
template which does what you want. But whether it is a small library or not can of course be debated..
Upvotes: 15
Reputation: 43452
On Mac OS X and Windows there are builtin CompareAndSwap functions you should be using anyway (InterlockedCompareExchange() and OSAtomicCompareAndSwapPtrBarrier() respectively). Thus will work regardless of the compilers on those platforms.
On other Unixes it is a bit trickier, if you are using GCC 4.1 or later you can just use its builtin __sync_val_compare_and_swap(), and many though not all unix compilers support reasonable gcc extensions since a lot of code originating on Linux assumes they are present.
So if you want to wrap them up in a way that works with most all compilers for all processors on OS X and Windows, and with GCC and some other compilers on other platforms you should do something like:
boolean CompareAndSwapPointer(volatile * void * ptr,
void * new_value,
void * old_value) {
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050
return OSAtomicCompareAndSwapPtr (old_value, new_value, ptr);
#elif defined(_MSC_VER)
return InterlockedCompareExchange(ptr, new_value, old_value);
#elif (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) > 40100
return __sync_val_compare_and_swap(ptr, old_value, new_value);
#else
# error No implementation
#endif
}
That is not tested, but I think it should be correct. Note how all the OS libraries take the args in different orders ;-)
Obviously you can do a few version for the different size compare and swaps and wrap them in templates if you want. The APIs are mostly C based and encode the type information into the functions in such a way that it is sort of nasty for people used to parameterizing types via templates.
Upvotes: 9
Reputation: 733
You might be interested in Glib's Atomic Operations functions,
g_atomic_int_compare_and_exchange()
implements the CAS semantics for various architectures. The implementation itself is relatively easy to understand and can be used stand-alone without too much effort, you can find it at svn.gnome.org/viewvc/ under glib/trunk/glib/gatomic.{c,h}. Hope this helps!
Upvotes: 11
Reputation: 11277
The boost interprocess library might be what you are after -- the Atomic.hpp include file contains compare-and-swap implementations for a variety of platforms and compilers.
Upvotes: 25