* eina: Add first version of a Quad Tree data type.
[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 S(quadtree);
104 #undef S
105
106 struct eina_desc_setup
107 {
108    const char *name;
109    Eina_Bool (*init)(void);
110    Eina_Bool (*shutdown)(void);
111 };
112
113 static const struct eina_desc_setup _eina_desc_setup[] = {
114 #define S(x) {#x, eina_##x##_init, eina_##x##_shutdown}
115   /* log is a special case as it needs printf */
116   S(error),
117   S(safety_checks),
118   S(magic_string),
119   S(iterator),
120   S(accessor),
121   S(array),
122   S(module),
123   S(mempool),
124   S(list),
125   S(stringshare),
126   S(matrixsparse),
127   S(convert),
128   S(counter),
129   S(benchmark),
130   S(rectangle),
131   S(strbuf),
132   S(quadtree)
133 #undef S
134 };
135 static const size_t _eina_desc_setup_len = sizeof(_eina_desc_setup) / sizeof(_eina_desc_setup[0]);
136
137 static void
138 _eina_shutdown_from_desc(const struct eina_desc_setup *itr)
139 {
140    for (itr--; itr >= _eina_desc_setup; itr--)
141      {
142         if (!itr->shutdown())
143           ERR("Problems shutting down eina module '%s', ignored.", itr->name);
144      }
145
146    eina_log_domain_unregister(_eina_log_dom);
147    _eina_log_dom = -1;
148    eina_log_shutdown();
149 }
150
151 /**
152  * @endcond
153  */
154
155 /*============================================================================*
156  *                                 Global                                     *
157  *============================================================================*/
158
159 /*============================================================================*
160  *                                   API                                      *
161  *============================================================================*/
162
163 /**
164  * @addtogroup Eina_Main_Group Main
165  *
166  * @brief These functions provide general initialisation and shut down
167  * functions.
168  *
169  * @{
170  */
171
172 /**
173  * @brief Initialize the Eina library.
174  *
175  * @return 1 or greater on success, 0 on error.
176  *
177  * This function sets up all the eina modules. It returns 0 on
178  * failure (that is, when one of the module fails to initialize),
179  * otherwise it returns the number of times it has already been
180  * called.
181  *
182  * When Eina is not used anymore, call eina_shutdown() to shut down
183  * the Eina library.
184  */
185 EAPI int
186 eina_init(void)
187 {
188    const struct eina_desc_setup *itr, *itr_end;
189
190    if (EINA_LIKELY(_eina_main_count > 0))
191      return ++_eina_main_count;
192
193    if (!eina_log_init())
194      {
195         fprintf(stderr, "Could not initialize eina logging system.\n");
196         return 0;
197      }
198    _eina_log_dom = eina_log_domain_register("eina", EINA_LOG_COLOR_DEFAULT);
199    if (_eina_log_dom < 0)
200      {
201         EINA_LOG_ERR("Could not register log domain: eina");
202         eina_log_shutdown();
203         return 0;
204      }
205
206    itr = _eina_desc_setup;
207    itr_end = itr + _eina_desc_setup_len;
208    for (; itr < itr_end; itr++)
209      {
210         if (!itr->init())
211           {
212              ERR("Could not initialize eina module '%s'.", itr->name);
213              _eina_shutdown_from_desc(itr);
214              return 0;
215           }
216      }
217
218    _eina_main_count = 1;
219    return 1;
220 }
221
222 /**
223  * @brief Shut down the Eina library.
224  *
225  * @return 0 when all the modules is completely shut down, 1 or
226  * greater otherwise.
227  *
228  * This function shuts down the Eina library. It returns 0 when it has
229  * been called the same number of times than eina_init(). In that case
230  * it shut down all the Eina modules.
231  *
232  * Once this function succeeds (that is, @c 0 is returned), you must
233  * not call any of the Eina function anymore. You must call
234  * eina_init() again to use the Eina functions again.
235  */
236 EAPI int
237 eina_shutdown(void)
238 {
239    _eina_main_count--;
240    if (EINA_UNLIKELY(_eina_main_count == 0))
241      _eina_shutdown_from_desc(_eina_desc_setup + _eina_desc_setup_len);
242    return _eina_main_count;
243 }
244
245
246 /**
247  * @brief Initialize the mutexs of the Eina library.
248  *
249  * @return 1 or greater on success, 0 on error.
250  *
251  * This function sets up all the mutexs in all eina modules. It returns 0 on
252  * failure (that is, when one of the module fails to initialize),
253  * otherwise it returns the number of times it has already been
254  * called.
255  *
256  * When the mutexs are not used anymore, call eina_thread_shutdown() to shut down
257  * the mutexs.
258  */
259 EAPI int
260 eina_threads_init(void)
261 {
262 #ifdef EFL_HAVE_PTHREAD
263     int ret;
264     
265     LOCK();
266     ++_eina_main_thread_count;
267     ret = _eina_main_thread_count;
268
269     if(_eina_main_thread_count > 1) 
270     {
271         UNLOCK();
272         return ret;
273     }
274
275     eina_stringshare_threads_init();
276     eina_log_threads_init();
277     _threads_activated = EINA_TRUE;
278
279     return ret;
280 #else
281     return 0;
282 #endif
283 }
284
285 /**
286  * @brief Shut down mutexs in the Eina library.
287  *
288  * @return 0 when all mutexs are completely shut down, 1 or
289  * greater otherwise.
290  *
291  * This function shuts down the mutexs in the Eina library. It returns 0 when it has
292  * been called the same number of times than eina_thread_init(). In that case
293  * it shut down all the mutexs.
294  *
295  * Once this function succeeds (that is, @c 0 is returned), you must
296  * not call any of the Eina function in a thread anymore. You must call
297  * eina_thread_init() again to use the Eina functions in a thread again.
298  */
299 EAPI int
300 eina_threads_shutdown(void)
301 {
302 #ifdef EFL_HAVE_PTHREAD
303     int ret;
304
305     LOCK();
306     ret = --_eina_main_thread_count;
307     if(_eina_main_thread_count > 0) 
308     {
309         UNLOCK();
310         return ret; 
311     }
312
313     eina_stringshare_threads_shutdown();
314     eina_log_threads_shutdown();
315
316     _threads_activated = EINA_FALSE;
317
318     UNLOCK_FORCE();
319
320     return ret;
321 #else
322     return 0;
323 #endif
324 }
325
326 /**
327  * @}
328  */