On Thu, May 07, 2020 at 08:14:57PM -0400, Thor Lancelot Simon wrote: > On Fri, May 08, 2020 at 01:51:16AM +0200, Kamil Rytarowski wrote: > > A runtime detection could be a part of ifunc (is it ready for NetBSD?). > > > > The standard C/C++ feature is to detect whether atomic operations are > > real (lock-free) through atomic_is_lock_free(). This is a feature, not a > > bug (as claimed by some people). atomic_is_lock_free() can be overloaded > > in libatomic and detect CPU type in runtime and redirect either to real > > CPU intrinsic of lock-free fallback. > > Not without performance penalty for every atomic operation, unless you propose > to do this by binary patch as is done in the kernel.
ifuncs are pretty cool in enabling to do this everywhere, not just in a kernel with binary patching. It lets you write a "resolver function" which is run by ld.so in the first run of the function, and it returns which variant of the function should be used. https://sourceware.org/glibc/wiki/GNU_IFUNC