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_POSIX_THREADS
29 #ifdef EFL_HAVE_WIN32_THREADS
30 # define WIN32_LEAN_AND_MEAN
32 # undef WIN32_LEAN_AND_MEAN
35 #include "eina_config.h"
36 #include "eina_private.h"
37 #include "eina_types.h"
38 #include "eina_main.h"
39 #include "eina_error.h"
41 #include "eina_hash.h"
42 #include "eina_binshare.h"
43 #include "eina_stringshare.h"
44 #include "eina_ustringshare.h"
45 #include "eina_list.h"
46 #include "eina_matrixsparse.h"
47 #include "eina_array.h"
48 #include "eina_counter.h"
49 #include "eina_benchmark.h"
50 #include "eina_magic.h"
51 #include "eina_rectangle.h"
52 #include "eina_safety_checks.h"
54 /*============================================================================*
56 *============================================================================*/
62 static Eina_Version _version = { VMAJ, VMIN, VMIC, VREV };
64 static int _eina_main_count = 0;
65 #ifdef EFL_HAVE_THREADS
66 static int _eina_main_thread_count = 0;
68 static int _eina_log_dom = -1;
73 #define ERR(...) EINA_LOG_DOM_ERR(_eina_log_dom, __VA_ARGS__)
78 #define DBG(...) EINA_LOG_DOM_DBG(_eina_log_dom, __VA_ARGS__)
80 Eina_Bool _threads_activated = EINA_FALSE;
82 #ifdef EFL_HAVE_THREADS
83 # ifdef EFL_HAVE_POSIX_THREADS
84 static pthread_mutex_t _mutex = PTHREAD_MUTEX_INITIALIZER;
85 # define LOCK() if(_threads_activated) pthread_mutex_lock(&_mutex)
86 # define UNLOCK() if(_threads_activated) pthread_mutex_unlock(&_mutex)
87 # define UNLOCK_FORCE() pthread_mutex_unlock(&_mutex)
88 # else /* EFL_HAVE_WIN32_THREADS */
89 static HANDLE _mutex = NULL;
90 # define LOCK() if(_threads_activated) WaitForSingleObject(_mutex, INFINITE)
91 # define UNLOCK() if(_threads_activated) ReleaseMutex(_mutex)
92 # define UNLOCK_FORCE() ReleaseMutex(_mutex)
95 # define LOCK() do {} while (0)
96 # define UNLOCK() do {} while (0)
97 # define UNLOCK_FORCE() do {} while (0)
100 /* place module init/shutdown functions here to avoid other modules
101 * calling them by mistake.
103 #define S(x) extern Eina_Bool eina_ ## x ## _init(void); \
104 extern Eina_Bool eina_ ## x ## _shutdown(void)
129 struct eina_desc_setup
132 Eina_Bool (*init)(void);
133 Eina_Bool (*shutdown)(void);
136 static const struct eina_desc_setup _eina_desc_setup[] = {
137 #define S(x) {# x, eina_ ## x ## _init, eina_ ## x ## _shutdown}
138 /* log is a special case as it needs printf */
162 static const size_t _eina_desc_setup_len = sizeof(_eina_desc_setup) /
163 sizeof(_eina_desc_setup[0]);
166 _eina_shutdown_from_desc(const struct eina_desc_setup *itr)
168 for (itr--; itr >= _eina_desc_setup; itr--)
170 if (!itr->shutdown())
171 ERR("Problems shutting down eina module '%s', ignored.", itr->name);
174 eina_log_domain_unregister(_eina_log_dom);
183 /*============================================================================*
185 *============================================================================*/
188 /*============================================================================*
190 *============================================================================*/
194 * @brief Eina version (defined at configuration time)
196 EAPI Eina_Version *eina_version = &_version;
201 const struct eina_desc_setup *itr, *itr_end;
203 if (EINA_LIKELY(_eina_main_count > 0))
204 return ++_eina_main_count;
206 if (!eina_log_init())
208 fprintf(stderr, "Could not initialize eina logging system.\n");
212 _eina_log_dom = eina_log_domain_register("eina", EINA_LOG_COLOR_DEFAULT);
213 if (_eina_log_dom < 0)
215 EINA_LOG_ERR("Could not register log domain: eina");
220 itr = _eina_desc_setup;
221 itr_end = itr + _eina_desc_setup_len;
222 for (; itr < itr_end; itr++)
226 ERR("Could not initialize eina module '%s'.", itr->name);
227 _eina_shutdown_from_desc(itr);
232 _eina_main_count = 1;
240 if (EINA_UNLIKELY(_eina_main_count == 0))
241 _eina_shutdown_from_desc(_eina_desc_setup + _eina_desc_setup_len);
243 return _eina_main_count;
248 eina_threads_init(void)
250 #ifdef EFL_HAVE_THREADS
253 # ifdef EFL_HAVE_WIN32_THREADS
255 _mutex = CreateMutex(NULL, FALSE, NULL);
263 ++_eina_main_thread_count;
264 ret = _eina_main_thread_count;
266 if(_eina_main_thread_count > 1)
272 eina_share_common_threads_init();
273 eina_log_threads_init();
274 _threads_activated = EINA_TRUE;
283 eina_threads_shutdown(void)
285 #ifdef EFL_HAVE_THREADS
289 ret = --_eina_main_thread_count;
290 if(_eina_main_thread_count > 0)
296 eina_share_common_threads_shutdown();
297 eina_log_threads_shutdown();
299 _threads_activated = EINA_FALSE;
303 # ifdef EFL_HAVE_WIN32_THREADS