cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: HTTPS and Multi-threading

From: Dirk Manske <dm_at_sysformance.com>
Date: Fri, 11 Jun 2004 14:22:38 +0200

On Wednesday 09 June 2004 03:14, Olano, Ever wrote:
> 2. > What's that [mttest.c]?
> It's the sample code in OpenSSL that shows how to implement and set
> the callback functions needed for multithreading to work. I wanted to know if
> anyone has tried it as I wanted to just copy it. Do you or anyone, by any
> chance, have their own code for the locking and id callback functions?

After a short look at mttest.c I wrote following code (for linux):

static pthread_mutex_t *ssl_mutexes = NULL;
static int ssl_mutexes_cnt = 0;

void sslLocking(int mode, int n, const char *file, int line) {
    if( n < ssl_mutexes_cnt ) {
        if( mode&CRYPTO_LOCK ) {
           pthread_mutex_lock( &(ssl_mutexes[n]) );
        } else if( mode&CRYPTO_UNLOCK ) {
           pthread_mutex_unlock( &(ssl_mutexes[n]) );
        }
        return;
    }
     // error...
}

unsigned long sslThreadId(void)
{
    return (unsigned long)pthread_self();
}

...

    // create ssl mutexes
    ssl_mutexes_cnt = CRYPTO_num_locks();
    ssl_mutexes = (pthread_mutex_t*) malloc( ssl_mutexes_cnt * sizeof(pthread_mutex_t) );
    for( int i=0; i<ssl_mutexes_cnt; i++ ) {
        if( pthread_mutex_init( &(ssl_mutexes[i]), NULL ) != 0 ) {
            // error...
            abort();
        }
    }
    CRYPTO_set_locking_callback( sslLocking );
    CRYPTO_set_id_callback( sslThreadId );

....

    // free ssl mutexes
    if( ssl_mutexes ) {
        for( int i=0; i<ssl_mutexes_cnt; i++ ) {
            pthread_mutex_destroy( &(ssl_mutexes[i]) );
        }
        free( ssl_mutexes );
    }
Received on 2004-06-11