1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: opensslthreadlock.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
10 * Example source code to show one way to set the necessary OpenSSL locking
11 * callbacks if you want to do multi-threaded transfers with HTTPS/FTPS with
12 * libcurl built to use OpenSSL.
14 * This is not a complete stand-alone example.
16 * Author: Jeremy Brown
22 #include <openssl/err.h>
24 #define MUTEX_TYPE pthread_mutex_t
25 #define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL)
26 #define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x))
27 #define MUTEX_LOCK(x) pthread_mutex_lock(&(x))
28 #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x))
29 #define THREAD_ID pthread_self( )
32 void handle_error(const char *file
, int lineno
, const char *msg
){
33 fprintf(stderr
, "** %s:%d %s\n", file
, lineno
, msg
);
34 ERR_print_errors_fp(stderr
);
38 /* This array will store all of the mutexes available to OpenSSL. */
39 static MUTEX_TYPE
*mutex_buf
= NULL
;
42 static void locking_function(int mode
, int n
, const char * file
, int line
)
44 if (mode
& CRYPTO_LOCK
)
45 MUTEX_LOCK(mutex_buf
[n
]);
47 MUTEX_UNLOCK(mutex_buf
[n
]);
50 static unsigned long id_function(void)
52 return ((unsigned long)THREAD_ID
);
55 int thread_setup(void)
59 mutex_buf
= (MUTEX_TYPE
*)malloc(CRYPTO_num_locks( ) * sizeof(MUTEX_TYPE
));
62 for (i
= 0; i
< CRYPTO_num_locks( ); i
++)
63 MUTEX_SETUP(mutex_buf
[i
]);
64 CRYPTO_set_id_callback(id_function
);
65 CRYPTO_set_locking_callback(locking_function
);
69 int thread_cleanup(void)
75 CRYPTO_set_id_callback(NULL
);
76 CRYPTO_set_locking_callback(NULL
);
77 for (i
= 0; i
< CRYPTO_num_locks( ); i
++)
78 MUTEX_CLEANUP(mutex_buf
[i
]);