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 #include "fusedav.h"
29 #include "openssl-thread.h"
31 static pthread_mutex_t *mutexes;
33 static void pthreads_locking_callback(int mode, int n, __unused const char *file, __unused int line) {
34 if (mode & CRYPTO_LOCK)
35 pthread_mutex_lock(mutexes+n);
36 else
37 pthread_mutex_unlock(mutexes+n);
38 }
40 static unsigned long pthreads_thread_id(void) {
41 return (unsigned long) pthread_self();
42 }
44 void openssl_thread_setup(void) {
45 int i, l;
47 mutexes = OPENSSL_malloc((l = CRYPTO_num_locks()) * sizeof(pthread_mutex_t));
49 for (i = 0; i < l; i++)
50 pthread_mutex_init(mutexes+i, NULL);
52 CRYPTO_set_id_callback(pthreads_thread_id);
53 CRYPTO_set_locking_callback(pthreads_locking_callback);
54 }
56 void openssl_thread_cleanup(void) {
57 int i, l;
59 CRYPTO_set_locking_callback(NULL);
61 l = CRYPTO_num_locks();
62 for (i = 0; i < l; i++)
63 pthread_mutex_destroy(mutexes+i);
65 OPENSSL_free(mutexes);
66 }