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 #ifdef EFL_HAVE_THREADS
32 # ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
50 #include "eina_lock.h"
51 #include "eina_config.h"
52 #include "eina_private.h"
53 #include "eina_types.h"
54 #include "eina_main.h"
55 #include "eina_error.h"
57 #include "eina_hash.h"
58 #include "eina_binshare.h"
59 #include "eina_stringshare.h"
60 #include "eina_ustringshare.h"
61 #include "eina_list.h"
62 #include "eina_matrixsparse.h"
63 #include "eina_array.h"
64 #include "eina_counter.h"
65 #include "eina_benchmark.h"
66 #include "eina_magic.h"
67 #include "eina_rectangle.h"
68 #include "eina_safety_checks.h"
69 #include "eina_inlist.h"
70 #include "eina_inarray.h"
71 #include "eina_value.h"
73 #include "eina_model.h"
76 /*============================================================================*
78 *============================================================================*/
84 static Eina_Version _version = { VMAJ, VMIN, VMIC, VREV };
86 static int _eina_main_count = 0;
87 #ifdef EFL_HAVE_THREADS
88 static int _eina_main_thread_count = 0;
90 static int _eina_log_dom = -1;
95 #define ERR(...) EINA_LOG_DOM_ERR(_eina_log_dom, __VA_ARGS__)
100 #define DBG(...) EINA_LOG_DOM_DBG(_eina_log_dom, __VA_ARGS__)
102 EAPI Eina_Bool _eina_threads_activated = EINA_FALSE;
103 EAPI Eina_Error EINA_ERROR_NOT_MAIN_LOOP = 0;
105 static const char EINA_ERROR_NOT_MAIN_LOOP_STR[] = "Main loop thread check failed.";
107 #ifdef EFL_HAVE_THREADS
109 EAPI DWORD _eina_main_loop;
111 EAPI pthread_t _eina_main_loop;
113 static pid_t _eina_pid;
117 static int _mt_enabled = 0;
120 #ifdef EFL_HAVE_THREADS
121 EAPI int _eina_threads_debug = 0;
122 # if !defined(_WIN32_WCE) && !defined(_WIN32)
123 EAPI pthread_mutex_t _eina_tracking_lock;
124 EAPI Eina_Inlist *_eina_tracking = NULL;
128 /* place module init/shutdown functions here to avoid other modules
129 * calling them by mistake.
131 #define S(x) extern Eina_Bool eina_ ## x ## _init(void); \
132 extern Eina_Bool eina_ ## x ## _shutdown(void)
164 struct eina_desc_setup
167 Eina_Bool (*init)(void);
168 Eina_Bool (*shutdown)(void);
171 static const struct eina_desc_setup _eina_desc_setup[] = {
172 #define S(x) {# x, eina_ ## x ## _init, eina_ ## x ## _shutdown}
173 /* log is a special case as it needs printf */
204 static const size_t _eina_desc_setup_len = sizeof(_eina_desc_setup) /
205 sizeof(_eina_desc_setup[0]);
208 _eina_shutdown_from_desc(const struct eina_desc_setup *itr)
210 for (itr--; itr >= _eina_desc_setup; itr--)
212 if (!itr->shutdown())
213 ERR("Problems shutting down eina module '%s', ignored.", itr->name);
216 eina_log_domain_unregister(_eina_log_dom);
225 /*============================================================================*
227 *============================================================================*/
230 /*============================================================================*
232 *============================================================================*/
236 * @brief Eina version (defined at configuration time)
238 EAPI Eina_Version *eina_version = &_version;
243 const struct eina_desc_setup *itr, *itr_end;
245 if (EINA_LIKELY(_eina_main_count > 0))
246 return ++_eina_main_count;
249 if ((getenv("EINA_MTRACE")) && (getenv("MALLOC_TRACE")))
256 if (!eina_log_init())
258 fprintf(stderr, "Could not initialize eina logging system.\n");
262 _eina_log_dom = eina_log_domain_register("eina", EINA_LOG_COLOR_DEFAULT);
263 if (_eina_log_dom < 0)
265 EINA_LOG_ERR("Could not register log domain: eina");
270 EINA_ERROR_NOT_MAIN_LOOP = eina_error_msg_static_register(
271 EINA_ERROR_NOT_MAIN_LOOP_STR);
273 #ifdef EFL_HAVE_THREADS
275 _eina_main_loop = GetCurrentThreadId();
277 _eina_main_loop = pthread_self();
279 _eina_pid = getpid();
282 #ifdef EINA_HAVE_DEBUG_THREADS
283 pthread_mutex_init(&_eina_tracking_lock, NULL);
285 if (getenv("EINA_DEBUG_THREADS"))
286 _eina_threads_debug = atoi(getenv("EINA_DEBUG_THREADS"));
289 itr = _eina_desc_setup;
290 itr_end = itr + _eina_desc_setup_len;
291 for (; itr < itr_end; itr++)
295 ERR("Could not initialize eina module '%s'.", itr->name);
296 _eina_shutdown_from_desc(itr);
301 eina_cpu_count_internal();
303 _eina_main_count = 1;
310 if (_eina_main_count <= 0)
312 ERR("Init count not greater than 0 in shutdown.");
316 if (EINA_UNLIKELY(_eina_main_count == 0))
318 _eina_shutdown_from_desc(_eina_desc_setup + _eina_desc_setup_len);
320 #ifdef EINA_HAVE_DEBUG_THREADS
321 pthread_mutex_destroy(&_eina_tracking_lock);
332 return _eina_main_count;
337 eina_threads_init(void)
339 #ifdef EFL_HAVE_THREADS
342 #ifdef EINA_HAVE_DEBUG_THREADS
343 assert(pthread_equal(_eina_main_loop, pthread_self()));
346 ++_eina_main_thread_count;
347 ret = _eina_main_thread_count;
349 if(_eina_main_thread_count > 1)
352 eina_share_common_threads_init();
353 eina_log_threads_init();
354 _eina_threads_activated = EINA_TRUE;
363 eina_threads_shutdown(void)
365 #ifdef EFL_HAVE_THREADS
368 #ifdef EINA_HAVE_DEBUG_THREADS
371 assert(pthread_equal(_eina_main_loop, pthread_self()));
372 assert(_eina_main_thread_count > 0);
375 ret = --_eina_main_thread_count;
376 if(_eina_main_thread_count > 0)
379 #ifdef EINA_HAVE_DEBUG_THREADS
380 pthread_mutex_lock(&_eina_tracking_lock);
383 fprintf(stderr, "*************************\n");
384 fprintf(stderr, "* The IMPOSSIBLE HAPPEN *\n");
385 fprintf(stderr, "* LOCK STILL TAKEN : *\n");
386 fprintf(stderr, "*************************\n");
387 EINA_INLIST_FOREACH(_eina_tracking, lk)
389 fprintf(stderr, "=======\n");
392 fprintf(stderr, "*************************\n");
395 pthread_mutex_unlock(&_eina_tracking_lock);
398 eina_share_common_threads_shutdown();
399 eina_log_threads_shutdown();
401 _eina_threads_activated = EINA_FALSE;
410 eina_main_loop_is(void)
412 #ifdef EFL_HAVE_THREADS
416 if (_eina_main_loop == GetCurrentThreadId())
419 if (pthread_equal(_eina_main_loop, pthread_self()))
425 if (pid != _eina_pid)
428 _eina_main_loop = GetCurrentThreadId();
432 if (pid != _eina_pid)
434 /* This is in case of a fork, but don't like the solution */
436 _eina_main_loop = pthread_self();
445 /** The purpose of this API should not be documented, it is used only by the one who know what they are doing. */
447 eina_main_loop_define(void)
449 #ifdef EFL_HAVE_THREADS
450 _eina_pid = getpid();
452 _eina_main_loop = GetCurrentThreadId();
454 _eina_main_loop = pthread_self();