9 BEGIN { delete $ENV{PERL_INDIRECT_PM_DISABLE} }
11 my ($module, $thread_safe_var);
14 $thread_safe_var = 'indirect::I_THREADSAFE()';
19 if (defined &indirect::msg) {
21 eval 'BEGIN { indirect->unimport(":fatal") if defined &indirect::msg } return; my $x = new X;';
24 if (defined $res and $res =~ /^Indirect call of method/) {
26 } elsif (not defined $res or $res eq '') {
33 # Keep the rest of the file untouched
38 if (defined $thread_safe_var) {
39 my $stat = run_perl "require POSIX; require $module; exit($thread_safe_var ? POSIX::EXIT_SUCCESS() : POSIX::EXIT_FAILURE())";
43 if ($res == POSIX::EXIT_SUCCESS()) {
45 } elsif ($res == POSIX::EXIT_FAILURE()) {
49 if (not defined $is_threadsafe) {
50 skip_all "Could not detect if $module is thread safe or not";
54 VPIT::TestHelpers->import(
55 threads => [ $module => $is_threadsafe ],
59 my $could_not_create_thread = 'Could not create thread';
61 use Test::Leaner tests => 1 + (2 + 2 * 2) + 6 + (2 * 4) + 2;
64 my ($affirmative, $desc) = @_;
66 my $res = load_test();
71 $desc = "$desc: module loaded";
74 $desc = "$desc: module not loaded";
77 is($res, $expected, $desc)
78 or diag("Test '$desc' failed: got '$res', expected '$expected'");
85 my $code = eval "sub { require $module }";
90 is_loaded 0, 'main body, beginning';
92 # Test serial loadings
96 my $here = "first serial thread";
97 is_loaded 0, "$here, beginning";
100 is_loaded 1, "$here, after loading";
105 skip "$could_not_create_thread (serial 1)" => 2 unless defined $thr;
108 if (my $err = $thr->error) {
113 is_loaded 0, 'main body, in between serial loadings';
116 my $thr = spawn(sub {
117 my $here = "second serial thread";
118 is_loaded 0, "$here, beginning";
121 is_loaded 1, "$here, after loading";
126 skip "$could_not_create_thread (serial 2)" => 2 unless defined $thr;
129 if (my $err = $thr->error) {
134 is_loaded 0, 'main body, after serial loadings';
136 # Test nested loadings
139 my $thr = spawn(sub {
140 my $here = 'parent thread';
141 is_loaded 0, "$here, beginning";
144 my $kid = spawn(sub {
145 my $here = 'child thread';
146 is_loaded 0, "$here, beginning";
149 is_loaded 1, "$here, after loading";
154 skip "$could_not_create_thread (nested child)" => 2 unless defined $kid;
157 if (my $err = $kid->error) {
158 die "in child thread: $err\n";
162 is_loaded 0, "$here, after child terminated";
165 is_loaded 1, "$here, after loading";
170 skip "$could_not_create_thread (nested parent)" => (3 + 2) unless defined $thr;
173 if (my $err = $thr->error) {
178 is_loaded 0, 'main body, after nested loadings';
180 # Test parallel loadings
185 my @locks_down = (1) x 5;
186 my @locks_up = (0) x scalar @locks_down;
187 share($_) for @locks_down, @locks_up;
195 lock $locks_down[$id];
196 $locks_down[$id] = 0;
197 cond_broadcast $locks_down[$id];
202 cond_wait $locks_up[$id] until $locks_up[$id] == $peers;
210 lock $locks_down[$id];
211 cond_wait $locks_down[$id] until $locks_down[$id] == 0;
217 cond_signal $locks_up[$id];
222 my $thr1 = spawn(sub {
223 my $here = 'first simultaneous thread';
224 is_loaded 0, "$here, beginning";
228 is_loaded 1, "$here, after loading";
233 is_loaded 1, "$here, still loaded while also loaded in the other thread";
236 is_loaded 1, "$here, end";
241 skip "$could_not_create_thread (parallel 1)" => (4 * 2) unless defined $thr1;
243 my $thr2 = spawn(sub {
244 my $here = 'second simultaneous thread';
245 is_loaded 0, "$here, beginning";
249 is_loaded 0, "$here, loaded in other thread but not here";
253 is_loaded 1, "$here, after loading";
257 is_loaded 1, "$here, end";
262 sync_master($_) for 0 .. $#locks_down;
265 if (my $err = $thr1->error) {
269 skip "$could_not_create_thread (parallel 2)" => (4 * 1) unless defined $thr2;
272 if (my $err = $thr2->error) {
277 is_loaded 0, 'main body, after simultaneous threads';
280 is_loaded 1, 'main body, loaded at end';