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 # if !(defined(_WIN32_WCE)) && !(defined(_WIN32))
33 # include <sys/types.h>
38 #include "eina_lock.h"
39 #include "eina_config.h"
40 #include "eina_private.h"
41 #include "eina_types.h"
42 #include "eina_main.h"
43 #include "eina_error.h"
45 #include "eina_hash.h"
46 #include "eina_binshare.h"
47 #include "eina_stringshare.h"
48 #include "eina_ustringshare.h"
49 #include "eina_list.h"
50 #include "eina_matrixsparse.h"
51 #include "eina_array.h"
52 #include "eina_counter.h"
53 #include "eina_benchmark.h"
54 #include "eina_magic.h"
55 #include "eina_rectangle.h"
56 #include "eina_safety_checks.h"
58 /*============================================================================*
60 *============================================================================*/
66 static Eina_Version _version = { VMAJ, VMIN, VMIC, VREV };
68 static int _eina_main_count = 0;
69 #ifdef EFL_HAVE_THREADS
70 static int _eina_main_thread_count = 0;
72 static int _eina_log_dom = -1;
77 #define ERR(...) EINA_LOG_DOM_ERR(_eina_log_dom, __VA_ARGS__)
82 #define DBG(...) EINA_LOG_DOM_DBG(_eina_log_dom, __VA_ARGS__)
84 EAPI Eina_Bool _eina_threads_activated = EINA_FALSE;
85 EAPI Eina_Error EINA_ERROR_NOT_MAIN_LOOP = 0;
87 static const char EINA_ERROR_NOT_MAIN_LOOP_STR[] = "Main loop thread check failed.";
89 #ifdef EFL_HAVE_THREADS
91 EAPI DWORD _eina_main_loop;
93 EAPI pthread_t _eina_main_loop;
95 static pid_t _eina_pid;
98 #ifdef EINA_HAVE_DEBUG_THREADS
99 EAPI int _eina_threads_debug = 0;
100 EAPI pthread_mutex_t _eina_tracking_lock;
101 EAPI Eina_Inlist *_eina_tracking = NULL;
104 /* place module init/shutdown functions here to avoid other modules
105 * calling them by mistake.
107 #define S(x) extern Eina_Bool eina_ ## x ## _init(void); \
108 extern Eina_Bool eina_ ## x ## _shutdown(void)
134 struct eina_desc_setup
137 Eina_Bool (*init)(void);
138 Eina_Bool (*shutdown)(void);
141 static const struct eina_desc_setup _eina_desc_setup[] = {
142 #define S(x) {# x, eina_ ## x ## _init, eina_ ## x ## _shutdown}
143 /* log is a special case as it needs printf */
168 static const size_t _eina_desc_setup_len = sizeof(_eina_desc_setup) /
169 sizeof(_eina_desc_setup[0]);
172 _eina_shutdown_from_desc(const struct eina_desc_setup *itr)
174 for (itr--; itr >= _eina_desc_setup; itr--)
176 if (!itr->shutdown())
177 ERR("Problems shutting down eina module '%s', ignored.", itr->name);
180 eina_log_domain_unregister(_eina_log_dom);
189 /*============================================================================*
191 *============================================================================*/
194 /*============================================================================*
196 *============================================================================*/
200 * @brief Eina version (defined at configuration time)
202 EAPI Eina_Version *eina_version = &_version;
207 const struct eina_desc_setup *itr, *itr_end;
209 if (EINA_LIKELY(_eina_main_count > 0))
210 return ++_eina_main_count;
212 if (!eina_log_init())
214 fprintf(stderr, "Could not initialize eina logging system.\n");
218 _eina_log_dom = eina_log_domain_register("eina", EINA_LOG_COLOR_DEFAULT);
219 if (_eina_log_dom < 0)
221 EINA_LOG_ERR("Could not register log domain: eina");
226 EINA_ERROR_NOT_MAIN_LOOP = eina_error_msg_static_register(
227 EINA_ERROR_NOT_MAIN_LOOP_STR);
229 #ifdef EFL_HAVE_THREADS
231 _eina_main_loop = GetCurrentThreadId();
233 _eina_main_loop = pthread_self();
235 _eina_pid = getpid();
238 #ifdef EINA_HAVE_DEBUG_THREADS
239 pthread_mutex_init(&_eina_tracking_lock, NULL);
241 if (getenv("EINA_DEBUG_THREADS"))
242 _eina_threads_debug = atoi(getenv("EINA_DEBUG_THREADS"));
245 itr = _eina_desc_setup;
246 itr_end = itr + _eina_desc_setup_len;
247 for (; itr < itr_end; itr++)
251 ERR("Could not initialize eina module '%s'.", itr->name);
252 _eina_shutdown_from_desc(itr);
257 _eina_main_count = 1;
265 if (EINA_UNLIKELY(_eina_main_count == 0))
267 _eina_shutdown_from_desc(_eina_desc_setup + _eina_desc_setup_len);
269 #ifdef EINA_HAVE_DEBUG_THREADS
270 pthread_mutex_destroy(&_eina_tracking_lock);
274 return _eina_main_count;
279 eina_threads_init(void)
281 #ifdef EFL_HAVE_THREADS
284 #ifdef EINA_HAVE_DEBUG_THREADS
285 assert(pthread_equal(_eina_main_loop, pthread_self()));
288 ++_eina_main_thread_count;
289 ret = _eina_main_thread_count;
291 if(_eina_main_thread_count > 1)
294 eina_share_common_threads_init();
295 eina_log_threads_init();
296 _eina_threads_activated = EINA_TRUE;
305 eina_threads_shutdown(void)
307 #ifdef EFL_HAVE_THREADS
310 #ifdef EINA_HAVE_DEBUG_THREADS
313 assert(pthread_equal(_eina_main_loop, pthread_self()));
314 assert(_eina_main_thread_count > 0);
317 ret = --_eina_main_thread_count;
318 if(_eina_main_thread_count > 0)
321 #ifdef EINA_HAVE_DEBUG_THREADS
322 pthread_mutex_lock(&_eina_tracking_lock);
325 fprintf(stderr, "*************************\n");
326 fprintf(stderr, "* The IMPOSSIBLE HAPPEN *\n");
327 fprintf(stderr, "* LOCK STILL TAKEN : *\n");
328 fprintf(stderr, "*************************\n");
329 EINA_INLIST_FOREACH(_eina_tracking, lk)
331 fprintf(stderr, "*************************\n");
334 pthread_mutex_unlock(&_eina_tracking_lock);
337 eina_share_common_threads_shutdown();
338 eina_log_threads_shutdown();
340 _eina_threads_activated = EINA_FALSE;
349 eina_main_loop_is(void)
351 #ifdef EFL_HAVE_THREADS
352 pid_t pid = getpid();
355 if (pid != _eina_pid)
358 _eina_main_loop = GetCurrentThreadId();
361 if (_eina_main_loop == GetCurrentThreadId())
364 if (pid != _eina_pid)
366 /* This is in case of a fork, but don't like the solution */
368 _eina_main_loop = pthread_self();
372 if (pthread_equal(_eina_main_loop, pthread_self()))
379 /** The purpose of this API should not be documented, it is used only by the one who know what they are doing. */
381 eina_main_loop_define(void)
383 #ifdef EFL_HAVE_THREADS
384 _eina_pid = getpid();
386 _eina_main_loop = GetCurrentThreadId();
388 _eina_main_loop = pthread_self();