Redmine #5337: Fix sporadic deadlock in dlsym().

Apparently dlsym() is not threadsafe, so we need to protect it with a
mutex.
Redmine #5337: Optimize by caching calls to dlsym().

- Introduce static function pointer which we save between calls.

- Thread safety is not needed, since even if two threads try to write
  to the function pointer simultaneously, both will write the same
  value.

- The old stub code is inline, so we cannot use a static variable
  there. Fixed my moving the stub code into the stub macro instead,
  where it doesn't need to be inline.