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_stringshare.h"
43 #include "eina_ustringshare.h"
44 #include "eina_list.h"
45 #include "eina_matrixsparse.h"
46 #include "eina_array.h"
47 #include "eina_counter.h"
48 #include "eina_benchmark.h"
49 #include "eina_magic.h"
50 #include "eina_rectangle.h"
51 #include "eina_safety_checks.h"
53 static Eina_Version _version = { VMAJ, VMIN, VMIC, VREV };
54 EAPI Eina_Version *eina_version = &_version;
56 /*============================================================================*
58 *============================================================================*/
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 #ifdef EFL_HAVE_THREADS
81 static Eina_Bool _threads_activated = EINA_FALSE;
82 # ifdef EFL_HAVE_POSIX_THREADS
83 static pthread_mutex_t _mutex = PTHREAD_MUTEX_INITIALIZER;
84 # define LOCK() if(_threads_activated) pthread_mutex_lock(&_mutex)
85 # define UNLOCK() if(_threads_activated) pthread_mutex_unlock(&_mutex)
86 # define UNLOCK_FORCE() pthread_mutex_unlock(&_mutex)
87 # else /* EFL_HAVE_WIN32_THREADS */
88 static HANDLE _mutex = NULL;
89 # define LOCK() if(_threads_activated) WaitForSingleObject(_mutex, INFINITE)
90 # define UNLOCK() if(_threads_activated) ReleaseMutex(_mutex)
91 # define UNLOCK_FORCE() ReleaseMutex(_mutex)
94 # define LOCK() do {} while (0)
95 # define UNLOCK() do {} while (0)
96 # define UNLOCK_FORCE() do {} while (0)
99 /* place module init/shutdown functions here to avoid other modules
100 * calling them by mistake.
102 #define S(x) extern Eina_Bool eina_##x##_init(void); extern Eina_Bool eina_##x##_shutdown(void)
124 struct eina_desc_setup
127 Eina_Bool (*init)(void);
128 Eina_Bool (*shutdown)(void);
131 static const struct eina_desc_setup _eina_desc_setup[] = {
132 #define S(x) {#x, eina_##x##_init, eina_##x##_shutdown}
133 /* log is a special case as it needs printf */
154 static const size_t _eina_desc_setup_len = sizeof(_eina_desc_setup) / sizeof(_eina_desc_setup[0]);
157 _eina_shutdown_from_desc(const struct eina_desc_setup *itr)
159 for (itr--; itr >= _eina_desc_setup; itr--)
161 if (!itr->shutdown())
162 ERR("Problems shutting down eina module '%s', ignored.", itr->name);
165 eina_log_domain_unregister(_eina_log_dom);
174 /*============================================================================*
176 *============================================================================*/
178 /*============================================================================*
180 *============================================================================*/
183 * @addtogroup Eina_Main_Group Main
185 * @brief These functions provide general initialisation and shut down
192 * @brief Initialize the Eina library.
194 * @return 1 or greater on success, 0 on error.
196 * This function sets up all the eina modules. It returns 0 on
197 * failure (that is, when one of the module fails to initialize),
198 * otherwise it returns the number of times it has already been
201 * When Eina is not used anymore, call eina_shutdown() to shut down
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");
217 _eina_log_dom = eina_log_domain_register("eina", EINA_LOG_COLOR_DEFAULT);
218 if (_eina_log_dom < 0)
220 EINA_LOG_ERR("Could not register log domain: eina");
225 itr = _eina_desc_setup;
226 itr_end = itr + _eina_desc_setup_len;
227 for (; itr < itr_end; itr++)
231 ERR("Could not initialize eina module '%s'.", itr->name);
232 _eina_shutdown_from_desc(itr);
237 _eina_main_count = 1;
242 * @brief Shut down the Eina library.
244 * @return 0 when all the modules is completely shut down, 1 or
247 * This function shuts down the Eina library. It returns 0 when it has
248 * been called the same number of times than eina_init(). In that case
249 * it shut down all the Eina modules.
251 * Once this function succeeds (that is, @c 0 is returned), you must
252 * not call any of the Eina function anymore. You must call
253 * eina_init() again to use the Eina functions again.
259 if (EINA_UNLIKELY(_eina_main_count == 0))
260 _eina_shutdown_from_desc(_eina_desc_setup + _eina_desc_setup_len);
261 return _eina_main_count;
266 * @brief Initialize the mutexs of the Eina library.
268 * @return 1 or greater on success, 0 on error.
270 * This function sets up all the mutexs in all eina modules. It returns 0 on
271 * failure (that is, when one of the module fails to initialize),
272 * otherwise it returns the number of times it has already been
275 * When the mutexs are not used anymore, call eina_thread_shutdown() to shut down
279 eina_threads_init(void)
281 #ifdef EFL_HAVE_THREADS
284 # ifdef EFL_HAVE_WIN32_THREADS
286 _mutex = CreateMutex(NULL, FALSE, NULL);
287 if (!_mutex) return 0;
291 ++_eina_main_thread_count;
292 ret = _eina_main_thread_count;
294 if(_eina_main_thread_count > 1)
300 eina_share_common_threads_init();
301 eina_log_threads_init();
302 _threads_activated = EINA_TRUE;
311 * @brief Shut down mutexs in the Eina library.
313 * @return 0 when all mutexs are completely shut down, 1 or
316 * This function shuts down the mutexs in the Eina library. It returns 0 when it has
317 * been called the same number of times than eina_thread_init(). In that case
318 * it shut down all the mutexs.
320 * Once this function succeeds (that is, @c 0 is returned), you must
321 * not call any of the Eina function in a thread anymore. You must call
322 * eina_thread_init() again to use the Eina functions in a thread again.
325 eina_threads_shutdown(void)
327 #ifdef EFL_HAVE_THREADS
331 ret = --_eina_main_thread_count;
332 if(_eina_main_thread_count > 0)
338 eina_share_common_threads_shutdown();
339 eina_log_threads_shutdown();
341 _threads_activated = EINA_FALSE;
345 # ifdef EFL_HAVE_WIN32_THREADS
346 if (_mutex) CloseHandle(_mutex);