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_lock.h"
32 #include "eina_config.h"
33 #include "eina_private.h"
34 #include "eina_types.h"
35 #include "eina_main.h"
36 #include "eina_error.h"
38 #include "eina_hash.h"
39 #include "eina_binshare.h"
40 #include "eina_stringshare.h"
41 #include "eina_ustringshare.h"
42 #include "eina_list.h"
43 #include "eina_matrixsparse.h"
44 #include "eina_array.h"
45 #include "eina_counter.h"
46 #include "eina_benchmark.h"
47 #include "eina_magic.h"
48 #include "eina_rectangle.h"
49 #include "eina_safety_checks.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_THREADS
81 # warning "no way to know the main loop thread id yet on Windows CE !"
82 # elif defined(_WIN32)
83 # warning "no way to know the main loop thread id yet on Windows !"
85 EAPI pthread_t _eina_main_loop;;
89 #ifdef EINA_HAVE_DEBUG_THREADS
90 EAPI int _eina_threads_debug = 0;
91 EAPI pthread_mutex_t _eina_tracking_lock;
92 EAPI Eina_Inlist *_eina_tracking = NULL;
95 /* place module init/shutdown functions here to avoid other modules
96 * calling them by mistake.
98 #define S(x) extern Eina_Bool eina_ ## x ## _init(void); \
99 extern Eina_Bool eina_ ## x ## _shutdown(void)
125 struct eina_desc_setup
128 Eina_Bool (*init)(void);
129 Eina_Bool (*shutdown)(void);
132 static const struct eina_desc_setup _eina_desc_setup[] = {
133 #define S(x) {# x, eina_ ## x ## _init, eina_ ## x ## _shutdown}
134 /* log is a special case as it needs printf */
159 static const size_t _eina_desc_setup_len = sizeof(_eina_desc_setup) /
160 sizeof(_eina_desc_setup[0]);
163 _eina_shutdown_from_desc(const struct eina_desc_setup *itr)
165 for (itr--; itr >= _eina_desc_setup; itr--)
167 if (!itr->shutdown())
168 ERR("Problems shutting down eina module '%s', ignored.", itr->name);
171 eina_log_domain_unregister(_eina_log_dom);
180 /*============================================================================*
182 *============================================================================*/
185 /*============================================================================*
187 *============================================================================*/
191 * @brief Eina version (defined at configuration time)
193 EAPI Eina_Version *eina_version = &_version;
198 const struct eina_desc_setup *itr, *itr_end;
200 if (EINA_LIKELY(_eina_main_count > 0))
201 return ++_eina_main_count;
203 if (!eina_log_init())
205 fprintf(stderr, "Could not initialize eina logging system.\n");
209 _eina_log_dom = eina_log_domain_register("eina", EINA_LOG_COLOR_DEFAULT);
210 if (_eina_log_dom < 0)
212 EINA_LOG_ERR("Could not register log domain: eina");
217 #if !(defined(_WIN32_WCE)) && !(defined(_WIN32))
218 _eina_main_loop = pthread_self();
220 #ifdef EINA_HAVE_DEBUG_THREADS
221 pthread_mutex_init(&_eina_tracking_lock, NULL);
223 if (getenv("EINA_DEBUG_THREADS"))
224 _eina_threads_debug = atoi(getenv("EINA_DEBUG_THREADS"));
227 itr = _eina_desc_setup;
228 itr_end = itr + _eina_desc_setup_len;
229 for (; itr < itr_end; itr++)
233 ERR("Could not initialize eina module '%s'.", itr->name);
234 _eina_shutdown_from_desc(itr);
239 _eina_main_count = 1;
247 if (EINA_UNLIKELY(_eina_main_count == 0))
249 _eina_shutdown_from_desc(_eina_desc_setup + _eina_desc_setup_len);
251 #ifdef EINA_HAVE_DEBUG_THREADS
252 pthread_mutex_destroy(&_eina_tracking_lock);
256 return _eina_main_count;
261 eina_threads_init(void)
263 #ifdef EFL_HAVE_THREADS
266 #ifdef EINA_HAVE_DEBUG_THREADS
267 assert(pthread_equal(_eina_main_loop, pthread_self()));
270 ++_eina_main_thread_count;
271 ret = _eina_main_thread_count;
273 if(_eina_main_thread_count > 1)
276 eina_share_common_threads_init();
277 eina_log_threads_init();
278 _eina_threads_activated = EINA_TRUE;
287 eina_threads_shutdown(void)
289 #ifdef EFL_HAVE_THREADS
292 #ifdef EINA_HAVE_DEBUG_THREADS
295 assert(pthread_equal(_eina_main_loop, pthread_self()));
296 assert(_eina_main_thread_count > 0);
299 ret = --_eina_main_thread_count;
300 if(_eina_main_thread_count > 0)
303 #ifdef EINA_HAVE_DEBUG_THREADS
304 pthread_mutex_lock(&_eina_tracking_lock);
307 fprintf(stderr, "*************************\n");
308 fprintf(stderr, "* The IMPOSSIBLE HAPPEN *\n");
309 fprintf(stderr, "* LOCK STILL TAKEN : *\n");
310 fprintf(stderr, "*************************\n");
311 EINA_INLIST_FOREACH(_eina_tracking, lk)
313 fprintf(stderr, "*************************\n");
316 pthread_mutex_unlock(&_eina_tracking_lock);
319 eina_share_common_threads_shutdown();
320 eina_log_threads_shutdown();
322 _eina_threads_activated = EINA_FALSE;
331 eina_main_loop_is(void)
333 #ifdef EINA_HAVE_THREADS
334 /* FIXME: need to check how to do this on windows */
337 # elif defined(_WIN32)
340 if (pthread_equal(_eina_main_loop, pthread_self()))