chapter9, OpenSSL multi-threading support

English

This code stores all objects in the memory allocated by “OPELSSL_malloc”. However, as of now, “OPENSSL_malloc” is equivalent to “malloc”, so you may not need to do so far.

used “new” code.

ppmutexOpenSSL = (CCriticalSection **)
    ::OPENSSL_malloc(::CRYPTO_num_locks() *
    sizeof(CCriticalSection *));

if(! ppmutexOpenSSL) {
    throw std::runtime_error("CInit OPENSSL_malloc allocate failure.");
}

for (int i = 0; i < ::CRYPTO_num_locks(); ++i)
{
    ppmutexOpenSSL[i] = new CCriticalSection();
}
::CRYPTO_set_locking_callback(locking_callback);

#ifdef WIN32
    ::RAND_screen();
    ::RAND_poll();
#endif

//
// Seed random number generator with performance counter
//
seed::RandAddSeed();

//
// We use OpenSSL.
//

//
// Shutdown OpenSSL library multithreading support
//
::CRYPTO_set_locking_callback(NULL);
for (int i = 0; i < ::CRYPTO_num_locks(); ++i)
{
    delete ppmutexOpenSSL[i];
}
::OPENSSL_free(ppmutexOpenSSL);


used "placement new" code.

pmutexOpenSSL = (CCriticalSection *)
    ::OPENSSL_malloc(::CRYPTO_num_locks() *
    sizeof(CCriticalSection));

if(! pmutexOpenSSL) {
    throw std::runtime_error("CInit OPENSSL_malloc allocate failure.");
}

for (int i=0; i < ::CRYPTO_num_locks(); ++i)
{
    new(pmutexOpenSSL + i) CCriticalSection;
}
::CRYPTO_set_locking_callback(locking_callback);

#ifdef WIN32
    ::RAND_screen();
    ::RAND_poll();
#endif

//
// Seed random number generator with performance counter
//
seed::RandAddSeed();

//
// We use OpenSSL.
//

//
// Shutdown OpenSSL library multithreading support
//
::CRYPTO_set_locking_callback(NULL);
for (int i=0; i < ::CRYPTO_num_locks(); ++i)
{
    (pmutexOpenSSL + i)->~CCriticalSection();
}
::OPENSSL_free(pmutexOpenSSL);
タイトルとURLをコピーしました