Merge branch 'master' into yaz-744
[yaz-moved-to-github.git] / src / init_globals.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file init_globals.c
8  * \brief Initialize global things
9  */
10
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #if YAZ_POSIX_THREADS
16 #include <pthread.h>
17 #endif
18
19 #if HAVE_GNUTLS_H
20 #include <gnutls/gnutls.h>
21 #endif
22
23 #if HAVE_GCRYPT_H
24 #include <gcrypt.h>
25 #endif
26
27 static int yaz_init_flag = 0;
28 #if YAZ_POSIX_THREADS
29 static pthread_mutex_t yaz_init_mutex = PTHREAD_MUTEX_INITIALIZER;
30 #endif
31
32 extern void yaz_log_init_globals(void);
33
34 void yaz_init_globals(void)
35 {
36     if (yaz_init_flag)
37         return;
38 #if YAZ_POSIX_THREADS
39     pthread_mutex_lock(&yaz_init_mutex);
40 #endif
41     if (!yaz_init_flag)
42     {
43         yaz_log_init_globals();
44 #if HAVE_GNUTLS_H
45         gnutls_global_init();
46 #endif
47 #if HAVE_GCRYPT_H
48         /* most likely, GnuTLS has already initialized libgcrypt */
49         if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
50         {
51             gcry_control(GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
52         }
53 #endif
54         yaz_init_flag = 1;
55     }
56 #if YAZ_POSIX_THREADS
57     pthread_mutex_unlock(&yaz_init_mutex);
58 #endif
59 }
60
61 /*
62  * Local variables:
63  * c-basic-offset: 4
64  * c-file-style: "Stroustrup"
65  * indent-tabs-mode: nil
66  * End:
67  * vim: shiftwidth=4 tabstop=8 expandtab
68  */
69