meta-tizen: neard: add Tizen patches
[scm/bb/tizen-distro.git] / meta-tizen / meta-tizen-adaptation / meta / recipes-core / dbus / files / 0010-Always-initialize-threading-before-allocating-a-dyna.patch
1 From: Simon McVittie <simon.mcvittie@collabora.co.uk>
2 Date: Tue, 16 Apr 2013 16:37:51 +0100
3 Subject: Always initialize threading before allocating a dynamic mutex
4
5 Dynamic allocation of mutexes can fail anyway, so this is easy.
6
7 Justification for not keeping the dummy mutex code-paths, even as an
8 opt-in thing for processes known to be high-performance and
9 single-threaded: real mutexes only cut the throughput of
10 test/dbus-daemon.c by a couple of percent on my laptop (from around
11 6700 to around 6600 messages per second), and libdbus crashes caused
12 by not calling dbus_threads_init_default() are sufficiently widespread
13 that they're wasting a lot of everyone's time.
14
15 Bug: https://bugs.freedesktop.org/show_bug.cgi?id=54972
16 Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
17 Reviewed-by: Alban Crequy <alban.crequy@collabora.co.uk>
18 Reviewed-by: Anas Nashif <anas.nashif@intel.com>
19 Bug-Tizen: TZPC-1971
20 Applied-upstream: 1.7.6, commit:08391b14616c248458e838691d068aa48dc70d18
21 Change-Id: I62e4fc541f6868ef44dc0654337b895e5392c16e
22 ---
23  dbus/dbus-threads.c | 300 ++++++++++------------------------------------------
24  1 file changed, 56 insertions(+), 244 deletions(-)
25
26 diff --git a/dbus/dbus-threads.c b/dbus/dbus-threads.c
27 index 2c2a816..29462eb 100644
28 --- a/dbus/dbus-threads.c
29 +++ b/dbus/dbus-threads.c
30 @@ -27,18 +27,6 @@
31  #include "dbus-list.h"
32  
33  static int thread_init_generation = 0;
34
35 -static DBusList *uninitialized_rmutex_list = NULL;
36 -static DBusList *uninitialized_cmutex_list = NULL;
37 -static DBusList *uninitialized_condvar_list = NULL;
38 -
39 -/** This is used for the no-op default mutex pointer, just to be distinct from #NULL */
40 -#define _DBUS_DUMMY_MUTEX ((DBusMutex*)0xABCDEF)
41 -#define _DBUS_DUMMY_RMUTEX ((DBusRMutex *) _DBUS_DUMMY_MUTEX)
42 -#define _DBUS_DUMMY_CMUTEX ((DBusCMutex *) _DBUS_DUMMY_MUTEX)
43 -
44 -/** This is used for the no-op default mutex pointer, just to be distinct from #NULL */
45 -#define _DBUS_DUMMY_CONDVAR ((DBusCondVar*)0xABCDEF2)
46  
47  /**
48   * @defgroup DBusThreadsInternals Thread functions
49 @@ -59,11 +47,6 @@ static DBusList *uninitialized_condvar_list = NULL;
50   * If possible, the mutex returned by this function is recursive, to
51   * avoid deadlocks. However, that cannot be relied on.
52   *
53 - * The extra level of indirection given by allocating a pointer
54 - * to point to the mutex location allows the threading
55 - * module to swap out dummy mutexes for a real mutex so libraries
56 - * can initialize threads even after the D-Bus API has been used.
57 - *
58   * @param location_p the location of the new mutex, can return #NULL on OOM
59   */
60  void
61 @@ -71,17 +54,13 @@ _dbus_rmutex_new_at_location (DBusRMutex **location_p)
62  {
63    _dbus_assert (location_p != NULL);
64  
65 -  if (thread_init_generation == _dbus_current_generation)
66 +  if (!dbus_threads_init_default ())
67      {
68 -      *location_p = _dbus_platform_rmutex_new ();
69 +      *location_p = NULL;
70 +      return;
71      }
72 -  else
73 -    {
74 -      *location_p = _DBUS_DUMMY_RMUTEX;
75  
76 -      if (!_dbus_list_append (&uninitialized_rmutex_list, location_p))
77 -        *location_p = NULL;
78 -    }
79 +  *location_p = _dbus_platform_rmutex_new ();
80  }
81  
82  /**
83 @@ -92,11 +71,6 @@ _dbus_rmutex_new_at_location (DBusRMutex **location_p)
84   *
85   * The returned mutex is suitable for use with condition variables.
86   *
87 - * The extra level of indirection given by allocating a pointer
88 - * to point to the mutex location allows the threading
89 - * module to swap out dummy mutexes for a real mutex so libraries
90 - * can initialize threads even after the D-Bus API has been used.
91 - *
92   * @param location_p the location of the new mutex, can return #NULL on OOM
93   */
94  void
95 @@ -104,22 +78,17 @@ _dbus_cmutex_new_at_location (DBusCMutex **location_p)
96  {
97    _dbus_assert (location_p != NULL);
98  
99 -  if (thread_init_generation == _dbus_current_generation)
100 +  if (!dbus_threads_init_default ())
101      {
102 -      *location_p = _dbus_platform_cmutex_new ();
103 +      *location_p = NULL;
104 +      return;
105      }
106 -  else
107 -    {
108 -      *location_p = _DBUS_DUMMY_CMUTEX;
109  
110 -      if (!_dbus_list_append (&uninitialized_cmutex_list, location_p))
111 -        *location_p = NULL;
112 -    }
113 +  *location_p = _dbus_platform_cmutex_new ();
114  }
115  
116  /**
117 - * Frees a DBusRMutex or removes it from the uninitialized mutex list;
118 - * does nothing if passed a #NULL pointer.
119 + * Frees a DBusRMutex; does nothing if passed a #NULL pointer.
120   */
121  void
122  _dbus_rmutex_free_at_location (DBusRMutex **location_p)
123 @@ -127,23 +96,12 @@ _dbus_rmutex_free_at_location (DBusRMutex **location_p)
124    if (location_p == NULL)
125      return;
126  
127 -  if (thread_init_generation == _dbus_current_generation)
128 -    {
129 -      if (*location_p != NULL)
130 -        _dbus_platform_rmutex_free (*location_p);
131 -    }
132 -  else
133 -    {
134 -      _dbus_assert (*location_p == NULL || *location_p == _DBUS_DUMMY_RMUTEX);
135 -
136 -      _dbus_list_remove (&uninitialized_rmutex_list, location_p);
137 -    }
138 +  if (*location_p != NULL)
139 +    _dbus_platform_rmutex_free (*location_p);
140  }
141  
142  /**
143 - * Frees a DBusCMutex and removes it from the
144 - * uninitialized mutex list;
145 - * does nothing if passed a #NULL pointer.
146 + * Frees a DBusCMutex; does nothing if passed a #NULL pointer.
147   */
148  void
149  _dbus_cmutex_free_at_location (DBusCMutex **location_p)
150 @@ -151,17 +109,8 @@ _dbus_cmutex_free_at_location (DBusCMutex **location_p)
151    if (location_p == NULL)
152      return;
153  
154 -  if (thread_init_generation == _dbus_current_generation)
155 -    {
156 -      if (*location_p != NULL)
157 -        _dbus_platform_cmutex_free (*location_p);
158 -    }
159 -  else
160 -    {
161 -      _dbus_assert (*location_p == NULL || *location_p == _DBUS_DUMMY_CMUTEX);
162 -
163 -      _dbus_list_remove (&uninitialized_cmutex_list, location_p);
164 -    }
165 +  if (*location_p != NULL)
166 +    _dbus_platform_cmutex_free (*location_p);
167  }
168  
169  /**
170 @@ -172,8 +121,10 @@ _dbus_cmutex_free_at_location (DBusCMutex **location_p)
171  void
172  _dbus_rmutex_lock (DBusRMutex *mutex)
173  {
174 -  if (mutex && thread_init_generation == _dbus_current_generation)
175 -    _dbus_platform_rmutex_lock (mutex);
176 +  if (mutex == NULL)
177 +    return;
178 +
179 +  _dbus_platform_rmutex_lock (mutex);
180  }
181  
182  /**
183 @@ -184,8 +135,10 @@ _dbus_rmutex_lock (DBusRMutex *mutex)
184  void
185  _dbus_cmutex_lock (DBusCMutex *mutex)
186  {
187 -  if (mutex && thread_init_generation == _dbus_current_generation)
188 -    _dbus_platform_cmutex_lock (mutex);
189 +  if (mutex == NULL)
190 +    return;
191 +
192 +  _dbus_platform_cmutex_lock (mutex);
193  }
194  
195  /**
196 @@ -196,8 +149,10 @@ _dbus_cmutex_lock (DBusCMutex *mutex)
197  void
198  _dbus_rmutex_unlock (DBusRMutex *mutex)
199  {
200 -  if (mutex && thread_init_generation == _dbus_current_generation)
201 -    _dbus_platform_rmutex_unlock (mutex);
202 +  if (mutex == NULL)
203 +    return;
204 +
205 +  _dbus_platform_rmutex_unlock (mutex);
206  }
207  
208  /**
209 @@ -208,8 +163,10 @@ _dbus_rmutex_unlock (DBusRMutex *mutex)
210  void
211  _dbus_cmutex_unlock (DBusCMutex *mutex)
212  {
213 -  if (mutex && thread_init_generation == _dbus_current_generation)
214 -    _dbus_platform_cmutex_unlock (mutex);
215 +  if (mutex == NULL)
216 +    return;
217 +
218 +  _dbus_platform_cmutex_unlock (mutex);
219  }
220  
221  /**
222 @@ -223,19 +180,17 @@ _dbus_cmutex_unlock (DBusCMutex *mutex)
223  DBusCondVar *
224  _dbus_condvar_new (void)
225  {
226 -  if (thread_init_generation == _dbus_current_generation)
227 -    return _dbus_platform_condvar_new ();
228 -  else
229 -    return _DBUS_DUMMY_CONDVAR;
230 +  if (!dbus_threads_init_default ())
231 +    return NULL;
232 +
233 +  return _dbus_platform_condvar_new ();
234  }
235  
236  
237  /**
238   * This does the same thing as _dbus_condvar_new.  It however
239   * gives another level of indirection by allocating a pointer
240 - * to point to the condvar location.  This allows the threading
241 - * module to swap out dummy condvars for a real condvar so libraries
242 - * can initialize threads even after the D-Bus API has been used.
243 + * to point to the condvar location; this used to be useful.
244   *
245   * @returns the location of a new condvar or #NULL on OOM
246   */
247 @@ -245,17 +200,7 @@ _dbus_condvar_new_at_location (DBusCondVar **location_p)
248  {
249    _dbus_assert (location_p != NULL);
250  
251 -  if (thread_init_generation == _dbus_current_generation)
252 -    {
253 -      *location_p = _dbus_condvar_new();
254 -    }
255 -  else
256 -    {
257 -      *location_p = _DBUS_DUMMY_CONDVAR;
258 -
259 -      if (!_dbus_list_append (&uninitialized_condvar_list, location_p))
260 -        *location_p = NULL;
261 -    }
262 +  *location_p = _dbus_condvar_new();
263  }
264  
265  
266 @@ -266,14 +211,14 @@ _dbus_condvar_new_at_location (DBusCondVar **location_p)
267  void
268  _dbus_condvar_free (DBusCondVar *cond)
269  {
270 -  if (cond && thread_init_generation == _dbus_current_generation)
271 -    _dbus_platform_condvar_free (cond);
272 +  if (cond == NULL)
273 +    return;
274 +
275 +  _dbus_platform_condvar_free (cond);
276  }
277  
278  /**
279 - * Frees a conditional variable and removes it from the 
280 - * uninitialized_condvar_list; 
281 - * does nothing if passed a #NULL pointer.
282 + * Frees a condition variable; does nothing if passed a #NULL pointer.
283   */
284  void
285  _dbus_condvar_free_at_location (DBusCondVar **location_p)
286 @@ -281,17 +226,8 @@ _dbus_condvar_free_at_location (DBusCondVar **location_p)
287    if (location_p == NULL)
288      return;
289  
290 -  if (thread_init_generation == _dbus_current_generation)
291 -    {
292 -      if (*location_p != NULL)
293 -        _dbus_platform_condvar_free (*location_p);
294 -    }
295 -  else
296 -    {
297 -      _dbus_assert (*location_p == NULL || *location_p == _DBUS_DUMMY_CONDVAR);
298 -
299 -      _dbus_list_remove (&uninitialized_condvar_list, location_p);
300 -    }
301 +  if (*location_p != NULL)
302 +    _dbus_platform_condvar_free (*location_p);
303  }
304  
305  /**
306 @@ -304,8 +240,10 @@ void
307  _dbus_condvar_wait (DBusCondVar *cond,
308                      DBusCMutex  *mutex)
309  {
310 -  if (cond && mutex && thread_init_generation == _dbus_current_generation)
311 -    _dbus_platform_condvar_wait (cond, mutex);
312 +  if (cond == NULL || mutex == NULL)
313 +    return;
314 +
315 +  _dbus_platform_condvar_wait (cond, mutex);
316  }
317  
318  /**
319 @@ -324,11 +262,11 @@ _dbus_condvar_wait_timeout (DBusCondVar               *cond,
320                              DBusCMutex                *mutex,
321                              int                        timeout_milliseconds)
322  {
323 -  if (cond && mutex && thread_init_generation == _dbus_current_generation)
324 -    return _dbus_platform_condvar_wait_timeout (cond, mutex,
325 -                                                timeout_milliseconds);
326 -  else
327 +  if (cond == NULL || mutex == NULL)
328      return TRUE;
329 +
330 +  return _dbus_platform_condvar_wait_timeout (cond, mutex,
331 +      timeout_milliseconds);
332  }
333  
334  /**
335 @@ -339,8 +277,10 @@ _dbus_condvar_wait_timeout (DBusCondVar               *cond,
336  void
337  _dbus_condvar_wake_one (DBusCondVar *cond)
338  {
339 -  if (cond && thread_init_generation == _dbus_current_generation)
340 -    _dbus_platform_condvar_wake_one (cond);
341 +  if (cond == NULL)
342 +    return;
343 +
344 +  _dbus_platform_condvar_wake_one (cond);
345  }
346  
347  static DBusRMutex *global_locks[_DBUS_N_GLOBAL_LOCKS] = { NULL };
348 @@ -358,132 +298,6 @@ shutdown_global_locks (void *nil)
349      }
350  }
351  
352 -static void
353 -shutdown_uninitialized_locks (void *data)
354 -{
355 -  _dbus_list_clear (&uninitialized_rmutex_list);
356 -  _dbus_list_clear (&uninitialized_cmutex_list);
357 -  _dbus_list_clear (&uninitialized_condvar_list);
358 -}
359 -
360 -/* init_global_locks() must be called first. */
361 -static dbus_bool_t
362 -init_uninitialized_locks (void)
363 -{
364 -  DBusList *link;
365 -  dbus_bool_t ok;
366 -
367 -  _dbus_assert (thread_init_generation != _dbus_current_generation);
368 -
369 -  link = uninitialized_rmutex_list;
370 -  while (link != NULL)
371 -    {
372 -      DBusRMutex **mp;
373 -
374 -      mp = link->data;
375 -      _dbus_assert (*mp == _DBUS_DUMMY_RMUTEX);
376 -
377 -      *mp = _dbus_platform_rmutex_new ();
378 -      if (*mp == NULL)
379 -        goto fail_mutex;
380 -
381 -      link = _dbus_list_get_next_link (&uninitialized_rmutex_list, link);
382 -    }
383 -
384 -  link = uninitialized_cmutex_list;
385 -  while (link != NULL)
386 -    {
387 -      DBusCMutex **mp;
388 -
389 -      mp = link->data;
390 -      _dbus_assert (*mp == _DBUS_DUMMY_CMUTEX);
391 -
392 -      *mp = _dbus_platform_cmutex_new ();
393 -      if (*mp == NULL)
394 -        goto fail_mutex;
395 -
396 -      link = _dbus_list_get_next_link (&uninitialized_cmutex_list, link);
397 -    }
398 -
399 -  link = uninitialized_condvar_list;
400 -  while (link != NULL)
401 -    {
402 -      DBusCondVar **cp;
403 -
404 -      cp = (DBusCondVar **)link->data;
405 -      _dbus_assert (*cp == _DBUS_DUMMY_CONDVAR);
406 -
407 -      *cp = _dbus_platform_condvar_new ();
408 -      if (*cp == NULL)
409 -        goto fail_condvar;
410 -
411 -      link = _dbus_list_get_next_link (&uninitialized_condvar_list, link);
412 -    }
413 -
414 -  _dbus_list_clear (&uninitialized_rmutex_list);
415 -  _dbus_list_clear (&uninitialized_cmutex_list);
416 -  _dbus_list_clear (&uninitialized_condvar_list);
417 -
418 -  /* This assumes that init_global_locks() has already been called. */
419 -  _dbus_platform_rmutex_lock (global_locks[_DBUS_LOCK_shutdown_funcs]);
420 -  ok = _dbus_register_shutdown_func_unlocked (shutdown_uninitialized_locks, NULL);
421 -  _dbus_platform_rmutex_unlock (global_locks[_DBUS_LOCK_shutdown_funcs]);
422 -
423 -  if (!ok)
424 -    goto fail_condvar;
425 -
426 -  return TRUE;
427 -
428 - fail_condvar:
429 -  link = uninitialized_condvar_list;
430 -  while (link != NULL)
431 -    {
432 -      DBusCondVar **cp;
433 -
434 -      cp = link->data;
435 -
436 -      if (*cp != _DBUS_DUMMY_CONDVAR && *cp != NULL)
437 -        _dbus_platform_condvar_free (*cp);
438 -
439 -      *cp = _DBUS_DUMMY_CONDVAR;
440 -
441 -      link = _dbus_list_get_next_link (&uninitialized_condvar_list, link);
442 -    }
443 -
444 - fail_mutex:
445 -  link = uninitialized_rmutex_list;
446 -  while (link != NULL)
447 -    {
448 -      DBusRMutex **mp;
449 -
450 -      mp = link->data;
451 -
452 -      if (*mp != _DBUS_DUMMY_RMUTEX && *mp != NULL)
453 -        _dbus_platform_rmutex_free (*mp);
454 -
455 -      *mp = _DBUS_DUMMY_RMUTEX;
456 -
457 -      link = _dbus_list_get_next_link (&uninitialized_rmutex_list, link);
458 -    }
459 -
460 -  link = uninitialized_cmutex_list;
461 -  while (link != NULL)
462 -    {
463 -      DBusCMutex **mp;
464 -
465 -      mp = link->data;
466 -
467 -      if (*mp != _DBUS_DUMMY_CMUTEX && *mp != NULL)
468 -        _dbus_platform_cmutex_free (*mp);
469 -
470 -      *mp = _DBUS_DUMMY_CMUTEX;
471 -
472 -      link = _dbus_list_get_next_link (&uninitialized_cmutex_list, link);
473 -    }
474 -
475 -  return FALSE;
476 -}
477 -
478  static dbus_bool_t
479  init_global_locks (void)
480  {
481 @@ -585,9 +399,7 @@ dbus_threads_init (const DBusThreadFunctions *functions)
482      }
483  
484    if (!_dbus_threads_init_platform_specific() ||
485 -      /* init_global_locks() must be called before init_uninitialized_locks. */
486 -      !init_global_locks () ||
487 -      !init_uninitialized_locks ())
488 +      !init_global_locks ())
489      {
490        _dbus_threads_unlock_platform_specific ();
491        return FALSE;