Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^4: Sharing XS object?

by menth0l (Monk)
on Mar 09, 2011 at 14:59 UTC ( #892218=note: print w/replies, xml ) Need Help??

in reply to Re^3: Sharing XS object?
in thread Sharing XS object?

Perl: ActiveState 5.10
Threads: 1.82
Threads-shared: 1.36
My new() function from BK_XS.xs file:
#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #include "BKTree.h" MODULE = Tree::BK_XS PACKAGE = Tree::BK_XS PREFIX= BK_ SV * BK_new(package) char * package PROTOTYPE: $ CODE: OBJECT t; SV *tp; t = new_object(); tp = newSVpv((char *)&t, sizeof(OBJECT)); RETVAL = newRV(tp); SvREFCNT_dec(tp); sv_bless(RETVAL, gv_stashpv(package, 0)); SvREADONLY_on(tp); OUTPUT: RETVAL

Replies are listed 'Best First'.
Re^5: Sharing XS object?
by BrowserUk (Pope) on Mar 09, 2011 at 15:34 UTC

    If this is possible--I've never done it, mostly because I've never seen a need for it--then success will resolve around using the bless() function exported from threads::shared rather than the built-in version.

    Whilst it is possible to gain access to threads::shared::bless() from within your XS code, the simplest method to test whether it will actually work for you, would be to export the RV (RETVAL) from your XS new() (perhaps renamed _new()) un-blessed; and then add a wrapper method in the .pm file that calls the XS _new() and then applies the modified bless() (imported from threads::shared) at the Perl level before returning it.

    That should get you past the "Invalid value for shared scalar" problem. I do not expect it to be a complete solution. You will almost certainly have to add a CLONE() method to the package. And you will need to deal with the problem of multiple DESTROY()s. But one step at a time.

    If you module isn't proprietary, and doesn't have a raft of dependancies, and you were prepared to let me have an installable copy, it might make solving the knock on problems quicker.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Hi, I don't mind to release code to the public since it's based on Tree-Ternary_XS code (you can still see it in POD in and includes Text-LevenshteinXS anyway.

      Here is a link to source code .rar package

      I would be appreciated if u could look at it and give me a help with this problem. PS. I no expert in C (last time i used it was in college;) nor in XS so please, be understanding while reading the code :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://892218]
and !@monks...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2018-01-17 22:17 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (206 votes). Check out past polls.