use Win32 native mutex on Windows. To use it, pass the following option
[profile/ivi/eina.git] / src / lib / eina_main.c
1 /* EINA - EFL data type library
2  * Copyright (C) 2008 Cedric Bail
3  *
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.
8  *
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.
13  *
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/>.
17  */
18
19 #include <stdio.h>
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #ifdef EFL_HAVE_POSIX_THREADS
26 # include <pthread.h>
27 #endif
28
29 #ifdef EFL_HAVE_WIN32_THREADS
30 # define WIN32_LEAN_AND_MEAN
31 # include <windows.h>
32 # undef WIN32_LEAN_AND_MEAN
33 #endif
34
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"
40 #include "eina_log.h"
41 #include "eina_hash.h"
42 #include "eina_stringshare.h"
43 #include "eina_list.h"
44 #include "eina_matrixsparse.h"
45 #include "eina_array.h"
46 #include "eina_counter.h"
47 #include "eina_benchmark.h"
48 #include "eina_magic.h"
49 #include "eina_rectangle.h"
50 #include "eina_safety_checks.h"
51
52 static Eina_Version _version = { VMAJ, VMIN, VMIC, VREV };
53 EAPI Eina_Version *eina_version = &_version;
54
55 /*============================================================================*
56  *                                  Local                                     *
57  *============================================================================*/
58
59 /**
60  * @cond LOCAL
61  */
62
63 static int _eina_main_count = 0;
64 #ifdef EFL_HAVE_THREADS
65 static int _eina_main_thread_count = 0;
66 #endif
67 static int _eina_log_dom = -1;
68
69 #ifdef ERR
70 #undef ERR
71 #endif
72 #define ERR(...) EINA_LOG_DOM_ERR(_eina_log_dom, __VA_ARGS__)
73
74 #ifdef DBG
75 #undef DBG
76 #endif
77 #define DBG(...) EINA_LOG_DOM_DBG(_eina_log_dom, __VA_ARGS__)
78
79 #ifdef EFL_HAVE_THREADS
80 static Eina_Bool _threads_activated = EINA_FALSE;
81 # ifdef EFL_HAVE_POSIX_THREADS
82 static pthread_mutex_t _mutex = PTHREAD_MUTEX_INITIALIZER;
83 #  define LOCK() if(_threads_activated) pthread_mutex_lock(&_mutex)
84 #  define UNLOCK() if(_threads_activated) pthread_mutex_unlock(&_mutex)
85 #  define UNLOCK_FORCE() pthread_mutex_unlock(&_mutex)
86 # else /* EFL_HAVE_WIN32_THREADS */
87 static HANDLE _mutex = NULL;
88 #  define LOCK() if(_threads_activated) WaitForSingleObject(_mutex, INFINITE)
89 #  define UNLOCK() if(_threads_activated) ReleaseMutex(_mutex)
90 #  define UNLOCK_FORCE() ReleaseMutex(_mutex)
91 # endif
92 #else
93 # define LOCK() do {} while (0)
94 # define UNLOCK() do {} while (0)
95 # define UNLOCK_FORCE() do {} while (0)
96 #endif
97
98 /* place module init/shutdown functions here to avoid other modules
99  * calling them by mistake.
100  */
101 #define S(x) extern Eina_Bool eina_##x##_init(void); extern Eina_Bool eina_##x##_shutdown(void)
102 S(log);
103 S(error);
104 S(safety_checks);
105 S(magic_string);
106 S(iterator);
107 S(accessor);
108 S(array);
109 S(module);
110 S(mempool);
111 S(list);
112 S(stringshare);
113 S(matrixsparse);
114 S(convert);
115 S(counter);
116 S(benchmark);
117 S(rectangle);
118 S(strbuf);
119 S(quadtree);
120 #undef S
121
122 struct eina_desc_setup
123 {
124    const char *name;
125    Eina_Bool (*init)(void);
126    Eina_Bool (*shutdown)(void);
127 };
128
129 static const struct eina_desc_setup _eina_desc_setup[] = {
130 #define S(x) {#x, eina_##x##_init, eina_##x##_shutdown}
131   /* log is a special case as it needs printf */
132   S(error),
133   S(safety_checks),
134   S(magic_string),
135   S(iterator),
136   S(accessor),
137   S(array),
138   S(module),
139   S(mempool),
140   S(list),
141   S(stringshare),
142   S(matrixsparse),
143   S(convert),
144   S(counter),
145   S(benchmark),
146   S(rectangle),
147   S(strbuf),
148   S(quadtree)
149 #undef S
150 };
151 static const size_t _eina_desc_setup_len = sizeof(_eina_desc_setup) / sizeof(_eina_desc_setup[0]);
152
153 static void
154 _eina_shutdown_from_desc(const struct eina_desc_setup *itr)
155 {
156    for (itr--; itr >= _eina_desc_setup; itr--)
157      {
158         if (!itr->shutdown())
159           ERR("Problems shutting down eina module '%s', ignored.", itr->name);
160      }
161
162    eina_log_domain_unregister(_eina_log_dom);
163    _eina_log_dom = -1;
164    eina_log_shutdown();
165 }
166
167 /**
168  * @endcond
169  */
170
171 /*============================================================================*
172  *                                 Global                                     *
173  *============================================================================*/
174
175 /*============================================================================*
176  *                                   API                                      *
177  *============================================================================*/
178
179 /**
180  * @addtogroup Eina_Main_Group Main
181  *
182  * @brief These functions provide general initialisation and shut down
183  * functions.
184  *
185  * @{
186  */
187
188 /**
189  * @brief Initialize the Eina library.
190  *
191  * @return 1 or greater on success, 0 on error.
192  *
193  * This function sets up all the eina modules. It returns 0 on
194  * failure (that is, when one of the module fails to initialize),
195  * otherwise it returns the number of times it has already been
196  * called.
197  *
198  * When Eina is not used anymore, call eina_shutdown() to shut down
199  * the Eina library.
200  */
201 EAPI int
202 eina_init(void)
203 {
204    const struct eina_desc_setup *itr, *itr_end;
205
206    if (EINA_LIKELY(_eina_main_count > 0))
207      return ++_eina_main_count;
208
209    if (!eina_log_init())
210      {
211         fprintf(stderr, "Could not initialize eina logging system.\n");
212         return 0;
213      }
214    _eina_log_dom = eina_log_domain_register("eina", EINA_LOG_COLOR_DEFAULT);
215    if (_eina_log_dom < 0)
216      {
217         EINA_LOG_ERR("Could not register log domain: eina");
218         eina_log_shutdown();
219         return 0;
220      }
221
222    itr = _eina_desc_setup;
223    itr_end = itr + _eina_desc_setup_len;
224    for (; itr < itr_end; itr++)
225      {
226         if (!itr->init())
227           {
228              ERR("Could not initialize eina module '%s'.", itr->name);
229              _eina_shutdown_from_desc(itr);
230              return 0;
231           }
232      }
233
234    _eina_main_count = 1;
235    return 1;
236 }
237
238 /**
239  * @brief Shut down the Eina library.
240  *
241  * @return 0 when all the modules is completely shut down, 1 or
242  * greater otherwise.
243  *
244  * This function shuts down the Eina library. It returns 0 when it has
245  * been called the same number of times than eina_init(). In that case
246  * it shut down all the Eina modules.
247  *
248  * Once this function succeeds (that is, @c 0 is returned), you must
249  * not call any of the Eina function anymore. You must call
250  * eina_init() again to use the Eina functions again.
251  */
252 EAPI int
253 eina_shutdown(void)
254 {
255    _eina_main_count--;
256    if (EINA_UNLIKELY(_eina_main_count == 0))
257      _eina_shutdown_from_desc(_eina_desc_setup + _eina_desc_setup_len);
258    return _eina_main_count;
259 }
260
261
262 /**
263  * @brief Initialize the mutexs of the Eina library.
264  *
265  * @return 1 or greater on success, 0 on error.
266  *
267  * This function sets up all the mutexs in all eina modules. It returns 0 on
268  * failure (that is, when one of the module fails to initialize),
269  * otherwise it returns the number of times it has already been
270  * called.
271  *
272  * When the mutexs are not used anymore, call eina_thread_shutdown() to shut down
273  * the mutexs.
274  */
275 EAPI int
276 eina_threads_init(void)
277 {
278 #ifdef EFL_HAVE_THREADS
279     int ret;
280
281 # ifdef EFL_HAVE_WIN32_THREADS
282     if (!_mutex)
283       _mutex = CreateMutex(NULL, FALSE, NULL);
284     if (!_mutex) return 0;
285 # endif
286     
287     LOCK();
288     ++_eina_main_thread_count;
289     ret = _eina_main_thread_count;
290
291     if(_eina_main_thread_count > 1) 
292     {
293         UNLOCK();
294         return ret;
295     }
296
297     eina_stringshare_threads_init();
298     eina_log_threads_init();
299     _threads_activated = EINA_TRUE;
300
301     return ret;
302 #else
303     return 0;
304 #endif
305 }
306
307 /**
308  * @brief Shut down mutexs in the Eina library.
309  *
310  * @return 0 when all mutexs are completely shut down, 1 or
311  * greater otherwise.
312  *
313  * This function shuts down the mutexs in the Eina library. It returns 0 when it has
314  * been called the same number of times than eina_thread_init(). In that case
315  * it shut down all the mutexs.
316  *
317  * Once this function succeeds (that is, @c 0 is returned), you must
318  * not call any of the Eina function in a thread anymore. You must call
319  * eina_thread_init() again to use the Eina functions in a thread again.
320  */
321 EAPI int
322 eina_threads_shutdown(void)
323 {
324 #ifdef EFL_HAVE_THREADS
325     int ret;
326
327     LOCK();
328     ret = --_eina_main_thread_count;
329     if(_eina_main_thread_count > 0) 
330     {
331         UNLOCK();
332         return ret; 
333     }
334
335     eina_stringshare_threads_shutdown();
336     eina_log_threads_shutdown();
337
338     _threads_activated = EINA_FALSE;
339
340     UNLOCK_FORCE();
341
342 # ifdef EFL_HAVE_WIN32_THREADS
343     if (_mutex) CloseHandle(_mutex);
344 # endif
345
346     return ret;
347 #else
348     return 0;
349 #endif
350 }
351
352 /**
353  * @}
354  */