Front page | perl.perl5.porters |
Postings from November 2003
Re: dealing with objects cloning under ithreads
Thread Previous
|
Thread Next
From:
Elizabeth Mattijsen
Date:
November 15, 2003 02:53
Subject:
Re: dealing with objects cloning under ithreads
Message ID:
p05111b00bbdbb47e04e3@[192.168.56.3]
At 01:05 +0100 11/15/03, Tels wrote:
> >here all 3 threads get a copy $Foo::gtop, and then try to destroy it (4
>>times). Only the first one does it successfully, as gtop returns an object
>>which is a wrapper around a C struct. If you don't have GTop, replace it with
>>some other class which returns a similar object and you will see what the
> >problem is.
>I think Math::BigInt::GMP would fall under this... oh...
>Te"never used threads"ls
Fear not. Thread::Bless 0.01 is here!
Current ithreads users should only need to do a:
use Thread::Bless package => 'Math::BigInt::GMP';
to make the cloned objects in the threads _not_ call DESTROY when the
reference count goes to zero.
Thread::Bless should be available on CPAN or from:
http://www.liz.nl/CPAN/Thread-Bless-0.01.tar.gz
Please pay extra attention to the BUGS section, though.
Liz
=========================================================
=head1 NAME
Thread::Bless - make blessed objects thread-aware
=head1 SYNOPSIS
use Thread::Bless; # make objects of this class thread-aware
use Thread::Bless # for your own module
destroy => 1, # default: 0 = original thread only
fixup => 'subname', # default: undef = no special cloning
;
sub new { bless {},shift } # bless now thread aware for selected modules
Thread::Bless->destroy( 0|1 ); # set/adapt destroy setting
$destroy = Thread::Bless->destroy; # obtain setting
Thread::Bless->fixup( \&fixup_sub ); # set/adapt fixup sub later
Thread::Bless->fixup( undef ); # disable fixup sub
$fixup = Thread::Bless->fixup; # obtain setting
use Thread::Bless ( # provide settings for other packages
package => 'Foo', # Foo
fixup => sub { 'Fixup for Foo' }, # destroy => 0 implied
package => 'Bar', # Bar, destroy => 0, no fixup
package => [qw(Baz Baz::Boo Baz::Bee)], # listed modules
destroy => 1, # destroy also in threads
fixup => 'Baz::fixup', # call this sub for fixup
);
=head1 DESCRIPTION
This module adds two features to threads that are sorely missed by some.
The first feature is that the DESTROY method is called only on the object if
the object is destroyed in the thread it was created in. This feature is
automatically activated when Thread::Bless is used.
The second feature is that an optional fixup method is called on the object
(automatically by Thread::Bless) just after the object is cloned (automatically
by Perl) when a thread is started. This is needed if the object contains
(references to) data structures that are not automatically handled by Perl.
Both features can be switched on/off seperately at compile or runtime to
provide the utmost flexibility.
For external modules that need to be thread aware but aren't yet (most notably
the ones that cannot handle having DESTROY called when cloned versions are
destroyed in threads), you can also activate Thread::Bless on them.
=head1 CLASS METHODS
These are the class methods.
=head2 destroy
Thread::Bless->destroy( 0 ); # call DESTROY on original only
Thread::Bless->destroy( 1 ); # call DESTROY on all objects
$destroy = Thread::Bless->destroy; # return current setting
The input parameter recognizes the following values:
=over 2
=item original (0)
If the value B<0> is specified, then B<only> objects will have the DESTROY
method called on them in the thread in which they were created. This is the
default setting.
=item all (1)
If the value B<1> is specified, then B<all> objects will have the DESTROY
method called on them when they are going out of scope.
=back
=head2 fixup
Thread::Bless->fixup( undef ); # don't execute anything on cloning
Thread::Bless->fixup( 'fixup' ); # call 'fixup' as an object method
Thread::Bless->fixup( \&fixup ); # code reference is also ok
$fixup = Thread::Bless->fixup; # return current code reference
The "fixup" class method sets and returns the subroutine that will be executed
when an object of the class from which this class method is called.
=head1 BUGS
None in the module itself (so far). However, several Perl versions have
problems with cloned, weakened references (which are used by Thread::Bless
to keep record of the objects that need fixing up and/or destroying. This
shows up as errors in the test-suite or lots of warnings being displayed.
Later versions of the Thread::Bless module may include XS code to circumvent
these problems for specific versions of Perl.
=over 2
=item Perl 5.8.0
Doesn't seem to handle weakened references at all: core dumps during the
test-suite with "Bizarre SvTYPE [80]" error. It is not recommended to use
Thread::Bless on this version of Perl (yet).
=item Perl 5.8.1
Issues warnings whenever a thread is shut down, one for each package that
has Thread::Bless enabled on it:
"Attempt to free unreferenced scalar during global destruction."
So far, this warning does not seem to affect further execution of Perl. The
test-suite should complete without finding any errors.
=item Perl 5.8.2
Issues warnings whenever a thread is shut down, one for each package that
has Thread::Bless enabled on it:
"Attempt to free unreferenced scalar: SV 0xdeadbeef during global
destruction."
So far, this warning does not seem to affect further execution of Perl. The
test-suite should complete without finding any errors.
Thread Previous
|
Thread Next