Skip to content

Commit c1ccb87

Browse files
author
jdhedden
committed
threads-shared v1.19
1 parent 2343ea3 commit c1ccb87

File tree

6 files changed

+83
-16
lines changed

6 files changed

+83
-16
lines changed

Changes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
Revision history for Perl extension threads::shared.
22

3+
1.19 Wed May 7 17:56:26 2008
4+
- Fixed return value of ref() on refs of refs
5+
- Document using is_shared() vs. refaddr() on shared refs
6+
- Don't fail t/stress.t if ENOMEM on thread creations
7+
38
1.18 Tue Feb 26 16:21:55 2008
49
- Stress test diagnostics
510
- 'die' properly if no compiler

README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
threads::shared version 1.18
1+
threads::shared version 1.19
22
============================
33

44
This module needs Perl 5.8.0 or later compiled with USEITHREADS.

shared.pm

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use 5.008;
55
use strict;
66
use warnings;
77

8-
our $VERSION = '1.18';
8+
our $VERSION = '1.19';
99
my $XS_VERSION = $VERSION;
1010
$VERSION = eval $VERSION;
1111

@@ -73,7 +73,7 @@ threads::shared - Perl extension for sharing data structures between threads
7373
7474
=head1 VERSION
7575
76-
This document describes threads::shared version 1.18
76+
This document describes threads::shared version 1.19
7777
7878
=head1 SYNOPSIS
7979
@@ -362,6 +362,23 @@ error message. But the C<< $hashref->{key} >> is B<not> shared, causing the
362362
error "locking can only be used on shared values" to occur when you attempt to
363363
C<< lock($hasref->{key}) >>.
364364
365+
Using L<refaddr()|Scalar::Util/"refaddr EXPR">) is unreliable for testing
366+
whether or not two shared references are equivalent (e.g., when testing for
367+
circular references). Use L<is_shared()/"is_shared VARIABLE">, instead:
368+
369+
use threads;
370+
use threads::shared;
371+
use Scalar::Util qw(refaddr);
372+
373+
# If ref is shared, use threads::shared's internal ID.
374+
# Otherwise, use refaddr().
375+
my $addr1 = is_shared($ref1) || refaddr($ref1);
376+
my $addr2 = is_shared($ref2) || refaddr($ref2);
377+
378+
if ($addr1 == $addr2) {
379+
# The refs are equivalent
380+
}
381+
365382
View existing bug reports at, and submit any new bugs, problems, patches, etc.
366383
to: L<http://rt.cpan.org/Public/Dist/Display.html?Name=threads-shared>
367384
@@ -371,7 +388,7 @@ L<threads::shared> Discussion Forum on CPAN:
371388
L<http://www.cpanforum.com/dist/threads-shared>
372389
373390
Annotated POD for L<threads::shared>:
374-
L<http://annocpan.org/~JDHEDDEN/threads-shared-1.18/shared.pm>
391+
L<http://annocpan.org/~JDHEDDEN/threads-shared-1.19/shared.pm>
375392
376393
Source repository:
377394
L<http://code.google.com/p/threads-shared/>

shared.xs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,11 @@ sharedsv_scalar_mg_get(pTHX_ SV *sv, MAGIC *mg)
712712
ENTER_LOCK;
713713
if (SvROK(ssv)) {
714714
S_get_RV(aTHX_ sv, ssv);
715+
/* Look ahead for refs of refs */
716+
if (SvROK(SvRV(ssv))) {
717+
SvROK_on(SvRV(sv));
718+
S_get_RV(aTHX_ SvRV(sv), SvRV(ssv));
719+
}
715720
} else {
716721
sv_setsv_nomg(sv, ssv);
717722
}
@@ -880,6 +885,11 @@ sharedsv_elem_mg_FETCH(pTHX_ SV *sv, MAGIC *mg)
880885
/* Exists in the array */
881886
if (SvROK(*svp)) {
882887
S_get_RV(aTHX_ sv, *svp);
888+
/* Look ahead for refs of refs */
889+
if (SvROK(SvRV(*svp))) {
890+
SvROK_on(SvRV(sv));
891+
S_get_RV(aTHX_ SvRV(sv), SvRV(*svp));
892+
}
883893
} else {
884894
/* XXX Can this branch ever happen? DAPM */
885895
/* XXX assert("no such branch"); */

t/stress.t

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,34 @@ use threads::shared;
7979
# Gather thread results
8080
my ($okay, $failures, $timeouts, $unknown) = (0, 0, 0, 0);
8181
for (1..$cnt) {
82-
my $rc = $threads[$_]->join();
83-
if (! $rc) {
82+
if (! $threads[$_]) {
8483
$failures++;
85-
} elsif ($rc =~ /^timed out/) {
86-
$timeouts++;
87-
} elsif ($rc eq 'okay') {
88-
$okay++;
8984
} else {
90-
$unknown++;
91-
print(STDERR "# Unknown error: $rc\n");
85+
my $rc = $threads[$_]->join();
86+
if (! $rc) {
87+
$failures++;
88+
} elsif ($rc =~ /^timed out/) {
89+
$timeouts++;
90+
} elsif ($rc eq 'okay') {
91+
$okay++;
92+
} else {
93+
$unknown++;
94+
print(STDERR "# Unknown error: $rc\n");
95+
}
9296
}
9397
}
98+
if ($failures) {
99+
# Most likely due to running out of memory
100+
print(STDERR "# Warning: $failures threads failed\n");
101+
print(STDERR "# Note: errno 12 = ENOMEM\n");
102+
$cnt -= $failures;
103+
}
94104

95-
if ($failures || $unknown || (($okay + $timeouts) != $cnt)) {
105+
if ($unknown || (($okay + $timeouts) != $cnt)) {
96106
print("not ok 1\n");
97-
my $too_few = $cnt - ($okay + $failures + $timeouts + $unknown);
107+
my $too_few = $cnt - ($okay + $timeouts + $unknown);
98108
print(STDERR "# Test failed:\n");
99109
print(STDERR "#\t$too_few too few threads reported\n") if $too_few;
100-
print(STDERR "#\t$failures threads failed\n") if $failures;
101110
print(STDERR "#\t$unknown unknown errors\n") if $unknown;
102111
print(STDERR "#\t$timeouts threads timed out\n") if $timeouts;
103112

t/sv_refs.t

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ sub ok {
3131

3232
BEGIN {
3333
$| = 1;
34-
print("1..11\n"); ### Number of tests that will be run ###
34+
print("1..21\n"); ### Number of tests that will be run ###
3535
};
3636

3737
use threads;
@@ -74,4 +74,30 @@ ok(10,$t1 eq 'bar',"Check that assign to a ROK works");
7474

7575
ok(11, is_shared($foo), "Check for sharing");
7676

77+
{
78+
# Circular references with 3 shared scalars
79+
my $x : shared;
80+
my $y : shared;
81+
my $z : shared;
82+
83+
$x = \$y;
84+
$y = \$z;
85+
$z = \$x;
86+
ok(12, ref($x) eq 'REF', '$x ref type');
87+
ok(13, ref($y) eq 'REF', '$y ref type');
88+
ok(14, ref($z) eq 'REF', '$z ref type');
89+
90+
my @q :shared = ($x);
91+
ok(15, ref($q[0]) eq 'REF', '$q[0] ref type');
92+
93+
my $w = $q[0];
94+
ok(16, ref($w) eq 'REF', '$w ref type');
95+
ok(17, ref($$w) eq 'REF', '$$w ref type');
96+
ok(18, ref($$$w) eq 'REF', '$$$w ref type');
97+
ok(19, ref($$$$w) eq 'REF', '$$$$w ref type');
98+
99+
ok(20, is_shared($x) == is_shared($w), '_id($x) == _id($w)');
100+
ok(21, is_shared($w) == is_shared($$$$w), '_id($w) == _id($$$$w)');
101+
}
102+
77103
# EOF

0 commit comments

Comments
 (0)