Discussion:
XS callback function
(too old to reply)
Private and Confidential
2010-02-11 00:32:36 UTC
Permalink
I've written a callback function for an XS module designed to call a
perl function when invoked.

If I call the function directly, from within the C code, it runs through
completely as expected.

If its invoked as a callback from the library I am wrapping, it does
immediately at "dSP;" i.e. SV **sp = PL_stack_sp;

Specifically PL_stack_sp seems to cause SEGV when touched - I can't even
see the value, printf dies also.

I can't find anything similar discussed anywhere, does anyone have an
idea what might be happening?
muppet
2010-02-11 03:48:53 UTC
Permalink
I've written a callback function for an XS module designed to call a perl function when invoked.
If I call the function directly, from within the C code, it runs through completely as expected.
If its invoked as a callback from the library I am wrapping, it does immediately at "dSP;" i.e. SV **sp = PL_stack_sp;
Specifically PL_stack_sp seems to cause SEGV when touched - I can't even see the value, printf dies also.
I can't find anything similar discussed anywhere, does anyone have an idea what might be happening?
Are there multiple threads involved?

If you're cold-calling from C code, you probably need to set up the context properly. This should be mentioned in the perlguts, perlxs, and perlxstut manpages.

If you're coming from another thread, then you have considerably more work to do to get the context set up safely for invoking code in the interpreter.



--
If the monkey could type one keystroke every nanosecond, the expected waiting time until the monkey types out Hamlet is so long that the estimated age of the universe is insignificant by comparison ... this is not a practical method for writing plays.
-- Gian-Carlo Rota
Private and Confidential
2010-02-11 05:37:10 UTC
Permalink
Confirmed, the library I am wrapping runs in a separate thread and runs
its callbacks from there. "context" is a good keyword and led me to dTHX
and PERL_SET_CONTEXT. I haven't found a good example of using these yet.
Adding dTHX did get me past dSP, but I die consistently in ENTER now.
Post by muppet
I've written a callback function for an XS module designed to call a perl function when invoked.
If I call the function directly, from within the C code, it runs through completely as expected.
If its invoked as a callback from the library I am wrapping, it does immediately at "dSP;" i.e. SV **sp = PL_stack_sp;
Specifically PL_stack_sp seems to cause SEGV when touched - I can't even see the value, printf dies also.
I can't find anything similar discussed anywhere, does anyone have an idea what might be happening?
Are there multiple threads involved?
If you're cold-calling from C code, you probably need to set up the context properly. This should be mentioned in the perlguts, perlxs, and perlxstut manpages.
If you're coming from another thread, then you have considerably more work to do to get the context set up safely for invoking code in the interpreter.
--
If the monkey could type one keystroke every nanosecond, the expected waiting time until the monkey types out Hamlet is so long that the estimated age of the universe is insignificant by comparison ... this is not a practical method for writing plays.
-- Gian-Carlo Rota
muppet
2010-02-11 17:43:38 UTC
Permalink
Post by Private and Confidential
Confirmed, the library I am wrapping runs in a separate thread and runs
its callbacks from there. "context" is a good keyword and led me to dTHX
and PERL_SET_CONTEXT. I haven't found a good example of using these yet.
Adding dTHX did get me past dSP, but I die consistently in ENTER now.
Everything i know on the subject is encoded in gperl_marshal.h and
GPerlClosure.xs in the Glib extension. I recall having to do "interesting"
work to get the interpreter context set up appropriately to allow callbacks on
other threads, but it was almost seven years ago and i couldn't begin to
explain it now.

http://git.gnome.org/browse/perl-Glib/tree/gperl_marshal.h
http://git.gnome.org/browse/perl-Glib/tree/GClosure.xs

Specifically, look at GPerlCallback, which is what gtk2-perl uses to wrap the
bare functions used as callbacks throughout the gnome api.

Things to look for: gperl_callback_invoke(), GPERL_CALLBACK_MARSHAL_INIT().
--
muppet <scott at asofyet dot org>
Loading...