9 my ($module, $thread_safe_var);
11 $module = 'Variable::Magic';
12 $thread_safe_var = 'Variable::Magic::VMG_THREADSAFE()';
17 if (defined &Variable::Magic::wizard) {
18 my $wiz = Variable::Magic::wizard(
19 free => sub { $res = 1; return },
22 &Variable::Magic::cast(\$var, $wiz);
28 # Keep the rest of the file untouched
33 if (defined $thread_safe_var) {
34 my $stat = run_perl "require POSIX; require $module; exit($thread_safe_var ? POSIX::EXIT_SUCCESS() : POSIX::EXIT_FAILURE())";
38 if ($res == POSIX::EXIT_SUCCESS()) {
40 } elsif ($res == POSIX::EXIT_FAILURE()) {
44 if (not defined $is_threadsafe) {
45 skip_all "Could not detect if $module is thread safe or not";
49 VPIT::TestHelpers->import(
50 threads => [ $module => $is_threadsafe ],
54 my $could_not_create_thread = 'Could not create thread';
56 use Test::Leaner tests => 1 + (2 + 2 * 2) + 6 + (2 * 4) + 2;
59 my ($affirmative, $desc) = @_;
61 my $res = load_test();
64 is $res, 1, "$desc: module loaded";
66 is $res, 0, "$desc: module not loaded";
72 my $code = eval "sub { require $module }";
77 is_loaded 0, 'main body, beginning';
79 # Test serial loadings
83 my $here = "first serial thread";
84 is_loaded 0, "$here, beginning";
87 is_loaded 1, "$here, after loading";
92 skip "$could_not_create_thread (serial 1)" => 2 unless defined $thr;
95 if (my $err = $thr->error) {
100 is_loaded 0, 'main body, in between serial loadings';
103 my $thr = spawn(sub {
104 my $here = "second serial thread";
105 is_loaded 0, "$here, beginning";
108 is_loaded 1, "$here, after loading";
113 skip "$could_not_create_thread (serial 2)" => 2 unless defined $thr;
116 if (my $err = $thr->error) {
121 is_loaded 0, 'main body, after serial loadings';
123 # Test nested loadings
126 my $thr = spawn(sub {
127 my $here = 'parent thread';
128 is_loaded 0, "$here, beginning";
131 my $kid = spawn(sub {
132 my $here = 'child thread';
133 is_loaded 0, "$here, beginning";
136 is_loaded 1, "$here, after loading";
141 skip "$could_not_create_thread (nested child)" => 2 unless defined $kid;
144 if (my $err = $kid->error) {
145 die "in child thread: $err\n";
149 is_loaded 0, "$here, after child terminated";
152 is_loaded 1, "$here, after loading";
157 skip "$could_not_create_thread (nested parent)" => (3 + 2) unless defined $thr;
160 if (my $err = $thr->error) {
165 is_loaded 0, 'main body, after nested loadings';
167 # Test parallel loadings
173 share($_) for @locks;
181 cond_broadcast $locks[$id];
190 cond_wait $locks[$id] until $locks[$id] == 0;
195 my $thr1 = spawn(sub {
196 my $here = 'first simultaneous thread';
197 is_loaded 0, "$here, beginning";
201 is_loaded 1, "$here, after loading";
206 is_loaded 1, "$here, still loaded while also loaded in the other thread";
209 is_loaded 1, "$here, end";
214 skip "$could_not_create_thread (parallel 1)" => (4 * 2) unless defined $thr1;
216 my $thr2 = spawn(sub {
217 my $here = 'second simultaneous thread';
218 is_loaded 0, "$here, beginning";
222 is_loaded 0, "$here, loaded in other thread but not here";
226 is_loaded 1, "$here, after loading";
230 is_loaded 1, "$here, end";
235 sync_master($_) for 0 .. $#locks;
238 if (my $err = $thr1->error) {
242 skip "$could_not_create_thread (parallel 2)" => (4 * 1) unless defined $thr2;
245 if (my $err = $thr2->error) {
250 is_loaded 0, 'main body, after simultaneous threads';
253 is_loaded 1, 'main body, loaded at end';