fixed deadlock scenarion where g_signal_lookup() would be called with the
[platform/upstream/glib.git] / gobject / gclosure.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2000 Red Hat, Inc.
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 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
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include        "gclosure.h"
20
21 #include        "gvalue.h"
22
23
24 /* FIXME: need caching allocators
25  */
26
27 #define CLOSURE_MAX_REF_COUNT           ((1 << 15) - 1)
28 #define CLOSURE_MAX_N_GUARDS            ((1 << 1) - 1)
29 #define CLOSURE_MAX_N_FNOTIFIERS        ((1 << 2) - 1)
30 #define CLOSURE_MAX_N_INOTIFIERS        ((1 << 8) - 1)
31 #define CLOSURE_N_MFUNCS(cl)            ((cl)->meta_marshal + \
32                                          ((cl)->n_guards << 1L))
33 #define CLOSURE_N_NOTIFIERS(cl)         (CLOSURE_N_MFUNCS (cl) + \
34                                          (cl)->n_fnotifiers + \
35                                          (cl)->n_inotifiers)
36 enum {
37   FNOTIFY,
38   INOTIFY,
39   PRE_NOTIFY,
40   POST_NOTIFY
41 };
42
43
44 /* --- functions --- */
45 GClosure*
46 g_closure_new_simple (guint           sizeof_closure,
47                       gpointer        data)
48 {
49   GClosure *closure;
50
51   g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL);
52
53   closure = g_malloc (sizeof_closure);
54   closure->ref_count = 1;
55   closure->meta_marshal = 0;
56   closure->n_guards = 0;
57   closure->n_fnotifiers = 0;
58   closure->n_inotifiers = 0;
59   closure->in_inotify = FALSE;
60   closure->floating = TRUE;
61   closure->derivative_flag = 0;
62   closure->in_marshal = FALSE;
63   closure->is_invalid = FALSE;
64   closure->marshal = NULL;
65   closure->data = data;
66   closure->notifiers = NULL;
67   memset (G_STRUCT_MEMBER_P (closure, sizeof (*closure)), 0, sizeof_closure - sizeof (*closure));
68
69   return closure;
70 }
71
72 static inline void
73 closure_invoke_notifiers (GClosure *closure,
74                           guint     notify_type)
75 {
76   /* notifier layout:
77    *     meta_marshal  n_guards    n_guards     n_fnotif.  n_inotifiers
78    * ->[[meta_marshal][pre_guards][post_guards][fnotifers][inotifiers]]
79    *
80    * CLOSURE_N_MFUNCS(cl)    = meta_marshal + n_guards + n_guards;
81    * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers
82    *
83    * constrains/catches:
84    * - closure->notifiers may be reloacted during callback
85    * - closure->n_fnotifiers and closure->n_inotifiers may change during callback
86    * - i.e. callbacks can be removed/added during invocation
87    * - have to prepare for callback removal during invocation (->marshal & ->data)
88    * - have to distinguish (->marshal & ->data) for INOTIFY/FNOTIFY (->in_inotify)
89    * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY
90    * + closure->meta_marshal is const for all cases
91    * + none of the callbacks can cause recursion
92    * + closure->n_inotifiers is const 0 during FNOTIFY
93    */
94   switch (notify_type)
95     {
96       GClosureNotifyData *ndata;
97       guint i, offs;
98     case FNOTIFY:
99       while (closure->n_fnotifiers)
100         {
101           register guint n = --closure->n_fnotifiers;
102
103           ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n;
104           closure->marshal = (gpointer) ndata->notify;
105           closure->data = ndata->data;
106           ndata->notify (ndata->data, closure);
107         }
108       closure->marshal = NULL;
109       closure->data = NULL;
110       break;
111     case INOTIFY:
112       closure->in_inotify = TRUE;
113       while (closure->n_inotifiers)
114         {
115           register guint n = --closure->n_inotifiers;
116
117           ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n;
118           closure->marshal = (gpointer) ndata->notify;
119           closure->data = ndata->data;
120           ndata->notify (ndata->data, closure);
121         }
122       closure->marshal = NULL;
123       closure->data = NULL;
124       closure->in_inotify = FALSE;
125       break;
126     case PRE_NOTIFY:
127       i = closure->n_guards;
128       offs = closure->meta_marshal;
129       while (i--)
130         {
131           ndata = closure->notifiers + offs + i;
132           ndata->notify (ndata->data, closure);
133         }
134       break;
135     case POST_NOTIFY:
136       i = closure->n_guards;
137       offs = closure->meta_marshal + i;
138       while (i--)
139         {
140           ndata = closure->notifiers + offs + i;
141           ndata->notify (ndata->data, closure);
142         }
143       break;
144     }
145 }
146
147 void
148 g_closure_set_meta_marshal (GClosure       *closure,
149                             gpointer        marshal_data,
150                             GClosureMarshal meta_marshal)
151 {
152   GClosureNotifyData *notifiers;
153   guint n;
154
155   g_return_if_fail (closure != NULL);
156   g_return_if_fail (meta_marshal != NULL);
157   g_return_if_fail (closure->is_invalid == FALSE);
158   g_return_if_fail (closure->in_marshal == FALSE);
159   g_return_if_fail (closure->meta_marshal == FALSE);
160
161   n = CLOSURE_N_NOTIFIERS (closure);
162   notifiers = closure->notifiers;
163   closure->notifiers = g_renew (GClosureNotifyData, NULL, CLOSURE_N_NOTIFIERS (closure) + 1);
164   if (notifiers)
165     {
166       /* usually the meta marshal will be setup right after creation, so the
167        * g_memmove() should be rare-case scenario
168        */
169       g_memmove (closure->notifiers + 1, notifiers, CLOSURE_N_NOTIFIERS (closure) * sizeof (notifiers[0]));
170       g_free (notifiers);
171     }
172   closure->notifiers[0].data = marshal_data;
173   closure->notifiers[0].notify = (GClosureNotify) meta_marshal;
174   closure->meta_marshal = 1;
175 }
176
177 void
178 g_closure_add_marshal_guards (GClosure      *closure,
179                               gpointer       pre_marshal_data,
180                               GClosureNotify pre_marshal_notify,
181                               gpointer       post_marshal_data,
182                               GClosureNotify post_marshal_notify)
183 {
184   guint i;
185
186   g_return_if_fail (closure != NULL);
187   g_return_if_fail (pre_marshal_notify != NULL);
188   g_return_if_fail (post_marshal_notify != NULL);
189   g_return_if_fail (closure->is_invalid == FALSE);
190   g_return_if_fail (closure->in_marshal == FALSE);
191   g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS);
192
193   closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2);
194   if (closure->n_inotifiers)
195     closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
196                         closure->n_fnotifiers +
197                         closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
198                                                                           closure->n_fnotifiers + 0)];
199   if (closure->n_inotifiers > 1)
200     closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
201                         closure->n_fnotifiers +
202                         closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
203                                                                       closure->n_fnotifiers + 1)];
204   if (closure->n_fnotifiers)
205     closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
206                         closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0];
207   if (closure->n_fnotifiers > 1)
208     closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
209                         closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1];
210   if (closure->n_guards)
211     closure->notifiers[(closure->meta_marshal +
212                         closure->n_guards +
213                         closure->n_guards + 1)] = closure->notifiers[closure->meta_marshal + closure->n_guards];
214   i = closure->n_guards++;
215   closure->notifiers[closure->meta_marshal + i].data = pre_marshal_data;
216   closure->notifiers[closure->meta_marshal + i].notify = pre_marshal_notify;
217   closure->notifiers[closure->meta_marshal + i + i].data = post_marshal_data;
218   closure->notifiers[closure->meta_marshal + i + i].notify = post_marshal_notify;
219 }
220
221 void
222 g_closure_add_fnotify (GClosure      *closure,
223                        gpointer       notify_data,
224                        GClosureNotify notify_func)
225 {
226   guint i;
227
228   g_return_if_fail (closure != NULL);
229   g_return_if_fail (notify_func != NULL);
230   g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS);
231
232   closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
233   if (closure->n_inotifiers)
234     closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
235                         closure->n_fnotifiers +
236                         closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
237                                                                       closure->n_fnotifiers + 0)];
238   i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers++;
239   closure->notifiers[i].data = notify_data;
240   closure->notifiers[i].notify = notify_func;
241 }
242
243 void
244 g_closure_add_inotify (GClosure      *closure,
245                        gpointer       notify_data,
246                        GClosureNotify notify_func)
247 {
248   guint i;
249
250   g_return_if_fail (closure != NULL);
251   g_return_if_fail (notify_func != NULL);
252   g_return_if_fail (closure->is_invalid == FALSE);
253   g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS);
254
255   closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
256   i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers++;
257   closure->notifiers[i].data = notify_data;
258   closure->notifiers[i].notify = notify_func;
259 }
260
261 static inline gboolean
262 closure_try_remove_inotify (GClosure       *closure,
263                             gpointer       notify_data,
264                             GClosureNotify notify_func)
265 {
266   GClosureNotifyData *ndata, *nlast;
267
268   nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1;
269   for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++)
270     if (ndata->notify == notify_func && ndata->data == notify_data)
271       {
272         closure->n_inotifiers -= 1;
273         if (ndata < nlast)
274           *ndata = *nlast;
275
276         return TRUE;
277       }
278   return FALSE;
279 }
280
281 static inline gboolean
282 closure_try_remove_fnotify (GClosure       *closure,
283                             gpointer       notify_data,
284                             GClosureNotify notify_func)
285 {
286   GClosureNotifyData *ndata, *nlast;
287
288   nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1;
289   for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++)
290     if (ndata->notify == notify_func && ndata->data == notify_data)
291       {
292         closure->n_fnotifiers -= 1;
293         if (ndata < nlast)
294           *ndata = *nlast;
295         if (closure->n_inotifiers)
296           closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
297                               closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
298                                                                             closure->n_fnotifiers +
299                                                                             closure->n_inotifiers)];
300         return TRUE;
301       }
302   return FALSE;
303 }
304
305 GClosure*
306 g_closure_ref (GClosure *closure)
307 {
308   g_return_val_if_fail (closure != NULL, NULL);
309   g_return_val_if_fail (closure->ref_count > 0, NULL);
310   g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL);
311
312   /* floating is basically a kludge to avoid creating closures
313    * with a ref_count of 0. so the first one doing _ref() will
314    * own the closure's initial ref_count
315    */
316   if (closure->floating)
317     closure->floating = FALSE;
318   else
319     closure->ref_count += 1;
320
321   return closure;
322 }
323
324 void
325 g_closure_invalidate (GClosure *closure)
326 {
327   g_return_if_fail (closure != NULL);
328
329   if (!closure->is_invalid)
330     {
331       closure->ref_count += 1;  /* preserve floating flag */
332       closure->is_invalid = TRUE;
333       closure_invoke_notifiers (closure, INOTIFY);
334       g_closure_unref (closure);
335     }
336 }
337
338 void
339 g_closure_unref (GClosure *closure)
340 {
341   g_return_if_fail (closure != NULL);
342   g_return_if_fail (closure->ref_count > 0);
343
344   if (closure->ref_count == 1)  /* last unref, invalidate first */
345     g_closure_invalidate (closure);
346
347   closure->ref_count -= 1;
348
349   if (closure->ref_count == 0)
350     {
351       closure_invoke_notifiers (closure, FNOTIFY);
352       g_free (closure);
353     }
354 }
355
356 void
357 g_closure_remove_inotify (GClosure      *closure,
358                           gpointer       notify_data,
359                           GClosureNotify notify_func)
360 {
361   g_return_if_fail (closure != NULL);
362   g_return_if_fail (notify_func != NULL);
363
364   if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while its called */
365       ((gpointer) closure->marshal) == ((gpointer) notify_func) && closure->data == notify_data)
366     closure->marshal = NULL;
367   else if (!closure_try_remove_inotify (closure, notify_data, notify_func))
368     g_warning (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)",
369                notify_func, notify_data);
370 }
371
372 void
373 g_closure_remove_fnotify (GClosure      *closure,
374                           gpointer       notify_data,
375                           GClosureNotify notify_func)
376 {
377   g_return_if_fail (closure != NULL);
378   g_return_if_fail (notify_func != NULL);
379
380   if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while its called */
381       ((gpointer) closure->marshal) == ((gpointer) notify_func) && closure->data == notify_data)
382     closure->marshal = NULL;
383   else if (!closure_try_remove_fnotify (closure, notify_data, notify_func))
384     g_warning (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)",
385                notify_func, notify_data);
386 }
387
388 void
389 g_closure_invoke (GClosure       *closure,
390                   guint           invocation_hint,
391                   GValue /*out*/ *return_value,
392                   guint           n_param_values,
393                   const GValue   *param_values)
394 {
395   g_return_if_fail (closure != NULL);
396   g_return_if_fail (closure->marshal || closure->meta_marshal);
397
398   if (!closure->is_invalid)
399     {
400       GClosureMarshal marshal;
401       gpointer marshal_data;
402       gboolean in_marshal = closure->in_marshal;
403
404       closure->ref_count += 1;  /* preserve floating flag */
405       closure->in_marshal = TRUE;
406       if (closure->meta_marshal)
407         {
408           marshal_data = closure->notifiers[0].data;
409           marshal = (GClosureMarshal) closure->notifiers[0].notify;
410         }
411       else
412         {
413           marshal_data = NULL;
414           marshal = closure->marshal;
415         }
416       if (!in_marshal)
417         closure_invoke_notifiers (closure, PRE_NOTIFY);
418       marshal (closure, invocation_hint,
419                return_value,
420                n_param_values, param_values,
421                marshal_data);
422       if (!in_marshal)
423         closure_invoke_notifiers (closure, POST_NOTIFY);
424       closure->in_marshal = in_marshal;
425       g_closure_unref (closure);
426     }
427 }
428
429 void
430 g_closure_set_marshal (GClosure       *closure,
431                        GClosureMarshal marshal)
432 {
433   g_return_if_fail (closure != NULL);
434   g_return_if_fail (marshal != NULL);
435
436   if (closure->marshal && closure->marshal != marshal)
437     g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
438                closure->marshal, marshal);
439   else
440     closure->marshal = marshal;
441 }
442
443 GClosure*
444 g_cclosure_new (GCallback      callback_func,
445                 gpointer       user_data,
446                 GClosureNotify destroy_data)
447 {
448   GClosure *closure;
449   
450   g_return_val_if_fail (callback_func != NULL, NULL);
451   
452   closure = g_closure_new_simple (sizeof (GCClosure), user_data);
453   if (destroy_data)
454     g_closure_add_fnotify (closure, user_data, destroy_data);
455   ((GCClosure*) closure)->callback = callback_func;
456   
457   return closure;
458 }
459
460 GClosure*
461 g_cclosure_new_swap (GCallback      callback_func,
462                      gpointer       user_data,
463                      GClosureNotify destroy_data)
464 {
465   GClosure *closure;
466   
467   g_return_val_if_fail (callback_func != NULL, NULL);
468   
469   closure = g_closure_new_simple (sizeof (GCClosure), user_data);
470   if (destroy_data)
471     g_closure_add_fnotify (closure, user_data, destroy_data);
472   ((GCClosure*) closure)->callback = callback_func;
473   closure->derivative_flag = TRUE;
474   
475   return closure;
476 }
477
478 static void
479 g_type_class_meta_marshal (GClosure       *closure,
480                            guint           invocation_hint,
481                            GValue /*out*/ *return_value,
482                            guint           n_param_values,
483                            const GValue   *param_values,
484                            gpointer        marshal_data)
485 {
486   GTypeClass *class;
487   gpointer callback;
488   /* GType itype = GPOINTER_TO_UINT (closure->data); */
489   guint offset = GPOINTER_TO_UINT (marshal_data);
490   
491   class = G_TYPE_INSTANCE_GET_CLASS (g_value_get_as_pointer (param_values + 0), itype, GTypeClass);
492   callback = G_STRUCT_MEMBER (gpointer, class, offset);
493   if (callback)
494     closure->marshal (closure, invocation_hint, return_value,
495                       n_param_values, param_values, callback);
496 }
497
498 static void
499 g_type_iface_meta_marshal (GClosure       *closure,
500                            guint           invocation_hint,
501                            GValue /*out*/ *return_value,
502                            guint           n_param_values,
503                            const GValue   *param_values,
504                            gpointer        marshal_data)
505 {
506   GTypeClass *class;
507   gpointer callback;
508   GType itype = GPOINTER_TO_UINT (closure->data);
509   guint offset = GPOINTER_TO_UINT (marshal_data);
510   
511   class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_get_as_pointer (param_values + 0), itype, GTypeClass);
512   callback = G_STRUCT_MEMBER (gpointer, class, offset);
513   if (callback)
514     closure->marshal (closure, invocation_hint, return_value,
515                       n_param_values, param_values, callback);
516 }
517
518 GClosure*
519 g_signal_type_closure_new (GType    itype,
520                            guint    struct_offset)
521 {
522   GClosure *closure;
523   
524   g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
525   g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL);
526   
527   closure = g_closure_new_simple (sizeof (GClosure), GUINT_TO_POINTER (itype));
528   if (G_TYPE_IS_INTERFACE (itype))
529     g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal);
530   else
531     g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);
532   
533   return closure;
534 }