fix header inclusion
[profile/ivi/eina.git] / src / lib / eina_main.c
1 /* EINA - EFL data type library
2  * Copyright (C) 2008 Cedric Bail
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library;
16  * if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdio.h>
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #ifdef EFL_HAVE_PTHREAD
26 # include <pthread.h>
27 #endif
28
29 #include "eina_config.h"
30 #include "eina_private.h"
31 #include "eina_types.h"
32 #include "eina_main.h"
33 #include "eina_error.h"
34 #include "eina_log.h"
35 #include "eina_hash.h"
36 #include "eina_stringshare.h"
37 #include "eina_list.h"
38 #include "eina_matrixsparse.h"
39 #include "eina_array.h"
40 #include "eina_counter.h"
41 #include "eina_benchmark.h"
42 #include "eina_magic.h"
43 #include "eina_rectangle.h"
44 #include "eina_safety_checks.h"
45
46 /*============================================================================*
47  *                                  Local                                     *
48  *============================================================================*/
49
50 /**
51  * @cond LOCAL
52  */
53
54 static int _eina_main_count = 0;
55 #ifdef EFL_HAVE_PTHREAD
56 static int _eina_main_thread_count = 0;
57 #endif
58 static int _eina_log_dom = -1;
59
60 #ifdef ERR
61 #undef ERR
62 #endif
63 #define ERR(...) EINA_LOG_DOM_ERR(_eina_log_dom, __VA_ARGS__)
64
65 #ifdef DBG
66 #undef DBG
67 #endif
68 #define DBG(...) EINA_LOG_DOM_DBG(_eina_log_dom, __VA_ARGS__)
69
70 #ifdef EFL_HAVE_PTHREAD
71 static Eina_Bool _threads_activated = EINA_FALSE;
72 static pthread_mutex_t _mutex = PTHREAD_MUTEX_INITIALIZER;
73 #define LOCK() if(_threads_activated) pthread_mutex_lock(&_mutex);
74 #define UNLOCK() if(_threads_activated) pthread_mutex_unlock(&_mutex);
75 #define UNLOCK_FORCE() pthread_mutex_unlock(&_mutex);
76 #else
77 #define LOCK() do {} while (0)
78 #define UNLOCK() do {} while (0)
79 #define UNLOCK_FORCE() do {} while (0)
80 #endif
81
82 /* place module init/shutdown functions here to avoid other modules
83  * calling them by mistake.
84  */
85 #define S(x) extern Eina_Bool eina_##x##_init(void); extern Eina_Bool eina_##x##_shutdown(void)
86 S(log);
87 S(error);
88 S(safety_checks);
89 S(magic_string);
90 S(iterator);
91 S(accessor);
92 S(array);
93 S(module);
94 S(mempool);
95 S(list);
96 S(stringshare);
97 S(matrixsparse);
98 S(convert);
99 S(counter);
100 S(benchmark);
101 S(rectangle);
102 S(strbuf);
103 #undef S
104
105 struct eina_desc_setup
106 {
107    const char *name;
108    Eina_Bool (*init)(void);
109    Eina_Bool (*shutdown)(void);
110 };
111
112 static const struct eina_desc_setup _eina_desc_setup[] = {
113 #define S(x) {#x, eina_##x##_init, eina_##x##_shutdown}
114   /* log is a special case as it needs printf */
115   S(error),
116   S(safety_checks),
117   S(magic_string),
118   S(iterator),
119   S(accessor),
120   S(array),
121   S(module),
122   S(mempool),
123   S(list),
124   S(stringshare),
125   S(matrixsparse),
126   S(convert),
127   S(counter),
128   S(benchmark),
129   S(rectangle),
130   S(strbuf)
131 #undef S
132 };
133 static const size_t _eina_desc_setup_len = sizeof(_eina_desc_setup) / sizeof(_eina_desc_setup[0]);
134
135 static void
136 _eina_shutdown_from_desc(const struct eina_desc_setup *itr)
137 {
138    for (itr--; itr >= _eina_desc_setup; itr--)
139      {
140         if (!itr->shutdown())
141           ERR("Problems shutting down eina module '%s', ignored.", itr->name);
142      }
143
144    eina_log_domain_unregister(_eina_log_dom);
145    _eina_log_dom = -1;
146    eina_log_shutdown();
147 }
148
149 /**
150  * @endcond
151  */
152
153 /*============================================================================*
154  *                                 Global                                     *
155  *============================================================================*/
156
157 /*============================================================================*
158  *                                   API                                      *
159  *============================================================================*/
160
161 /**
162  * @addtogroup Eina_Main_Group Main
163  *
164  * @brief These functions provide general initialisation and shut down
165  * functions.
166  *
167  * @{
168  */
169
170 /**
171  * @brief Initialize the Eina library.
172  *
173  * @return 1 or greater on success, 0 on error.
174  *
175  * This function sets up all the eina modules. It returns 0 on
176  * failure (that is, when one of the module fails to initialize),
177  * otherwise it returns the number of times it has already been
178  * called.
179  *
180  * When Eina is not used anymore, call eina_shutdown() to shut down
181  * the Eina library.
182  */
183 EAPI int
184 eina_init(void)
185 {
186    const struct eina_desc_setup *itr, *itr_end;
187
188    if (EINA_LIKELY(_eina_main_count > 0))
189      return ++_eina_main_count;
190
191    if (!eina_log_init())
192      {
193         fprintf(stderr, "Could not initialize eina logging system.\n");
194         return 0;
195      }
196    _eina_log_dom = eina_log_domain_register("eina", EINA_LOG_COLOR_DEFAULT);
197    if (_eina_log_dom < 0)
198      {
199         EINA_LOG_ERR("Could not register log domain: eina");
200         eina_log_shutdown();
201         return 0;
202      }
203
204    itr = _eina_desc_setup;
205    itr_end = itr + _eina_desc_setup_len;
206    for (; itr < itr_end; itr++)
207      {
208         if (!itr->init())
209           {
210              ERR("Could not initialize eina module '%s'.", itr->name);
211              _eina_shutdown_from_desc(itr);
212              return 0;
213           }
214      }
215
216    _eina_main_count = 1;
217    return 1;
218 }
219
220 /**
221  * @brief Shut down the Eina library.
222  *
223  * @return 0 when all the modules is completely shut down, 1 or
224  * greater otherwise.
225  *
226  * This function shuts down the Eina library. It returns 0 when it has
227  * been called the same number of times than eina_init(). In that case
228  * it shut down all the Eina modules.
229  *
230  * Once this function succeeds (that is, @c 0 is returned), you must
231  * not call any of the Eina function anymore. You must call
232  * eina_init() again to use the Eina functions again.
233  */
234 EAPI int
235 eina_shutdown(void)
236 {
237    _eina_main_count--;
238    if (EINA_UNLIKELY(_eina_main_count == 0))
239      _eina_shutdown_from_desc(_eina_desc_setup + _eina_desc_setup_len);
240    return _eina_main_count;
241 }
242
243
244 /**
245  * @brief Initialize the mutexs of the Eina library.
246  *
247  * @return 1 or greater on success, 0 on error.
248  *
249  * This function sets up all the mutexs in all eina modules. It returns 0 on
250  * failure (that is, when one of the module fails to initialize),
251  * otherwise it returns the number of times it has already been
252  * called.
253  *
254  * When the mutexs are not used anymore, call eina_thread_shutdown() to shut down
255  * the mutexs.
256  */
257 EAPI int
258 eina_threads_init(void)
259 {
260 #ifdef EFL_HAVE_PTHREAD
261     int ret;
262     
263     LOCK();
264     ++_eina_main_thread_count;
265     ret = _eina_main_thread_count;
266
267     if(_eina_main_thread_count > 1) 
268     {
269         UNLOCK();
270         return ret;
271     }
272
273     eina_stringshare_threads_init();
274     eina_log_threads_init();
275     _threads_activated = EINA_TRUE;
276
277     return ret;
278 #else
279     return 0;
280 #endif
281 }
282
283 /**
284  * @brief Shut down mutexs in the Eina library.
285  *
286  * @return 0 when all mutexs are completely shut down, 1 or
287  * greater otherwise.
288  *
289  * This function shuts down the mutexs in the Eina library. It returns 0 when it has
290  * been called the same number of times than eina_thread_init(). In that case
291  * it shut down all the mutexs.
292  *
293  * Once this function succeeds (that is, @c 0 is returned), you must
294  * not call any of the Eina function in a thread anymore. You must call
295  * eina_thread_init() again to use the Eina functions in a thread again.
296  */
297 EAPI int
298 eina_threads_shutdown(void)
299 {
300 #ifdef EFL_HAVE_PTHREAD
301     int ret;
302
303     LOCK();
304     ret = --_eina_main_thread_count;
305     if(_eina_main_thread_count > 0) 
306     {
307         UNLOCK();
308         return ret; 
309     }
310
311     eina_stringshare_threads_shutdown();
312     eina_log_threads_shutdown();
313
314     _threads_activated = EINA_FALSE;
315
316     UNLOCK_FORCE();
317
318     return ret;
319 #else
320     return 0;
321 #endif
322 }
323
324 /**
325  * @}
326  */