6 my ($module, $thread_safe_var);
8 $module = 'Scope::Upper';
9 $thread_safe_var = 'Scope::Upper::SU_THREADSAFE()';
16 if (defined &Scope::Upper::reap) {
17 &Scope::Upper::reap(sub { $var *= 2 });
31 # Keep the rest of the file untouched
34 use VPIT::TestHelpers threads => [ $module, $thread_safe_var ];
36 my $could_not_create_thread = 'Could not create thread';
38 use Test::Leaner tests => 1 + (2 + 2 * 2) + 6 + (2 * 4 + 1) * 2 + 1;
41 my ($affirmative, $desc) = @_;
43 my $res = load_test();
48 $desc = "$desc: module loaded";
51 $desc = "$desc: module not loaded";
54 unless (is $res, $expected, $desc) {
55 $res = defined $res ? "'$res'" : 'undef';
56 $expected = "'$expected'";
57 diag("Test '$desc' failed: got $res, expected $expected");
65 my $code = eval "sub { require $module }";
70 is_loaded 0, 'main body, beginning';
72 # Test serial loadings
76 my $here = "first serial thread";
77 is_loaded 0, "$here, beginning";
80 is_loaded 1, "$here, after loading";
85 skip "$could_not_create_thread (serial 1)" => 2 unless defined $thr;
88 if (my $err = $thr->error) {
93 is_loaded 0, 'main body, in between serial loadings';
97 my $here = "second serial thread";
98 is_loaded 0, "$here, beginning";
101 is_loaded 1, "$here, after loading";
106 skip "$could_not_create_thread (serial 2)" => 2 unless defined $thr;
109 if (my $err = $thr->error) {
114 is_loaded 0, 'main body, after serial loadings';
116 # Test nested loadings
119 my $thr = spawn(sub {
120 my $here = 'parent thread';
121 is_loaded 0, "$here, beginning";
124 my $kid = spawn(sub {
125 my $here = 'child thread';
126 is_loaded 0, "$here, beginning";
129 is_loaded 1, "$here, after loading";
134 skip "$could_not_create_thread (nested child)" => 2 unless defined $kid;
137 if (my $err = $kid->error) {
138 die "in child thread: $err\n";
142 is_loaded 0, "$here, after child terminated";
145 is_loaded 1, "$here, after loading";
150 skip "$could_not_create_thread (nested parent)" => (3 + 2) unless defined $thr;
153 if (my $err = $thr->error) {
158 is_loaded 0, 'main body, after nested loadings';
160 # Test parallel loadings
167 my @locks_down = (1) x $sync_points;
168 my @locks_up = (0) x $sync_points;
169 share($_) for @locks_down, @locks_up;
171 my $default_peers = 2;
174 my ($id, $peers) = @_;
176 $peers = $default_peers unless defined $peers;
179 lock $locks_down[$id];
180 $locks_down[$id] = 0;
181 cond_broadcast $locks_down[$id];
186 cond_wait $locks_up[$id] until $locks_up[$id] == $peers;
194 lock $locks_down[$id];
195 cond_wait $locks_down[$id] until $locks_down[$id] == 0;
201 cond_signal $locks_up[$id];
205 for my $first_thread_ends_first (0, 1) {
206 for my $id (0 .. $sync_points - 1) {
208 lock $locks_down[$id];
209 $locks_down[$id] = 1;
217 my $thr1_end = 'finishes first';
218 my $thr2_end = 'finishes last';
220 ($thr1_end, $thr2_end) = ($thr2_end, $thr1_end)
221 unless $first_thread_ends_first;
224 my $thr1 = spawn(sub {
225 my $here = "first simultaneous thread ($thr1_end)";
228 is_loaded 0, "$here, beginning";
232 is_loaded 1, "$here, after loading";
237 is_loaded 1, "$here, still loaded while also loaded in the other thread";
240 sync_slave 6 unless $first_thread_ends_first;
242 is_loaded 1, "$here, end";
247 skip "$could_not_create_thread (parallel 1)" => (4 * 2) unless defined $thr1;
249 my $thr2 = spawn(sub {
250 my $here = "second simultaneous thread ($thr2_end)";
253 is_loaded 0, "$here, beginning";
258 is_loaded 0, "$here, loaded in other thread but not here";
261 is_loaded 1, "$here, after loading";
265 sync_slave 6 if $first_thread_ends_first;
267 is_loaded 1, "$here, end";
272 sync_master($_) for 0 .. 5;
275 ($thr2, $thr1) = ($thr1, $thr2) unless $first_thread_ends_first;
278 if (my $err = $thr1->error) {
285 if (my $err = $thr1->error) {
289 sync_master(6, 1) unless $first_thread_ends_first;
292 if (my $err = $thr1->error) {
296 skip "$could_not_create_thread (parallel 2)" => (4 * 1);
300 is_loaded 0, 'main body, after simultaneous threads';
304 is_loaded 1, 'main body, loaded at end';