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