1 /* EINA - EFL data type library
2 * Copyright (C) 2008 Cedric Bail
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.
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.
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/>.
25 #ifdef EFL_HAVE_PTHREAD
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"
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"
46 /*============================================================================*
48 *============================================================================*/
54 static int _eina_main_count = 0;
55 #ifdef EFL_HAVE_PTHREAD
56 static int _eina_main_thread_count = 0;
58 static int _eina_log_dom = -1;
63 #define ERR(...) EINA_LOG_DOM_ERR(_eina_log_dom, __VA_ARGS__)
68 #define DBG(...) EINA_LOG_DOM_DBG(_eina_log_dom, __VA_ARGS__)
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);
77 #define LOCK() do {} while (0)
78 #define UNLOCK() do {} while (0)
79 #define UNLOCK_FORCE() do {} while (0)
82 /* place module init/shutdown functions here to avoid other modules
83 * calling them by mistake.
85 #define S(x) extern Eina_Bool eina_##x##_init(void); extern Eina_Bool eina_##x##_shutdown(void)
105 struct eina_desc_setup
108 Eina_Bool (*init)(void);
109 Eina_Bool (*shutdown)(void);
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 */
133 static const size_t _eina_desc_setup_len = sizeof(_eina_desc_setup) / sizeof(_eina_desc_setup[0]);
136 _eina_shutdown_from_desc(const struct eina_desc_setup *itr)
138 for (itr--; itr >= _eina_desc_setup; itr--)
140 if (!itr->shutdown())
141 ERR("Problems shutting down eina module '%s', ignored.", itr->name);
144 eina_log_domain_unregister(_eina_log_dom);
153 /*============================================================================*
155 *============================================================================*/
157 /*============================================================================*
159 *============================================================================*/
162 * @addtogroup Eina_Main_Group Main
164 * @brief These functions provide general initialisation and shut down
171 * @brief Initialize the Eina library.
173 * @return 1 or greater on success, 0 on error.
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
180 * When Eina is not used anymore, call eina_shutdown() to shut down
186 const struct eina_desc_setup *itr, *itr_end;
188 if (EINA_LIKELY(_eina_main_count > 0))
189 return ++_eina_main_count;
191 if (!eina_log_init())
193 fprintf(stderr, "Could not initialize eina logging system.\n");
196 _eina_log_dom = eina_log_domain_register("eina", EINA_LOG_COLOR_DEFAULT);
197 if (_eina_log_dom < 0)
199 EINA_LOG_ERR("Could not register log domain: eina");
204 itr = _eina_desc_setup;
205 itr_end = itr + _eina_desc_setup_len;
206 for (; itr < itr_end; itr++)
210 ERR("Could not initialize eina module '%s'.", itr->name);
211 _eina_shutdown_from_desc(itr);
216 _eina_main_count = 1;
221 * @brief Shut down the Eina library.
223 * @return 0 when all the modules is completely shut down, 1 or
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.
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.
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;
245 * @brief Initialize the mutexs of the Eina library.
247 * @return 1 or greater on success, 0 on error.
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
254 * When the mutexs are not used anymore, call eina_thread_shutdown() to shut down
258 eina_threads_init(void)
260 #ifdef EFL_HAVE_PTHREAD
264 ++_eina_main_thread_count;
265 ret = _eina_main_thread_count;
267 if(_eina_main_thread_count > 1)
273 eina_stringshare_threads_init();
274 eina_log_threads_init();
275 _threads_activated = EINA_TRUE;
284 * @brief Shut down mutexs in the Eina library.
286 * @return 0 when all mutexs are completely shut down, 1 or
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.
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.
298 eina_threads_shutdown(void)
300 #ifdef EFL_HAVE_PTHREAD
304 ret = --_eina_main_thread_count;
305 if(_eina_main_thread_count > 0)
311 eina_stringshare_threads_shutdown();
312 eina_log_threads_shutdown();
314 _threads_activated = EINA_FALSE;