| 1 | package ExtUtils::XSpp::Plugin::Cloning; | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | our $VERSION = '0.02'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use Carp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use ExtUtils::XSpp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | ExtUtils::XSpp::Plugin::Cloning - An XS++ plugin for controlling cloning on thread creation | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | Use it in your XS++ code as follows. No other interface required. | ||||||||||
| 17 | |||||||||||
| 18 | %module{Your::Module} | ||||||||||
| 19 | |||||||||||
| 20 | %loadplugin{Cloning} | ||||||||||
| 21 | |||||||||||
| 22 | # Objects of this class will just be undef in a cloned interpreter | ||||||||||
| 23 | class MyThreadSafeClass { | ||||||||||
| 24 | %PreventCloning; | ||||||||||
| 25 | ... | ||||||||||
| 26 | }; | ||||||||||
| 27 | |||||||||||
| 28 | # TODO More to come | ||||||||||
| 29 | |||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||
| 31 | |||||||||||
| 32 | C<ExtUtils::XSpp::Plugin::Cloning> is a plugin for C<XS++> (See L<ExtUtils::XSpp>) | ||||||||||
| 33 | for controlling the behavior of a class's objects when the interpreter/thread they | ||||||||||
| 34 | live in is cloned. | ||||||||||
| 35 | |||||||||||
| 36 | Since C<ExtUtils::XSpp>'s plugin interface is considered experimental, so is this | ||||||||||
| 37 | module! | ||||||||||
| 38 | |||||||||||
| 39 | =head1 DIRECTIVES | ||||||||||
| 40 | |||||||||||
| 41 | =head2 C<%PreventCloning> | ||||||||||
| 42 | |||||||||||
| 43 | Specify this directive inside your class to prevent objects of the class | ||||||||||
| 44 | from being cloned on thread spawning. They will simply be undefined in the | ||||||||||
| 45 | new interperter/thread. | ||||||||||
| 46 | |||||||||||
| 47 | This defines a new C<CLONE_SKIP> method in the given class that prevents | ||||||||||
| 48 | the instances from being cloned. Note that due to this implementation detail, | ||||||||||
| 49 | the effect of the C<%PreventCloning> directive is inheritable. | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 54 | 0 | my $class = shift; | |||||||||
| 55 | 0 | my $self = {@_}; | |||||||||
| 56 | 0 | bless $self => $class; | |||||||||
| 57 | 0 | return $self; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 0 | ▶ | sub register_plugin { | ||||||||
|
Subroutine
register_plugin: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 61 | 0 | my ($class, $parser) = @_; | |||||||||
| 62 | |||||||||||
| 63 | 0 | $parser->add_class_tag_plugin( | |||||||||
| 64 | plugin => $class->new, | ||||||||||
| 65 | tag => 'PreventCloning', | ||||||||||
| 66 | ); | ||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | sub handle_class_tag { | ||||||||
|
Subroutine
handle_class_tag: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 70 | 0 | my ($self, $class, $tag, %args) = @_; | |||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | if ($tag eq 'PreventCloning') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | $self->_handle_prevent_cloning($class); | |||||||||
| 74 | 0 | return 1; | |||||||||
| 75 | } | ||||||||||
| 76 | 0 | return(); | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 0 | ▶ | sub _handle_prevent_cloning { | ||||||||
|
Subroutine
_handle_prevent_cloning: not called CC=4 SCAR=30.0
| |||||||||||
| 80 | 0 | my ($self, $class) = @_; | |||||||||
| 81 | 0 | my $class_name = $class->perl_name; | |||||||||
| 82 | |||||||||||
| 83 | 0 | my $cpp_name = '__CLONE'; | |||||||||
| 84 | 0 | foreach my $method (@{$class->methods}) { | |||||||||
| 85 | 0 | ▶ | if ($method->name eq 'CLONE_SKIP') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | Carp::confess("Perl class '$class_name' already has a 'CLONE_SKIP' method"); | |||||||||
| 87 | } | ||||||||||
| 88 | 0 | ▶ | if ($cpp_name eq $method->cpp_name) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | $cpp_name .= '_'; | |||||||||
| 90 | } | ||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 0 | my $inttype = ExtUtils::XSpp::Node::Type->new(base => 'int'); | |||||||||
| 94 | 0 | my $chartype = ExtUtils::XSpp::Node::Type->new(base => 'char', pointer => '*'); | |||||||||
| 95 | 0 | my $arg = ExtUtils::XSpp::Node::Argument->new( | |||||||||
| 96 | type => $chartype, | ||||||||||
| 97 | name => 'class_name', | ||||||||||
| 98 | ); | ||||||||||
| 99 | 0 | my $meth = ExtUtils::XSpp::Node::Function->new( | |||||||||
| 100 | class => $class, | ||||||||||
| 101 | cpp_name => $cpp_name, | ||||||||||
| 102 | perl_name => 'CLONE_SKIP', | ||||||||||
| 103 | arguments => [$arg], | ||||||||||
| 104 | ret_type => $inttype, | ||||||||||
| 105 | code => ["RETVAL = 1;\n"], | ||||||||||
| 106 | ); | ||||||||||
| 107 | 0 | $meth->set_static("package_static"); | |||||||||
| 108 | 0 | $class->add_methods($meth); | |||||||||
| 109 | |||||||||||
| 110 | 0 | return; | |||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | 1 | 1; | |||||||||
| 114 | __END__ | ||||||||||