Posted on | February 21, 2010 | No Comments
I have seen no less than four questions this week on Stack Overflow regarding facilities for atomic ops on Linux (and portably speaking, anything else that hosts GCC). Yes, gcc does have some facilities for this, take a look at this rather good overview.
Unfortunately, these don’t seem to work on most ARM platforms, which tend to only exaggerate just how slow the typical “fast mutex” can be on hand held devices. Because of that, I have been eschewing global counters / flags / etc as much as possible to keep time spent acquiring locks to a minimum. I was hoping to see some answers on the Stack Overflow questions that lead in another direction, especially for ARM, but no love as of yet.
Still, on almost every other platform, these are very handy and worth noting. You can use them to effect any operation you would normally do on integer types of various sizes without wrapping the operation in a mutex like a scallop in bacon
Unfortunately, you do have to wrap them in portability kludges, which is what I’m doing now. I’ll post the code in a new HG once I get it working. It will use sync_fetch_and*() if gcc is used and the platform is agreeable, otherwise resort to an ordinary arbitrated mutex of whatever type the system provides. I spent a great deal of time looking for something like this that can plug right into autoconf, but I did not find it.
If you happen to know of something that exists, please leave a link in comments to keep me from re-inventing yet another wheel.