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