1 : /* -*- mode: C -*-
2 : *
3 : * File: pdf-global.c
4 : * Date: Thu Jul 5 21:07:10 2007
5 : *
6 : * GNU PDF Library - Global code
7 : *
8 : */
9 :
10 : /* Copyright (C) 2007, 2009, 2010 Free Software Foundation, Inc. */
11 :
12 : /* This program is free software: you can redistribute it and/or modify
13 : * it under the terms of the GNU General Public License as published by
14 : * the Free Software Foundation, either version 3 of the License, or
15 : * (at your option) any later version.
16 : *
17 : * This program is distributed in the hope that it will be useful,
18 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 : * GNU General Public License for more details.
21 : *
22 : * You should have received a copy of the GNU General Public License
23 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 : */
25 :
26 : #include <config.h>
27 :
28 : #include <string.h>
29 : #include <pdf-global.h>
30 : #include <pdf-types.h>
31 : #include <pdf-crypt.h>
32 : #include <pdf-text.h>
33 : #include <pdf-time.h>
34 :
35 : #if PDF_FSYS_HTTP
36 : #include <curl/curl.h>
37 : #endif
38 :
39 : /* Global variables */
40 :
41 : char *pdf_library_name = "libgnupdf";
42 : char *pdf_version = "0.1";
43 :
44 : struct pdf_globals_s pdf_globals = {
45 : PDF_FALSE,
46 : PTHREAD_MUTEX_INITIALIZER
47 : };
48 :
49 :
50 :
51 : /* Library initialization routine */
52 : int
53 : pdf_init (void)
54 736 : {
55 : int ret;
56 : #if PDF_FSYS_HTTP
57 : CURLcode crv;
58 : #endif
59 :
60 736 : ret = pthread_mutex_lock ( &(pdf_globals.init_mutex) );
61 736 : if (0 == ret)
62 : {
63 : /* The mutex is locked within this brace. NO "return" statements allowed
64 : * from within this block as they will leave the mutex locked and set us
65 : * up for a deadlock.
66 : */
67 :
68 736 : if (pdf_globals.initialized == PDF_FALSE)
69 : {
70 618 : ret = pdf_crypt_init ();
71 :
72 618 : if (PDF_OK == ret)
73 : {
74 618 : ret = pdf_text_init ();
75 : }
76 :
77 618 : if (PDF_OK == ret)
78 : {
79 618 : ret = pdf_time_init ();
80 : }
81 :
82 : #if PDF_FSYS_HTTP
83 :
84 : /* The documentation for libcurl indicates that this initialization
85 : * ( curl_global_init () ) is _not_ thread-safe, and that it should
86 : * therefore be performed during application startup while the
87 : * application as a whole is single-threaded. If this is not possible
88 : * it must be ensured that no other threads are initializing any of
89 : * the libraries that libcurl will be initializing.
90 : */
91 :
92 : /* From curl_global_init(3) (libcurl manpage):
93 : * This function is not thread safe. You must not call it when any
94 : * other thread in the program (i.e. a thread sharing the same
95 : * memory) is running. This doesn't just mean no other thread that
96 : * is using libcurl. Because curl_global_init() calls functions of
97 : * other libraries that are similarly thread unsafe, it could
98 : * conflict with any other thread that uses these other libraries.
99 : */
100 :
101 : if (PDF_OK == ret)
102 : {
103 : crv = curl_global_init (CURL_GLOBAL_ALL);
104 : if (0 != crv )
105 : {
106 : ret = PDF_ERROR;
107 : }
108 : }
109 : #endif // PDF_FSYS_HTTP
110 :
111 618 : if (PDF_OK == ret)
112 : {
113 618 : pdf_globals.initialized = PDF_TRUE;
114 : }
115 : }
116 :
117 736 : pthread_mutex_unlock ( &(pdf_globals.init_mutex) );
118 : }
119 : else
120 : { /* locking the mutex returned an error. This shouldn't happen. */
121 0 : ret = PDF_ERROR;
122 : }
123 :
124 736 : return ret;
125 : }
126 :
127 :
128 : /* Library finalization routine */
129 : int
130 : pdf_finish (void)
131 12 : {
132 : int ret;
133 12 : ret = PDF_OK;
134 :
135 : #if PDF_FSYS_HTTP
136 : ret = pthread_mutex_lock ( &(pdf_globals.init_mutex) );
137 : if (0 == ret)
138 : {
139 : /* The mutex is locked within this brace. NO "return" statements allowed
140 : * from within this block as they will leave the mutex locked and set us
141 : * up for a deadlock.
142 : */
143 :
144 : /* From curl_global_cleanup(3) (libcurl manpage):
145 :
146 : * This function is not thread safe. You must not call it when any other
147 : * thread in the program (i.e. a thread sharing the same memory) is
148 : * running. This doesn't just mean no other thread that is using
149 : * libcurl. Because curl_global_cleanup(3) calls functions of
150 : * other libraries that are similarly thread unsafe, it could
151 : * conflict with any other thread that uses these other libraries.
152 : */
153 :
154 : curl_global_cleanup ();
155 :
156 : pthread_mutex_unlock ( &(pdf_globals.init_mutex) );
157 : }
158 : #endif // PDF_FSYS_HTTP
159 :
160 12 : return ret;
161 : }
162 :
163 : /* End of pdf.c */
|