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_WIN32_THREADS
26 # define WIN32_LEAN_AND_MEAN
28 # undef WIN32_LEAN_AND_MEAN
31 #include "eina_config.h"
32 #include "eina_private.h"
33 #include "eina_types.h"
34 #include "eina_main.h"
35 #include "eina_error.h"
37 #include "eina_hash.h"
38 #include "eina_binshare.h"
39 #include "eina_stringshare.h"
40 #include "eina_ustringshare.h"
41 #include "eina_list.h"
42 #include "eina_matrixsparse.h"
43 #include "eina_array.h"
44 #include "eina_counter.h"
45 #include "eina_benchmark.h"
46 #include "eina_magic.h"
47 #include "eina_rectangle.h"
48 #include "eina_safety_checks.h"
49 #include "eina_lock.h"
51 /*============================================================================*
53 *============================================================================*/
59 static Eina_Version _version = { VMAJ, VMIN, VMIC, VREV };
61 static int _eina_main_count = 0;
62 #ifdef EFL_HAVE_THREADS
63 static int _eina_main_thread_count = 0;
65 static int _eina_log_dom = -1;
70 #define ERR(...) EINA_LOG_DOM_ERR(_eina_log_dom, __VA_ARGS__)
75 #define DBG(...) EINA_LOG_DOM_DBG(_eina_log_dom, __VA_ARGS__)
77 EAPI Eina_Bool _eina_threads_activated = EINA_FALSE;
79 #ifdef EINA_HAVE_DEBUG_THREADS
80 EAPI int _eina_threads_debug = 0;
83 static Eina_Lock _mutex;
85 /* place module init/shutdown functions here to avoid other modules
86 * calling them by mistake.
88 #define S(x) extern Eina_Bool eina_ ## x ## _init(void); \
89 extern Eina_Bool eina_ ## x ## _shutdown(void)
115 struct eina_desc_setup
118 Eina_Bool (*init)(void);
119 Eina_Bool (*shutdown)(void);
122 static const struct eina_desc_setup _eina_desc_setup[] = {
123 #define S(x) {# x, eina_ ## x ## _init, eina_ ## x ## _shutdown}
124 /* log is a special case as it needs printf */
149 static const size_t _eina_desc_setup_len = sizeof(_eina_desc_setup) /
150 sizeof(_eina_desc_setup[0]);
153 _eina_shutdown_from_desc(const struct eina_desc_setup *itr)
155 for (itr--; itr >= _eina_desc_setup; itr--)
157 if (!itr->shutdown())
158 ERR("Problems shutting down eina module '%s', ignored.", itr->name);
161 eina_log_domain_unregister(_eina_log_dom);
170 /*============================================================================*
172 *============================================================================*/
175 /*============================================================================*
177 *============================================================================*/
181 * @brief Eina version (defined at configuration time)
183 EAPI Eina_Version *eina_version = &_version;
188 const struct eina_desc_setup *itr, *itr_end;
190 if (EINA_LIKELY(_eina_main_count > 0))
191 return ++_eina_main_count;
193 if (!eina_log_init())
195 fprintf(stderr, "Could not initialize eina logging system.\n");
199 _eina_log_dom = eina_log_domain_register("eina", EINA_LOG_COLOR_DEFAULT);
200 if (_eina_log_dom < 0)
202 EINA_LOG_ERR("Could not register log domain: eina");
207 itr = _eina_desc_setup;
208 itr_end = itr + _eina_desc_setup_len;
209 for (; itr < itr_end; itr++)
213 ERR("Could not initialize eina module '%s'.", itr->name);
214 _eina_shutdown_from_desc(itr);
219 eina_lock_new(&_mutex);
220 #ifdef EINA_HAVE_DEBUG_THREADS
221 if (getenv("EINA_DEBUG_THREADS"))
222 _eina_threads_debug = atoi(getenv("EINA_DEBUG_THREADS");
225 _eina_main_count = 1;
233 if (EINA_UNLIKELY(_eina_main_count == 0))
235 _eina_shutdown_from_desc(_eina_desc_setup + _eina_desc_setup_len);
237 eina_lock_free(&_mutex);
240 return _eina_main_count;
245 eina_threads_init(void)
247 #ifdef EFL_HAVE_THREADS
251 eina_lock_take(&_mutex);
252 ++_eina_main_thread_count;
253 ret = _eina_main_thread_count;
255 if(_eina_main_thread_count > 1)
257 eina_lock_release(&_mutex);
261 eina_share_common_threads_init();
262 eina_log_threads_init();
263 _eina_threads_activated = EINA_TRUE;
265 eina_lock_release(&_mutex);
274 eina_threads_shutdown(void)
276 #ifdef EFL_HAVE_THREADS
279 eina_lock_take(&_mutex);
280 ret = --_eina_main_thread_count;
281 if(_eina_main_thread_count > 0)
283 eina_lock_release(&_mutex);
287 eina_share_common_threads_shutdown();
288 eina_log_threads_shutdown();
290 eina_lock_release(&_mutex);
292 _eina_threads_activated = EINA_FALSE;