Do an optimization/issue-correcting step during opening the database

Optimization step detects broken databases, so it lowers chances of agents
hanging forever or segfaulting inside libtokyocabinet.
(cherry picked from commit 85c165bd072a1c5a64c11faa7d1541f555226df5)
Don't provoke database corruption by mixing read/write and cursor operations
(cherry picked from commit 15176584a52ffa09bef437a04c2a07985753aea9)
Silence deprecated warnings when compiling on Mac OSX.

Majority of functions in /usr/include/openssl/crypto.h are marked as
deprecated for Mac OSX 10.7 and later. Silence warnings.
Initialize OpenSSL with callback functions for threadlocking.

This makes OpenSSL threadsafe, and allows us to remove locking around the
calls to the RSA encryption functions in cf-serverd's AuthenticationDialogue.
Implements #2678

Note: the functions used - as well as most other functions used from
OpenSSL - are marked as deprecated on Mac OSX 10.7 and later.
Compiler warnings explicitly disabled. Functions documented in OpenSSL
documentation (http://www.openssl.org/docs/crypto/threads.html) to replace
some of these functions (such as CRYPTO_THREADID_set_callback) are not
available. This implementation follows the example provided on
http://www.opensource.apple.com/source/OpenSSL/OpenSSL-10/openssl/crypto/threads/mttest.c
With OpenSSL initialized to be made thread-safe, we don't have to serialize
access to the OpenSSL functions anymore.

Some other things were protected here by the cft_system locking:

- the ServerConnectionState *conn

This structure is initialized in the main thread, and then passed to the
thread handler, which takes exclusive ownership. No access to this object
from other threads is possible or allowed.

- malloc calls

Those are implicily threadsafe on all supported platforms today
Exclude callbacks that make OpenSSL threadsafe if we don't have pthread.
Don't de-initialize OpenSSL from function called via signal handler.

Threads are still running and might call OpenSSL functions. Removing the
callbacks etc leads to segfaults.

This leaves a memory leak at shutdown.
Don't register a thread-ID callback on Windows.

pthread_t on MinGW32 is a struct, and long is not large enough to hold the void*
within.

OpenSSL documentation states: If the application does not register such a callback
using CRYPTO_THREADID_set_callback(), then a default implementation is used - on
Windows and BeOS this uses the system's default thread identifying APIs

So not setting this callback at all will be safe.
Implement throtteling of main server loop if there is a thread for each
core already.

Environment variable CFENGINE_THROTTLE_SERVER_LOOP can be set to an
integer multiplier that allows fine tuning. If not set or set to 0, no
throttling.

Only implemented on platforms where we can trivially read the number of
cores.