1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
3 * Copyright (C) 2005 Imendio AB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
23 * MT safe with regards to reference counting.
27 #include "gobjectalias.h"
31 #define CLOSURE_MAX_REF_COUNT ((1 << 15) - 1)
32 #define CLOSURE_MAX_N_GUARDS ((1 << 1) - 1)
33 #define CLOSURE_MAX_N_FNOTIFIERS ((1 << 2) - 1)
34 #define CLOSURE_MAX_N_INOTIFIERS ((1 << 8) - 1)
35 #define CLOSURE_N_MFUNCS(cl) ((cl)->meta_marshal + \
36 ((cl)->n_guards << 1L))
37 /* same as G_CLOSURE_N_NOTIFIERS() (keep in sync) */
38 #define CLOSURE_N_NOTIFIERS(cl) (CLOSURE_N_MFUNCS (cl) + \
39 (cl)->n_fnotifiers + \
47 #define CHANGE_FIELD(_closure, _field, _OP, _value, _must_set, _SET_OLD, _SET_NEW) \
49 ClosureInt *cunion = (ClosureInt*) _closure; \
50 gint new_int, old_int, success; \
54 tmp.vint = old_int = cunion->vint; \
55 _SET_OLD tmp.closure._field; \
56 tmp.closure._field _OP _value; \
57 _SET_NEW tmp.closure._field; \
59 success = g_atomic_int_compare_and_exchange (&cunion->vint, old_int, new_int); \
61 while (!success && _must_set); \
64 #define SWAP(_closure, _field, _value, _oldv) CHANGE_FIELD (_closure, _field, =, _value, TRUE, *(_oldv) =, (void) )
65 #define SET(_closure, _field, _value) CHANGE_FIELD (_closure, _field, =, _value, TRUE, (void), (void) )
66 #define INC(_closure, _field) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), (void) )
67 #define INC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, +=, 1, TRUE, (void), *(_newv) = )
68 #define DEC(_closure, _field) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), (void) )
69 #define DEC_ASSIGN(_closure, _field, _newv) CHANGE_FIELD (_closure, _field, -=, 1, TRUE, (void), *(_newv) = )
71 #if 0 // for non-thread-safe closures
72 #define SWAP(cl,f,v,o) (void) (*(o) = cl->f, cl->f = v)
73 #define SET(cl,f,v) (void) (cl->f = v)
74 #define INC(cl,f) (void) (cl->f += 1)
75 #define INC_ASSIGN(cl,f,n) (void) (cl->f += 1, *(n) = cl->f)
76 #define DEC(cl,f) (void) (cl->f -= 1)
77 #define DEC_ASSIGN(cl,f,n) (void) (cl->f -= 1, *(n) = cl->f)
88 /* --- functions --- */
90 g_closure_new_simple (guint sizeof_closure,
95 g_return_val_if_fail (sizeof_closure >= sizeof (GClosure), NULL);
97 closure = g_malloc0 (sizeof_closure);
98 SET (closure, ref_count, 1);
99 SET (closure, meta_marshal, 0);
100 SET (closure, n_guards, 0);
101 SET (closure, n_fnotifiers, 0);
102 SET (closure, n_inotifiers, 0);
103 SET (closure, in_inotify, FALSE);
104 SET (closure, floating, TRUE);
105 SET (closure, derivative_flag, 0);
106 SET (closure, in_marshal, FALSE);
107 SET (closure, is_invalid, FALSE);
108 closure->marshal = NULL;
109 closure->data = data;
110 closure->notifiers = NULL;
111 memset (G_STRUCT_MEMBER_P (closure, sizeof (*closure)), 0, sizeof_closure - sizeof (*closure));
117 closure_invoke_notifiers (GClosure *closure,
121 * meta_marshal n_guards n_guards n_fnotif. n_inotifiers
122 * ->[[meta_marshal][pre_guards][post_guards][fnotifiers][inotifiers]]
124 * CLOSURE_N_MFUNCS(cl) = meta_marshal + n_guards + n_guards;
125 * CLOSURE_N_NOTIFIERS(cl) = CLOSURE_N_MFUNCS(cl) + n_fnotifiers + n_inotifiers
127 * constrains/catches:
128 * - closure->notifiers may be reloacted during callback
129 * - closure->n_fnotifiers and closure->n_inotifiers may change during callback
130 * - i.e. callbacks can be removed/added during invocation
131 * - must prepare for callback removal during FNOTIFY and INOTIFY (done via ->marshal= & ->data=)
132 * - must distinguish (->marshal= & ->data=) for INOTIFY vs. FNOTIFY (via ->in_inotify)
133 * + closure->n_guards is const during PRE_NOTIFY & POST_NOTIFY
134 * + closure->meta_marshal is const for all cases
135 * + none of the callbacks can cause recursion
136 * + closure->n_inotifiers is const 0 during FNOTIFY
140 GClosureNotifyData *ndata;
143 while (closure->n_fnotifiers)
146 DEC_ASSIGN (closure, n_fnotifiers, &n);
148 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + n;
149 closure->marshal = (GClosureMarshal) ndata->notify;
150 closure->data = ndata->data;
151 ndata->notify (ndata->data, closure);
153 closure->marshal = NULL;
154 closure->data = NULL;
157 SET (closure, in_inotify, TRUE);
158 while (closure->n_inotifiers)
161 DEC_ASSIGN (closure, n_inotifiers, &n);
163 ndata = closure->notifiers + CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + n;
164 closure->marshal = (GClosureMarshal) ndata->notify;
165 closure->data = ndata->data;
166 ndata->notify (ndata->data, closure);
168 closure->marshal = NULL;
169 closure->data = NULL;
170 SET (closure, in_inotify, FALSE);
173 i = closure->n_guards;
174 offs = closure->meta_marshal;
177 ndata = closure->notifiers + offs + i;
178 ndata->notify (ndata->data, closure);
182 i = closure->n_guards;
183 offs = closure->meta_marshal + i;
186 ndata = closure->notifiers + offs + i;
187 ndata->notify (ndata->data, closure);
194 g_closure_set_meta_marshal (GClosure *closure,
195 gpointer marshal_data,
196 GClosureMarshal meta_marshal)
198 GClosureNotifyData *notifiers;
201 g_return_if_fail (closure != NULL);
202 g_return_if_fail (meta_marshal != NULL);
203 g_return_if_fail (closure->is_invalid == FALSE);
204 g_return_if_fail (closure->in_marshal == FALSE);
205 g_return_if_fail (closure->meta_marshal == 0);
207 n = CLOSURE_N_NOTIFIERS (closure);
208 notifiers = closure->notifiers;
209 closure->notifiers = g_renew (GClosureNotifyData, NULL, CLOSURE_N_NOTIFIERS (closure) + 1);
212 /* usually the meta marshal will be setup right after creation, so the
213 * g_memmove() should be rare-case scenario
215 g_memmove (closure->notifiers + 1, notifiers, CLOSURE_N_NOTIFIERS (closure) * sizeof (notifiers[0]));
218 closure->notifiers[0].data = marshal_data;
219 closure->notifiers[0].notify = (GClosureNotify) meta_marshal;
220 SET (closure, meta_marshal, 1);
224 g_closure_add_marshal_guards (GClosure *closure,
225 gpointer pre_marshal_data,
226 GClosureNotify pre_marshal_notify,
227 gpointer post_marshal_data,
228 GClosureNotify post_marshal_notify)
232 g_return_if_fail (closure != NULL);
233 g_return_if_fail (pre_marshal_notify != NULL);
234 g_return_if_fail (post_marshal_notify != NULL);
235 g_return_if_fail (closure->is_invalid == FALSE);
236 g_return_if_fail (closure->in_marshal == FALSE);
237 g_return_if_fail (closure->n_guards < CLOSURE_MAX_N_GUARDS);
239 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 2);
240 if (closure->n_inotifiers)
241 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
242 closure->n_fnotifiers +
243 closure->n_inotifiers + 1)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
244 closure->n_fnotifiers + 0)];
245 if (closure->n_inotifiers > 1)
246 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
247 closure->n_fnotifiers +
248 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
249 closure->n_fnotifiers + 1)];
250 if (closure->n_fnotifiers)
251 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
252 closure->n_fnotifiers + 1)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 0];
253 if (closure->n_fnotifiers > 1)
254 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
255 closure->n_fnotifiers)] = closure->notifiers[CLOSURE_N_MFUNCS (closure) + 1];
256 if (closure->n_guards)
257 closure->notifiers[(closure->meta_marshal +
259 closure->n_guards + 1)] = closure->notifiers[closure->meta_marshal + closure->n_guards];
260 i = closure->n_guards;
261 closure->notifiers[closure->meta_marshal + i].data = pre_marshal_data;
262 closure->notifiers[closure->meta_marshal + i].notify = pre_marshal_notify;
263 closure->notifiers[closure->meta_marshal + i + 1].data = post_marshal_data;
264 closure->notifiers[closure->meta_marshal + i + 1].notify = post_marshal_notify;
265 INC (closure, n_guards);
269 g_closure_add_finalize_notifier (GClosure *closure,
270 gpointer notify_data,
271 GClosureNotify notify_func)
275 g_return_if_fail (closure != NULL);
276 g_return_if_fail (notify_func != NULL);
277 g_return_if_fail (closure->n_fnotifiers < CLOSURE_MAX_N_FNOTIFIERS);
279 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
280 if (closure->n_inotifiers)
281 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
282 closure->n_fnotifiers +
283 closure->n_inotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
284 closure->n_fnotifiers + 0)];
285 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers;
286 closure->notifiers[i].data = notify_data;
287 closure->notifiers[i].notify = notify_func;
288 INC (closure, n_fnotifiers);
292 g_closure_add_invalidate_notifier (GClosure *closure,
293 gpointer notify_data,
294 GClosureNotify notify_func)
298 g_return_if_fail (closure != NULL);
299 g_return_if_fail (notify_func != NULL);
300 g_return_if_fail (closure->is_invalid == FALSE);
301 g_return_if_fail (closure->n_inotifiers < CLOSURE_MAX_N_INOTIFIERS);
303 closure->notifiers = g_renew (GClosureNotifyData, closure->notifiers, CLOSURE_N_NOTIFIERS (closure) + 1);
304 i = CLOSURE_N_MFUNCS (closure) + closure->n_fnotifiers + closure->n_inotifiers;
305 closure->notifiers[i].data = notify_data;
306 closure->notifiers[i].notify = notify_func;
307 INC (closure, n_inotifiers);
310 static inline gboolean
311 closure_try_remove_inotify (GClosure *closure,
312 gpointer notify_data,
313 GClosureNotify notify_func)
315 GClosureNotifyData *ndata, *nlast;
317 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - 1;
318 for (ndata = nlast + 1 - closure->n_inotifiers; ndata <= nlast; ndata++)
319 if (ndata->notify == notify_func && ndata->data == notify_data)
321 DEC (closure, n_inotifiers);
330 static inline gboolean
331 closure_try_remove_fnotify (GClosure *closure,
332 gpointer notify_data,
333 GClosureNotify notify_func)
335 GClosureNotifyData *ndata, *nlast;
337 nlast = closure->notifiers + CLOSURE_N_NOTIFIERS (closure) - closure->n_inotifiers - 1;
338 for (ndata = nlast + 1 - closure->n_fnotifiers; ndata <= nlast; ndata++)
339 if (ndata->notify == notify_func && ndata->data == notify_data)
341 DEC (closure, n_fnotifiers);
344 if (closure->n_inotifiers)
345 closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
346 closure->n_fnotifiers)] = closure->notifiers[(CLOSURE_N_MFUNCS (closure) +
347 closure->n_fnotifiers +
348 closure->n_inotifiers)];
355 g_closure_ref (GClosure *closure)
358 g_return_val_if_fail (closure != NULL, NULL);
359 g_return_val_if_fail (closure->ref_count > 0, NULL);
360 g_return_val_if_fail (closure->ref_count < CLOSURE_MAX_REF_COUNT, NULL);
362 INC_ASSIGN (closure, ref_count, &new_ref_count);
363 g_return_val_if_fail (new_ref_count > 1, NULL);
369 g_closure_invalidate (GClosure *closure)
371 g_return_if_fail (closure != NULL);
373 if (!closure->is_invalid)
375 gboolean was_invalid;
376 g_closure_ref (closure); /* preserve floating flag */
377 SWAP (closure, is_invalid, TRUE, &was_invalid);
378 /* invalidate only once */
380 closure_invoke_notifiers (closure, INOTIFY);
381 g_closure_unref (closure);
386 g_closure_unref (GClosure *closure)
390 g_return_if_fail (closure != NULL);
391 g_return_if_fail (closure->ref_count > 0);
393 if (closure->ref_count == 1) /* last unref, invalidate first */
394 g_closure_invalidate (closure);
396 DEC_ASSIGN (closure, ref_count, &new_ref_count);
398 if (new_ref_count == 0)
400 closure_invoke_notifiers (closure, FNOTIFY);
401 g_free (closure->notifiers);
407 g_closure_sink (GClosure *closure)
409 g_return_if_fail (closure != NULL);
410 g_return_if_fail (closure->ref_count > 0);
412 /* floating is basically a kludge to avoid creating closures
413 * with a ref_count of 0. so the intial ref_count a closure has
414 * is unowned. with invoking g_closure_sink() code may
415 * indicate that it takes over that intiial ref_count.
417 if (closure->floating)
419 gboolean was_floating;
420 SWAP (closure, floating, FALSE, &was_floating);
421 /* unref floating flag only once */
423 g_closure_unref (closure);
428 g_closure_remove_invalidate_notifier (GClosure *closure,
429 gpointer notify_data,
430 GClosureNotify notify_func)
432 g_return_if_fail (closure != NULL);
433 g_return_if_fail (notify_func != NULL);
435 if (closure->is_invalid && closure->in_inotify && /* account removal of notify_func() while its called */
436 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
437 closure->data == notify_data)
438 closure->marshal = NULL;
439 else if (!closure_try_remove_inotify (closure, notify_data, notify_func))
440 g_warning (G_STRLOC ": unable to remove uninstalled invalidation notifier: %p (%p)",
441 notify_func, notify_data);
445 g_closure_remove_finalize_notifier (GClosure *closure,
446 gpointer notify_data,
447 GClosureNotify notify_func)
449 g_return_if_fail (closure != NULL);
450 g_return_if_fail (notify_func != NULL);
452 if (closure->is_invalid && !closure->in_inotify && /* account removal of notify_func() while its called */
453 ((gpointer) closure->marshal) == ((gpointer) notify_func) &&
454 closure->data == notify_data)
455 closure->marshal = NULL;
456 else if (!closure_try_remove_fnotify (closure, notify_data, notify_func))
457 g_warning (G_STRLOC ": unable to remove uninstalled finalization notifier: %p (%p)",
458 notify_func, notify_data);
462 g_closure_invoke (GClosure *closure,
463 GValue /*out*/ *return_value,
464 guint n_param_values,
465 const GValue *param_values,
466 gpointer invocation_hint)
468 g_return_if_fail (closure != NULL);
470 g_closure_ref (closure); /* preserve floating flag */
471 if (!closure->is_invalid)
473 GClosureMarshal marshal;
474 gpointer marshal_data;
475 gboolean in_marshal = closure->in_marshal;
477 g_return_if_fail (closure->marshal || closure->meta_marshal);
479 SET (closure, in_marshal, TRUE);
480 if (closure->meta_marshal)
482 marshal_data = closure->notifiers[0].data;
483 marshal = (GClosureMarshal) closure->notifiers[0].notify;
488 marshal = closure->marshal;
491 closure_invoke_notifiers (closure, PRE_NOTIFY);
494 n_param_values, param_values,
498 closure_invoke_notifiers (closure, POST_NOTIFY);
499 SET (closure, in_marshal, in_marshal);
501 g_closure_unref (closure);
505 g_closure_set_marshal (GClosure *closure,
506 GClosureMarshal marshal)
508 g_return_if_fail (closure != NULL);
509 g_return_if_fail (marshal != NULL);
511 if (closure->marshal && closure->marshal != marshal)
512 g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
513 closure->marshal, marshal);
515 closure->marshal = marshal;
519 g_cclosure_new (GCallback callback_func,
521 GClosureNotify destroy_data)
525 g_return_val_if_fail (callback_func != NULL, NULL);
527 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
529 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
530 ((GCClosure*) closure)->callback = (gpointer) callback_func;
536 g_cclosure_new_swap (GCallback callback_func,
538 GClosureNotify destroy_data)
542 g_return_val_if_fail (callback_func != NULL, NULL);
544 closure = g_closure_new_simple (sizeof (GCClosure), user_data);
546 g_closure_add_finalize_notifier (closure, user_data, destroy_data);
547 ((GCClosure*) closure)->callback = (gpointer) callback_func;
548 SET (closure, derivative_flag, TRUE);
554 g_type_class_meta_marshal (GClosure *closure,
555 GValue /*out*/ *return_value,
556 guint n_param_values,
557 const GValue *param_values,
558 gpointer invocation_hint,
559 gpointer marshal_data)
563 /* GType itype = (GType) closure->data; */
564 guint offset = GPOINTER_TO_UINT (marshal_data);
566 class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
567 callback = G_STRUCT_MEMBER (gpointer, class, offset);
569 closure->marshal (closure,
571 n_param_values, param_values,
577 g_type_iface_meta_marshal (GClosure *closure,
578 GValue /*out*/ *return_value,
579 guint n_param_values,
580 const GValue *param_values,
581 gpointer invocation_hint,
582 gpointer marshal_data)
586 GType itype = (GType) closure->data;
587 guint offset = GPOINTER_TO_UINT (marshal_data);
589 class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values + 0), itype, GTypeClass);
590 callback = G_STRUCT_MEMBER (gpointer, class, offset);
592 closure->marshal (closure,
594 n_param_values, param_values,
600 g_signal_type_cclosure_new (GType itype,
605 g_return_val_if_fail (G_TYPE_IS_CLASSED (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
606 g_return_val_if_fail (struct_offset >= sizeof (GTypeClass), NULL);
608 closure = g_closure_new_simple (sizeof (GClosure), (gpointer) itype);
609 if (G_TYPE_IS_INTERFACE (itype))
610 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_iface_meta_marshal);
612 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (struct_offset), g_type_class_meta_marshal);
617 #define __G_CLOSURE_C__
618 #include "gobjectaliasdef.c"