81cb966c3d3971380119db639b9c41343d39290f
[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 #if YAZ_HAVE_EXSLT
28 #include <libexslt/exslt.h>
29 #endif
30
31 static int yaz_init_flag = 0;
32 #if YAZ_POSIX_THREADS
33 static pthread_mutex_t yaz_init_mutex = PTHREAD_MUTEX_INITIALIZER;
34 #endif
35
36 extern void yaz_log_init_globals(void);
37
38 void yaz_init_globals(void)
39 {
40     if (yaz_init_flag)
41         return;
42 #if YAZ_POSIX_THREADS
43     pthread_mutex_lock(&yaz_init_mutex);
44 #endif
45     if (!yaz_init_flag)
46     {
47         yaz_log_init_globals();
48 #if HAVE_GNUTLS_H
49         gnutls_global_init();
50 #endif
51 #if HAVE_GCRYPT_H
52         /* most likely, GnuTLS has already initialized libgcrypt */
53         if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
54         {
55             gcry_control(GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
56         }
57 #endif
58 #if YAZ_HAVE_EXSLT
59         exsltRegisterAll();
60 #endif
61         yaz_init_flag = 1; /* must be last, before unlocking */
62     }
63 #if YAZ_POSIX_THREADS
64     pthread_mutex_unlock(&yaz_init_mutex);
65 #endif
66 }
67
68 /*
69  * Local variables:
70  * c-basic-offset: 4
71  * c-file-style: "Stroustrup"
72  * indent-tabs-mode: nil
73  * End:
74  * vim: shiftwidth=4 tabstop=8 expandtab
75  */
76