File: 02-lock.t

package info (click to toggle)
nqp 2022.12%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,436 kB
  • sloc: java: 28,030; perl: 3,394; ansic: 451; makefile: 200; javascript: 68; sh: 1
file content (220 lines) | stat: -rw-r--r-- 5,140 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
plan(15);

my class Lock    is repr('ReentrantMutex')    { }
my class CondVar is repr('ConditionVariable') { }

# 2 tests
{
    my $l := Lock.new;
    nqp::lock($l);
    { ok(1, 'Code runs under lock'); }
    nqp::unlock($l);

    nqp::lock($l);
    { ok(1, 'Lock can be used more than once'); }
    nqp::unlock($l);
}

# 5 tests
{
    my $l := Lock.new;
    my $died := 0;
    my $thrown_message := 'Dying with lock held';
    my $caught_message := '';
    {
        nqp::lock($l);
        nqp::die($thrown_message);
        CATCH {
            $died := 1;
            $caught_message := $_;
            nqp::unlock($l);
        }
    }

    ok($died, 'Can die with a lock held and CATCH it');
    ok($thrown_message eq $caught_message,
       'Got expected message from die with lock held');

    {
        nqp::lock($l);
        ok(1, 'Can unlock in CATCH and relock after');
        nqp::unlock($l);
    }

    my $t := nqp::newthread({
        nqp::lock($l);
        ok(1, 'Lock that survived CATCH works in another thread too');
        nqp::unlock($l);
    }, 0);
    nqp::threadrun($t);
    nqp::threadjoin($t);

    {
        nqp::lock($l);
        ok(1, 'Lock from CATCH and other thread works in main thread again');
        nqp::unlock($l);
    }
}

# 2 tests
# XXXX: Currently slow on nqp-j, and probably too fast on nqp-m
{
    my $l      := Lock.new;
    my $count  := 100_000;
    my $output := '';

    my $t1     := nqp::newthread({
        nqp::lock($l);
        my $i := 0;
        while ++$i <= $count {
            $output := $output ~ 'a';
        }
        nqp::unlock($l);
    }, 0);

    my $t2     := nqp::newthread({
        nqp::lock($l);
        my $i := 0;
        while ++$i <= $count {
            $output := $output ~ 'b';
        }
        nqp::unlock($l);
    }, 0);

    nqp::threadrun($t1);
    nqp::threadrun($t2);
    nqp::threadjoin($t1);
    nqp::threadjoin($t2);

    ok($output ~~ /^ [ a+: b+: | b+: a+: ] $/,
       'Lock is at least somewhat effective');
    ok(nqp::chars($output) == 2 * $count, 'Result is correct length');
}

# 2 tests
{
    my $l := Lock.new;
    my $c := nqp::getlockcondvar($l, CondVar);
    ok(nqp::defined($c), 'Can create condition variable from lock');

    my $now1 := 0;
    my $now2 := nqp::time();
    my @log;

    my $t1 := nqp::newthread({
        nqp::lock($l);
        nqp::push(@log, 'ale');
        until nqp::elems(@log) == 2 {
            nqp::condwait($c);
        }
        nqp::push(@log, 'stout');
        nqp::condsignalall($c);
        $now1 := nqp::time();
        nqp::unlock($l);
    }, 0);
    nqp::threadrun($t1);

    my $elems := 0;
    until $elems == 1 {
        nqp::lock($l);
        $elems := nqp::elems(@log);
        nqp::unlock($l);
    }

    my $t2 := nqp::newthread({
        nqp::lock($l);
        nqp::push(@log, 'porter');
        nqp::condsignalone($c);
        until nqp::elems(@log) == 3 {
            nqp::condwait($c);
        }
        nqp::push(@log, 'lager');
        nqp::unlock($l);
    }, 0);
    nqp::threadrun($t2);

    nqp::threadjoin($t1);
    $now2 := nqp::time();
    nqp::threadjoin($t2);

    my $ok := nqp::join(',', @log) eq 'ale,porter,stout,lager';
    ok($ok, 'Condition variable worked');

    say("# log = {@log}{ $now1 > $now2 ?? ', thread was running *after* join' !! ''}") if !$ok;
}

# 4 tests
{
    my $l  := Lock.new;
    my $c1 := nqp::getlockcondvar($l, CondVar);
    my $c2 := nqp::getlockcondvar($l, CondVar);
    ok(nqp::defined($c1) && nqp::defined($c2),
       'Can create more than one condvar from same lock');
    ok(nqp::where($c1) != nqp::where($c2),
       'Multiple condvars from same lock are different');

    my $count_one := 0;
    my $count_all := 0;

    my $t1 := nqp::newthread({
        nqp::lock($l);
        nqp::condsignalone($c1);
        nqp::condsignalall($c2);
        nqp::unlock($l);
    }, 0);

    my $t2 := nqp::newthread({
        nqp::lock($l);
        nqp::condwait($c1);
        $count_one++;
        nqp::unlock($l);
    }, 0);

    my $t3 := nqp::newthread({
        nqp::lock($l);
        nqp::condwait($c1);
        $count_one++;
        nqp::unlock($l);
    }, 0);

    my $t4 := nqp::newthread({
        nqp::lock($l);
        nqp::condwait($c2);
        $count_all++;
        nqp::unlock($l);
    }, 0);

    my $t5 := nqp::newthread({
        nqp::lock($l);
        nqp::condwait($c2);
        $count_all++;
        nqp::unlock($l);
    }, 0);

    # Start all waiting threads
    nqp::threadrun($t2);
    nqp::threadrun($t3);
    nqp::threadrun($t4);
    nqp::threadrun($t5);
    nqp::sleep(1.0);

    # Start signaling thread
    nqp::threadrun($t1);

    # Check for condsignalone result, then signal it again to unblock
    nqp::sleep(2.0);
    my $c1_snap := $count_one;
    nqp::lock($l);
    nqp::condsignalone($c1);
    nqp::unlock($l);

    # Join 'em up
    nqp::threadjoin($t2);
    nqp::threadjoin($t3);
    nqp::threadjoin($t4);
    nqp::threadjoin($t5);
    nqp::threadjoin($t1);

    ok($c1_snap   == 1, 'condsignalone signaled exactly one waiting thread');
    ok($count_all == 2, 'condsignalall signaled both waiting threads');
}