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
;
200 g_return_if_fail (closure
!= NULL
);
201 g_return_if_fail (meta_marshal
!= NULL
);
202 g_return_if_fail (closure
->is_invalid
== FALSE
);
203 g_return_if_fail (closure
->in_marshal
== FALSE
);
204 g_return_if_fail (closure
->meta_marshal
== 0);
206 notifiers
= closure
->notifiers
;
207 closure
->notifiers
= g_renew (GClosureNotifyData
, NULL
, CLOSURE_N_NOTIFIERS (closure
) + 1);
210 /* usually the meta marshal will be setup right after creation, so the
211 * g_memmove() should be rare-case scenario
213 g_memmove (closure
->notifiers
+ 1, notifiers
, CLOSURE_N_NOTIFIERS (closure
) * sizeof (notifiers
[0]));
216 closure
->notifiers
[0].data
= marshal_data
;
217 closure
->notifiers
[0].notify
= (GClosureNotify
) meta_marshal
;
218 SET (closure
, meta_marshal
, 1);
222 g_closure_add_marshal_guards (GClosure
*closure
,
223 gpointer pre_marshal_data
,
224 GClosureNotify pre_marshal_notify
,
225 gpointer post_marshal_data
,
226 GClosureNotify post_marshal_notify
)
230 g_return_if_fail (closure
!= NULL
);
231 g_return_if_fail (pre_marshal_notify
!= NULL
);
232 g_return_if_fail (post_marshal_notify
!= NULL
);
233 g_return_if_fail (closure
->is_invalid
== FALSE
);
234 g_return_if_fail (closure
->in_marshal
== FALSE
);
235 g_return_if_fail (closure
->n_guards
< CLOSURE_MAX_N_GUARDS
);
237 closure
->notifiers
= g_renew (GClosureNotifyData
, closure
->notifiers
, CLOSURE_N_NOTIFIERS (closure
) + 2);
238 if (closure
->n_inotifiers
)
239 closure
->notifiers
[(CLOSURE_N_MFUNCS (closure
) +
240 closure
->n_fnotifiers
+
241 closure
->n_inotifiers
+ 1)] = closure
->notifiers
[(CLOSURE_N_MFUNCS (closure
) +
242 closure
->n_fnotifiers
+ 0)];
243 if (closure
->n_inotifiers
> 1)
244 closure
->notifiers
[(CLOSURE_N_MFUNCS (closure
) +
245 closure
->n_fnotifiers
+
246 closure
->n_inotifiers
)] = closure
->notifiers
[(CLOSURE_N_MFUNCS (closure
) +
247 closure
->n_fnotifiers
+ 1)];
248 if (closure
->n_fnotifiers
)
249 closure
->notifiers
[(CLOSURE_N_MFUNCS (closure
) +
250 closure
->n_fnotifiers
+ 1)] = closure
->notifiers
[CLOSURE_N_MFUNCS (closure
) + 0];
251 if (closure
->n_fnotifiers
> 1)
252 closure
->notifiers
[(CLOSURE_N_MFUNCS (closure
) +
253 closure
->n_fnotifiers
)] = closure
->notifiers
[CLOSURE_N_MFUNCS (closure
) + 1];
254 if (closure
->n_guards
)
255 closure
->notifiers
[(closure
->meta_marshal
+
257 closure
->n_guards
+ 1)] = closure
->notifiers
[closure
->meta_marshal
+ closure
->n_guards
];
258 i
= closure
->n_guards
;
259 closure
->notifiers
[closure
->meta_marshal
+ i
].data
= pre_marshal_data
;
260 closure
->notifiers
[closure
->meta_marshal
+ i
].notify
= pre_marshal_notify
;
261 closure
->notifiers
[closure
->meta_marshal
+ i
+ 1].data
= post_marshal_data
;
262 closure
->notifiers
[closure
->meta_marshal
+ i
+ 1].notify
= post_marshal_notify
;
263 INC (closure
, n_guards
);
267 g_closure_add_finalize_notifier (GClosure
*closure
,
268 gpointer notify_data
,
269 GClosureNotify notify_func
)
273 g_return_if_fail (closure
!= NULL
);
274 g_return_if_fail (notify_func
!= NULL
);
275 g_return_if_fail (closure
->n_fnotifiers
< CLOSURE_MAX_N_FNOTIFIERS
);
277 closure
->notifiers
= g_renew (GClosureNotifyData
, closure
->notifiers
, CLOSURE_N_NOTIFIERS (closure
) + 1);
278 if (closure
->n_inotifiers
)
279 closure
->notifiers
[(CLOSURE_N_MFUNCS (closure
) +
280 closure
->n_fnotifiers
+
281 closure
->n_inotifiers
)] = closure
->notifiers
[(CLOSURE_N_MFUNCS (closure
) +
282 closure
->n_fnotifiers
+ 0)];
283 i
= CLOSURE_N_MFUNCS (closure
) + closure
->n_fnotifiers
;
284 closure
->notifiers
[i
].data
= notify_data
;
285 closure
->notifiers
[i
].notify
= notify_func
;
286 INC (closure
, n_fnotifiers
);
290 g_closure_add_invalidate_notifier (GClosure
*closure
,
291 gpointer notify_data
,
292 GClosureNotify notify_func
)
296 g_return_if_fail (closure
!= NULL
);
297 g_return_if_fail (notify_func
!= NULL
);
298 g_return_if_fail (closure
->is_invalid
== FALSE
);
299 g_return_if_fail (closure
->n_inotifiers
< CLOSURE_MAX_N_INOTIFIERS
);
301 closure
->notifiers
= g_renew (GClosureNotifyData
, closure
->notifiers
, CLOSURE_N_NOTIFIERS (closure
) + 1);
302 i
= CLOSURE_N_MFUNCS (closure
) + closure
->n_fnotifiers
+ closure
->n_inotifiers
;
303 closure
->notifiers
[i
].data
= notify_data
;
304 closure
->notifiers
[i
].notify
= notify_func
;
305 INC (closure
, n_inotifiers
);
308 static inline gboolean
309 closure_try_remove_inotify (GClosure
*closure
,
310 gpointer notify_data
,
311 GClosureNotify notify_func
)
313 GClosureNotifyData
*ndata
, *nlast
;
315 nlast
= closure
->notifiers
+ CLOSURE_N_NOTIFIERS (closure
) - 1;
316 for (ndata
= nlast
+ 1 - closure
->n_inotifiers
; ndata
<= nlast
; ndata
++)
317 if (ndata
->notify
== notify_func
&& ndata
->data
== notify_data
)
319 DEC (closure
, n_inotifiers
);
328 static inline gboolean
329 closure_try_remove_fnotify (GClosure
*closure
,
330 gpointer notify_data
,
331 GClosureNotify notify_func
)
333 GClosureNotifyData
*ndata
, *nlast
;
335 nlast
= closure
->notifiers
+ CLOSURE_N_NOTIFIERS (closure
) - closure
->n_inotifiers
- 1;
336 for (ndata
= nlast
+ 1 - closure
->n_fnotifiers
; ndata
<= nlast
; ndata
++)
337 if (ndata
->notify
== notify_func
&& ndata
->data
== notify_data
)
339 DEC (closure
, n_fnotifiers
);
342 if (closure
->n_inotifiers
)
343 closure
->notifiers
[(CLOSURE_N_MFUNCS (closure
) +
344 closure
->n_fnotifiers
)] = closure
->notifiers
[(CLOSURE_N_MFUNCS (closure
) +
345 closure
->n_fnotifiers
+
346 closure
->n_inotifiers
)];
353 g_closure_ref (GClosure
*closure
)
356 g_return_val_if_fail (closure
!= NULL
, NULL
);
357 g_return_val_if_fail (closure
->ref_count
> 0, NULL
);
358 g_return_val_if_fail (closure
->ref_count
< CLOSURE_MAX_REF_COUNT
, NULL
);
360 INC_ASSIGN (closure
, ref_count
, &new_ref_count
);
361 g_return_val_if_fail (new_ref_count
> 1, NULL
);
367 g_closure_invalidate (GClosure
*closure
)
369 g_return_if_fail (closure
!= NULL
);
371 if (!closure
->is_invalid
)
373 gboolean was_invalid
;
374 g_closure_ref (closure
); /* preserve floating flag */
375 SWAP (closure
, is_invalid
, TRUE
, &was_invalid
);
376 /* invalidate only once */
378 closure_invoke_notifiers (closure
, INOTIFY
);
379 g_closure_unref (closure
);
384 g_closure_unref (GClosure
*closure
)
388 g_return_if_fail (closure
!= NULL
);
389 g_return_if_fail (closure
->ref_count
> 0);
391 if (closure
->ref_count
== 1) /* last unref, invalidate first */
392 g_closure_invalidate (closure
);
394 DEC_ASSIGN (closure
, ref_count
, &new_ref_count
);
396 if (new_ref_count
== 0)
398 closure_invoke_notifiers (closure
, FNOTIFY
);
399 g_free (closure
->notifiers
);
405 g_closure_sink (GClosure
*closure
)
407 g_return_if_fail (closure
!= NULL
);
408 g_return_if_fail (closure
->ref_count
> 0);
410 /* floating is basically a kludge to avoid creating closures
411 * with a ref_count of 0. so the intial ref_count a closure has
412 * is unowned. with invoking g_closure_sink() code may
413 * indicate that it takes over that intiial ref_count.
415 if (closure
->floating
)
417 gboolean was_floating
;
418 SWAP (closure
, floating
, FALSE
, &was_floating
);
419 /* unref floating flag only once */
421 g_closure_unref (closure
);
426 g_closure_remove_invalidate_notifier (GClosure
*closure
,
427 gpointer notify_data
,
428 GClosureNotify notify_func
)
430 g_return_if_fail (closure
!= NULL
);
431 g_return_if_fail (notify_func
!= NULL
);
433 if (closure
->is_invalid
&& closure
->in_inotify
&& /* account removal of notify_func() while its called */
434 ((gpointer
) closure
->marshal
) == ((gpointer
) notify_func
) &&
435 closure
->data
== notify_data
)
436 closure
->marshal
= NULL
;
437 else if (!closure_try_remove_inotify (closure
, notify_data
, notify_func
))
438 g_warning (G_STRLOC
": unable to remove uninstalled invalidation notifier: %p (%p)",
439 notify_func
, notify_data
);
443 g_closure_remove_finalize_notifier (GClosure
*closure
,
444 gpointer notify_data
,
445 GClosureNotify notify_func
)
447 g_return_if_fail (closure
!= NULL
);
448 g_return_if_fail (notify_func
!= NULL
);
450 if (closure
->is_invalid
&& !closure
->in_inotify
&& /* account removal of notify_func() while its called */
451 ((gpointer
) closure
->marshal
) == ((gpointer
) notify_func
) &&
452 closure
->data
== notify_data
)
453 closure
->marshal
= NULL
;
454 else if (!closure_try_remove_fnotify (closure
, notify_data
, notify_func
))
455 g_warning (G_STRLOC
": unable to remove uninstalled finalization notifier: %p (%p)",
456 notify_func
, notify_data
);
460 g_closure_invoke (GClosure
*closure
,
461 GValue
/*out*/ *return_value
,
462 guint n_param_values
,
463 const GValue
*param_values
,
464 gpointer invocation_hint
)
466 g_return_if_fail (closure
!= NULL
);
468 g_closure_ref (closure
); /* preserve floating flag */
469 if (!closure
->is_invalid
)
471 GClosureMarshal marshal
;
472 gpointer marshal_data
;
473 gboolean in_marshal
= closure
->in_marshal
;
475 g_return_if_fail (closure
->marshal
|| closure
->meta_marshal
);
477 SET (closure
, in_marshal
, TRUE
);
478 if (closure
->meta_marshal
)
480 marshal_data
= closure
->notifiers
[0].data
;
481 marshal
= (GClosureMarshal
) closure
->notifiers
[0].notify
;
486 marshal
= closure
->marshal
;
489 closure_invoke_notifiers (closure
, PRE_NOTIFY
);
492 n_param_values
, param_values
,
496 closure_invoke_notifiers (closure
, POST_NOTIFY
);
497 SET (closure
, in_marshal
, in_marshal
);
499 g_closure_unref (closure
);
503 g_closure_set_marshal (GClosure
*closure
,
504 GClosureMarshal marshal
)
506 g_return_if_fail (closure
!= NULL
);
507 g_return_if_fail (marshal
!= NULL
);
509 if (closure
->marshal
&& closure
->marshal
!= marshal
)
510 g_warning ("attempt to override closure->marshal (%p) with new marshal (%p)",
511 closure
->marshal
, marshal
);
513 closure
->marshal
= marshal
;
517 g_cclosure_new (GCallback callback_func
,
519 GClosureNotify destroy_data
)
523 g_return_val_if_fail (callback_func
!= NULL
, NULL
);
525 closure
= g_closure_new_simple (sizeof (GCClosure
), user_data
);
527 g_closure_add_finalize_notifier (closure
, user_data
, destroy_data
);
528 ((GCClosure
*) closure
)->callback
= (gpointer
) callback_func
;
534 g_cclosure_new_swap (GCallback callback_func
,
536 GClosureNotify destroy_data
)
540 g_return_val_if_fail (callback_func
!= NULL
, NULL
);
542 closure
= g_closure_new_simple (sizeof (GCClosure
), user_data
);
544 g_closure_add_finalize_notifier (closure
, user_data
, destroy_data
);
545 ((GCClosure
*) closure
)->callback
= (gpointer
) callback_func
;
546 SET (closure
, derivative_flag
, TRUE
);
552 g_type_class_meta_marshal (GClosure
*closure
,
553 GValue
/*out*/ *return_value
,
554 guint n_param_values
,
555 const GValue
*param_values
,
556 gpointer invocation_hint
,
557 gpointer marshal_data
)
561 /* GType itype = (GType) closure->data; */
562 guint offset
= GPOINTER_TO_UINT (marshal_data
);
564 class = G_TYPE_INSTANCE_GET_CLASS (g_value_peek_pointer (param_values
+ 0), itype
, GTypeClass
);
565 callback
= G_STRUCT_MEMBER (gpointer
, class, offset
);
567 closure
->marshal (closure
,
569 n_param_values
, param_values
,
575 g_type_iface_meta_marshal (GClosure
*closure
,
576 GValue
/*out*/ *return_value
,
577 guint n_param_values
,
578 const GValue
*param_values
,
579 gpointer invocation_hint
,
580 gpointer marshal_data
)
584 GType itype
= (GType
) closure
->data
;
585 guint offset
= GPOINTER_TO_UINT (marshal_data
);
587 class = G_TYPE_INSTANCE_GET_INTERFACE (g_value_peek_pointer (param_values
+ 0), itype
, GTypeClass
);
588 callback
= G_STRUCT_MEMBER (gpointer
, class, offset
);
590 closure
->marshal (closure
,
592 n_param_values
, param_values
,
598 g_signal_type_cclosure_new (GType itype
,
603 g_return_val_if_fail (G_TYPE_IS_CLASSED (itype
) || G_TYPE_IS_INTERFACE (itype
), NULL
);
604 g_return_val_if_fail (struct_offset
>= sizeof (GTypeClass
), NULL
);
606 closure
= g_closure_new_simple (sizeof (GClosure
), (gpointer
) itype
);
607 if (G_TYPE_IS_INTERFACE (itype
))
608 g_closure_set_meta_marshal (closure
, GUINT_TO_POINTER (struct_offset
), g_type_iface_meta_marshal
);
610 g_closure_set_meta_marshal (closure
, GUINT_TO_POINTER (struct_offset
), g_type_class_meta_marshal
);
615 #define __G_CLOSURE_C__
616 #include "gobjectaliasdef.c"