ea3a565a71b9d8b55b166f289db715227a6a6204
[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 #include <yaz/log.h>
16
17 #if YAZ_POSIX_THREADS
18 #include <pthread.h>
19 #endif
20 #include <errno.h>
21
22 #if HAVE_GNUTLS_H
23 #include <gnutls/gnutls.h>
24 #endif
25
26 #if HAVE_GCRYPT_H
27 #include <gcrypt.h>
28 #endif
29
30 #if YAZ_HAVE_XML2
31 #include <libxml/parser.h>
32 #endif
33
34 #if YAZ_HAVE_XSLT
35 #include <libxslt/xslt.h>
36 #endif
37
38 #if YAZ_HAVE_EXSLT
39 #include <libexslt/exslt.h>
40 #endif
41
42 static int yaz_init_flag = 0;
43 #if YAZ_POSIX_THREADS
44 static pthread_mutex_t yaz_init_mutex = PTHREAD_MUTEX_INITIALIZER;
45 #endif
46
47 extern void yaz_log_init_globals(void);
48
49 #if HAVE_GCRYPT_H
50 GCRY_THREAD_OPTION_PTHREAD_IMPL;
51 #endif
52
53 void yaz_init_globals(void)
54 {
55     if (yaz_init_flag)
56         return;
57 #if YAZ_POSIX_THREADS
58     pthread_mutex_lock(&yaz_init_mutex);
59 #endif
60     if (!yaz_init_flag)
61     {
62         yaz_log_init_globals();
63 #if HAVE_GCRYPT_H
64         /* POSIX threads locking. In case gnutls_global_init do not override */
65         gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
66 #endif
67 #if HAVE_GNUTLS_H
68         gnutls_global_init();
69 #endif
70 #if HAVE_GCRYPT_H
71         /* most likely, GnuTLS has already initialized libgcrypt */
72         if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
73         {
74             gcry_control(GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
75         }
76 #endif
77 #if YAZ_HAVE_XML2
78         xmlInitParser();
79 #endif
80 #if YAZ_HAVE_XSLT
81         xsltInit();
82 #endif
83 #if YAZ_HAVE_EXSLT
84         exsltRegisterAll();
85 #endif
86         yaz_init_flag = 1; /* must be last, before unlocking */
87     }
88 #if YAZ_POSIX_THREADS
89     pthread_mutex_unlock(&yaz_init_mutex);
90 #endif
91 }
92
93 void yaz_deinit_globals(void)
94 {
95     if (!yaz_init_flag)
96         return;
97 #if YAZ_POSIX_THREADS
98     pthread_mutex_lock(&yaz_init_mutex);
99 #endif
100     if (yaz_init_flag)
101     {
102         yaz_log_init_file(0);
103 #if HAVE_GNUTLS_H
104         gnutls_global_deinit();
105 #endif
106 #if YAZ_HAVE_XSLT
107         xsltCleanupGlobals();
108 #endif
109 #if YAZ_HAVE_XML2
110         xmlCleanupParser();
111 #endif
112         yaz_init_flag = 0;
113     }
114 #if YAZ_POSIX_THREADS
115     pthread_mutex_unlock(&yaz_init_mutex);
116 #endif
117 }
118
119 /*
120  * Local variables:
121  * c-basic-offset: 4
122  * c-file-style: "Stroustrup"
123  * indent-tabs-mode: nil
124  * End:
125  * vim: shiftwidth=4 tabstop=8 expandtab
126  */
127