Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / docs / examples / opensslthreadlock.c
blob7e3255cc8389f0c8acbc2903eb3f36eb6e4f38db
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
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
20 #include <stdio.h>
21 #include <pthread.h>
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);
35 /* exit(-1); */
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]);
46 else
47 MUTEX_UNLOCK(mutex_buf[n]);
50 static unsigned long id_function(void)
52 return ((unsigned long)THREAD_ID);
55 int thread_setup(void)
57 int i;
59 mutex_buf = (MUTEX_TYPE *)malloc(CRYPTO_num_locks( ) * sizeof(MUTEX_TYPE));
60 if (!mutex_buf)
61 return 0;
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);
66 return 1;
69 int thread_cleanup(void)
71 int i;
73 if (!mutex_buf)
74 return 0;
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]);
79 free(mutex_buf);
80 mutex_buf = NULL;
81 return 1;