1 /* $Id$ */
3 /***
4 This file is part of fusedav.
6 fusedav is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 fusedav is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with fusedav; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ***/
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #include <pthread.h>
26 #include <openssl/crypto.h>
28 static pthread_mutex_t *mutexes;
30 static void pthreads_locking_callback(int mode, int n, const char *file, int line) {
31 if (mode & CRYPTO_LOCK)
32 pthread_mutex_lock(mutexes+n);
33 else
34 pthread_mutex_unlock(mutexes+n);
35 }
37 static unsigned long pthreads_thread_id(void) {
38 return (unsigned long)pthread_self();
39 }
41 void openssl_thread_setup(void) {
42 int i, l;
44 mutexes = OPENSSL_malloc((l = CRYPTO_num_locks()) * sizeof(pthread_mutex_t));
46 for (i = 0; i < l; i++)
47 pthread_mutex_init(mutexes+i, NULL);
49 CRYPTO_set_id_callback(pthreads_thread_id);
50 CRYPTO_set_locking_callback(pthreads_locking_callback);
51 }
53 void openssl_thread_cleanup(void) {
54 int i, l;
56 CRYPTO_set_locking_callback(NULL);
58 l = CRYPTO_num_locks();
59 for (i = 0; i < l; i++)
60 pthread_mutex_destroy(mutexes+i);
62 OPENSSL_free(mutexes);
63 }