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/;
24 plan tests => 3 + 2 * (2 * 8 + 2) + 2 * (2 * 5 + 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;
29 diag 'This will leak a few scalars';
31 plan skip_all => 'This Variable::Magic isn\'t thread safe';
34 my $destroyed : shared = 0;
37 wizard get => sub { ++$c },
38 data => sub { $_[1] + threads->tid() },
39 free => sub { ++$destroyed }
41 is($@, '', "wizard in main thread doesn't croak");
42 isnt($wiz, undef, "wizard in main thread is defined");
43 is($c, 0, "wizard in main thread doesn't trigger magic");
49 my $tid = threads->tid();
51 my $res = eval { cast $a, $sig, sub { 5 }->() };
52 is($@, '', "cast in thread $tid doesn't croak");
55 is($@, '', "get in thread $tid doesn't croak");
56 is($b, 3, "get in thread $tid returns the right thing");
57 my $d = eval { getdata $a, $sig };
58 is($@, '', "getdata in thread $tid doesn't croak");
59 is($d, 5 + $tid, "getdata in thread $tid returns the right thing");
61 $res = eval { dispell $a, $sig };
62 is($@, '', "dispell in thread $tid doesn't croak");
65 is($@, '', "get in thread $tid after dispell doesn't croak");
66 is($b, 3, "get in thread $tid after dispell returns the right thing");
68 return; # Ugly if not here
71 for my $dispell (1, 0) {
76 my @t = map { threads->create(\&try, $dispell) } 1 .. 2;
80 is($c, 2, "get triggered twice");
81 is($destroyed, (1 - $dispell) * 2, 'destructors');
87 @t = map { threads->create(\&try, $dispell) } 1 .. 2;
91 is($c, 2, "get triggered twice");
92 is($destroyed, (1 - $dispell) * 2, 'destructors');