Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^7: Extending & Embedding Perl simultaneously: How to share a scalar between perl callback sub and main body?

by perlmonk1729 (Acolyte)
on Dec 03, 2009 at 19:12 UTC ( [id://810910]=note: print w/replies, xml ) Need Help??


in reply to Re^6: Extending & Embedding Perl simultaneously: How to share a scalar between perl callback sub and main body?
in thread Extending & Embedding Perl simultaneously: How to share a scalar between perl callback sub and main body?

Note: Completely rewrote this post as I got some sleep and have the code to demo the issue. Hoping you can now really see the cause/problem and offer ideas.

Thanks in advance!

PS: I think there some leaks etc in that code, but didnt care about those at this point.

myModule.xs

#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #include <pthread.h> static PerlInterpreter *orig_perl=NULL; static PerlInterpreter *cb_perl=NULL; SV* cb_ptr = NULL; void InvokeCB () { static int val = 0; val++; dTHX; dSP; ENTER; SAVETMPS; PUSHMARK(SP); SV * sv = cb_ptr; if (my_perl != orig_perl) { CLONE_PARAMS clone_param; clone_param.stashes = NULL; clone_param.flags = CLONEf_COPY_STACKS | CLONEf_KEEP_PTR_TABLE; clone_param.proto_perl = cb_perl; sv = sv_dup(sv, &clone_param); } XPUSHs(sv_2mortal(newSViv(val))); PUTBACK; call_sv(sv, G_DISCARD); FREETMPS; LEAVE; } void * BGThread(void * dontcare) { PERL_SET_CONTEXT(orig_perl); cb_perl = perl_clone(orig_perl, CLONEf_COPY_STACKS | CLONEf_KEEP_PT +R_TABLE); PERL_SET_CONTEXT(cb_perl); while (1) { sleep(5); InvokeCB(); } } MODULE = myModule PACKAGE = myModule int RegisterCB (SV *SubRef) CODE: pthread_t tid; pthread_create(&tid, NULL, BGThread, NULL); orig_perl = PERL_GET_CONTEXT; cb_ptr = newSVsv(SubRef); SvSHARE(cb_ptr); RETVAL = 1; OUTPUT: RETVAL

test.pl

#! /usr/local/bin/perl use myModule; use warnings; #shared scalar $cb_done = 0; @results = (); #This cb would be invoked by the cloned interpreter in the #C library. This works fine. sub cb_one { ($value) = @_; print "CB called. val received : ", $value, "\n"; $results[scalar(@results)] = $value; if ($value == 5) { print "cb_done changed to one.\n"; $cb_done = 1; } } print "Registered CB...\n"; $status = myModule::RegisterCB(\&main::cb_one); do { print "Waiting for CB to be done...\n"; sleep (5); } until ($cb_done == 1); #These line should be printed when $cb_done becomes 1 #in the main of the perl script print "CB was invoked : $cb_done\n"; print "results are : @results \n";

script output

Name "main::status" used only once: possible typo at ./test.pl line 27 +. Registered CB... Waiting for CB to be done... Waiting for CB to be done... CB called. val received : 1 Waiting for CB to be done... CB called. val received : 2 Waiting for CB to be done... CB called. val received : 3 Waiting for CB to be done... CB called. val received : 4 Waiting for CB to be done... CB called. val received : 5 cb_done changed to one. Waiting for CB to be done... CB called. val received : 6 Waiting for CB to be done... CB called. val received : 7
  • Comment on Re^7: Extending & Embedding Perl simultaneously: How to share a scalar between perl callback sub and main body?
  • Select or Download Code

Replies are listed 'Best First'.
Re^8: Extending & Embedding Perl simultaneously: How to share a scalar between perl callback sub and main body?
by perlmonk1729 (Acolyte) on Dec 07, 2009 at 07:36 UTC
    Adding more pieces to complete the code sample.

    myModule.pm

    package myModule; use 5.008005; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); # Items to export into callers namespace by default. Note: do not expo +rt # names by default without a very good reason. Use EXPORT_OK instead. # Do not simply export all your public functions/methods/constants. # This allows declaration use myModule ':all'; # If you do not need this, moving things directly into @EXPORT or @EXP +ORT_OK # will save memory. our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( ); our $VERSION = '0.01'; require XSLoader; XSLoader::load('myModule', $VERSION); # Preloaded methods go here. 1; __END__ # Below is stub documentation for your module. You'd better edit it! =head1 NAME

    makefile.pl : Changed "AUTHOR DETAILS" for posting

    use 5.008005; use ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. WriteMakefile( NAME => 'myModule', VERSION_FROM => 'lib/myModule.pm', # finds $VERSION PREREQ_PM => {}, # e.g., Module::Name => 1.1 ($] >= 5.005 ? ## Add these new keywords supported since 5.005 (ABSTRACT_FROM => 'lib/myModule.pm', # retrieve abstract from m +odule AUTHOR => 'Removed by me') : ()), LIBS => [''], # e.g., '-lm' DEFINE => '', # e.g., '-DHAVE_SOMETHING' INC => '-I.', # e.g., '-I. -I/usr/include/other' # Un-comment this if you add C files to link with later: # OBJECT => '$(O_FILES)', # link all the C files too );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://810910]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2024-04-25 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found