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 getsig VMG_THREADSAFE VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT/;
24 plan tests => 2 * 3 + 4 * (2 * 10 + 2) + 4 * (2 * 7 + 2);
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;
40 wizard data => sub { $_[1] + threads->tid() },
41 get => sub { lock $c; ++$c; 0 },
44 my $tid = threads->tid();
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 with op_info $op_info in main thread doesn't croak");
58 isnt($wiz, undef, "wizard with op_info $op_info in main thread is defined");
59 is($c, 0, "wizard with op_info $op_info in main thread doesn't trigger magic");
65 my ($dispell, $sig) = @_;
66 my $tid = threads->tid();
68 my $res = eval { cast $a, $sig, sub { 5 }->() };
69 is($@, '', "cast in thread $tid doesn't croak");
72 is($@, '', "get in thread $tid doesn't croak");
73 is($b, 3, "get in thread $tid returns the right thing");
74 my $d = eval { getdata $a, $sig };
75 is($@, '', "getdata in thread $tid doesn't croak");
76 is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
78 is($@, '', "set in thread $tid (check opname) doesn't croak");
80 $res = eval { dispell $a, $sig };
81 is($@, '', "dispell in thread $tid doesn't croak");
84 is($@, '', "get in thread $tid after dispell doesn't croak");
85 is($b, 9, "get in thread $tid after dispell returns the right thing");
87 return; # Ugly if not here
90 my $wiz_name = spawn_wiz VMG_OP_INFO_NAME;
91 my $wiz_obj = spawn_wiz VMG_OP_INFO_OBJECT;
93 for my $dispell (1, 0) {
94 for my $sig ($wiz_name, getsig($wiz_name), $wiz_obj, getsig($wiz_obj)) {
104 my @t = map { threads->create(\&try, $dispell, $sig) } 1 .. 2;
109 is $c, 2, "get triggered twice";
113 is $destroyed, (1 - $dispell) * 2, 'destructors';