Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

sky (1411)

  (email not shown publicly)

sky on IRC

Journal of sky (1411)

Tuesday May 01, 2001
04:29 AM

Sharing a SV between Perls

[ #122 ]
The main problem I have come across is the sharing of a SV between two interpreters, not even active sharing but creating a SV in one interpreter, even from the C level, and then passing it over to another thread.

Now this works as I mentioned before, but when perl_destruct is called all the arenas (I have no clue what an arena is, but apperntly that is where SVs are situated) are destroyed. This means that the SV is destroyed and when another thread tries to use it BOOM segv here we come.

One solution to this is just set PL_perl_destruct_level to 0, but then we leak memory because nothing is cleaned up! This is clearly not a good solution.

My current idea is to take a copy of the main perl interpereter before execution, then use that to create SVs and then return a reference to that SV. That means all shared variables are anonymous and only accessed by a reference.
One day perl will hopefully have my $foo :shared; but part of this project is to add perl level support for interpreter threads without modifying the current perl core, so I will have to develop a workaround

Ideas could be

  • $arrayref = shared( [ ] );
  • $hashref = shared( {} );
  • $hashref->{foo} = shared("foo");
  • lock($arrayref);
  • unlock($arrayref);

Unfourntatly ALL variables that are shared between two threads have to be declared shared, even members of a hashref.
In those cases when you want to pass a variable, I will do Perl_newSVsv(copy_of_main_perl, SV); to create a copy that will live past original interpreter destruction.