Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
A Lock primitive, for use in low-level things.
  • Loading branch information
jnthn committed Oct 31, 2013
1 parent 2bf7cbc commit 09a2ec1
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/vm/jvm/core/Threading.pm
Expand Up @@ -73,6 +73,28 @@ my class Thread {
});
}

# A simple, reentrant lock mechanism.
my class Lock {
has $!lock;

submethod BUILD() {
my \ReentrantLock := nqp::jvmbootinterop().typeForName('java.util.concurrent.locks.ReentrantLock');
$!lock := ReentrantLock.'constructor/new/()V'();
}

method lock() { $!lock.lock() }

method unlock() { $!lock.unlock() }

method run(&code) {
$!lock.lock();
my \res := code();
$!lock.unlock();
CATCH { $!lock.unlock(); }
res
}
}

# Schedulers do this role. It mostly serves as an interface for the things
# that schedulers must do, as well as a way to factor out some common "sugar"
# and infrastructure.
Expand Down

0 comments on commit 09a2ec1

Please sign in to comment.