Use atexit() only if it's safe to call from shared library

Apparently they are not (advertised as?) safe on BSD systems.
We ignore the case of static libraries.

Whitelisted on glibc, Android, and MSVC / mingw.

https://bugs.freedesktop.org/show_bug.cgi?id=82246