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 { ++$c; 0 },
44 $name = $name->name if $op_info == VMG_OP_INFO_OBJECT;
45 my $tid = threads->tid();
46 is $name, 'sassign', "opname for op_info $op_info in thread $tid is correct";
49 free => sub { ++$destroyed; 0 },
52 is($@, '', "wizard with op_info $op_info in main thread doesn't croak");
53 isnt($wiz, undef, "wizard with op_info $op_info in main thread is defined");
54 is($c, 0, "wizard with op_info $op_info in main thread doesn't trigger magic");
60 my ($dispell, $sig) = @_;
61 my $tid = threads->tid();
63 my $res = eval { cast $a, $sig, sub { 5 }->() };
64 is($@, '', "cast in thread $tid doesn't croak");
67 is($@, '', "get in thread $tid doesn't croak");
68 is($b, 3, "get in thread $tid returns the right thing");
69 my $d = eval { getdata $a, $sig };
70 is($@, '', "getdata in thread $tid doesn't croak");
71 is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
73 is($@, '', "set in thread $tid (check opname) doesn't croak");
75 $res = eval { dispell $a, $sig };
76 is($@, '', "dispell in thread $tid doesn't croak");
79 is($@, '', "get in thread $tid after dispell doesn't croak");
80 is($b, 9, "get in thread $tid after dispell returns the right thing");
82 return; # Ugly if not here
85 my $wiz_name = spawn_wiz VMG_OP_INFO_NAME;
86 my $wiz_obj = spawn_wiz VMG_OP_INFO_OBJECT;
88 for my $dispell (1, 0) {
89 for my $sig ($wiz_name, getsig($wiz_name), $wiz_obj, getsig($wiz_obj)) {
93 my @t = map { threads->create(\&try, $dispell, $sig) } 1 .. 2;
96 is($c, 2, "get triggered twice");
97 is($destroyed, (1 - $dispell) * 2, 'destructors');