No more reachable memory with yaz_deinit_globals
[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 extern void yaz_log_deinit_globals(void);
49
50 #if HAVE_GCRYPT_H
51 GCRY_THREAD_OPTION_PTHREAD_IMPL;
52 #endif
53
54 void yaz_init_globals(void)
55 {
56     if (yaz_init_flag)
57         return;
58 #if YAZ_POSIX_THREADS
59     pthread_mutex_lock(&yaz_init_mutex);
60 #endif
61     if (!yaz_init_flag)
62     {
63         yaz_log_init_globals();
64 #if HAVE_GCRYPT_H
65         /* POSIX threads locking. In case gnutls_global_init do not override */
66         gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
67 #endif
68 #if HAVE_GNUTLS_H
69         gnutls_global_init();
70 #endif
71 #if HAVE_GCRYPT_H
72         /* most likely, GnuTLS has already initialized libgcrypt */
73         if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
74         {
75             gcry_control(GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
76         }
77 #endif
78 #if YAZ_HAVE_XML2
79         xmlInitParser();
80 #endif
81 #if YAZ_HAVE_XSLT
82         xsltInit();
83 #endif
84 #if YAZ_HAVE_EXSLT
85         exsltRegisterAll();
86 #endif
87         yaz_init_flag = 1; /* must be last, before unlocking */
88     }
89 #if YAZ_POSIX_THREADS
90     pthread_mutex_unlock(&yaz_init_mutex);
91 #endif
92 }
93
94 void yaz_deinit_globals(void)
95 {
96     if (!yaz_init_flag)
97         return;
98 #if YAZ_POSIX_THREADS
99     pthread_mutex_lock(&yaz_init_mutex);
100 #endif
101     if (yaz_init_flag)
102     {
103         yaz_log_deinit_globals();
104 #if HAVE_GNUTLS_H
105         gnutls_global_deinit();
106 #endif
107 #if YAZ_HAVE_XSLT
108         xsltCleanupGlobals();
109 #endif
110 #if YAZ_HAVE_XML2
111         xmlCleanupParser();
112 #endif
113         yaz_init_flag = 0;
114     }
115 #if YAZ_POSIX_THREADS
116     pthread_mutex_unlock(&yaz_init_mutex);
117 #endif
118 }
119
120 /*
121  * Local variables:
122  * c-basic-offset: 4
123  * c-file-style: "Stroustrup"
124  * indent-tabs-mode: nil
125  * End:
126  * vim: shiftwidth=4 tabstop=8 expandtab
127  */
128