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 _threads_activated = EINA_FALSE;
79 static Eina_Lock _mutex;
81 /* place module init/shutdown functions here to avoid other modules
82 * calling them by mistake.
84 #define S(x) extern Eina_Bool eina_ ## x ## _init(void); \
85 extern Eina_Bool eina_ ## x ## _shutdown(void)
111 struct eina_desc_setup
114 Eina_Bool (*init)(void);
115 Eina_Bool (*shutdown)(void);
118 static const struct eina_desc_setup _eina_desc_setup[] = {
119 #define S(x) {# x, eina_ ## x ## _init, eina_ ## x ## _shutdown}
120 /* log is a special case as it needs printf */
145 static const size_t _eina_desc_setup_len = sizeof(_eina_desc_setup) /
146 sizeof(_eina_desc_setup[0]);
149 _eina_shutdown_from_desc(const struct eina_desc_setup *itr)
151 for (itr--; itr >= _eina_desc_setup; itr--)
153 if (!itr->shutdown())
154 ERR("Problems shutting down eina module '%s', ignored.", itr->name);
157 eina_log_domain_unregister(_eina_log_dom);
166 /*============================================================================*
168 *============================================================================*/
171 /*============================================================================*
173 *============================================================================*/
177 * @brief Eina version (defined at configuration time)
179 EAPI Eina_Version *eina_version = &_version;
184 const struct eina_desc_setup *itr, *itr_end;
186 if (EINA_LIKELY(_eina_main_count > 0))
187 return ++_eina_main_count;
189 if (!eina_log_init())
191 fprintf(stderr, "Could not initialize eina logging system.\n");
195 _eina_log_dom = eina_log_domain_register("eina", EINA_LOG_COLOR_DEFAULT);
196 if (_eina_log_dom < 0)
198 EINA_LOG_ERR("Could not register log domain: eina");
203 itr = _eina_desc_setup;
204 itr_end = itr + _eina_desc_setup_len;
205 for (; itr < itr_end; itr++)
209 ERR("Could not initialize eina module '%s'.", itr->name);
210 _eina_shutdown_from_desc(itr);
215 eina_lock_new(&_mutex);
217 _eina_main_count = 1;
225 if (EINA_UNLIKELY(_eina_main_count == 0))
227 _eina_shutdown_from_desc(_eina_desc_setup + _eina_desc_setup_len);
229 eina_lock_free(_mutex);
232 return _eina_main_count;
237 eina_threads_init(void)
239 #ifdef EFL_HAVE_THREADS
243 eina_lock_take(_mutex);
244 ++_eina_main_thread_count;
245 ret = _eina_main_thread_count;
247 if(_eina_main_thread_count > 1)
249 eina_lock_release(_mutex);
253 eina_share_common_threads_init();
254 eina_log_threads_init();
255 _threads_activated = EINA_TRUE;
257 eina_lock_release(_mutex);
266 eina_threads_shutdown(void)
268 #ifdef EFL_HAVE_THREADS
271 eina_lock_take(_mutex);
272 ret = --_eina_main_thread_count;
273 if(_eina_main_thread_count > 0)
275 eina_lock_release(_mutex);
279 eina_share_common_threads_shutdown();
280 eina_log_threads_shutdown();
282 eina_lock_release(_mutex);
284 _threads_activated = EINA_FALSE;