Ross McFarland > Glib-1.043 > GClosure.xs


Annotate this POD


New  3
Open  3
View/Report Bugs

GClosure / GPerlClosure

GPerlClosure is a wrapper around the gobject library's GClosure with special handling for marshalling perl subroutines as callbacks. This is specially tuned for use with GSignal and stuff like io watch, timeout, and idle handlers.

For generic callback functions, which need parameters but do not get registered with the type system, this is sometimes overkill. See GPerlCallback, below.

GClosure * gperl_closure_new (SV * callback, SV * data, gboolean swap)

Create and return a new GPerlClosure. callback and data will be copied for storage; callback must not be NULL. If swap is TRUE, data will be swapped with the instance during invocation (this is used to implement g_signal_connect_swapped()).

If compiled under a thread-enabled perl, the closure will be created and marshaled in such a way as to ensure that the same interpreter which created the closure will be used to invoke it.

GClosure * gperl_closure_new_with_marshaller (SV * callback, SV * data, gboolean swap, GClosureMarshal marshaller)

Like gperl_closure_new, but uses a caller-supplied marshaller. This is provided for use in those sticky circumstances when you just can't do it any other way; in general, you want to use the default marshaller, which you get if you provide NULL for marshaller.

If you use you own marshaller, you need to take care of everything yourself, including swapping the instance and data if GPERL_CLOSURE_SWAP_DATA (closure) is true, calling gperl_run_exception_handlers if ERRSV is true after invoking the perl sub, and ensuring that you properly use the marshal_data parameter as the perl interpreter when PERL_IMPLICIT_CONTEXT is defined. See the implementation of the default marshaller, gperl_closure_marshal, in Glib/GClosure.xs for inspiration.


generic callback functions usually get invoked directly, and are not passed parameter lists as GValues. we could very easily wrap up such generic callbacks with something that converts the parameters to GValues and then channels everything through GClosure, but this has two problems: 1) the above implementation of GClosure is tuned to marshalling signal handlers, which always have an instance object, and 2) it's more work than is strictly necessary.

additionally, generic callbacks aren't always kind to the GClosure paradigm.

so, here's GPerlCallback, which is designed specifically to run generic callback functions. it reads parameters off the C stack and converts them into parameters on the perl stack. (it uses the GValue to/from SV mechanism to do so, but doesn't allocate any temps on the heap.) the callback object itself stores the parameter type list.

unfortunately, since the data element is always last, but the number of arguments is not known until we have the callback object, we can't pass gperl_callback_invoke directly to functions requiring a callback; you'll have to write a proxy callback which calls gperl_callback_invoke.

GPerlCallback * gperl_callback_new (SV * func, SV * data, gint n_params, GType param_types[], GType return_type)

Create and return a new GPerlCallback; use gperl_callback_destroy when you are finished with it.

func: perl subroutine to call. this SV will be copied, so don't worry about reference counts. must not be #NULL.

data: scalar to pass to func in addition to all other arguments. the SV will be copied, so don't worry about reference counts. may be #NULL.

n_params: the number of elements in param_types.

param_types: the #GType of each argument that should be passed from the invocation to func. may be #NULL if n_params is zero, otherwise it must be n_params elements long or nasty things will happen. this array will be copied; see gperl_callback_invoke() for how it is used.

return_type: the #GType of the return value, or 0 if the function has void return.

void gperl_callback_destroy (GPerlCallback * callback)

Dispose of callback.

void gperl_callback_invoke (GPerlCallback * callback, GValue * return_value, ...)

Marshall the variadic parameters according to callback's param_types, and then invoke callback's subroutine in scalar context, or void context if the return type is G_TYPE_VOID. If return_value is not NULL, then value returned (if any) will be copied into return_value.

A typical callback handler would look like this:

  static gint
  real_c_callback (Foo * f, Bar * b, int a, gpointer data)
          GPerlCallback * callback = (GPerlCallback*)data;
          GValue return_value = {0,};
          gint retval;
          g_value_init (&return_value, callback->return_type);
          gperl_callback_invoke (callback, &return_value,
                                 f, b, a);
          retval = g_value_get_int (&return_value);
          g_value_unset (&return_value);
          return retval;

Exception Handling

Like Event, Tk, and most other callback-using, event-based perl modules, Glib traps exceptions that happen in callbacks. To enable your code to do something about these exceptions, Glib stores a list of exception handlers which will be called on the trapped exceptions. This is completely distinct from the $SIG{__DIE__} mechanism provided by Perl itself, for various reasons (not the least of which is that the Perl docs and source code say that $SIG{__DIE__} is intended for running as the program is about to exit, and other behaviors may be removed in the future (apparently a source of much debate on p5p)).

int gperl_install_exception_handler (GClosure * closure)

Install a GClosure to be executed when gperl_closure_invoke() traps an exception. The closure should return boolean (TRUE if the handler should remain installed) and expect to receive a perl scalar. This scalar will be a private copy of ERRSV ($@) which the handler can mangle to its heart's content.

The return value is an integer id tag that may be passed to gperl_removed_exception_handler().

void gperl_remove_exception_handler (guint tag)

Remove the exception handler identified by tag, as returned by gperl_install_exception_handler(). If tag cannot be found, this does nothing.

WARNING: this function locks a global data structure, so do NOT call it recursively. also, calling this from within an exception handler will result in a deadlock situation. if you want to remove your handler just have it return FALSE.

void gperl_run_exception_handlers (void)

Invoke whatever exception handlers are installed. You will need this if you have written a custom marshaler. Uses the value of the global ERRSV.

Install a subroutine to be executed when a signal emission traps an exception (a croak or die). $func should return boolean (true if the handler should remain installed) and expect to receive a single scalar. This scalar will be a private copy of $@ which the handler can mangle to its heart's content.

Returns an identifier that may be used with remove_exception_handler.

See gperl_install_exception_handler() in Glib::xsapi.

Remove the exception handler identified by $tag, as returned by install_exception_handler. If $tag cannot be found, this does nothing.

WARNING: Do not call this function from within an exception handler. If you want to remove your handler during its execution just have it return false.

See gperl_remove_exception_handler() in Glib::xsapi.

syntax highlighting: