From: Vincent Pit Date: Mon, 16 Mar 2009 15:44:37 +0000 (+0100) Subject: Lock shared variables X-Git-Tag: v0.33~11 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=4ae554f588e33ed5ca7d26edd634a03016c1a2d6;p=perl%2Fmodules%2FVariable-Magic.git Lock shared variables --- diff --git a/t/40-threads.t b/t/40-threads.t index 0b16c6a..d2c536e 100644 --- a/t/40-threads.t +++ b/t/40-threads.t @@ -51,7 +51,7 @@ sub try { } 0 }, - free => sub { ++$destroyed; 0 }, + free => sub { lock $destroyed; ++$destroyed; 0 }, op_info => $op_info }; is($@, '', "wizard in thread $tid doesn't croak"); @@ -87,10 +87,18 @@ sub try { for my $dispell (1, 0) { for my $sig (undef, Variable::Magic::gensig()) { - $destroyed = 0; + { + lock $destroyed; + $destroyed = 0; + } + my @t = map { threads->create(\&try, $dispell, $sig, $_) } (VMG_OP_INFO_NAME) x 2, (VMG_OP_INFO_OBJECT) x 2; $_->join for @t; - is($destroyed, (1 - $dispell) * 4, 'destructors'); + + { + lock $destroyed; + is $destroyed, (1 - $dispell) * 4, 'destructors'; + } } } diff --git a/t/41-clone.t b/t/41-clone.t index e410f88..a10a356 100644 --- a/t/41-clone.t +++ b/t/41-clone.t @@ -38,7 +38,7 @@ sub spawn_wiz { my $wiz = eval { wizard data => sub { $_[1] + threads->tid() }, - get => sub { ++$c; 0 }, + get => sub { lock $c; ++$c; 0 }, set => sub { my $op = $_[-1]; my $tid = threads->tid(); @@ -51,7 +51,7 @@ sub spawn_wiz { } 0 }, - free => sub { ++$destroyed; 0 }, + free => sub { lock $destroyed; ++$destroyed; 0 }, op_info => $op_info }; is($@, '', "wizard with op_info $op_info in main thread doesn't croak"); @@ -92,13 +92,25 @@ my $wiz_obj = spawn_wiz VMG_OP_INFO_OBJECT; for my $dispell (1, 0) { for my $sig ($wiz_name, getsig($wiz_name), $wiz_obj, getsig($wiz_obj)) { - $c = 0; - $destroyed = 0; + { + lock $c; + $c = 0; + } + { + lock $destroyed; + $destroyed = 0; + } my @t = map { threads->create(\&try, $dispell, $sig) } 1 .. 2; $_->join for @t; - is($c, 2, "get triggered twice"); - is($destroyed, (1 - $dispell) * 2, 'destructors'); + { + lock $c; + is $c, 2, "get triggered twice"; + } + { + lock $destroyed; + is $destroyed, (1 - $dispell) * 2, 'destructors'; + } } }