6 use Config qw/%Config/;
9 if (!$Config{useithreads}) {
12 plan(skip_all => 'This perl wasn\'t built to support threads');
16 use threads; # Before Test::More
21 use Variable::Magic qw/wizard cast dispell getdata VMG_THREADSAFE VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT/;
24 plan tests => 2 * (4 * 18 + 1) + 2 * (4 * 13 + 1);
25 my $v = $threads::VERSION;
26 diag "Using threads $v" if defined $v;
27 $v = $threads::shared::VERSION;
28 diag "Using threads::shared $v" if defined $v;
30 plan skip_all => 'This Variable::Magic isn\'t thread safe';
33 my $destroyed : shared = 0;
36 my ($dispell, $sig, $op_info) = @_;
37 my $tid = threads->tid();
40 wizard data => sub { $_[1] + $tid },
42 get => sub { ++$c; 0 },
45 if ($op_info == VMG_OP_INFO_OBJECT) {
46 is_deeply { class => ref($op), name => $op->name },
47 { class => 'B::BINOP', name => 'sassign' },
48 "op object in thread $tid is correct";
50 is $op, 'sassign', "op name in thread $tid is correct";
54 free => sub { lock $destroyed; ++$destroyed; 0 },
57 is($@, '', "wizard in thread $tid doesn't croak");
58 isnt($wiz, undef, "wizard in thread $tid is defined");
59 is($c, 0, "wizard in thread $tid doesn't trigger magic");
61 my $res = eval { cast $a, $wiz, sub { 5 }->() };
62 is($@, '', "cast in thread $tid doesn't croak");
63 is($c, 0, "cast in thread $tid doesn't trigger magic");
66 is($@, '', "get in thread $tid doesn't croak");
67 is($b, 3, "get in thread $tid returns the right thing");
68 is($c, 1, "get in thread $tid triggers magic");
69 my $d = eval { getdata $a, $wiz };
70 is($@, '', "getdata in thread $tid doesn't croak");
71 is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
72 is($c, 1, "getdata in thread $tid doesn't trigger magic");
74 is($@, '', "set in thread $tid (check opname) doesn't croak");
76 $res = eval { dispell $a, $wiz };
77 is($@, '', "dispell in thread $tid doesn't croak");
78 is($c, 1, "dispell in thread $tid doesn't trigger magic");
81 is($@, '', "get in thread $tid after dispell doesn't croak");
82 is($b, 9, "get in thread $tid after dispell returns the right thing");
83 is($c, 1, "get in thread $tid after dispell doesn't trigger magic");
85 return; # Ugly if not here
88 for my $dispell (1, 0) {
89 for my $sig (undef, Variable::Magic::gensig()) {
95 my @t = map { threads->create(\&try, $dispell, $sig, $_) }
96 (VMG_OP_INFO_NAME) x 2, (VMG_OP_INFO_OBJECT) x 2;
101 is $destroyed, (1 - $dispell) * 4, 'destructors';