issue-496: Fix possible deadlock in thread+fork

This patch adds a pthread_atfork handler to set the internal
locks in consistent state.
issue-443: Tunning small allocation