/
Lock.pm
34 lines (28 loc) · 942 Bytes
/
Lock.pm
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
# A reentrant lock mechanism with condition variable support.
my class X::Lock::ConditionVariable::New is Exception {
method message() {
"Cannot directly create a ConditionVariable; use the 'condition' method on a lock"
}
}
my class Lock {
class ConditionVariable is repr('ConditionVariable') {
method new() {
X::Lock::ConditionVariable::New.new.throw
}
method wait() { nqp::condwait(self) }
method signal() { nqp::condsignalone(self) }
method signal_all() { nqp::condsignalall(self) }
}
method new() { nqp::create(self) }
method lock(Lock:D:) { nqp::lock(self) }
method unlock(Lock:D:) { nqp::unlock(self) }
method protect(Lock:D: &code) {
nqp::lock(self);
LEAVE nqp::unlock(self);
code()
}
method condition(Lock:D:) {
nqp::getlockcondvar(self, ConditionVariable)
}
}
# vim: ft=perl6 expandtab sw=4