* Nick Ing-Simmons wrote:
>>  http://search.cpan.org/src/BJOERN/SGML-Parser-OpenSP-0.02/OpenSP.xs
>>is a C++ XS extension. I am trying to figure out what is needed, if
>>anything, to make it play nicely in the context of multiple threads.
>
>The MOST important thing is to make sure your XS code and any library 
>code it calls is ready for threads. That means no static variables
>without MUTEX exclusion and all the usual thready things.

I am mostly concerned about understanding best practise in this regard
as the documentation is not all that clear to me; my understanding for
the specific module is that the library is said to be thread-safe and
the only global variables in my extensions are some static U32 values
which are initialized in the constructor using PERL_HASH(...) and then
never changed, so from that I would say the code is ready for threads.

Is there a simple method to actually test whether there would be major
issues when using the module in some multi-threaded Perl environment?

>Probably true, but any sane callback scheme will pass some 
>"user data" pointer to the C function. You can put the interpreter
>pointer in that if you want to go to extreams.

For C++ `this` would always be passed so there is generally no need to
pass user data around. I did not think about this here as perlguts does
not discuss the actual implementation in the relevant section hence I
considered that to be something I am not supposed to know and assumed
magic :-)

>class PerlBase {
>...
>private:
>  PerlInterpreter *my_perl;
>...
>};

>Perhaps a constructor like:
>
>   PerlBase(pTHX) { this->my_perl = my_perl; }  
>
>is all it needs ???

Makes sense to me! I tried that, I've changed all helper functions to
methods of the main class and added

  ...
  #define PERL_NO_GET_CONTEXT
  ...
  PerlInterpreter* my_perl;
  ...

and changed the constructor (adding the two lines) to

  SgmlParserOpenSP::SgmlParserOpenSP()
  {
      dTHX;
      this->my_perl = my_perl;
  ...  

This compiles fine and all tests pass; Benchmark.pm tells me on Windows
2000 on a Intel Mobile Celeron 1066 with ActivePerl 5.8.2 and no thread
stuff involved for a 7,2 MB XML doc this version is about 10% faster.

Thanks!

Reply via email to