Олег Пронин wrote:
> Currently i'm having a lot of troubles with developing XS frameworks
> while keeping support for threaded perls
>
> 1) annoying pTHX/aTHX
typedef struct{
#ifdef PERL_IMPLICIT_CONTEXT
pTHX;
#endif
MORE_TYPE elements;
} some_struct;
// later on
#ifdef PERL_IMPLICIT_CONTEXT
# define dMYTHX tTHX aTHX = var->aTHX
#else
# define dMYTHX dNOOP
#endif
void
some_function(OBJECT var) {
dMYTHX;
.....
}
> 2) annoying class members 'my_perl' needed for destructors (or dTHX
> instead) when PERL_NO_GET_CONTEXT (because C++ desctructors has no
> args, you cant pass aTHX, so you either save my_perl as an object
> member in constructor, or use less efficient dTHX in desctructor).
> The same applies for callbacks which are called from C libs
> (libevent/libuv) and works with perl variables.
C callbacks almost always have void pointers you supply. Simple remember
to record the aTHX/my_perl in your void * struct. Remember, that "dTHX"
and passing my_perl on C stack must always be syncronized. If your C
library switches OS threads or uses a thread pool, you must call must
call PERL_SET_CONTEXT to move a perl thread between OS threads. If you
move a perl thread between OS threads you must have your own
mutexes/locks to make sure 1 perl thread is never ever running on 2 OS
threads simultaneously. See
http://perlmonks.org/?node_id=870109 and
http://perlmonks.org/?node_id=870516 and example isage
http://grep.cpan.me/?q=PERL_SET_THX|PERL_SET_CONTEXT
> 3) C++ XS frameworks not working after threads->create, because you
> either get core dump on double C++ object delete, or (if CLONE_SKIP)
> get a ref to undef which is not better anyway.
> I didn't find any callback/hook that perl calls on every blessed
> object when perl_clone().
> Is there any?
svt_dup see
http://perldoc.perl.org/perlguts.html#Magic-Virtual-Tables
you have to decide if your C++ object is "duplicatable" in concept or
not, which can only be determined by design of that C++ library.
See my implementation of using atomic CPU instructions to create a
non-perl refcount ontop of Perl's SV refcounts to handle multiple
ithreads owning and using the same C pointer, and last ithread owner of
the pointer frees the pointer. The C pointer that is shared between
ithreads in Win32-API came from a special "executable" malloc that
allocates memory blocks to place machine code to execute as C functions
later.
https://metacpan.org/source/BULKDD/Win32-API-0.79/Callback/Callback.xs#L273
https://metacpan.org/source/BULKDD/Win32-API-0.79/Callback/Callback.xs#L666
https://metacpan.org/source/BULKDD/Win32-API-0.79/Callback/Callback.xs#L692
> If not it seems impossible to properly implement XS objects with C
> underlying data.
Not true. All the resources are provided to you by Perl core.
> What is the status of threaded perl?
99% of Win32 Perls are threaded. ithreads are the only way to use more
than 1 CPU core without fork, or maintain a "thread pool" or workers.
Other languages such as
> does anybody use it? will it be
> okay if XS cpan modules will no longer support it?
That is your choice. There are modules on CPAN that hard code author's
/home directory with his computer's username to look up their
resources/permanent data tables. Of course, they will never work on
anyone elses computer.