Skip to content

Commit 7993775

Browse files
committed
Add an NQPLock type.
To make locks and condition variables available a little more easily.
1 parent d5b3a73 commit 7993775

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/core/NQPLock.nqp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#?if !parrot
2+
my class NQPLock is repr('ReentrantMutex') {
3+
class ConditionVariable is repr('ConditionVariable') {
4+
method new() {
5+
nqp::die("Cannot directly create a ConditionVariable; use the 'condition' method on a lock");
6+
}
7+
method wait() { nqp::condwait(self) }
8+
method signal() { nqp::condsignalone(self) }
9+
method signal_all() { nqp::condsignalall(self) }
10+
}
11+
12+
method new() { nqp::create(self) }
13+
14+
method lock() { nqp::lock(self) }
15+
16+
method unlock() { nqp::unlock(self) }
17+
18+
method protect($code) {
19+
nqp::lock(self);
20+
my $res := $code();
21+
nqp::unlock(self);
22+
CATCH { nqp::unlock(self); }
23+
$res
24+
}
25+
26+
method condition() {
27+
nqp::getlockcondvar(self, ConditionVariable)
28+
}
29+
}
30+
#?endif

tools/build/Makefile-common.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CORE_SETTING_SOURCES = \
1818
src/core/IO.nqp \
1919
src/core/Regex.nqp \
2020
src/core/Hash.nqp \
21+
src/core/NQPLock.nqp \
2122
src/core/testing.nqp \
2223
src/core/YOUAREHERE.nqp \
2324

0 commit comments

Comments
 (0)