Atomics (or GCC intrinsics) were first introduced in InnoDB (5.0) by a patch  from Mark Callaghan’s team at Google for mutexes and rw-locks. In

InnoDB: Converting old atomic code to C++11

submited by
Style Pass
2021-06-28 19:00:10

Atomics (or GCC intrinsics) were first introduced in InnoDB (5.0) by a patch  from Mark Callaghan’s team at Google for mutexes and rw-locks. InnoDB code then was written in C.  When the code was ported to C++ ,  part of the 5.6 release, there was no C++ standard for atomics. Over time this led to a hodge lodge of code tweaked for different platforms, hardware, operating systems and compilers.

There were many things wrong with what we ended up with. This old code didn’t handle all types, the supported  types were hardcoded for a few platforms and compilers only, with fallback to mutexes for all other access. Memory ordering rules were difficult to reason about because some of the GCC intrinsics  were more strict than required and this made the rules unclear, we usually tried to take the more conservative approach. This would sometimes be at the cost of some performance. What is more important we did not have means to force developers to always access a given variable through atomic operation – the decision to use atomic or non-atomic access was taken independently for each access. Therefore there were some variables on which some operations were performed atomically and some not, causing to be a UndefinedBehaviour at best.

This change to move all atomic operations to the C++11 standard  has increased portability – we use well tested code from C++ standard and don’t need to reinvent the wheel anymore.

Leave a Comment