Skip to content

Commit b009c7f

Browse files
committed
Make thread yield concurrency tests pass
These t/concurrency tests haven't been run so far, despite passing fine except the ones this commit fixes. They made a bad assumption about `nqp::threadyield`: on a multi-core system, multiple threads can run be scheduled at once, so one cannot simply rely on a thread yield to run the other thread at that point in time, as they may both be running at the same time.
1 parent cccaf20 commit b009c7f

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

t/concurrency/01-thread.t

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ plan(24);
9292
{
9393
my @a;
9494
my $t := nqp::newthread({
95-
nqp::threadyield() until @a;
95+
nqp::threadyield() until nqp::elems(@a) == 1;
9696
nqp::push(@a, '1');
97-
nqp::threadyield();
97+
nqp::threadyield() until nqp::elems(@a) == 3;
9898
nqp::push(@a, '2');
9999
}, 0);
100100

@@ -104,9 +104,9 @@ plan(24);
104104

105105
{
106106
nqp::push(@a, 'a');
107-
nqp::threadyield();
107+
nqp::threadyield() until nqp::elems(@a) == 2;
108108
nqp::push(@a, 'b');
109-
nqp::threadyield();
109+
nqp::threadyield() until nqp::elems(@a) == 4;
110110
nqp::push(@a, 'c');
111111
}
112112

@@ -115,8 +115,6 @@ plan(24);
115115
ok(@a[0] eq 'a',
116116
'Looped threadyield() can force parent thread to act first');
117117

118-
# XXXX: This test goes wrong on nqp-j, always giving a,b,c,1,2;
119-
# It appears the threadyield() ops get ignored.
120118
my $order := nqp::join(',', @a);
121119
my $ok := $order eq 'a,1,b,2,c';
122120
ok($ok, 'threadyield() properly interleaved parent and child threads');
@@ -131,15 +129,15 @@ plan(24);
131129
my @a;
132130
my $t1 := nqp::newthread({
133131
nqp::push(@a, 'a');
134-
nqp::threadyield();
132+
nqp::threadyield() until nqp::elems(@a) == 2;
135133
nqp::push(@a, 'b');
136-
nqp::threadyield();
134+
nqp::threadyield() until nqp::elems(@a) == 4;
137135
nqp::push(@a, 'c');
138136
}, 0);
139137
my $t2 := nqp::newthread({
140-
nqp::threadyield() until @a;
138+
nqp::threadyield() until nqp::elems(@a) == 1;
141139
nqp::push(@a, '1');
142-
nqp::threadyield();
140+
nqp::threadyield() until nqp::elems(@a) == 3;
143141
nqp::push(@a, '2');
144142
}, 0);
145143

@@ -155,9 +153,6 @@ plan(24);
155153
ok(@a[0] eq 'a',
156154
'Looped threadyield() can force other thread to act first');
157155

158-
# XXXX: This test is flaky on nqp-j, often giving a,1,2,b,c
159-
# or more rarely a,1,b,c,2; in either case, one of the
160-
# threadyield() ops get ignored.
161156
my $order := nqp::join(',', @a);
162157
my $ok := $order eq 'a,1,b,2,c';
163158
ok($ok, 'threadyield() properly interleaved two child threads');

0 commit comments

Comments
 (0)