| 1 |
#!perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Variable::Magic qw< |
| 7 |
wizard cast dispell getdata |
| 8 |
VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT |
| 9 |
>; |
| 10 |
|
| 11 |
use lib 't/lib'; |
| 12 |
use VPIT::TestHelpers ( |
| 13 |
threads => [ 'Variable::Magic' => 'Variable::Magic::VMG_THREADSAFE()' ], |
| 14 |
); |
| 15 |
|
| 16 |
use Test::Leaner 'no_plan'; |
| 17 |
|
| 18 |
my $destroyed : shared = 0; |
| 19 |
my $c : shared = 0; |
| 20 |
|
| 21 |
sub spawn_wiz { |
| 22 |
my ($op_info) = @_; |
| 23 |
|
| 24 |
my $desc = "wizard with op_info $op_info in main thread"; |
| 25 |
|
| 26 |
local $@; |
| 27 |
my $wiz = eval { |
| 28 |
wizard( |
| 29 |
data => sub { $_[1] + threads->tid() }, |
| 30 |
get => sub { lock $c; ++$c; 0 }, |
| 31 |
set => sub { |
| 32 |
my $op = $_[-1]; |
| 33 |
my $tid = threads->tid(); |
| 34 |
|
| 35 |
my $assign_op = ("$]" < 5.037_003) ? 'sassign': 'padsv_store'; |
| 36 |
my $assign_op_cl = ("$]" < 5.037_003) ? 'B::BINOP': 'B::UNOP'; |
| 37 |
|
| 38 |
if ($op_info == VMG_OP_INFO_OBJECT) { |
| 39 |
is_deeply { class => ref($op), name => $op->name }, |
| 40 |
{ class => $assign_op_cl, name => $assign_op }, |
| 41 |
"op object in thread $tid is correct"; |
| 42 |
} else { |
| 43 |
is $op, $assign_op, "op name in thread $tid is correct"; |
| 44 |
} |
| 45 |
|
| 46 |
return 0 |
| 47 |
}, |
| 48 |
free => sub { lock $destroyed; ++$destroyed; 0 }, |
| 49 |
op_info => $op_info, |
| 50 |
); |
| 51 |
}; |
| 52 |
is $@, '', "$desc doesn't croak"; |
| 53 |
isnt $wiz, undef, "$desc is defined"; |
| 54 |
is $c, 0, "$desc doesn't trigger magic"; |
| 55 |
|
| 56 |
return $wiz; |
| 57 |
} |
| 58 |
|
| 59 |
sub try { |
| 60 |
my ($dispell, $wiz) = @_; |
| 61 |
my $tid = threads->tid; |
| 62 |
|
| 63 |
my $a = 3; |
| 64 |
|
| 65 |
{ |
| 66 |
local $@; |
| 67 |
my $res = eval { cast $a, $wiz, sub { 5 }->() }; |
| 68 |
is $@, '', "cast in thread $tid doesn't croak"; |
| 69 |
} |
| 70 |
|
| 71 |
{ |
| 72 |
local $@; |
| 73 |
my $b; |
| 74 |
eval { $b = $a }; |
| 75 |
is $@, '', "get in thread $tid doesn't croak"; |
| 76 |
is $b, 3, "get in thread $tid returns the right thing"; |
| 77 |
} |
| 78 |
|
| 79 |
{ |
| 80 |
local $@; |
| 81 |
my $d = eval { getdata $a, $wiz }; |
| 82 |
is $@, '', "getdata in thread $tid doesn't croak"; |
| 83 |
is $d, 5 + $tid, "getdata in thread $tid returns the right thing"; |
| 84 |
} |
| 85 |
|
| 86 |
{ |
| 87 |
local $@; |
| 88 |
eval { $a = 9 }; |
| 89 |
is $@, '', "set in thread $tid (check opname) doesn't croak"; |
| 90 |
} |
| 91 |
|
| 92 |
if ($dispell) { |
| 93 |
{ |
| 94 |
local $@; |
| 95 |
my $res = eval { dispell $a, $wiz }; |
| 96 |
is $@, '', "dispell in thread $tid doesn't croak"; |
| 97 |
} |
| 98 |
|
| 99 |
{ |
| 100 |
local $@; |
| 101 |
my $b; |
| 102 |
eval { $b = $a }; |
| 103 |
is $@, '', "get in thread $tid after dispell doesn't croak"; |
| 104 |
is $b, 9, "get in thread $tid after dispell returns the right thing"; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return 1; |
| 109 |
} |
| 110 |
|
| 111 |
my $wiz_name = spawn_wiz VMG_OP_INFO_NAME; |
| 112 |
my $wiz_obj = spawn_wiz VMG_OP_INFO_OBJECT; |
| 113 |
|
| 114 |
for my $dispell (1, 0) { |
| 115 |
for my $wiz ($wiz_name, $wiz_obj) { |
| 116 |
{ |
| 117 |
lock $c; |
| 118 |
$c = 0; |
| 119 |
} |
| 120 |
{ |
| 121 |
lock $destroyed; |
| 122 |
$destroyed = 0; |
| 123 |
} |
| 124 |
|
| 125 |
my $completed = 0; |
| 126 |
|
| 127 |
my @threads = map spawn(\&try, $dispell, $wiz), 1 .. 2; |
| 128 |
for my $thr (@threads) { |
| 129 |
my $res = $thr->join; |
| 130 |
$completed += $res if defined $res; |
| 131 |
} |
| 132 |
|
| 133 |
{ |
| 134 |
lock $c; |
| 135 |
is $c, $completed, "get triggered twice"; |
| 136 |
} |
| 137 |
{ |
| 138 |
lock $destroyed; |
| 139 |
is $destroyed, (1 - $dispell) * $completed, 'destructors'; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
{ |
| 145 |
my @threads; |
| 146 |
my $flag : shared = 0; |
| 147 |
my $destroyed; |
| 148 |
|
| 149 |
{ |
| 150 |
my $wiz = wizard( |
| 151 |
set => sub { |
| 152 |
my $tid = threads->tid; |
| 153 |
pass "set callback called in thread $tid" |
| 154 |
}, |
| 155 |
free => sub { ++$destroyed }, |
| 156 |
); |
| 157 |
|
| 158 |
my $var = 123; |
| 159 |
cast $var, $wiz; |
| 160 |
|
| 161 |
@threads = map spawn( |
| 162 |
sub { |
| 163 |
my $tid = threads->tid; |
| 164 |
my $exp = 456 + $tid; |
| 165 |
{ |
| 166 |
lock $flag; |
| 167 |
threads::shared::cond_wait($flag) until $flag; |
| 168 |
} |
| 169 |
$var = $exp; |
| 170 |
is $var, $exp, "\$var could be assigned to in thread $tid"; |
| 171 |
} |
| 172 |
), 1 .. 5; |
| 173 |
} |
| 174 |
|
| 175 |
is $destroyed, 1, 'wizard is destroyed'; |
| 176 |
|
| 177 |
{ |
| 178 |
lock $flag; |
| 179 |
$flag = 1; |
| 180 |
threads::shared::cond_broadcast($flag); |
| 181 |
} |
| 182 |
|
| 183 |
$_->join for @threads; |
| 184 |
} |