1 #include <pthread.h>
2 #include <openssl/crypto.h>
4 static pthread_mutex_t *mutexes;
6 static void pthreads_locking_callback(int mode, int n, const char *file, int line) {
7 if (mode & CRYPTO_LOCK)
8 pthread_mutex_lock(mutexes+n);
9 else
10 pthread_mutex_unlock(mutexes+n);
11 }
13 static unsigned long pthreads_thread_id(void) {
14 return (unsigned long)pthread_self();
15 }
17 void openssl_thread_setup(void) {
18 int i, l;
20 mutexes = OPENSSL_malloc((l = CRYPTO_num_locks()) * sizeof(pthread_mutex_t));
22 for (i = 0; i < l; i++)
23 pthread_mutex_init(mutexes+i, NULL);
25 CRYPTO_set_id_callback(pthreads_thread_id);
26 CRYPTO_set_locking_callback(pthreads_locking_callback);
27 }
29 void openssl_thread_cleanup(void) {
30 int i, l;
32 CRYPTO_set_locking_callback(NULL);
34 l = CRYPTO_num_locks();
35 for (i = 0; i < l; i++)
36 pthread_mutex_destroy(mutexes+i);
38 OPENSSL_free(mutexes);
39 }