1 /*
2 Copyright (C) 2006-2007 Werner Dittmann
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <pthread.h>
21 #include <errno.h>
22 #include <gcrypt.h>
23
24 #if GCRYPT_VERSION_NUMBER < 0x010600
25 /*
26 * The following macro was copied from gcrypt.h and modified to explicitly
27 * cast the pointer types to keep the compiler happy.
28 */
29 #define GCRY_THREAD_OPTION_PTHREAD_CPP_IMPL \
30 static int gcry_pthread_mutex_init (void **priv) \
31 { \
32 int err = 0; \
33 pthread_mutex_t *lock = (pthread_mutex_t *)malloc (sizeof (pthread_mutex_t)); \
34 \
35 if (!lock) \
36 err = ENOMEM; \
37 if (!err) \
38 { \
39 err = pthread_mutex_init (lock, NULL); \
40 if (err) \
41 free (lock); \
42 else \
43 *priv = lock; \
44 } \
45 return err; \
46 } \
47 static int gcry_pthread_mutex_destroy (void **lock) \
48 { int err = pthread_mutex_destroy ((pthread_mutex_t *)*lock); free (*lock); return err; } \
49 static int gcry_pthread_mutex_lock (void **lock) \
50 { return pthread_mutex_lock ((pthread_mutex_t *)*lock); } \
51 static int gcry_pthread_mutex_unlock (void **lock) \
52 { return pthread_mutex_unlock ((pthread_mutex_t *)*lock); } \
53 \
54 static struct gcry_thread_cbs gcry_threads_pthread = \
55 { GCRY_THREAD_OPTION_PTHREAD, NULL, \
56 gcry_pthread_mutex_init, gcry_pthread_mutex_destroy, \
57 gcry_pthread_mutex_lock, gcry_pthread_mutex_unlock }
58
63 #ifdef __cplusplus
64 extern "C" {
65 #endif
67 #ifdef __cplusplus
68 }
69 #endif
70
71 #else
72 GCRY_THREAD_OPTION_PTHREAD_IMPL;
73 #endif
74
75 static int initialized = 0;
76
78 {
79
80 if (initialized) {
81 return 1;
82 }
83 #if GCRYPT_VERSION_NUMBER < 0x010600
84 gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
85 #endif
86 gcry_check_version(NULL);
87 gcry_control(GCRYCTL_DISABLE_SECMEM);
88 initialized = 1;
89 return 1;
90 }
#define GCRY_THREAD_OPTION_PTHREAD_CPP_IMPL