Sharing a SV between Perls

sky on 2001-05-01T09:29:33

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.