cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: AIX multithreaded problem in CURLE_SSL_CONNECT_ERROR,why?

From: Frank Hempel <red_socks_at_gmx.de>
Date: Sun, 21 Nov 2004 18:23:34 +0100

Hi hzhijun, sorry for the late answer...I was away for a while.
But I also would have pointed to the same code as Andy did. A helpfull
source for me was the OpenSSL-Source-Package itself. Untar it and look
up /crypto/threads/mttest.c. Or take a look at the "pthreads-essence" of
it in the attachted file. It is the same as the attatched module a mail ago.

cu, Frank.

/* this code was taken from the openssl-sources and is the "pthreads"-excerpt from /crypto/threads/mttest.c */

#include <openssl/crypto.h>

#define debug_sslcallback 0

static pthread_mutex_t *lock_cs;
static long *lock_count;

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

void openssl_locking_callback(int mode, int type, char *file, int line)
{
#if debug_sslcallback
        printf("[OPENSSL_CALLBACK] thread=%4d mode=%s lock=%s %s:%d", CRYPTO_thread_id(), (mode&CRYPTO_LOCK)?"l":"u",
                     (type&CRYPTO_READ)?"r":"w",file,line);
#endif

        if (mode & CRYPTO_LOCK) {
                pthread_mutex_lock(&(lock_cs[type]));
                lock_count[type]++;
        }
        else
                pthread_mutex_unlock(&(lock_cs[type]));
}

int openssl_setup(void)
{
        int i;

        lock_cs = malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
        if (lock_cs == NULL) {
                fprintf(stderr, "No Memory for mutexes!");
                return -1;
        }

        lock_count = malloc(CRYPTO_num_locks() * sizeof(long));
        if (lock_count == NULL) {
                fprintf(stderr, "No Memory for mutexes' counter!");
                free(lock_cs);
                return -1;
        }

        for (i=0; i<CRYPTO_num_locks(); i++) {
                lock_count[i]=0;
                int ret = pthread_mutex_init(&(lock_cs[i]),NULL);
                if (ret) {
                        fprintf(stderr, "pthread_mutex_init(): %s", strerror(ret));
                        return -1;
                }
        }

        CRYPTO_set_id_callback((unsigned long (*)())openssl_thread_id);
        CRYPTO_set_locking_callback((void (*)())openssl_locking_callback);

        return 0;
}

void openssl_cleanup(void)
{
        int i;

        CRYPTO_set_locking_callback(NULL);

        for (i=0; i<CRYPTO_num_locks(); i++)
        {
                pthread_mutex_destroy(&(lock_cs[i]));
#if debug_sslcallback
                printf("%8ld:%s\n",lock_count[i], CRYPTO_get_lock_name(i));
#endif
        }

        free(lock_cs);
        free(lock_count);
}
Received on 2004-11-21